Spaces:
Sleeping
Sleeping
File size: 1,061 Bytes
e793c54 | 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 | 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)),
}
|