Skip to content
This repository has been archived by the owner on Dec 8, 2024. It is now read-only.

Commit

Permalink
feat: added register face embeddings func and face match for dlib fac…
Browse files Browse the repository at this point in the history
…e-rec
  • Loading branch information
MitchellJC committed Oct 1, 2024
1 parent 91ab3da commit 0878d9b
Showing 1 changed file with 41 additions and 8 deletions.
49 changes: 41 additions & 8 deletions client/models/face_recognition/recognition.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from importlib import resources

import numpy as np
from data.routines import FACES_FOLDER
from data.routines import FACES_FOLDER, register_face_embeddings, iter_face_embeddings
from deepface import DeepFace
import face_recognition

MODEL_NAME = "GhostFaceNet"

Expand All @@ -21,11 +22,43 @@ def get_face_match(login_face: np.ndarray) -> int:
Returns:
Matching user id, or -1 if no users matched
"""
with resources.as_file(FACES_FOLDER) as faces_folder:
try:
dfs = DeepFace.find(login_face, str(faces_folder), model_name=MODEL_NAME)
except ValueError:
# with resources.as_file(FACES_FOLDER) as faces_folder:
# try:
# dfs = DeepFace.find(login_face, str(faces_folder), model_name=MODEL_NAME)
# except ValueError:
# return -1
# df = dfs[0]
# user_id = int(_path_to_user_id(df.iloc[0]["identity"]))
# return user_id

for user_id, user_embeddings in iter_face_embeddings():
login_embeddings = face_recognition.face_encodings(login_face, model="small")

# Should only detect one face
if len(login_embeddings) != 1:
return -1

login_embedding = login_embeddings[0]
matches = face_recognition.compare_faces(user_embeddings, login_embedding)

if any(matches):
return user_id

return -1


def register_faces(user_id: int, faces: list[np.ndarray]) -> int:
face_embeddings = []
for face in faces:
all_faces_embed = face_recognition.face_encodings(face, model="small")

# Should only detect one face
if len(all_faces_embed) != 1:
return -1
df = dfs[0]
user_id = int(_path_to_user_id(df.iloc[0]["identity"]))
return user_id

face_embedding = all_faces_embed[0]
face_embeddings.append(face_embedding)

register_face_embeddings(user_id, face_embeddings)

return 0

0 comments on commit 0878d9b

Please sign in to comment.