Update app.py
Browse files
app.py
CHANGED
|
@@ -11,8 +11,8 @@ import gradio as gr
|
|
| 11 |
# ------------------------------
|
| 12 |
# MODEL REPOSITORIES
|
| 13 |
# ------------------------------
|
| 14 |
-
YOLO_REPO = "arnabdhar/YOLOv8-Face-Detection" # Face detection
|
| 15 |
-
ARCFACE_ONNX_REPO = "garavv/arcface-onnx" #
|
| 16 |
|
| 17 |
# ------------------------------
|
| 18 |
# DOWNLOAD MODELS FROM HUGGING FACE
|
|
@@ -40,21 +40,23 @@ arcface_input = arcface_sess.get_inputs()[0].name
|
|
| 40 |
# ------------------------------
|
| 41 |
# HELPER FUNCTIONS
|
| 42 |
# ------------------------------
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 46 |
img = (img - 127.5) / 128.0
|
| 47 |
-
img = np.
|
| 48 |
-
return img
|
| 49 |
|
| 50 |
-
|
| 51 |
-
x = preprocess_face(face)
|
| 52 |
-
emb = arcface_sess.run(None, {arcface_input: x})[0][0]
|
| 53 |
emb = emb / np.linalg.norm(emb)
|
| 54 |
return emb
|
| 55 |
|
| 56 |
# ------------------------------
|
| 57 |
-
# KNOWN FACES DATABASE
|
| 58 |
# ------------------------------
|
| 59 |
known_faces = {}
|
| 60 |
|
|
@@ -78,7 +80,7 @@ def register_face(name, image):
|
|
| 78 |
else:
|
| 79 |
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
|
| 80 |
|
| 81 |
-
#
|
| 82 |
emb = get_embedding(img)
|
| 83 |
if emb is None or np.isnan(emb).any():
|
| 84 |
return f"❌ Failed to compute embedding for {name}. Try another image."
|
|
|
|
| 11 |
# ------------------------------
|
| 12 |
# MODEL REPOSITORIES
|
| 13 |
# ------------------------------
|
| 14 |
+
YOLO_REPO = "arnabdhar/YOLOv8-Face-Detection" # Face detection model
|
| 15 |
+
ARCFACE_ONNX_REPO = "garavv/arcface-onnx" # ArcFace ONNX model
|
| 16 |
|
| 17 |
# ------------------------------
|
| 18 |
# DOWNLOAD MODELS FROM HUGGING FACE
|
|
|
|
| 40 |
# ------------------------------
|
| 41 |
# HELPER FUNCTIONS
|
| 42 |
# ------------------------------
|
| 43 |
+
|
| 44 |
+
def get_embedding(face):
|
| 45 |
+
"""
|
| 46 |
+
Preprocess the face and compute embedding using ArcFace ONNX model.
|
| 47 |
+
Fixed to match NHWC format (1, 112, 112, 3).
|
| 48 |
+
"""
|
| 49 |
+
img = cv2.resize(face, (112, 112))
|
| 50 |
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 51 |
img = (img - 127.5) / 128.0
|
| 52 |
+
img = np.expand_dims(img.astype(np.float32), axis=0) # (1, 112, 112, 3)
|
|
|
|
| 53 |
|
| 54 |
+
emb = arcface_sess.run(None, {arcface_input: img})[0][0]
|
|
|
|
|
|
|
| 55 |
emb = emb / np.linalg.norm(emb)
|
| 56 |
return emb
|
| 57 |
|
| 58 |
# ------------------------------
|
| 59 |
+
# KNOWN FACES DATABASE (in memory)
|
| 60 |
# ------------------------------
|
| 61 |
known_faces = {}
|
| 62 |
|
|
|
|
| 80 |
else:
|
| 81 |
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
|
| 82 |
|
| 83 |
+
# Compute embedding
|
| 84 |
emb = get_embedding(img)
|
| 85 |
if emb is None or np.isnan(emb).any():
|
| 86 |
return f"❌ Failed to compute embedding for {name}. Try another image."
|