Spaces:
Build error
Build error
metadata v12
Browse files
app.py
CHANGED
@@ -50,39 +50,42 @@ def build_database():
|
|
50 |
batch = dataset[i:i + batch_size]
|
51 |
print(f"📦 Procesando lote {i // batch_size + 1}/{(len(dataset) + batch_size - 1) // batch_size}")
|
52 |
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
image_url = item["image"]
|
60 |
-
if not isinstance(image_url, str) or not image_url.startswith("http"):
|
61 |
-
print(f"⚠️ Saltando item {i + j} - URL inválida: {image_url}")
|
62 |
-
continue
|
63 |
-
|
64 |
-
# Descargar imagen desde la URL
|
65 |
-
response = requests.get(image_url, timeout=10)
|
66 |
-
response.raise_for_status()
|
67 |
-
img = Image.open(BytesIO(response.content)).convert("RGB")
|
68 |
-
|
69 |
-
img_processed = preprocess_image(img)
|
70 |
-
embedding = DeepFace.represent(
|
71 |
-
img_path=img_processed,
|
72 |
-
model_name="Facenet",
|
73 |
-
enforce_detection=False
|
74 |
-
)[0]["embedding"]
|
75 |
-
|
76 |
-
database.append((f"image_{i + j}", img, embedding))
|
77 |
-
print(f"✅ Procesada imagen {i + j + 1}/{len(dataset)}")
|
78 |
-
|
79 |
-
del img_processed
|
80 |
-
gc.collect()
|
81 |
-
|
82 |
-
except Exception as e:
|
83 |
-
print(f"❌ Error al procesar imagen {i + j}: {str(e)}")
|
84 |
continue
|
85 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
# Guardar después de cada batch
|
87 |
if database:
|
88 |
print("💾 Guardando embeddings...")
|
|
|
50 |
batch = dataset[i:i + batch_size]
|
51 |
print(f"📦 Procesando lote {i // batch_size + 1}/{(len(dataset) + batch_size - 1) // batch_size}")
|
52 |
|
53 |
+
for j, item in enumerate(batch):
|
54 |
+
try:
|
55 |
+
# Validar estructura
|
56 |
+
if not isinstance(item, dict) or "image" not in item:
|
57 |
+
print(f"⚠️ Saltando item {i + j} - estructura inválida: {item}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
continue
|
59 |
|
60 |
+
image_url = item["image"]
|
61 |
+
|
62 |
+
# Validar tipo y formato
|
63 |
+
if not isinstance(image_url, str) or not image_url.startswith("http"):
|
64 |
+
print(f"⚠️ Saltando item {i + j} - URL inválida: {image_url}")
|
65 |
+
continue
|
66 |
+
|
67 |
+
# Descargar y procesar imagen
|
68 |
+
response = requests.get(image_url, timeout=10)
|
69 |
+
response.raise_for_status()
|
70 |
+
img = Image.open(BytesIO(response.content)).convert("RGB")
|
71 |
+
|
72 |
+
img_processed = preprocess_image(img)
|
73 |
+
embedding = DeepFace.represent(
|
74 |
+
img_path=img_processed,
|
75 |
+
model_name="Facenet",
|
76 |
+
enforce_detection=False
|
77 |
+
)[0]["embedding"]
|
78 |
+
|
79 |
+
database.append((f"image_{i + j}", img, embedding))
|
80 |
+
print(f"✅ Procesada imagen {i + j + 1}/{len(dataset)}")
|
81 |
+
|
82 |
+
del img_processed
|
83 |
+
gc.collect()
|
84 |
+
|
85 |
+
except Exception as e:
|
86 |
+
print(f"❌ Error al procesar imagen {i + j}: {str(e)}")
|
87 |
+
continue
|
88 |
+
|
89 |
# Guardar después de cada batch
|
90 |
if database:
|
91 |
print("💾 Guardando embeddings...")
|