Using JavaScript and Python Flask, I created a functional software prototype of the Sound Camera: rossgoodwin.com/soundcamera
The front-end JavaScript code is available on GitHub. Here is the primary back-end Python code:
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 |
import os import json import uuid from base64 import decodestring import time from random import choice as rc from random import sample as rs import re import PIL from PIL import Image import requests import exifread from flask import Flask, request, abort, jsonify from flask.ext.cors import CORS from werkzeug import secure_filename from clarifai.client import ClarifaiApi app = Flask(__name__) CORS(app) app.config['UPLOAD_FOLDER'] = '/var/www/SoundCamera/SoundCamera/static/img' IMGPATH = '/var/www/SoundCamera/SoundCamera/static/img/' clarifai_api = ClarifaiApi() @app.route("/") def index(): return "These aren't the droids you're looking for." @app.route("/img", methods=["POST"]) def img(): request.get_data() if request.method == "POST": f = request.files['file'] if f: filename = secure_filename(f.filename) f.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) new_filename = resize_image(filename) return jsonify(uri=main(new_filename)) else: abort(501) @app.route("/b64", methods=["POST"]) def base64(): if request.method == "POST": fstring = request.form['base64str'] filename = str(uuid.uuid4())+'.jpg' file_obj = open(IMGPATH+filename, 'w') file_obj.write(fstring.decode('base64')) file_obj.close() return jsonify(uri=main(filename)) @app.route("/url") def url(): img_url = request.args.get('url') response = requests.get(img_url, stream=True) orig_filename = img_url.split('/')[-1] if response.status_code == 200: with open(IMGPATH+orig_filename, 'wb') as f: for chunk in response.iter_content(1024): f.write(chunk) new_filename = resize_image(orig_filename) return jsonify(uri=main(new_filename)) else: abort(500) # def allowed_img_file(filename): # return '.' in filename and \ # filename.rsplit('.', 1)[1].lower() in set(['.jpg', '.jpeg', '.png']) def resize_image(fn): longedge = 640 orientDict = { 1: (0, 1), 2: (0, PIL.Image.FLIP_LEFT_RIGHT), 3: (-180, 1), 4: (0, PIL.Image.FLIP_TOP_BOTTOM), 5: (-90, PIL.Image.FLIP_LEFT_RIGHT), 6: (-90, 1), 7: (90, PIL.Image.FLIP_LEFT_RIGHT), 8: (90, 1) } imgOriList = [] try: f = open(IMGPATH+fn, "rb") exifTags = exifread.process_file(f, details=False, stop_tag='Image Orientation') if 'Image Orientation' in exifTags: imgOriList.extend(exifTags['Image Orientation'].values) except: pass img = Image.open(IMGPATH+fn) w, h = img.size newName = str(uuid.uuid4())+'.jpeg' if w >= h: wpercent = (longedge/float(w)) hsize = int((float(h)*float(wpercent))) img = img.resize((longedge,hsize), PIL.Image.ANTIALIAS) else: hpercent = (longedge/float(h)) wsize = int((float(w)*float(hpercent))) img = img.resize((wsize,longedge), PIL.Image.ANTIALIAS) for val in imgOriList: if val in orientDict: deg, flip = orientDict[val] img = img.rotate(deg) if flip != 1: img = img.transpose(flip) img.save(IMGPATH+newName, format='JPEG') os.remove(IMGPATH+fn) return newName 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 = 'd2IuV9fGKzYEWVnzmLVtFnm-EYvBQKR8Uh3I1cfZOdr8j-BGVTPThDES532dym5a' 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(IMGPATH+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: return result_uri if __name__ == "__main__": app.run() |
It uses the same algorithm discussed in my prior post. Now that I have the opportunity to test it more, I am not quite satisfied with the results it is providing. First of all, they are not entirely deterministic (you can upload the same photo twice and end up with two different songs in some cases). Moreover, the results from a human face — which I expect to be a common use case — are not very personal. For the next steps in this project, I plan to integrate additional data including GPS, weather, time of day, and possibly even facial expressions in order to improve the output.
The broken cameras I ordered from eBay have arrived, and I have been considering how to use them as cases for the new models. I also purchased a GPS module for my Raspberry Pi, so the next Sound Camera prototype, with new features integrated, will likely be a physical version. I’m planning to use this Kodak Brownie camera (c. 1916):