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

Commit

Permalink
ref,doc: Cleaned up recognition after dlib changes
Browse files Browse the repository at this point in the history
  • Loading branch information
MitchellJC committed Oct 1, 2024
1 parent a85449e commit 1859636
Showing 1 changed file with 21 additions and 24 deletions.
45 changes: 21 additions & 24 deletions client/models/face_recognition/recognition.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
from importlib import resources

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

MODEL_NAME = "GhostFaceNet"

from data.routines import register_face_embeddings, iter_face_embeddings

def _path_to_user_id(path: str) -> str:
return path.split("/")[-2]
MODEL_NAME = "small"
TOO_MANY_FACES = -2
NO_MATCH = -1
OK = 0


def get_face_match(login_face: np.ndarray) -> int:
Expand All @@ -20,22 +17,13 @@ def get_face_match(login_face: np.ndarray) -> int:
login_face: Image of user's face as an array
Returns:
Matching user id, or -1 if no users matched
Matching user id, -1 if no users matched, -2 if too many faces
"""
# 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

login_embeddings = face_recognition.face_encodings(login_face, model="small")
login_embeddings = face_recognition.face_encodings(login_face, model=MODEL_NAME)

# Should only detect one face
if len(login_embeddings) != 1:
return -1
return TOO_MANY_FACES
login_embedding = login_embeddings[0]

for user_id, user_embeddings in iter_face_embeddings():
Expand All @@ -44,21 +32,30 @@ def get_face_match(login_face: np.ndarray) -> int:
if any(matches):
return user_id

return -1
return NO_MATCH


def register_faces(user_id: int, faces: list[np.ndarray]) -> int:
"""Compute and store face embeddings in the database.
Args:
user_id: Id of the user who belongs to the faces
faces: List of face images in the shape HxWxC where (C)hannels are in RGB
Returns:
Status, -2 if too many faces in an image, 0 if registration was sucessful.
"""
face_embeddings = []
for face in faces:
all_faces_embed = face_recognition.face_encodings(face, model="small")
all_faces_embed = face_recognition.face_encodings(face, model=MODEL_NAME)

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

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

register_face_embeddings(user_id, face_embeddings)

return 0
return OK

0 comments on commit 1859636

Please sign in to comment.