|
from deepface import DeepFace |
|
import cv2 |
|
import os |
|
from modal_app.modal_app import verify_faces_remote |
|
from fastapi import UploadFile |
|
|
|
def face_verify_tool(img1: UploadFile, img2: UploadFile): |
|
img1_bytes = img1.file.read() |
|
img2_bytes = img2.file.read() |
|
|
|
results = verify_faces_remote(img1_bytes, img2_bytes) |
|
|
|
return results |
|
|
|
|
|
|
|
|
|
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)} |