# This is a _very simple_ example of a web service that recognizes faces in uploaded images. # Upload an image file and it will check if the image contains a picture of Barack Obama. # The result is returned as json. For example: # # $ curl -XPOST -F "file=@obama2.jpg" http://127.0.0.1:5001 # # Returns: # # { # "face_found_in_image": true, # "is_picture_of_obama": true # } # # This example is based on the Flask file upload example: http://flask.pocoo.org/docs/0.12/patterns/fileuploads/ # NOTE: This example requires flask to be installed! You can install it with pip: # $ pip3 install flask import os import json import requests import pickle import face_recognition from flask import Flask, jsonify, request, redirect # You can change this to any folder on your system ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif'} known_faces = [] def reload_faces(): global known_faces response = requests.get("http://fmt.i3display.com/staging/facedb/api/faces/all") data = response.json() known_faces = [] for face in data['faces']: if(face['name']): file = '/var/www/html/fmt/staging/facedb/webroot/'+face['filename'] if os.path.isfile(file): name = face['name'] # print (name) cache_file = "cache/"+ str(face['id'])+".pk" if os.path.isfile(cache_file): known_encodings = pickle.load(open(cache_file, "rb" )) else: known_file = face_recognition.load_image_file(file) known_encodings = face_recognition.face_encodings(known_file) if len(known_encodings) > 0: known_faces.append({'enc': known_encodings[0], 'name': name}) if os.path.isfile(cache_file) == False: pickle_out = open(cache_file,"wb") pickle.dump(known_encodings, pickle_out) pickle_out.close() reload_faces() app = Flask(__name__) @app.route('/get_encoding', methods=['GET']) def get_encoding(): file = '/var/www/html/fmt/staging/facedb/webroot/'+request.args.get('file') known_file = face_recognition.load_image_file(file) known_encoding = face_recognition.face_encodings(known_file)[0] result = { "status": 1, "message": 'Encoding done', "encoding": known_encoding } return jsonify(result) @app.route('/analyze_photo', methods=['GET']) def analyze_photo(): global known_faces face_found = False name_found = False file = '/var/www/html/fmt/staging/facedb/webroot/'+request.args.get('file') known_file = face_recognition.load_image_file(file) unknown_face_encodings = face_recognition.face_encodings(known_file) if len(unknown_face_encodings) > 0: face_found = True for known_face in known_faces: print('Comparing with: '+known_face['name']) match_results = face_recognition.compare_faces([known_face['enc']], unknown_face_encodings[0]) if match_results[0]: print('Found match! '+known_face['name']) print(match_results) name_found = known_face['name'] face_found = True break # Return the result as json result = { "name": name_found, "face_found": face_found, } return jsonify(result) @app.route('/reload', methods=['GET']) def reload(): reload_faces() result = { "status": 1, "message": 'Reloaded' } return jsonify(result) def allowed_file(filename): return '.' in filename and \ filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS @app.route('/', methods=['GET', 'POST']) def upload_image(): # Check if a valid image file was uploaded if request.method == 'POST': if 'file' not in request.files: return redirect(request.url) file = request.files['file'] if file.filename == '': return redirect(request.url) print('File received') # The image file seems valid! Detect faces and return the result. return detect_faces_in_image(file) # If no valid image file was uploaded, show the file upload form: return ''' Is this a picture of Obama?

Upload a picture and see if it's a picture of Obama!

''' def detect_faces_in_image(file_stream): global known_faces face_found = False name_found = False # print('Comparing with '+len(known_faces) + ' faces') # Load the uploaded image file img = face_recognition.load_image_file(file_stream) # Get face encodings for any faces in the uploaded image unknown_face_encodings = face_recognition.face_encodings(img) if len(unknown_face_encodings) > 0: face_found = True for known_face in known_faces: print('Comparing with: '+known_face['name']) match_results = face_recognition.compare_faces([known_face['enc']], unknown_face_encodings[0]) if match_results[0]: print('Found match! '+known_face['name']) print(match_results) name_found = known_face['name'] face_found = True break # Return the result as json result = { "name": name_found, "face_found": face_found, } return jsonify(result) if __name__ == "__main__": app.run(host='0.0.0.0', port=5001, debug=True)