I completed the physical prototype of the sound camera inside the enclosure I specified in my prior post, the Kodak Brownie Model 2.
I started by adding a shutter button to the top of the enclosure. I used a Cherry MX Blue mechanical keyboard switch that I had leftover from a project last year.
The battery and Raspberry Pi just barely fit into the enclosure:
The Raspberry Pi camera module is wedged snugly beneath the camera’s front plate:
In additional to playing the song, I added some functionality that provides a bit of context to the user. Using the pico2wave text-to-speech utility, the camera speaks the tags aloud before playing the song. Additionally, using SoX, the camera plays an initialization tone generated from the color histogram of the image before reading the tags.
Here’s the code that’s currently running on the Raspberry Pi:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
from __future__ import unicode_literals import os import json import uuid import time from random import choice as rc from random import sample as rs import re import subprocess import RPi.GPIO as GPIO import picamera from clarifai.client import ClarifaiApi import requests from PIL import Image import sys import threading import spotify import genius_token # SPOTIFY STUFF # Assuming a spotify_appkey.key in the current dir session = spotify.Session() # Process events in the background loop = spotify.EventLoop(session) loop.start() # Connect an audio sink audio = spotify.AlsaSink(session) # Events for coordination logged_in = threading.Event() logged_out = threading.Event() end_of_track = threading.Event() logged_out.set() def on_connection_state_updated(session): if session.connection.state is spotify.ConnectionState.LOGGED_IN: logged_in.set() logged_out.clear() elif session.connection.state is spotify.ConnectionState.LOGGED_OUT: logged_in.clear() logged_out.set() def on_end_of_track(self): end_of_track.set() # Register event listeners session.on( spotify.SessionEvent.CONNECTION_STATE_UPDATED, on_connection_state_updated) session.on(spotify.SessionEvent.END_OF_TRACK, on_end_of_track) # Assuming a previous login with remember_me=True and a proper logout # session.relogin() # session.login(genius_token.spotify_un, genius_token.spotify_pwd, remember_me=True) # logged_in.wait() # CAMERA STUFF # Init Camera camera = picamera.PiCamera() # Init GPIO GPIO.setmode(GPIO.BCM) # Button Pin GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP) IMGPATH = '/home/pi/soundcamera/img/' clarifai_api = ClarifaiApi() def chunks(l, n): """Yield successive n-sized chunks from l.""" for i in xrange(0, len(l), n): yield l[i:i+n] def take_photo(): fn = str(int(time.time()))+'.jpg' # TODO: Change to timestamp hash fp = IMGPATH+fn camera.capture(fp) return fp def chunks(l, n): """Yield successive n-sized chunks from l.""" for i in xrange(0, len(l), n): yield l[i:i+n] def get_tags(fp): fileObj = open(fp) result = clarifai_api.tag_images(fileObj) resultObj = result['results'][0] tags = resultObj['result']['tag']['classes'] return tags def genius_search(tags): access_token = genius_token.token payload = { 'q': ' '.join(tags), 'access_token': access_token } endpt = 'http://api.genius.com/search' response = requests.get(endpt, params=payload) results = response.json() hits = results['response']['hits'] artists_titles = [] for h in hits: hit_result = h['result'] if hit_result['url'].endswith('lyrics'): artists_titles.append( (hit_result['primary_artist']['name'], hit_result['title']) ) return artists_titles def spotify_search(query): endpt = "https://api.spotify.com/v1/search" payload = { 'q': query, 'type': 'track' } response = requests.get(endpt, params=payload) result = response.json() result_zero = result['tracks']['items'][0] return result_zero['uri'] def main(fn): tags = get_tags(fn) for tag_chunk in chunks(tags,3): artists_titles = genius_search(tag_chunk) for artist, title in artists_titles: try: result_uri = spotify_search(artist+' '+title) except IndexError: pass else: print tag_chunk byline = "%s by %s" % (title, artist) print byline to_read = ', '.join(tag_chunk) + ". " + byline return to_read, result_uri def play_uri(track_uri): # Play a track # audio = spotify.AlsaSink(session) session.login(genius_token.spotify_un, genius_token.spotify_pwd, remember_me=True) logged_in.wait() track = session.get_track(track_uri).load() session.player.load(track) session.player.play() def stop_track(): session.player.play(False) session.player.unload() session.logout() logged_out.wait() audio._close() def talk(msg): proc = subprocess.Popen( ['bash', '/home/pi/soundcamera/play_text.sh', msg] ) proc.communicate() def play_tone(freqs): freq1, freq2 = freqs proc = subprocess.Popen( ['play', '-n', 'synth', '0.25', 'saw', "%i-%i" % (freq1, freq2)] ) proc.communicate() def histo_tone(fp): im = Image.open(fp) hist = im.histogram() vals = map(sum, chunks(hist, 64)) # list of 12 values print vals map(play_tone, chunks(vals,2)) if __name__ == "__main__": input_state = True new_state = True hold_counter = 0 while 1: input_state = GPIO.input(18) if not (input_state and new_state): talk("capturing") # Hold for 15 seconds to turn off while not GPIO.input(18): time.sleep(0.1) hold_counter += 1 if hold_counter > 150: os.system('shutdown now -h') sys.exit() # Reset hold counter hold_counter = 0 # Else take photo try: img_fp = take_photo() msg, uri = main(img_fp) histo_tone(img_fp) talk(msg) play_uri(uri) except: print sys.exc_info() # Wait for playback to complete or Ctrl+C try: while not end_of_track.wait(0.1): # If new photo, play new song new_state = GPIO.input(18) if not new_state: stop_track() # time.sleep(2) break except KeyboardInterrupt: pass |