jarondon82 commited on
Commit
91f8838
·
1 Parent(s): c23feac

Add missing face embedding extraction functions

Browse files
Files changed (1) hide show
  1. face_comparison.py +56 -1
face_comparison.py CHANGED
@@ -248,4 +248,59 @@ def draw_face_matches(image1, bboxes1, image2, bboxes2, comparison_results, thre
248
  cv2.putText(combined_img, f"{similarity:.1f}%", (text_x, text_y),
249
  cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)
250
 
251
- return combined_img
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
248
  cv2.putText(combined_img, f"{similarity:.1f}%", (text_x, text_y),
249
  cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)
250
 
251
+ return combined_img
252
+
253
+ def extract_face_embeddings(image, bbox, model_name="VGG-Face"):
254
+ """
255
+ Extract facial embeddings from a face using DeepFace
256
+ """
257
+ try:
258
+ from deepface import DeepFace
259
+ except ImportError:
260
+ st.error("DeepFace library is not available. Please install with 'pip install deepface' to use embeddings.")
261
+ return None
262
+
263
+ # Extract bbox coordinates
264
+ x1, y1, x2, y2, _ = bbox
265
+
266
+ # Check if the face region is valid
267
+ if x1 >= x2 or y1 >= y2:
268
+ return None
269
+
270
+ # Extract face region
271
+ face_roi = image[y1:y2, x1:x2]
272
+
273
+ # Get embedding for the face
274
+ try:
275
+ embedding_info = DeepFace.represent(face_roi, model_name=model_name, enforce_detection=False)[0]
276
+ return {
277
+ "embedding": embedding_info["embedding"],
278
+ "model": model_name
279
+ }
280
+ except Exception as e:
281
+ st.warning(f"Error extracting embedding with {model_name}: {str(e)}")
282
+ # Try with a fallback model
283
+ try:
284
+ fallback_model = "OpenFace"
285
+ embedding_info = DeepFace.represent(face_roi, model_name=fallback_model, enforce_detection=False)[0]
286
+ return {
287
+ "embedding": embedding_info["embedding"],
288
+ "model": fallback_model
289
+ }
290
+ except Exception as e:
291
+ st.error(f"Failed to extract embeddings: {str(e)}")
292
+ return None
293
+
294
+ def extract_face_embeddings_all_models(image, bbox):
295
+ """
296
+ Extract facial embeddings using multiple models (VGG-Face, Facenet, OpenFace, ArcFace)
297
+ """
298
+ models = ["VGG-Face", "Facenet", "OpenFace", "ArcFace"]
299
+ embeddings = []
300
+
301
+ for model_name in models:
302
+ embedding = extract_face_embeddings(image, bbox, model_name=model_name)
303
+ if embedding:
304
+ embeddings.append(embedding)
305
+
306
+ return embeddings if embeddings else None