File size: 4,537 Bytes
7823cea
016fe76
7823cea
 
2592e48
b78ec6d
36f95d9
 
9bc27e3
2592e48
 
a36d980
288a128
78e29c2
 
36f95d9
288a128
8d88e43
ae0535c
8d88e43
37efbf7
 
288a128
6773de5
016fe76
37efbf7
 
 
 
288a128
97364cf
59cebaf
 
 
 
288a128
 
 
e4617b7
288a128
 
 
97364cf
e4617b7
8d88e43
 
288a128
e4617b7
8196356
288a128
 
6c43e7e
288a128
 
 
 
6c43e7e
288a128
 
 
 
8196356
288a128
f5a1125
016fe76
 
6c43e7e
016fe76
 
 
 
 
 
6c43e7e
288a128
 
 
6c43e7e
288a128
016fe76
 
6c43e7e
016fe76
288a128
016fe76
e4617b7
288a128
97364cf
7823cea
59cebaf
fff17ea
59cebaf
fff17ea
 
 
9bc27e3
 
ff6b580
288a128
bf687e5
7823cea
bf687e5
288a128
 
 
 
7823cea
288a128
 
 
 
 
 
 
 
 
 
 
 
 
 
7823cea
288a128
 
 
bf687e5
288a128
 
 
fff17ea
288a128
bf687e5
7823cea
 
bf687e5
288a128
 
bf687e5
288a128
 
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
import numpy as np
from PIL import Image
import gradio as gr
from deepface import DeepFace
from datasets import load_dataset
import os
import pickle
from pathlib import Path
import gc
import requests
from io import BytesIO

# πŸ“ Carpeta para guardar cada embedding
EMBEDDINGS_DIR = Path("embeddings")
EMBEDDINGS_DIR.mkdir(exist_ok=True)

# βœ… Cargar dataset CSV
dataset = load_dataset(
    "csv",
    data_files="metadata.csv",
    split="train",
    column_names=["image"],
    header=0
)

print("βœ… ValidaciΓ³n post-carga")
print(dataset[0])
print("Columnas:", dataset.column_names)

# πŸ”„ Preprocesamiento para DeepFace
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)

# πŸ” Header si el dataset es privado
HF_TOKEN = os.getenv("HF_TOKEN")
headers = {"Authorization": f"Bearer {HF_TOKEN}"} if HF_TOKEN else {}

# πŸ“¦ Construir base (embedding por archivo)
def build_database():
    print("πŸ”„ Generando embeddings...")
    batch_size = 10

    for i in range(0, len(dataset), batch_size):
        batch = dataset[i:i + batch_size]
        print(f"πŸ“¦ Lote {i // batch_size + 1}/{(len(dataset) + batch_size - 1) // batch_size}")

        for j in range(len(batch["image"])):
            item = {"image": batch["image"][j]}
            image_url = item["image"]

            # Validar
            if not isinstance(image_url, str) or not image_url.startswith("http") or image_url.strip().lower() == "image":
                print(f"⚠️ Saltando {i + j} - URL invÑlida: {image_url}")
                continue

            name = f"image_{i + j}"
            emb_path = EMBEDDINGS_DIR / f"{name}.pkl"
            if emb_path.exists():
                continue  # Ya existe

            try:
                response = requests.get(image_url, headers=headers, timeout=10)
                response.raise_for_status()
                img = Image.open(BytesIO(response.content)).convert("RGB")

                img_processed = preprocess_image(img)
                embedding = DeepFace.represent(
                    img_path=img_processed,
                    model_name="Facenet",
                    enforce_detection=False
                )[0]["embedding"]

                # Guardar como archivo individual
                with open(emb_path, "wb") as f:
                    pickle.dump({"name": name, "img": img, "embedding": embedding}, f)

                print(f"βœ… Guardado: {name}")
                del img_processed
                gc.collect()

            except Exception as e:
                print(f"❌ Error en {name}: {e}")
                continue

# πŸ” Buscar similitudes
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:
        return [], f"⚠ Error procesando imagen: {str(e)}"

    similarities = []

    for emb_file in EMBEDDINGS_DIR.glob("*.pkl"):
        try:
            with open(emb_file, "rb") as f:
                record = pickle.load(f)

            name = record["name"]
            img = record["img"]
            emb = record["embedding"]

            dist = np.linalg.norm(np.array(query_embedding) - np.array(emb))
            sim_score = 1 / (1 + dist)
            similarities.append((sim_score, name, np.array(img)))

        except Exception as e:
            print(f"⚠ Error leyendo {emb_file}: {e}")
            continue

    similarities.sort(reverse=True)
    top = similarities[:5]

    gallery = [(img, f"{name} - Similitud: {sim:.2f}") for sim, name, img in top]
    summary = "\n".join([f"{name} - Similitud: {sim:.2f}" for sim, name, _ in top])
    return gallery, summary

# πŸš€ Ejecutar al inicio
print("πŸš€ Iniciando app...")
build_database()

# πŸŽ›οΈ Interfaz Gradio
demo = gr.Interface(
    fn=find_similar_faces,
    inputs=gr.Image(label="πŸ“€ Sube una imagen", type="pil"),
    outputs=[
        gr.Gallery(label="πŸ“Έ Rostros similares"),
        gr.Textbox(label="🧠 Detalle", lines=6)
    ],
    title="πŸ” Reconocimiento facial con DeepFace",
    description="Sube una imagen y encuentra coincidencias en el dataset privado de Hugging Face usando embeddings Facenet."
)

demo.launch()