diff --git a/application/endpoints.py b/application/endpoints.py index 8387736..de99ee6 100644 --- a/application/endpoints.py +++ b/application/endpoints.py @@ -6,7 +6,7 @@ import json import cv2 import base64 from application.db import Session, Person, Fingerprint - +import application.face_rec as fr lastImage = "" class PersonList(Resource): @@ -58,17 +58,19 @@ class PersonList(Resource): if id is not None: # validate data = list(session.query(Person).filter_by(person_id=id))[0].serialize() - data["matching_score"] = 0.95 + results = fr.identifyFace(lastImage) + data["matching_score"] = 1 - results[int(id)] # return identified person object + matching score return flask.make_response(flask.jsonify({'data': data}), 200) else: # replace by Biometric function # identify # return identified person object + matching score + results = fr.identifyFace(lastImage) data = [] for x in list(session.query(Person).all()): ser = x.serialize() - ser["matching_score"] = 0.95 + ser["matching_score"] = 1 - results[x.person_id] data.append(ser) return flask.make_response(flask.jsonify({'data': data}), 200) diff --git a/application/face_rec.py b/application/face_rec.py index ce180e1..b5b5fd3 100644 --- a/application/face_rec.py +++ b/application/face_rec.py @@ -1,7 +1,7 @@ import face_recognition import os import cv2 -from db import Session, Person +from application.db import Session, Person import base64 import numpy as np from base64 import decodestring @@ -34,77 +34,51 @@ def readb64(base64_string): print('Loading known faces...') known_faces = [] known_names = [] -session = Session() - -# We oranize known faces as subfolders of KNOWN_FACES_DIR -# Each subfolder's name becomes our label (name) -for face, name in session.query(Person.face, Person.lname).all(): - # Load an image - nparr = np.fromstring(base64.b64decode(face), np.uint8) - image = cv2.imdecode(nparr, cv2.IMREAD_COLOR) - - # Get 128-dimension face encoding - # Always returns a list of found faces, for this purpose we take first face only (assuming one face per image as you can't be twice on one image) - encoding = face_recognition.face_encodings(image)[0] - - # Append encodings and name - known_faces.append(encoding) - known_names.append(name) -print('Processing unknown faces...') -# Now let's loop over a folder of faces we want to label -image = face_recognition.load_image_file('C:/Users/ofjok/Desktop/1.png') +def initFaceRec(): + session = Session() + # We oranize known faces as subfolders of KNOWN_FACES_DIR + # Each subfolder's name becomes our label (name) + for face, name in session.query(Person.face, Person.person_id).all(): + # Load an image + nparr = np.fromstring(base64.b64decode(face), np.uint8) + image = cv2.imdecode(nparr, cv2.IMREAD_COLOR) + + # Get 128-dimension face encoding + # Always returns a list of found faces, for this purpose we take first face only (assuming one face per image as you can't be twice on one image) + encoding = face_recognition.face_encodings(image)[0] -# This time we first grab face locations - we'll need them to draw boxes -locations = face_recognition.face_locations(image, model=MODEL) + # Append encodings and name + known_faces.append(encoding) + known_names.append(name) -# Now since we know loctions, we can pass them to face_encodings as second argument -# Without that it will search for faces once again slowing down whole process -encodings = face_recognition.face_encodings(image, locations) +def identifyFace(imgage): + print('Processing unknown faces...') + image = face_recognition.load_image_file('C:/Users/ofjok/Desktop/1.png') -# We passed our image through face_locations and face_encodings, so we can modify it -# First we need to convert it from RGB to BGR as we are going to work with cv2 -image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) + locations = face_recognition.face_locations(image, model=MODEL) + encodings = face_recognition.face_encodings(image, locations) -# But this time we assume that there might be more faces in an image - we can find faces of dirrerent people -for face_encoding, face_location in zip(encodings, locations): + image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) - # We use compare_faces (but might use face_distance as well) - # Returns array of True/False values in order of passed known_faces - results = face_recognition.compare_faces(known_faces, face_encoding, TOLERANCE) + res = {} - # Since order is being preserved, we check if any face was found then grab index - # then label (name) of first matching known face withing a tolerance - match = None - if True in results: # If at least one is true, get a name of first of found labels - match = known_names[results.index(True)] - print(f' - {match} from {results}') + for face_encoding, face_location in zip(encodings, locations): - # Each location contains positions in order: top, right, bottom, left - top_left = (face_location[3], face_location[0]) - bottom_right = (face_location[1], face_location[2]) + # We use compare_faces (but might use face_distance as well) + # Returns array of True/False values in order of passed known_faces + results = face_recognition.face_distance(known_faces, face_encoding) - # Get color by name using our fancy function - color = name_to_color(match) + # Since order is being preserved, we check if any face was found then grab index + # then label (name) of first matching known face withing a tolerance + # If at least one is true, get a name of first of found labels + res = {known_names[i]: results[i] for i in range(0, len(results)) } + + return res - # Paint frame - cv2.rectangle(image, top_left, bottom_right, color, FRAME_THICKNESS) - # Now we need smaller, filled grame below for a name - # This time we use bottom in both corners - to start from bottom and move 50 pixels down - top_left = (face_location[3], face_location[2]) - bottom_right = (face_location[1], face_location[2] + 22) - - # Paint frame - cv2.rectangle(image, top_left, bottom_right, color, cv2.FILLED) - - # Wite a name - cv2.putText(image, match, (face_location[3] + 10, face_location[2] + 15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (200, 200, 200), FONT_THICKNESS) - - # Show image - cv2.imshow("1", image) - cv2.waitKey(0) - cv2.destroyWindow("1") \ No newline at end of file +initFaceRec() +identifyFace("") \ No newline at end of file diff --git a/test.sqlite b/test.sqlite index a1cb0d3..99a9ad4 100644 Binary files a/test.sqlite and b/test.sqlite differ