ParentCloseTesting / src /backends /deepface_backend.py
Prince-1's picture
Updated the Project
e793c54 verified
Raw
History Blame Contribute Delete
1.06 kB
import numpy as np
from deepface import DeepFace
_MODEL = "VGG-Face"
def _cosine_sim(a, b):
a, b = np.array(a, dtype=float), np.array(b, dtype=float)
return float(np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b)))
def _to_pct(sim):
return round(max(0.0, min(1.0, (sim + 1) / 2)) * 100, 1)
def analyze(father_path, mother_path, child_path):
child_info = DeepFace.analyze(
child_path, actions=["age"], enforce_detection=False, silent=True
)
age = int(child_info[0]["age"])
child_emb = DeepFace.represent(
child_path, model_name=_MODEL, enforce_detection=False
)[0]["embedding"]
father_emb = DeepFace.represent(
father_path, model_name=_MODEL, enforce_detection=False
)[0]["embedding"]
mother_emb = DeepFace.represent(
mother_path, model_name=_MODEL, enforce_detection=False
)[0]["embedding"]
return {
"age": age,
"father_score": _to_pct(_cosine_sim(child_emb, father_emb)),
"mother_score": _to_pct(_cosine_sim(child_emb, mother_emb)),
}