Segizu commited on
Commit
1194d1c
·
1 Parent(s): e4617b7

metadata v4

Browse files
Files changed (1) hide show
  1. app.py +21 -21
app.py CHANGED
@@ -2,7 +2,7 @@ import numpy as np
2
  from PIL import Image, UnidentifiedImageError
3
  import gradio as gr
4
  from deepface import DeepFace
5
- from datasets import load_dataset, DownloadConfig, Image as HfImage
6
  import os
7
  import pickle
8
  from pathlib import Path
@@ -10,23 +10,19 @@ import gc
10
  import requests
11
  from io import BytesIO
12
 
13
- # 🔑 Configurar token de Hugging Face
14
  HF_TOKEN = os.getenv("HF_TOKEN")
15
  if not HF_TOKEN:
16
  raise ValueError("⚠️ Por favor, configura la variable de entorno HF_TOKEN para acceder al dataset privado")
17
 
18
- # 📁 Configurar directorio de embeddings
19
  EMBEDDINGS_DIR = Path("embeddings")
20
  EMBEDDINGS_DIR.mkdir(exist_ok=True)
21
  EMBEDDINGS_FILE = EMBEDDINGS_DIR / "embeddings.pkl"
22
 
23
- # ✅ Cargar el dataset de Hugging Face con imágenes decodificadas
24
- dataset = load_dataset("Segizu/facial-recognition", data_files="metadata.csv", token=HF_TOKEN)
25
- if "train" in dataset:
26
- dataset = dataset["train"]
27
-
28
- # Cast the image column to HfImage format
29
- dataset = dataset.cast_column("image", HfImage())
30
 
31
  # 🔄 Preprocesar imagen para Facenet
32
  def preprocess_image(img: Image.Image) -> np.ndarray:
@@ -51,17 +47,21 @@ def build_database():
51
 
52
  for j, item in enumerate(batch):
53
  try:
54
- # Manejar ruta local o URL remota
55
  img_data = item["image"]
56
- if isinstance(img_data, str):
57
- response = requests.get(img_data)
58
- img = Image.open(BytesIO(response.content))
59
- elif isinstance(img_data, dict) and "bytes" in img_data:
60
- img = Image.open(BytesIO(img_data["bytes"]))
 
 
 
61
  elif isinstance(img_data, Image.Image):
62
  img = img_data
 
63
  else:
64
- raise ValueError(f"Formato de imagen no soportado: {type(img_data)}")
 
65
 
66
  img_processed = preprocess_image(img)
67
  embedding = DeepFace.represent(
@@ -76,14 +76,14 @@ def build_database():
76
  del img_processed
77
  gc.collect()
78
 
79
- except UnidentifiedImageError:
80
- print(f"❌ Imagen no válida en {i+j}: no se pudo identificar")
81
  continue
82
  except Exception as e:
83
  print(f"❌ No se pudo procesar imagen {i+j}: {str(e)}")
84
  continue
85
 
86
- # Guardar progreso
87
  if database:
88
  print("💾 Guardando progreso...")
89
  with open(EMBEDDINGS_FILE, 'wb') as f:
@@ -126,7 +126,7 @@ def find_similar_faces(uploaded_image: Image.Image):
126
 
127
  return gallery_items, text_summary
128
 
129
- # ⚙️ Inicializar base
130
  print("🚀 Iniciando aplicación...")
131
  database = build_database()
132
  print(f"✅ Base de datos cargada con {len(database)} imágenes")
 
2
  from PIL import Image, UnidentifiedImageError
3
  import gradio as gr
4
  from deepface import DeepFace
5
+ from datasets import load_dataset, Image as HfImage
6
  import os
7
  import pickle
8
  from pathlib import Path
 
10
  import requests
11
  from io import BytesIO
12
 
13
+ # 🔑 Token de autenticación
14
  HF_TOKEN = os.getenv("HF_TOKEN")
15
  if not HF_TOKEN:
16
  raise ValueError("⚠️ Por favor, configura la variable de entorno HF_TOKEN para acceder al dataset privado")
17
 
18
+ # 📁 Directorio para embeddings
19
  EMBEDDINGS_DIR = Path("embeddings")
20
  EMBEDDINGS_DIR.mkdir(exist_ok=True)
21
  EMBEDDINGS_FILE = EMBEDDINGS_DIR / "embeddings.pkl"
22
 
23
+ # ✅ Cargar dataset desde metadata.csv (con URLs absolutas)
24
+ dataset = load_dataset("csv", data_files="metadata.csv")
25
+ dataset = dataset["train"].cast_column("image", HfImage())
 
 
 
 
26
 
27
  # 🔄 Preprocesar imagen para Facenet
28
  def preprocess_image(img: Image.Image) -> np.ndarray:
 
47
 
48
  for j, item in enumerate(batch):
49
  try:
 
50
  img_data = item["image"]
51
+
52
+ # 📥 Descargar imagen si es una URL
53
+ if isinstance(img_data, str) and img_data.startswith("http"):
54
+ response = requests.get(img_data, timeout=10)
55
+ response.raise_for_status()
56
+ img = Image.open(BytesIO(response.content)).convert("RGB")
57
+
58
+ # Usar imagen si ya viene decodificada
59
  elif isinstance(img_data, Image.Image):
60
  img = img_data
61
+
62
  else:
63
+ print(f"Formato desconocido en imagen {i+j}: {type(img_data)}")
64
+ continue
65
 
66
  img_processed = preprocess_image(img)
67
  embedding = DeepFace.represent(
 
76
  del img_processed
77
  gc.collect()
78
 
79
+ except (requests.RequestException, UnidentifiedImageError) as e:
80
+ print(f"❌ Falló descarga/lectura de imagen {i+j}: {e}")
81
  continue
82
  except Exception as e:
83
  print(f"❌ No se pudo procesar imagen {i+j}: {str(e)}")
84
  continue
85
 
86
+ # 💾 Guardar después de cada batch
87
  if database:
88
  print("💾 Guardando progreso...")
89
  with open(EMBEDDINGS_FILE, 'wb') as f:
 
126
 
127
  return gallery_items, text_summary
128
 
129
+ # ⚙️ Inicializar
130
  print("🚀 Iniciando aplicación...")
131
  database = build_database()
132
  print(f"✅ Base de datos cargada con {len(database)} imágenes")