Spaces:
Build error
Build error
File size: 13,224 Bytes
8baf080 7823cea 016fe76 7823cea 2592e48 36f95d9 b6a67be 51fa2e3 36f95d9 9bc27e3 2592e48 51fa2e3 9c5866b 8baf080 9c5866b d9e1976 9c5866b a36d980 b6a67be d9e1976 b6a67be 51fa2e3 b6a67be 36f95d9 8baf080 51fa2e3 8baf080 51fa2e3 8baf080 d9e1976 8baf080 51fa2e3 b6a67be 8d88e43 ae0535c 8d88e43 37efbf7 288a128 6773de5 288a128 8baf080 288a128 97364cf 9c5866b e4617b7 8d88e43 288a128 e4617b7 8196356 8baf080 6c43e7e 288a128 6c43e7e 288a128 51fa2e3 b6a67be 8baf080 b6a67be 9c5866b b6a67be 9c5866b b6a67be 8196356 288a128 f5a1125 016fe76 6c43e7e 016fe76 6c43e7e b6a67be 288a128 6c43e7e 9c5866b 016fe76 6c43e7e 8baf080 9c5866b 8baf080 9c5866b 016fe76 288a128 016fe76 e4617b7 9c5866b 8baf080 97364cf d9e1976 7823cea d9e1976 59cebaf d9e1976 9bc27e3 d9e1976 ff6b580 d9e1976 288a128 bf687e5 7823cea d9e1976 bf687e5 b6a67be d9e1976 b6a67be d9e1976 b6a67be d9e1976 51fa2e3 b6a67be d9e1976 288a128 d9e1976 288a128 d9e1976 288a128 bf687e5 288a128 8baf080 8fed1b4 d9e1976 fe816e4 |
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 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 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 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 |
import os
import numpy as np
from PIL import Image
import gradio as gr
from deepface import DeepFace
from datasets import load_dataset
import pickle
from io import BytesIO
from huggingface_hub import upload_file, hf_hub_download, list_repo_files
from pathlib import Path
import gc
import requests
import time
import shutil
import tarfile
import tensorflow as tf
# Configuración de GPU
print("Dispositivos GPU disponibles:", tf.config.list_physical_devices('GPU'))
# Configurar memoria GPU
gpus = tf.config.list_physical_devices('GPU')
if gpus:
try:
# Permitir crecimiento de memoria
for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu, True)
print("✅ GPU configurada correctamente")
# Configurar para usar solo GPU
tf.config.set_visible_devices(gpus[0], 'GPU')
print(f"✅ Usando GPU: {gpus[0]}")
except RuntimeError as e:
print(f"⚠️ Error configurando GPU: {e}")
else:
print("⚠️ No se detectó GPU, usando CPU")
# Configurar para usar mixed precision
tf.keras.mixed_precision.set_global_policy('mixed_float16')
# 🔁 Limpiar almacenamiento temporal si existe
def clean_temp_dirs():
print("🧹 Limpiando carpetas temporales...")
for folder in ["embeddings", "batches"]:
path = Path(folder)
if path.exists() and path.is_dir():
shutil.rmtree(path)
print(f"✅ Carpeta eliminada: {folder}")
path.mkdir(exist_ok=True)
clean_temp_dirs()
# 📁 Parámetros
DATASET_ID = "Segizu/facial-recognition-preview"
EMBEDDINGS_SUBFOLDER = "embeddings"
LOCAL_EMB_DIR = Path("embeddings")
LOCAL_EMB_DIR.mkdir(exist_ok=True)
HF_TOKEN = os.getenv("HF_TOKEN")
headers = {"Authorization": f"Bearer {HF_TOKEN}"} if HF_TOKEN else {}
# 💾 Configuración
MAX_TEMP_STORAGE_GB = 40
UPLOAD_EVERY = 50
def get_folder_size(path):
total = 0
for dirpath, _, filenames in os.walk(path):
for f in filenames:
fp = os.path.join(dirpath, f)
total += os.path.getsize(fp)
return total / (1024 ** 3)
def preprocess_image(img: Image.Image) -> np.ndarray:
# Convertir a RGB si no lo es
if img.mode != 'RGB':
img = img.convert('RGB')
# Obtener la orientación EXIF si existe
try:
exif = img._getexif()
if exif is not None:
orientation = exif.get(274) # 274 es el tag de orientación en EXIF
if orientation is not None:
# Rotar la imagen según la orientación EXIF
if orientation == 3:
img = img.rotate(180, expand=True)
elif orientation == 6:
img = img.rotate(270, expand=True)
elif orientation == 8:
img = img.rotate(90, expand=True)
except:
pass # Si no hay EXIF o hay error, continuamos con la imagen original
# Intentar detectar la orientación del rostro
try:
# Convertir a array numpy para DeepFace
img_array = np.array(img)
# Detectar rostros con GPU
face_objs = DeepFace.extract_faces(
img_path=img_array,
target_size=(160, 160),
detector_backend='retinaface',
enforce_detection=False
)
if face_objs and len(face_objs) > 0:
# Si se detecta un rostro, usar la imagen detectada
img_array = face_objs[0]['face']
return img_array
except:
pass # Si falla la detección, continuamos con el procesamiento normal
# Si no se detectó rostro o falló la detección, redimensionar la imagen original
img_resized = img.resize((160, 160), Image.Resampling.LANCZOS)
return np.array(img_resized)
# ✅ Cargar CSV desde el dataset
dataset = load_dataset(
"csv",
data_files="metadata.csv",
split="train",
column_names=["image"],
header=0
)
def build_database():
print(f"📊 Uso actual de almacenamiento temporal INICIO: {get_folder_size('.'):.2f} GB")
print("🔄 Generando embeddings...")
batch_size = 10
archive_batch_size = 50
batch_files = []
batch_index = 0
ARCHIVE_DIR = Path("batches")
ARCHIVE_DIR.mkdir(exist_ok=True)
for i in range(0, len(dataset), batch_size):
batch = dataset[i:i + batch_size]
print(f"📦 Lote {i // batch_size + 1}/{(len(dataset) + batch_size - 1) // batch_size}")
for j in range(len(batch["image"])):
image_url = batch["image"][j]
if not isinstance(image_url, str) or not image_url.startswith("http") or image_url.strip().lower() == "image":
print(f"⚠️ Saltando {i + j} - URL inválida: {image_url}")
continue
name = f"image_{i + j}"
filename = LOCAL_EMB_DIR / f"{name}.pkl"
# Verificar si ya fue subido
try:
hf_hub_download(
repo_id=DATASET_ID,
repo_type="dataset",
filename=f"{EMBEDDINGS_SUBFOLDER}/batch_{batch_index:03}.tar.gz",
token=HF_TOKEN
)
print(f"⏩ Ya existe en remoto: {name}.pkl")
continue
except:
pass
try:
response = requests.get(image_url, headers=headers, timeout=10)
response.raise_for_status()
img = Image.open(BytesIO(response.content)).convert("RGB")
img_processed = preprocess_image(img)
embedding = DeepFace.represent(
img_path=img_processed,
model_name="Facenet",
enforce_detection=False
)[0]["embedding"]
with open(filename, "wb") as f:
pickle.dump({"name": name, "img": img, "embedding": embedding}, f)
batch_files.append(filename)
del img_processed
gc.collect()
if len(batch_files) >= archive_batch_size or get_folder_size(".") > MAX_TEMP_STORAGE_GB:
archive_path = ARCHIVE_DIR / f"batch_{batch_index:03}.tar.gz"
with tarfile.open(archive_path, "w:gz") as tar:
for file in batch_files:
tar.add(file, arcname=file.name)
print(f"📦 Empaquetado: {archive_path}")
upload_file(
path_or_fileobj=str(archive_path),
path_in_repo=f"{EMBEDDINGS_SUBFOLDER}/{archive_path.name}",
repo_id=DATASET_ID,
repo_type="dataset",
token=HF_TOKEN
)
print(f"✅ Subido: {archive_path.name}")
for f in batch_files:
f.unlink()
archive_path.unlink()
print("🧹 Limpieza completada tras subida")
batch_files = []
batch_index += 1
time.sleep(2)
print(f"📊 Uso actual FINAL: {get_folder_size('.'):.2f} GB")
except Exception as e:
print(f"❌ Error en {name}: {e}")
continue
if batch_files:
archive_path = ARCHIVE_DIR / f"batch_{batch_index:03}.tar.gz"
with tarfile.open(archive_path, "w:gz") as tar:
for file in batch_files:
tar.add(file, arcname=file.name)
print(f"📦 Empaquetado final: {archive_path}")
upload_file(
path_or_fileobj=str(archive_path),
path_in_repo=f"{EMBEDDINGS_SUBFOLDER}/{archive_path.name}",
repo_id=DATASET_ID,
repo_type="dataset",
token=HF_TOKEN
)
for f in batch_files:
f.unlink()
archive_path.unlink()
print("✅ Subida y limpieza final")
# 🔍 Buscar similitudes
def find_similar_faces(uploaded_image: Image.Image):
if uploaded_image is None:
return [], "⚠ Por favor, sube una imagen primero"
try:
print("🔄 Procesando imagen de entrada...")
# Convertir a RGB si no lo es
if uploaded_image.mode != 'RGB':
uploaded_image = uploaded_image.convert('RGB')
# Mostrar dimensiones de la imagen
print(f"📐 Dimensiones de la imagen: {uploaded_image.size}")
img_processed = preprocess_image(uploaded_image)
print("✅ Imagen preprocesada correctamente")
# Intentar primero con enforce_detection=True
try:
query_embedding = DeepFace.represent(
img_path=img_processed,
model_name="Facenet",
enforce_detection=True,
detector_backend='retinaface'
)[0]["embedding"]
print("✅ Rostro detectado con enforce_detection=True")
except Exception as e:
print(f"⚠ No se pudo detectar rostro con enforce_detection=True, intentando con False: {str(e)}")
# Si falla, intentar con enforce_detection=False
query_embedding = DeepFace.represent(
img_path=img_processed,
model_name="Facenet",
enforce_detection=False,
detector_backend='retinaface'
)[0]["embedding"]
print("✅ Embedding generado con enforce_detection=False")
del img_processed
gc.collect()
except Exception as e:
print(f"❌ Error en procesamiento de imagen: {str(e)}")
return [], f"⚠ Error procesando imagen: {str(e)}"
similarities = []
print("🔍 Buscando similitudes en la base de datos...")
try:
embedding_files = [
f for f in list_repo_files(DATASET_ID, repo_type="dataset", token=HF_TOKEN)
if f.startswith(f"{EMBEDDINGS_SUBFOLDER}/") and f.endswith(".tar.gz")
]
print(f"📁 Encontrados {len(embedding_files)} archivos de embeddings")
except Exception as e:
print(f"❌ Error obteniendo archivos: {str(e)}")
return [], f"⚠ Error obteniendo archivos: {str(e)}"
# Procesar en lotes para mejor rendimiento
batch_size = 10
for i in range(0, len(embedding_files), batch_size):
batch_files = embedding_files[i:i + batch_size]
print(f"📦 Procesando lote {i//batch_size + 1}/{(len(embedding_files) + batch_size - 1)//batch_size}")
for file_path in batch_files:
try:
file_bytes = requests.get(
f"https://huggingface.co/datasets/{DATASET_ID}/resolve/main/{file_path}",
headers=headers,
timeout=30
).content
# Crear un archivo temporal para el tar.gz
temp_archive = Path("temp_archive.tar.gz")
with open(temp_archive, "wb") as f:
f.write(file_bytes)
# Extraer el contenido
with tarfile.open(temp_archive, "r:gz") as tar:
tar.extractall(path="temp_extract")
# Procesar cada archivo .pkl en el tar
for pkl_file in Path("temp_extract").glob("*.pkl"):
with open(pkl_file, "rb") as f:
record = pickle.load(f)
name = record["name"]
img = record["img"]
emb = record["embedding"]
dist = np.linalg.norm(np.array(query_embedding) - np.array(emb))
sim_score = 1 / (1 + dist)
similarities.append((sim_score, name, np.array(img)))
# Limpiar archivos temporales
shutil.rmtree("temp_extract")
temp_archive.unlink()
except Exception as e:
print(f"⚠ Error procesando {file_path}: {e}")
continue
if not similarities:
return [], "⚠ No se encontraron similitudes en la base de datos"
print(f"✅ Encontradas {len(similarities)} similitudes")
similarities.sort(reverse=True)
top = similarities[:5]
gallery = [(img, f"{name} - Similitud: {sim:.2f}") for sim, name, img in top]
summary = "\n".join([f"{name} - Similitud: {sim:.2f}" for sim, name, _ in top])
return gallery, summary
# 🎛️ Interfaz Gradio
with gr.Blocks() as demo:
gr.Markdown("## 🔍 Reconocimiento facial con DeepFace + ZeroGPU")
with gr.Row():
image_input = gr.Image(label="📤 Sube una imagen", type="pil")
find_btn = gr.Button("🔎 Buscar similares")
gallery = gr.Gallery(label="📸 Rostros similares")
summary = gr.Textbox(label="🧠 Detalle", lines=6)
find_btn.click(fn=find_similar_faces, inputs=image_input, outputs=[gallery, summary])
with gr.Row():
build_btn = gr.Button("⚙️ Construir base de embeddings (usa GPU)")
build_btn.click(fn=build_database, inputs=[], outputs=[])
demo.launch(share=True)
|