Spaces:
Sleeping
Sleeping
Create insightface_verification.py
Browse files- insightface_verification.py +37 -0
insightface_verification.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import numpy as np
|
| 3 |
+
import insightface
|
| 4 |
+
|
| 5 |
+
model = insightface.app.FaceAnalysis(name="buffalo_l", providers=["CPUExecutionProvider"])
|
| 6 |
+
model.prepare(ctx_id=0)
|
| 7 |
+
|
| 8 |
+
def verify_face_and_liveness(image_path, template_path, threshold=0.5):
|
| 9 |
+
img = cv2.imread(image_path)
|
| 10 |
+
if img is None:
|
| 11 |
+
raise ValueError("❌ Image illisible")
|
| 12 |
+
|
| 13 |
+
faces = model.get(img)
|
| 14 |
+
if not faces:
|
| 15 |
+
print("⚠️ Aucun visage détecté dans l'image.")
|
| 16 |
+
return False, False, 0.0
|
| 17 |
+
|
| 18 |
+
face = faces[0]
|
| 19 |
+
|
| 20 |
+
# Récupération sécurisée du score de liveness
|
| 21 |
+
liveness = getattr(face, "liveness_score", None)
|
| 22 |
+
if liveness is None:
|
| 23 |
+
print("⚠️ Liveness non disponible (None détecté) — valeur par défaut = 0.0")
|
| 24 |
+
liveness = 0.0
|
| 25 |
+
else:
|
| 26 |
+
print(f"🔍 Liveness score : {liveness:.4f}")
|
| 27 |
+
|
| 28 |
+
is_real = liveness > threshold
|
| 29 |
+
|
| 30 |
+
emb = face.embedding
|
| 31 |
+
template = np.load(template_path)
|
| 32 |
+
|
| 33 |
+
# Similarité cosinus
|
| 34 |
+
similarity = np.dot(template, emb) / (np.linalg.norm(template) * np.linalg.norm(emb))
|
| 35 |
+
is_match = similarity >= threshold
|
| 36 |
+
|
| 37 |
+
return is_match, is_real, similarity
|