Spaces:
Runtime error
Runtime error
no cache embeddings baches
Browse files
app.py
CHANGED
@@ -7,6 +7,7 @@ import os
|
|
7 |
import pickle
|
8 |
from pathlib import Path
|
9 |
import gc
|
|
|
10 |
|
11 |
# 🔑 Configurar token de Hugging Face
|
12 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
@@ -31,6 +32,13 @@ if "train" in dataset:
|
|
31 |
|
32 |
# 🔄 Preprocesar imagen para Facenet
|
33 |
def preprocess_image(img):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
img_rgb = img.convert("RGB")
|
35 |
img_resized = img_rgb.resize((160, 160), Image.Resampling.LANCZOS)
|
36 |
return np.array(img_resized)
|
@@ -53,7 +61,22 @@ def build_database():
|
|
53 |
|
54 |
for j, item in enumerate(batch):
|
55 |
try:
|
56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
img_processed = preprocess_image(img)
|
58 |
embedding = DeepFace.represent(
|
59 |
img_path=img_processed,
|
@@ -68,12 +91,15 @@ def build_database():
|
|
68 |
gc.collect()
|
69 |
|
70 |
except Exception as e:
|
71 |
-
print(f"❌ No se pudo procesar imagen {i+j}: {e}")
|
|
|
|
|
72 |
|
73 |
# Guardar progreso después de cada lote
|
74 |
-
|
75 |
-
|
76 |
-
|
|
|
77 |
|
78 |
# Liberar memoria después de cada lote
|
79 |
gc.collect()
|
@@ -94,7 +120,8 @@ def find_similar_faces(uploaded_image):
|
|
94 |
del img_processed
|
95 |
gc.collect()
|
96 |
|
97 |
-
except:
|
|
|
98 |
return [], "⚠ No se detectó un rostro válido en la imagen."
|
99 |
|
100 |
similarities = []
|
|
|
7 |
import pickle
|
8 |
from pathlib import Path
|
9 |
import gc
|
10 |
+
import io
|
11 |
|
12 |
# 🔑 Configurar token de Hugging Face
|
13 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
|
|
32 |
|
33 |
# 🔄 Preprocesar imagen para Facenet
|
34 |
def preprocess_image(img):
|
35 |
+
if isinstance(img, str):
|
36 |
+
# Si es una ruta de archivo o bytes en string
|
37 |
+
img = Image.open(io.BytesIO(img.encode() if isinstance(img, str) else img))
|
38 |
+
elif isinstance(img, bytes):
|
39 |
+
# Si son bytes directos
|
40 |
+
img = Image.open(io.BytesIO(img))
|
41 |
+
|
42 |
img_rgb = img.convert("RGB")
|
43 |
img_resized = img_rgb.resize((160, 160), Image.Resampling.LANCZOS)
|
44 |
return np.array(img_resized)
|
|
|
61 |
|
62 |
for j, item in enumerate(batch):
|
63 |
try:
|
64 |
+
# Debug: Imprimir la estructura del item
|
65 |
+
print(f"Estructura del item {i+j}:", type(item), item.keys() if hasattr(item, 'keys') else "No tiene keys")
|
66 |
+
|
67 |
+
# Intentar diferentes formas de acceder a la imagen
|
68 |
+
if isinstance(item, dict):
|
69 |
+
if 'image' in item:
|
70 |
+
img = item['image']
|
71 |
+
elif 'bytes' in item:
|
72 |
+
img = item['bytes']
|
73 |
+
else:
|
74 |
+
print(f"❌ No se encontró la imagen en el item {i+j}")
|
75 |
+
continue
|
76 |
+
else:
|
77 |
+
print(f"❌ Formato de item no reconocido: {type(item)}")
|
78 |
+
continue
|
79 |
+
|
80 |
img_processed = preprocess_image(img)
|
81 |
embedding = DeepFace.represent(
|
82 |
img_path=img_processed,
|
|
|
91 |
gc.collect()
|
92 |
|
93 |
except Exception as e:
|
94 |
+
print(f"❌ No se pudo procesar imagen {i+j}: {str(e)}")
|
95 |
+
print(f"Tipo de error: {type(e)}")
|
96 |
+
continue
|
97 |
|
98 |
# Guardar progreso después de cada lote
|
99 |
+
if database: # Solo guardar si hay datos
|
100 |
+
print("💾 Guardando progreso...")
|
101 |
+
with open(EMBEDDINGS_FILE, 'wb') as f:
|
102 |
+
pickle.dump(database, f)
|
103 |
|
104 |
# Liberar memoria después de cada lote
|
105 |
gc.collect()
|
|
|
120 |
del img_processed
|
121 |
gc.collect()
|
122 |
|
123 |
+
except Exception as e:
|
124 |
+
print(f"Error al procesar imagen de consulta: {str(e)}")
|
125 |
return [], "⚠ No se detectó un rostro válido en la imagen."
|
126 |
|
127 |
similarities = []
|