Spaces:
Runtime error
Runtime error
File size: 5,456 Bytes
7823cea e4617b7 7823cea 1194d1c b78ec6d 36f95d9 9bc27e3 e4617b7 a36d980 1194d1c a36d980 1194d1c 78e29c2 36f95d9 1194d1c 97364cf 59cebaf 97364cf 59cebaf fff17ea 7823cea 78e29c2 36f95d9 e4617b7 36f95d9 7823cea 97364cf e4617b7 752c0fd e30a3b3 752c0fd e30a3b3 752c0fd e4617b7 9bc27e3 38ec03b 14e3122 c177ec8 14e3122 57ae169 38ec03b 14e3122 57ae169 faa12b9 510e6ef 9bc27e3 e4617b7 9bc27e3 752c0fd e4617b7 9bc27e3 e4617b7 9bc27e3 ff6b580 e30a3b3 ff6b580 e4617b7 1194d1c 97364cf ff6b580 e4617b7 9bc27e3 e4617b7 7823cea fff17ea 97364cf 7823cea 59cebaf fff17ea 59cebaf fff17ea 9bc27e3 ff6b580 7823cea bf687e5 7823cea fff17ea bf687e5 7823cea 36f95d9 7823cea bf687e5 7823cea 17df602 7823cea d771ba3 7823cea bf687e5 1194d1c 36f95d9 fff17ea 36f95d9 fff17ea bf687e5 7823cea bf687e5 f134081 7823cea bf687e5 7823cea 36f95d9 bf687e5 8fed1b4 |
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 |
import numpy as np
from PIL import Image, UnidentifiedImageError
import gradio as gr
from deepface import DeepFace
from datasets import load_dataset, Image as HfImage
import os
import pickle
from pathlib import Path
import gc
import requests
from io import BytesIO
# 🔑 Token de autenticación
HF_TOKEN = os.getenv("HF_TOKEN")
if not HF_TOKEN:
raise ValueError("⚠️ Por favor, configura la variable de entorno HF_TOKEN para acceder al dataset privado")
# 📁 Directorio para embeddings
EMBEDDINGS_DIR = Path("embeddings")
EMBEDDINGS_DIR.mkdir(exist_ok=True)
EMBEDDINGS_FILE = EMBEDDINGS_DIR / "embeddings.pkl"
# ✅ Cargar dataset desde metadata.csv (con URLs absolutas)
dataset = load_dataset("csv", data_files="metadata.csv")
# 🔄 Preprocesar imagen para Facenet
def preprocess_image(img: Image.Image) -> np.ndarray:
img_rgb = img.convert("RGB")
img_resized = img_rgb.resize((160, 160), Image.Resampling.LANCZOS)
return np.array(img_resized)
# 📦 Construir base de datos de embeddings
def build_database():
if EMBEDDINGS_FILE.exists():
print("📂 Cargando embeddings desde el archivo...")
with open(EMBEDDINGS_FILE, 'rb') as f:
return pickle.load(f)
print("🔄 Calculando embeddings (esto puede tomar unos minutos)...")
database = []
batch_size = 10
# Get the train split
train_dataset = dataset["train"]
# Debug: Print dataset structure
print("Dataset structure:", train_dataset.features)
print("First item structure:", train_dataset[0])
print("Dataset type:", type(train_dataset))
print("Dataset item type:", type(train_dataset[0]))
for i in range(0, len(train_dataset), batch_size):
batch = train_dataset[i:i + batch_size]
print(f"📦 Procesando lote {i // batch_size + 1}/{(len(train_dataset) + batch_size - 1) // batch_size}")
for j, item in enumerate(batch):
try:
print(f"Debug - Processing item {i+j}")
print(f"Debug - Item type: {type(item)}")
print(f"Debug - Item content: {item}")
# Get the image URL
image_url = item["image"]
if not isinstance(image_url, str) or not image_url.startswith("http"):
print(f"⚠️ Skipping item {i+j} - Invalid URL format")
continue
# Download and process the image
response = requests.get(image_url, timeout=10)
response.raise_for_status()
img = Image.open(BytesIO(response.content))
# Ensure image is in RGB mode
img = img.convert("RGB")
img_processed = preprocess_image(img)
embedding = DeepFace.represent(
img_path=img_processed,
model_name="Facenet",
enforce_detection=False
)[0]["embedding"]
database.append((f"image_{i+j}", img, embedding))
print(f"✅ Procesada imagen {i+j+1}/{len(train_dataset)}")
del img_processed
gc.collect()
except Exception as e:
print(f"❌ No se pudo procesar imagen {i+j}: {str(e)}")
print(f"Error details: {type(e).__name__}")
import traceback
print(traceback.format_exc())
continue
# 💾 Guardar después de cada batch
if database:
print("💾 Guardando progreso...")
with open(EMBEDDINGS_FILE, 'wb') as f:
pickle.dump(database, f)
gc.collect()
return database
# 🔍 Buscar rostros similares
def find_similar_faces(uploaded_image: Image.Image):
try:
img_processed = preprocess_image(uploaded_image)
query_embedding = DeepFace.represent(
img_path=img_processed,
model_name="Facenet",
enforce_detection=False
)[0]["embedding"]
del img_processed
gc.collect()
except Exception as e:
print(f"Error al procesar imagen de consulta: {str(e)}")
return [], "⚠ No se detectó un rostro válido en la imagen."
similarities = []
for name, db_img, embedding in database:
dist = np.linalg.norm(np.array(query_embedding) - np.array(embedding))
sim_score = 1 / (1 + dist)
similarities.append((sim_score, name, db_img))
similarities.sort(reverse=True)
top_matches = similarities[:5]
gallery_items = []
text_summary = ""
for sim, name, img in top_matches:
caption = f"{name} - Similitud: {sim:.2f}"
gallery_items.append((img, caption))
text_summary += caption + "\n"
return gallery_items, text_summary
# ⚙️ Inicializar
print("🚀 Iniciando aplicación...")
database = build_database()
print(f"✅ Base de datos cargada con {len(database)} imágenes")
# 🎛️ Interfaz Gradio
demo = gr.Interface(
fn=find_similar_faces,
inputs=gr.Image(label="📤 Sube una imagen", type="pil"),
outputs=[
gr.Gallery(label="📸 Rostros más similares"),
gr.Textbox(label="🧠 Similitud", lines=6)
],
title="🔍 Buscador de Rostros con DeepFace",
description="Sube una imagen y se comparará contra los rostros del dataset alojado en Hugging Face (`Segizu/facial-recognition`)."
)
demo.launch()
|