02-ago: [proyecto] vigilancia - alineacion facial ArcFace (landmarks YuNet) + consenso 3 frames en alerta + enroll sin rellenar nombres fantasma. Prueba 45s: INES reconocida 80% frames

This commit is contained in:
minguezsanzjuanjose
2026-08-02 13:44:30 +02:00
parent 12b794ecfb
commit 821a673fe7
3 changed files with 47 additions and 9 deletions

View File

@@ -69,19 +69,48 @@ class Reconocedor:
return [c for c in caras if c[-1] >= 0.6]
def embedding_cara(self, frame, caja):
x, y, w, h = (int(v) for v in caja[:4])
x, y = max(0, x), max(0, y)
recorte = frame[y:y + h, x:x + w]
if recorte.size == 0:
alineada = self._alinear_cara(frame, caja)
if alineada is None:
return None
blob = cv2.dnn.blobFromImage(
recorte, 1.0 / 128, (112, 112), (127.5, 127.5, 127.5), swapRB=True
alineada, 1.0 / 128, (112, 112), (127.5, 127.5, 127.5), swapRB=True
)
self.arcface.setInput(blob)
emb = self.arcface.forward().flatten()
norm = np.linalg.norm(emb)
return emb / norm if norm > 0 else None
@staticmethod
def _alinear_cara(frame, caja):
"""Alinea el rostro con los landmarks (ArcFace espera 112x112 alineado)."""
if len(caja) < 15:
return None
pts = caja[4:14].reshape(5, 2)
if pts.min() < 0:
return None
# Template estandar de ArcFace (112x112)
dst = np.array([
[38.2946, 51.6963],
[73.5318, 51.5014],
[56.0252, 71.7366],
[41.5493, 92.3655],
[70.7299, 92.2041],
], dtype=np.float32)
# Orden YuNet: ojo der, ojo izq, nariz, comisura der, comisura izq
src = np.array([
pts[1], pts[0], pts[2], pts[4], pts[3],
], dtype=np.float32)
try:
M, _ = cv2.estimateAffinePartial2D(src, dst, method=cv2.LMEDS)
except Exception:
return None
if M is None:
return None
alineada = cv2.warpAffine(
frame, M, (112, 112), borderValue=0.0
)
return alineada
# ------------------------------------------------------------------
# Cuerpo
# ------------------------------------------------------------------