├── templates ├── file.html~ ├── file.html ├── aviary.html~ ├── index.html └── aviary.html ├── hash.py ├── diff.js ├── hash_movie.py └── app.py /templates/file.html~: -------------------------------------------------------------------------------- 1 | filepicker.setKey(apiKey); 2 | -------------------------------------------------------------------------------- /templates/file.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 |

hi

7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /hash.py: -------------------------------------------------------------------------------- 1 | from PIL import Image 2 | import sys 3 | import imagehash 4 | hash = imagehash.average_hash(Image.open(sys.argv[1])) 5 | print 'ahash: ' + str(hash) 6 | 7 | hash = imagehash.phash(Image.open(sys.argv[1])) 8 | print 'phash: ' + str(hash) 9 | 10 | hash = imagehash.dhash(Image.open(sys.argv[1])) 11 | print 'dhash: ' + str(hash) 12 | -------------------------------------------------------------------------------- /diff.js: -------------------------------------------------------------------------------- 1 | function hashDiff(a, b) { 2 | var diff = 0; 3 | for (var i = 0; i < a.length && i < b.length; i++) { 4 | x = atoi(a[i]); 5 | y = atoi(b[i]); 6 | diff += Math.min( 7 | Math.abs(Math.min(x,y) + 16 - Math.max(x,y)), 8 | Math.abs(x - y)); 9 | } 10 | return diff; 11 | } 12 | 13 | function atoi(c) { 14 | return parseInt('0x' + c); 15 | } 16 | 17 | console.log(hashDiff(process.argv[2], process.argv[3])); 18 | -------------------------------------------------------------------------------- /hash_movie.py: -------------------------------------------------------------------------------- 1 | from PIL import Image 2 | from os import listdir 3 | import sys 4 | import pymongo 5 | import imagehash 6 | 7 | client = pymongo.MongoClient('localhost', 27017) 8 | db = client.hackny 9 | title = sys.argv[1] 10 | path = sys.argv[2] 11 | 12 | 13 | for img in listdir(path): 14 | ahash = imagehash.average_hash(Image.open(path + '/' + img)) 15 | phash = imagehash.phash(Image.open(path + '/' + img)) 16 | dhash = imagehash.dhash(Image.open(path + '/' + img)) 17 | imghash = { 18 | "title": title, 19 | "ahash": str(ahash), 20 | "phash": str(phash), 21 | "dhash": str(dhash), 22 | "filename": img 23 | } 24 | 25 | # print imghash 26 | db.images.insert(imghash) 27 | 28 | 29 | def usage(): 30 | print "usage: hash_movie " 31 | -------------------------------------------------------------------------------- /templates/aviary.html~: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 30 | 31 | 32 |
33 | 34 | 35 | 36 | 37 |

38 | 39 | 40 | -------------------------------------------------------------------------------- /templates/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 |
6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /templates/aviary.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 8 | 9 | 10 | 11 | 12 | 64 | 65 | 66 |
67 |

Search for Movies and TV Shows by uploading Images

68 | 69 |

70 |
71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | -------------------------------------------------------------------------------- /app.py: -------------------------------------------------------------------------------- 1 | from flask import Flask, render_template, request, jsonify 2 | import os 3 | import requests 4 | import urllib2 5 | import pymongo 6 | from PIL import Image 7 | import sys 8 | import imagehash 9 | import heapq 10 | import subprocess 11 | from bs4 import BeautifulSoup 12 | import json 13 | 14 | client = pymongo.MongoClient('localhost', 27017) 15 | db = client.hackny 16 | movieTitle = '' 17 | app = Flask(__name__, static_url_path = "", static_folder = "") 18 | 19 | 20 | def computeHash(filename): 21 | return str(imagehash.average_hash(Image.open(filename))) 22 | 23 | def query(target): 24 | mins = [[1000000, None], [1000000, None], [1000000, None], [1000000, None], [1000000, None]] 25 | 26 | for obj in db.images.find(): 27 | # compute the hamming weight of the bitwise xor 28 | try: 29 | weight = bin(int(target, 16) ^ int(obj['ahash'], 16)).count('1') 30 | m = max(mins, key=lambda x:x[0]) 31 | 32 | if weight > m[0]: 33 | m[0] = weight 34 | m[1] = obj 35 | print target, obj['ahash'], obj['filename'] 36 | except: 37 | pass 38 | 39 | return mins 40 | 41 | 42 | def foursquare(query): 43 | url = 'https://api.foursquare.com/v2/venues/search?client_id=SIUVLZ0OHAY5YW1W4F0GCDD4XUP0ILCPU3UYXVQBY0SDWSIZ&client_secret=5AKY4AQ0I4F3OOBSB0IWOSFZNJDAPYMU0BEPF0MGIWJPINQ1&v=20130815&ll=40.7,-74&query=%s' % query 44 | 45 | data = urllib2.urlopen(url) 46 | soup = BeautifulSoup(data) 47 | dictionary = json.loads(str(soup)) 48 | places = dictionary['response']['venues'] 49 | lines = [] 50 | for place in places: 51 | name = place['name'] 52 | location = place['location'] 53 | try: 54 | city = location['city'] 55 | except: 56 | city = "NYC" 57 | try: 58 | state = location['state'] 59 | except: 60 | city = "NY" 61 | 62 | line = '
  • %s

    Distance: %s,%s,%s
  • ' % (name, location['distance'], city, state) 63 | lines.append(line) 64 | return '' 65 | 66 | 67 | 68 | 69 | @app.route('/', methods=['GET']) 70 | def index(): 71 | return render_template("index.html") 72 | 73 | 74 | @app.route('/file', methods=['GET']) 75 | def filePicker(): 76 | return render_template("file.html") 77 | 78 | @app.route('/query/', methods=['GET']) 79 | def echo(): 80 | ret_data = {"value": frameObjs} 81 | #ret_data = {"value": foursquare('steak')} 82 | return jsonify(ret_data) 83 | 84 | @app.route('/aviary', methods=['GET']) 85 | def aviary(): 86 | return render_template("aviary.html") 87 | 88 | 89 | @app.route('/upload', methods=['POST']) 90 | def upload(): 91 | global movieTitle 92 | global frameObjs 93 | 94 | url = request.form['url'] 95 | print url 96 | file_name = 'temp.'+url.split('/')[-1].split('.')[-1] 97 | u = urllib2.urlopen(url) 98 | f = open(file_name, 'wb') 99 | meta = u.info() 100 | file_size = int(meta.getheaders("Content-Length")[0]) 101 | print "Downloading: %s Bytes: %s" % (file_name, file_size) 102 | 103 | file_size_dl = 0 104 | block_sz = 8192 105 | while True: 106 | buffer = u.read(block_sz) 107 | if not buffer: 108 | break 109 | 110 | file_size_dl += len(buffer) 111 | f.write(buffer) 112 | status = r"%10d [%3.2f%%]" % (file_size_dl, file_size_dl * 100. / file_size) 113 | status = status + chr(8)*(len(status)+1) 114 | #print status, 115 | 116 | f.close() 117 | frameObjs = query(computeHash(file_name)) 118 | #movieTitle = [frameObj['title'] for frameObj in frameObjs] 119 | # frame = frameObjs[0][1]['filename'] 120 | # print(frame) 121 | # subprocess.call(['cp','-f','./images/'+frame,'./temp2.jpg']) 122 | # render_template('file.html') 123 | return "200" 124 | 125 | if __name__ == '__main__': 126 | port = int(os.environ.get("PORT", 5000)) 127 | app.debug = True 128 | app.run(host='0.0.0.0', port=port) 129 | --------------------------------------------------------------------------------