Segizu commited on
Commit
9bc27e3
·
1 Parent(s): 19d4ac3

no cache embeddings baches

Browse files
Files changed (1) hide show
  1. app.py +37 -17
app.py CHANGED
@@ -6,6 +6,7 @@ from datasets import load_dataset, DownloadConfig
6
  import os
7
  import pickle
8
  from pathlib import Path
 
9
 
10
  # 🔑 Configurar token de Hugging Face
11
  HF_TOKEN = os.getenv("HF_TOKEN")
@@ -44,24 +45,38 @@ def build_database():
44
 
45
  print("🔄 Calculando embeddings (esto puede tomar unos minutos)...")
46
  database = []
47
- for i, item in enumerate(dataset):
48
- try:
49
- img = item["image"]
50
- img_processed = preprocess_image(img)
51
- embedding = DeepFace.represent(
52
- img_path=img_processed,
53
- model_name="Facenet",
54
- enforce_detection=False
55
- )[0]["embedding"]
56
- database.append((f"image_{i}", img, embedding))
57
- print(f"✅ Procesada imagen {i+1}/{len(dataset)}")
58
- except Exception as e:
59
- print(f"❌ No se pudo procesar imagen {i}: {e}")
60
 
61
- # Guardar embeddings en el archivo
62
- print("💾 Guardando embeddings en el archivo...")
63
- with open(EMBEDDINGS_FILE, 'wb') as f:
64
- pickle.dump(database, f)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
 
66
  return database
67
 
@@ -74,6 +89,11 @@ def find_similar_faces(uploaded_image):
74
  model_name="Facenet",
75
  enforce_detection=False
76
  )[0]["embedding"]
 
 
 
 
 
77
  except:
78
  return [], "⚠ No se detectó un rostro válido en la imagen."
79
 
 
6
  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")
 
45
 
46
  print("🔄 Calculando embeddings (esto puede tomar unos minutos)...")
47
  database = []
48
+ batch_size = 10 # Procesar 10 imágenes a la vez
 
 
 
 
 
 
 
 
 
 
 
 
49
 
50
+ for i in range(0, len(dataset), batch_size):
51
+ batch = dataset[i:i + batch_size]
52
+ print(f"📦 Procesando lote {i//batch_size + 1}/{(len(dataset) + batch_size - 1)//batch_size}")
53
+
54
+ for j, item in enumerate(batch):
55
+ try:
56
+ img = item["image"]
57
+ img_processed = preprocess_image(img)
58
+ embedding = DeepFace.represent(
59
+ img_path=img_processed,
60
+ model_name="Facenet",
61
+ enforce_detection=False
62
+ )[0]["embedding"]
63
+ database.append((f"image_{i+j}", img, embedding))
64
+ print(f"✅ Procesada imagen {i+j+1}/{len(dataset)}")
65
+
66
+ # Liberar memoria
67
+ del img_processed
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
+ print("💾 Guardando progreso...")
75
+ with open(EMBEDDINGS_FILE, 'wb') as f:
76
+ pickle.dump(database, f)
77
+
78
+ # Liberar memoria después de cada lote
79
+ gc.collect()
80
 
81
  return database
82
 
 
89
  model_name="Facenet",
90
  enforce_detection=False
91
  )[0]["embedding"]
92
+
93
+ # Liberar memoria
94
+ del img_processed
95
+ gc.collect()
96
+
97
  except:
98
  return [], "⚠ No se detectó un rostro válido en la imagen."
99