Abraham E. Tavarez
verify_face, analyze_face functions and extract embedding functions tested
4c7f58c
from deepface import DeepFace | |
import cv2 | |
import os | |
def verify_faces(img1_path, img2_path, model_name="Facenet", detector_backend="opencv"): | |
""" | |
Compares two face images and returns whether they belong to the same person. | |
""" | |
try: | |
result = DeepFace.verify( | |
img1_path, | |
img2_path, | |
model_name=model_name, | |
detector_backend=detector_backend, | |
enforce_detection=True | |
) | |
return { | |
"verified": result["verified"], | |
"distance": result["distance"], | |
"threshold": result["threshold"], | |
"model": model_name | |
} | |
except Exception as e: | |
return {"error": str(e)} | |
def analyze_face(img_path, actions=["age", "gender", "emotion"], detector_backend="opencv"): | |
""" | |
Analyze attributes of a face in the image. | |
""" | |
try: | |
analysis = DeepFace.analyze( | |
img_path=img_path, | |
actions=actions, | |
detector_backend=detector_backend, | |
enforce_detection=True | |
) | |
return analysis[0] if isinstance(analysis, list) else analysis | |
except Exception as e: | |
return {"error": str(e)} | |
def extract_embedding(img_path, model_name="Facenet", detector_backend="opencv"): | |
""" | |
Extract a face embedding from an image. | |
""" | |
try: | |
embedding = DeepFace.represent( | |
img_path=img_path, | |
model_name=model_name, | |
detector_backend=detector_backend, | |
enforce_detection=True | |
) | |
return embedding[0] if isinstance(embedding, list) else embedding | |
except Exception as e: | |
return {"error": str(e)} |