verification and identification works

This commit is contained in:
Askill 2020-04-27 19:44:55 +02:00
parent 33a49a25d1
commit 8e1f765c52
3 changed files with 40 additions and 64 deletions

View File

@ -6,7 +6,7 @@ import json
import cv2 import cv2
import base64 import base64
from application.db import Session, Person, Fingerprint from application.db import Session, Person, Fingerprint
import application.face_rec as fr
lastImage = "" lastImage = ""
class PersonList(Resource): class PersonList(Resource):
@ -58,17 +58,19 @@ class PersonList(Resource):
if id is not None: if id is not None:
# validate # validate
data = list(session.query(Person).filter_by(person_id=id))[0].serialize() 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 identified person object + matching score
return flask.make_response(flask.jsonify({'data': data}), 200) return flask.make_response(flask.jsonify({'data': data}), 200)
else: else:
# replace by Biometric function # replace by Biometric function
# identify # identify
# return identified person object + matching score # return identified person object + matching score
results = fr.identifyFace(lastImage)
data = [] data = []
for x in list(session.query(Person).all()): for x in list(session.query(Person).all()):
ser = x.serialize() ser = x.serialize()
ser["matching_score"] = 0.95 ser["matching_score"] = 1 - results[x.person_id]
data.append(ser) data.append(ser)
return flask.make_response(flask.jsonify({'data': data}), 200) return flask.make_response(flask.jsonify({'data': data}), 200)

View File

@ -1,7 +1,7 @@
import face_recognition import face_recognition
import os import os
import cv2 import cv2
from db import Session, Person from application.db import Session, Person
import base64 import base64
import numpy as np import numpy as np
from base64 import decodestring from base64 import decodestring
@ -34,77 +34,51 @@ def readb64(base64_string):
print('Loading known faces...') print('Loading known faces...')
known_faces = [] known_faces = []
known_names = [] 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 # Append encodings and name
locations = face_recognition.face_locations(image, model=MODEL) known_faces.append(encoding)
known_names.append(name)
# Now since we know loctions, we can pass them to face_encodings as second argument def identifyFace(imgage):
# Without that it will search for faces once again slowing down whole process print('Processing unknown faces...')
encodings = face_recognition.face_encodings(image, locations) 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 locations = face_recognition.face_locations(image, model=MODEL)
# First we need to convert it from RGB to BGR as we are going to work with cv2 encodings = face_recognition.face_encodings(image, locations)
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
# 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) res = {}
# Returns array of True/False values in order of passed known_faces
results = face_recognition.compare_faces(known_faces, face_encoding, TOLERANCE)
# Since order is being preserved, we check if any face was found then grab index for face_encoding, face_location in zip(encodings, locations):
# 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}')
# Each location contains positions in order: top, right, bottom, left # We use compare_faces (but might use face_distance as well)
top_left = (face_location[3], face_location[0]) # Returns array of True/False values in order of passed known_faces
bottom_right = (face_location[1], face_location[2]) results = face_recognition.face_distance(known_faces, face_encoding)
# Get color by name using our fancy function # Since order is being preserved, we check if any face was found then grab index
color = name_to_color(match) # 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 initFaceRec()
# This time we use bottom in both corners - to start from bottom and move 50 pixels down identifyFace("")
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")

Binary file not shown.