Upload 3 files
Browse files- face_classifier.py +260 -215
face_classifier.py
CHANGED
|
@@ -1,215 +1,260 @@
|
|
| 1 |
-
"""
|
| 2 |
-
Face Classifier Module
|
| 3 |
-
Valida caras y detecta género usando DeepFace para filtrar falsos positivos
|
| 4 |
-
y asignar nombres automáticos según el género detectado.
|
| 5 |
-
"""
|
| 6 |
-
import logging
|
| 7 |
-
from typing import Optional, Dict, Any
|
| 8 |
-
|
| 9 |
-
logging.basicConfig(level=logging.INFO)
|
| 10 |
-
logger = logging.getLogger(__name__)
|
| 11 |
-
|
| 12 |
-
# Configuración de thresholds
|
| 13 |
-
# FACE_CONFIDENCE_THRESHOLD: Confianza mínima para aceptar una cara
|
| 14 |
-
# Valores: 0.
|
| 15 |
-
# 0.
|
| 16 |
-
# 0.
|
| 17 |
-
# 0.
|
| 18 |
-
FACE_CONFIDENCE_THRESHOLD = 0.
|
| 19 |
-
GENDER_NEUTRAL_THRESHOLD = 0.2 # Diferencia mínima para género neutro
|
| 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 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
'
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
print(f"[DeepFace]
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Face Classifier Module
|
| 3 |
+
Valida caras y detecta género usando DeepFace para filtrar falsos positivos
|
| 4 |
+
y asignar nombres automáticos según el género detectado.
|
| 5 |
+
"""
|
| 6 |
+
import logging
|
| 7 |
+
from typing import Optional, Dict, Any
|
| 8 |
+
|
| 9 |
+
logging.basicConfig(level=logging.INFO)
|
| 10 |
+
logger = logging.getLogger(__name__)
|
| 11 |
+
|
| 12 |
+
# Configuración de thresholds
|
| 13 |
+
# FACE_CONFIDENCE_THRESHOLD: Confianza mínima para aceptar una cara
|
| 14 |
+
# Valores: 0.5 = permisivo (acepta muchos falsos positivos)
|
| 15 |
+
# 0.7 = balanceado
|
| 16 |
+
# 0.85 = estricto (elimina falsos positivos pero puede perder caras reales)
|
| 17 |
+
# 0.92 = MUY estricto (solo caras muy claras)
|
| 18 |
+
FACE_CONFIDENCE_THRESHOLD = 0.92 # MUY ESTRICTO: preferir perder caras a tener falsos positivos
|
| 19 |
+
GENDER_NEUTRAL_THRESHOLD = 0.2 # Diferencia mínima para género neutro
|
| 20 |
+
|
| 21 |
+
# Configuración adicional de filtrado
|
| 22 |
+
MIN_FACE_SIZE_PIXELS = 48 # Tamaño mínimo de cara en píxeles (ancho o alto)
|
| 23 |
+
MAX_ASPECT_RATIO = 2.0 # Proporción máxima ancho/alto (caras reales ~0.7-1.3)
|
| 24 |
+
MIN_ASPECT_RATIO = 0.5 # Proporción mínima ancho/alto
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def validate_and_classify_face(image_path: str) -> Optional[Dict[str, Any]]:
|
| 28 |
+
"""
|
| 29 |
+
Valida si és una cara real i detecta el gènere usant DeepFace.
|
| 30 |
+
Usa extract_faces() para obtener score de confianza REAL de detección.
|
| 31 |
+
|
| 32 |
+
Args:
|
| 33 |
+
image_path: Ruta a la imagen de la cara
|
| 34 |
+
|
| 35 |
+
Returns:
|
| 36 |
+
Dict amb: {
|
| 37 |
+
'is_valid_face': bool, # True si és una cara amb confiança alta
|
| 38 |
+
'face_confidence': float, # Score de detecció de cara (0-1)
|
| 39 |
+
'gender': 'Man' | 'Woman' | 'Neutral',
|
| 40 |
+
'gender_confidence': float, # Score de confiança del gènere (0-1)
|
| 41 |
+
'man_prob': float,
|
| 42 |
+
'woman_prob': float,
|
| 43 |
+
'rejection_reason': str | None # Motivo de rechazo si is_valid_face=False
|
| 44 |
+
}
|
| 45 |
+
o None si falla completament
|
| 46 |
+
"""
|
| 47 |
+
try:
|
| 48 |
+
import cv2
|
| 49 |
+
import numpy as np
|
| 50 |
+
from deepface import DeepFace
|
| 51 |
+
|
| 52 |
+
print(f"[DeepFace] Analitzant: {image_path}")
|
| 53 |
+
|
| 54 |
+
# Cargar imagen para verificaciones de tamaño
|
| 55 |
+
img = cv2.imread(str(image_path))
|
| 56 |
+
if img is None:
|
| 57 |
+
print(f"[DeepFace] No se pudo cargar la imagen: {image_path}")
|
| 58 |
+
return None
|
| 59 |
+
|
| 60 |
+
img_height, img_width = img.shape[:2]
|
| 61 |
+
print(f"[DeepFace] Tamaño imagen: {img_width}x{img_height}")
|
| 62 |
+
|
| 63 |
+
# VERIFICACIÓN 1: Tamaño mínimo de imagen
|
| 64 |
+
if img_width < MIN_FACE_SIZE_PIXELS or img_height < MIN_FACE_SIZE_PIXELS:
|
| 65 |
+
print(f"[DeepFace] ✗ RECHAZADA: Imagen demasiado pequeña ({img_width}x{img_height} < {MIN_FACE_SIZE_PIXELS}px)")
|
| 66 |
+
return {
|
| 67 |
+
'is_valid_face': False,
|
| 68 |
+
'face_confidence': 0.0,
|
| 69 |
+
'gender': 'Neutral',
|
| 70 |
+
'gender_confidence': 0.0,
|
| 71 |
+
'man_prob': 0.0,
|
| 72 |
+
'woman_prob': 0.0,
|
| 73 |
+
'rejection_reason': f'Imagen demasiado pequeña ({img_width}x{img_height})'
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
# VERIFICACIÓN 2: Proporción de la imagen (caras reales tienen proporción ~0.7-1.3)
|
| 77 |
+
aspect_ratio = img_width / img_height
|
| 78 |
+
if aspect_ratio > MAX_ASPECT_RATIO or aspect_ratio < MIN_ASPECT_RATIO:
|
| 79 |
+
print(f"[DeepFace] ✗ RECHAZADA: Proporción anómala ({aspect_ratio:.2f}, esperado {MIN_ASPECT_RATIO}-{MAX_ASPECT_RATIO})")
|
| 80 |
+
return {
|
| 81 |
+
'is_valid_face': False,
|
| 82 |
+
'face_confidence': 0.0,
|
| 83 |
+
'gender': 'Neutral',
|
| 84 |
+
'gender_confidence': 0.0,
|
| 85 |
+
'man_prob': 0.0,
|
| 86 |
+
'woman_prob': 0.0,
|
| 87 |
+
'rejection_reason': f'Proporción anómala ({aspect_ratio:.2f})'
|
| 88 |
+
}
|
| 89 |
+
|
| 90 |
+
# PASO 1: Usar extract_faces() para obtener score de confianza REAL
|
| 91 |
+
# Este es el método correcto para obtener el confidence score de detección
|
| 92 |
+
print(f"[DeepFace] Ejecutando extract_faces() para obtener confidence score...")
|
| 93 |
+
|
| 94 |
+
try:
|
| 95 |
+
faces = DeepFace.extract_faces(
|
| 96 |
+
img_path=str(image_path),
|
| 97 |
+
detector_backend='retinaface', # Más preciso que opencv
|
| 98 |
+
enforce_detection=True,
|
| 99 |
+
align=True
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
if not faces:
|
| 103 |
+
print(f"[DeepFace] ✗ extract_faces() no detectó ninguna cara")
|
| 104 |
+
return {
|
| 105 |
+
'is_valid_face': False,
|
| 106 |
+
'face_confidence': 0.0,
|
| 107 |
+
'gender': 'Neutral',
|
| 108 |
+
'gender_confidence': 0.0,
|
| 109 |
+
'man_prob': 0.0,
|
| 110 |
+
'woman_prob': 0.0,
|
| 111 |
+
'rejection_reason': 'No se detectó cara con retinaface'
|
| 112 |
+
}
|
| 113 |
+
|
| 114 |
+
# Tomar la cara con mayor confianza
|
| 115 |
+
best_face = max(faces, key=lambda f: f.get('confidence', 0))
|
| 116 |
+
face_confidence = best_face.get('confidence', 0.0)
|
| 117 |
+
facial_area = best_face.get('facial_area', {})
|
| 118 |
+
|
| 119 |
+
print(f"[DeepFace] extract_faces() encontró {len(faces)} cara(s)")
|
| 120 |
+
print(f"[DeepFace] Mejor cara - confidence: {face_confidence:.4f}")
|
| 121 |
+
print(f"[DeepFace] Facial area: {facial_area}")
|
| 122 |
+
|
| 123 |
+
# VERIFICACIÓN 3: Score de confianza de detección
|
| 124 |
+
if face_confidence < FACE_CONFIDENCE_THRESHOLD:
|
| 125 |
+
print(f"[DeepFace] ✗ RECHAZADA: Confianza baja ({face_confidence:.4f} < {FACE_CONFIDENCE_THRESHOLD})")
|
| 126 |
+
return {
|
| 127 |
+
'is_valid_face': False,
|
| 128 |
+
'face_confidence': face_confidence,
|
| 129 |
+
'gender': 'Neutral',
|
| 130 |
+
'gender_confidence': 0.0,
|
| 131 |
+
'man_prob': 0.0,
|
| 132 |
+
'woman_prob': 0.0,
|
| 133 |
+
'rejection_reason': f'Confianza baja ({face_confidence:.4f})'
|
| 134 |
+
}
|
| 135 |
+
|
| 136 |
+
# VERIFICACIÓN 4: Tamaño del área facial detectada
|
| 137 |
+
face_w = facial_area.get('w', 0)
|
| 138 |
+
face_h = facial_area.get('h', 0)
|
| 139 |
+
if face_w < MIN_FACE_SIZE_PIXELS * 0.5 or face_h < MIN_FACE_SIZE_PIXELS * 0.5:
|
| 140 |
+
print(f"[DeepFace] ✗ RECHAZADA: Área facial muy pequeña ({face_w}x{face_h})")
|
| 141 |
+
return {
|
| 142 |
+
'is_valid_face': False,
|
| 143 |
+
'face_confidence': face_confidence,
|
| 144 |
+
'gender': 'Neutral',
|
| 145 |
+
'gender_confidence': 0.0,
|
| 146 |
+
'man_prob': 0.0,
|
| 147 |
+
'woman_prob': 0.0,
|
| 148 |
+
'rejection_reason': f'Área facial muy pequeña ({face_w}x{face_h})'
|
| 149 |
+
}
|
| 150 |
+
|
| 151 |
+
except ValueError as e:
|
| 152 |
+
print(f"[DeepFace] ✗ extract_faces() ValueError: {e}")
|
| 153 |
+
return {
|
| 154 |
+
'is_valid_face': False,
|
| 155 |
+
'face_confidence': 0.0,
|
| 156 |
+
'gender': 'Neutral',
|
| 157 |
+
'gender_confidence': 0.0,
|
| 158 |
+
'man_prob': 0.0,
|
| 159 |
+
'woman_prob': 0.0,
|
| 160 |
+
'rejection_reason': f'extract_faces falló: {e}'
|
| 161 |
+
}
|
| 162 |
+
|
| 163 |
+
# PASO 2: Analizar género (solo si pasó las verificaciones anteriores)
|
| 164 |
+
print(f"[DeepFace] ✓ Cara válida, analizando género...")
|
| 165 |
+
|
| 166 |
+
try:
|
| 167 |
+
result = DeepFace.analyze(
|
| 168 |
+
img_path=str(image_path),
|
| 169 |
+
actions=['gender'],
|
| 170 |
+
enforce_detection=False, # Ya verificamos que hay cara
|
| 171 |
+
detector_backend='skip', # Saltar detección, ya la hicimos
|
| 172 |
+
silent=True
|
| 173 |
+
)
|
| 174 |
+
|
| 175 |
+
if isinstance(result, list):
|
| 176 |
+
result = result[0] if result else {}
|
| 177 |
+
|
| 178 |
+
gender_info = result.get('gender', {})
|
| 179 |
+
|
| 180 |
+
if isinstance(gender_info, dict):
|
| 181 |
+
man_prob = gender_info.get('Man', 0) / 100.0
|
| 182 |
+
woman_prob = gender_info.get('Woman', 0) / 100.0
|
| 183 |
+
else:
|
| 184 |
+
man_prob = 0.5
|
| 185 |
+
woman_prob = 0.5
|
| 186 |
+
|
| 187 |
+
gender_diff = abs(man_prob - woman_prob)
|
| 188 |
+
|
| 189 |
+
if gender_diff < GENDER_NEUTRAL_THRESHOLD:
|
| 190 |
+
gender = 'Neutral'
|
| 191 |
+
gender_confidence = 0.5
|
| 192 |
+
else:
|
| 193 |
+
gender = 'Man' if man_prob > woman_prob else 'Woman'
|
| 194 |
+
gender_confidence = max(man_prob, woman_prob)
|
| 195 |
+
|
| 196 |
+
except Exception as e:
|
| 197 |
+
print(f"[DeepFace] Análisis de género falló: {e}, usando valores neutros")
|
| 198 |
+
gender = 'Neutral'
|
| 199 |
+
gender_confidence = 0.5
|
| 200 |
+
man_prob = 0.5
|
| 201 |
+
woman_prob = 0.5
|
| 202 |
+
|
| 203 |
+
print(f"[DeepFace] ===== RESUMEN FINAL =====")
|
| 204 |
+
print(f"[DeepFace] ✓ is_valid_face: True")
|
| 205 |
+
print(f"[DeepFace] face_confidence: {face_confidence:.4f} (threshold: {FACE_CONFIDENCE_THRESHOLD})")
|
| 206 |
+
print(f"[DeepFace] gender: {gender}")
|
| 207 |
+
print(f"[DeepFace] gender_confidence: {gender_confidence:.3f}")
|
| 208 |
+
print(f"[DeepFace] ==========================")
|
| 209 |
+
|
| 210 |
+
return {
|
| 211 |
+
'is_valid_face': True,
|
| 212 |
+
'face_confidence': face_confidence,
|
| 213 |
+
'gender': gender,
|
| 214 |
+
'gender_confidence': gender_confidence,
|
| 215 |
+
'man_prob': man_prob,
|
| 216 |
+
'woman_prob': woman_prob,
|
| 217 |
+
'rejection_reason': None
|
| 218 |
+
}
|
| 219 |
+
|
| 220 |
+
except Exception as e:
|
| 221 |
+
print(f"[DeepFace] Error validant cara: {e}")
|
| 222 |
+
return None
|
| 223 |
+
|
| 224 |
+
|
| 225 |
+
def get_random_catalan_name_by_gender(gender: str, seed_value: str = "") -> str:
|
| 226 |
+
"""
|
| 227 |
+
Genera un nom català aleatori basat en el gènere.
|
| 228 |
+
|
| 229 |
+
Args:
|
| 230 |
+
gender: 'Man', 'Woman', o 'Neutral'
|
| 231 |
+
seed_value: Valor per fer el random determinista (opcional)
|
| 232 |
+
|
| 233 |
+
Returns:
|
| 234 |
+
Nom català
|
| 235 |
+
"""
|
| 236 |
+
noms_home = [
|
| 237 |
+
"Jordi", "Marc", "Pau", "Pere", "Joan", "Josep", "David", "Guillem", "Albert",
|
| 238 |
+
"Arnau", "Martí", "Bernat", "Oriol", "Roger", "Pol", "Lluís", "Sergi", "Carles", "Xavier"
|
| 239 |
+
]
|
| 240 |
+
noms_dona = [
|
| 241 |
+
"Maria", "Anna", "Laura", "Marta", "Cristina", "Núria", "Montserrat", "Júlia", "Sara", "Carla",
|
| 242 |
+
"Alba", "Elisabet", "Rosa", "Gemma", "Sílvia", "Teresa", "Irene", "Laia", "Marina", "Bet"
|
| 243 |
+
]
|
| 244 |
+
noms_neutre = ["Àlex", "Andrea", "Francis", "Cris", "Noa"]
|
| 245 |
+
|
| 246 |
+
# Seleccionar llista segons gènere
|
| 247 |
+
if gender == 'Woman':
|
| 248 |
+
noms = noms_dona
|
| 249 |
+
elif gender == 'Man':
|
| 250 |
+
noms = noms_home
|
| 251 |
+
else: # Neutral
|
| 252 |
+
noms = noms_neutre
|
| 253 |
+
|
| 254 |
+
# Usar hash del seed per seleccionar nom de forma determinista
|
| 255 |
+
if seed_value:
|
| 256 |
+
hash_val = hash(seed_value)
|
| 257 |
+
return noms[abs(hash_val) % len(noms)]
|
| 258 |
+
else:
|
| 259 |
+
import random
|
| 260 |
+
return random.choice(noms)
|