File size: 2,024 Bytes
4c7f58c bd55878 4c7f58c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
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)} |