ERROR: Missing Upload Input Param
' 37 | return 38 | form_file = form['file_upload'] 39 | if not form_file.file: 40 | print 'ERROR: Missing upload file
' 41 | return 42 | if not form_file.filename: 43 | print 'ERROR: Missing upload filename
' 44 | return 45 | form_file.file.seek(0, os.SEEK_END) 46 | filesize = form_file.file.tell() 47 | form_file.file.seek(0) # reset to the start of the file 48 | if filesize > 5 * 1024 * 1024: 49 | print 'ERROR: File size should be less than 5MB
' 50 | return 51 | 52 | uploaded_file_path = os.path.join(UPLOAD_DIR, str(uuid.uuid4()) + ".jpg") 53 | with open(uploaded_file_path, 'wb') as fout: 54 | while True: 55 | chunk = form_file.file.read(100000) 56 | if not chunk: 57 | break 58 | fout.write(chunk) 59 | print 'Upload completed
' 60 | 61 | # open the image for inspection, load image from disk 62 | image = cv2.imread(uploaded_file_path) 63 | 64 | # skip if the image cannot be loaded 65 | if image is None: 66 | print 'ERROR: Image cannot be loaded
' 67 | return 68 | 69 | # convert the image to grayscale and compute hash 70 | image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) 71 | imagehash = dhash(image) 72 | 73 | # open the database 74 | mysql = MySQLdb.connect(host=config.Config.DB_HOST,user=config.Config.DB_USER,passwd=config.Config.DB_PASS,db=config.Config.DB_NAME) 75 | cursor = mysql.cursor() 76 | 77 | sql = "SELECT ig_name FROM igdb WHERE ig_key='%s'" % (imagehash) 78 | cursor.execute(sql) 79 | 80 | if cursor.rowcount > 0: 81 | rows = cursor.fetchall() 82 | print "Found %d result(s)
" % (cursor.rowcount) 83 | for row in rows: 84 | print "IG Username is %s
" % (row[0]) 85 | else: 86 | print "Found 0 results
" 87 | 88 | mysql.close() 89 | 90 | # remove the file afterwards 91 | os.remove(uploaded_file_path) 92 | 93 | print "