J
File size: 9,026 Bytes
85fa7d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
"""
Ejemplo de uso de Aliah-Plus
Demuestra cómo usar las funcionalidades principales del sistema
"""

import asyncio
import sys
from pathlib import Path

# Añadir el directorio padre al path
sys.path.insert(0, str(Path(__file__).parent.parent))

from src.face_processor import FaceProcessor
from src.embedding_engine import EmbeddingEngine
from src.scrapers.stealth_engine import StealthSearch
from src.ocr_extractor import OCRExtractor
from src.cross_referencer import CrossReferencer
from src.comparator import FaceComparator
import cv2


async def example_complete_search(image_path: str):
    """
    Ejemplo completo de búsqueda con todas las características de Aliah-Plus.
    """
    print("=" * 60)
    print("ALIAH-PLUS - Búsqueda Completa")
    print("=" * 60)
    
    # 1. Inicializar componentes
    print("\n[1/7] Inicializando componentes...")
    face_processor = FaceProcessor()
    embedding_engine = EmbeddingEngine(model="ArcFace")
    stealth_search = StealthSearch(headless=True)
    ocr_extractor = OCRExtractor(gpu=False)  # CPU para ejemplo
    cross_referencer = CrossReferencer()
    comparator = FaceComparator(threshold=0.75)
    
    # 2. Cargar y procesar imagen
    print(f"\n[2/7] Cargando imagen: {image_path}")
    image = cv2.imread(image_path)
    if image is None:
        print("❌ Error: No se pudo cargar la imagen")
        return
    
    image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    
    # 3. Detectar y alinear rostro
    print("\n[3/7] Detectando y alineando rostro...")
    aligned_face = face_processor.align_face(image_rgb)
    
    if aligned_face is None:
        print("❌ No se detectó ningún rostro en la imagen")
        return
    
    print("✓ Rostro detectado y alineado")
    
    # 4. Generar embedding
    print("\n[4/7] Generando embedding facial...")
    embedding = embedding_engine.generate_embedding(aligned_face)
    
    if embedding is None:
        print("❌ Error generando embedding")
        return
    
    print(f"✓ Embedding generado: {len(embedding)} dimensiones")
    
    # 5. Buscar en múltiples motores
    print("\n[5/7] Buscando en múltiples motores...")
    print("   → Yandex Images")
    print("   → Bing Images")
    print("   → PimEyes (stealth)")
    
    search_results = await stealth_search.search_all_engines(image_path)
    
    total_results = sum(len(v) for v in search_results.values())
    print(f"✓ Total de resultados encontrados: {total_results}")
    
    for engine, results in search_results.items():
        print(f"   • {engine}: {len(results)} resultados")
    
    # 6. Extraer dominios con OCR (de miniaturas de PimEyes)
    print("\n[6/7] Extrayendo dominios con OCR...")
    ocr_domains = []
    
    if 'pimeyes' in search_results:
        for pim_result in search_results['pimeyes'][:5]:  # Solo primeros 5 para ejemplo
            if pim_result.get('screenshot'):
                screenshot_np = cv2.imdecode(
                    pim_result['screenshot'], 
                    cv2.IMREAD_COLOR
                )
                
                extracted = ocr_extractor.extract_domain_from_thumb(screenshot_np)
                ocr_domains.extend(extracted)
    
    print(f"✓ Dominios extraídos por OCR: {len(ocr_domains)}")
    
    if ocr_domains:
        print("\n   Dominios encontrados:")
        for dom in ocr_domains[:5]:  # Mostrar solo primeros 5
            print(f"   • {dom['domain']} (confianza: {dom['confidence']:.2%})")
    
    # 7. Cross-referencing
    print("\n[7/7] Correlacionando resultados (Cross-Referencing)...")
    
    cross_referenced = cross_referencer.find_cross_references(
        search_results,
        ocr_domains
    )
    
    correlations = sum(1 for r in cross_referenced if r.get('cross_referenced', False))
    
    print(f"✓ Correlaciones encontradas: {correlations}")
    print(f"✓ Resultados totales procesados: {len(cross_referenced)}")
    
    # Mostrar top 5 resultados
    print("\n" + "=" * 60)
    print("TOP 5 RESULTADOS")
    print("=" * 60)
    
    for idx, result in enumerate(cross_referenced[:5], 1):
        print(f"\n[{idx}] {result.get('domain', 'N/A')}")
        print(f"    URL: {result.get('url', 'N/A')}")
        print(f"    Fuentes: {', '.join(result.get('sources', []))}")
        print(f"    Verificado por OCR: {'Sí' if result.get('ocr_verified') else 'No'}")
        print(f"    Confianza: {result.get('confidence', 0):.2%}")
        
        if result.get('cross_referenced'):
            print(f"    ✓ Correlacionado entre múltiples fuentes")
    
    print("\n" + "=" * 60)
    print("✓ Búsqueda completada")
    print("=" * 60)


async def example_ocr_only(thumbnail_path: str):
    """
    Ejemplo de extracción OCR de una miniatura.
    """
    print("\n" + "=" * 60)
    print("ALIAH-PLUS - Extracción OCR")
    print("=" * 60)
    
    ocr = OCRExtractor(gpu=False)
    
    print(f"\nProcesando: {thumbnail_path}")
    
    image = cv2.imread(thumbnail_path)
    if image is None:
        print("❌ Error cargando imagen")
        return
    
    # Extraer dominios
    domains = ocr.extract_domain_from_thumb(image)
    
    print(f"\n✓ Dominios encontrados: {len(domains)}")
    
    if domains:
        print("\nResultados:")
        for idx, dom in enumerate(domains, 1):
            print(f"\n[{idx}] {dom['domain']}")
            print(f"    Confianza: {dom['confidence']:.2%}")
            print(f"    Texto original: {dom['original_text']}")
            print(f"    Método: #{dom['method']}")
    else:
        print("\n⚠ No se encontraron dominios en la imagen")


async def example_compare_faces(image1_path: str, image2_path: str):
    """
    Ejemplo de comparación directa entre dos rostros.
    """
    print("\n" + "=" * 60)
    print("ALIAH-PLUS - Comparación de Rostros")
    print("=" * 60)
    
    face_processor = FaceProcessor()
    embedding_engine = EmbeddingEngine(model="ArcFace")
    comparator = FaceComparator()
    
    # Procesar imagen 1
    print(f"\nImagen 1: {image1_path}")
    img1 = cv2.imread(image1_path)
    img1_rgb = cv2.cvtColor(img1, cv2.COLOR_BGR2RGB)
    face1 = face_processor.align_face(img1_rgb)
    
    if face1 is None:
        print("❌ No se detectó rostro en imagen 1")
        return
    
    emb1 = embedding_engine.generate_embedding(face1)
    print("✓ Rostro 1 procesado")
    
    # Procesar imagen 2
    print(f"\nImagen 2: {image2_path}")
    img2 = cv2.imread(image2_path)
    img2_rgb = cv2.cvtColor(img2, cv2.COLOR_BGR2RGB)
    face2 = face_processor.align_face(img2_rgb)
    
    if face2 is None:
        print("❌ No se detectó rostro en imagen 2")
        return
    
    emb2 = embedding_engine.generate_embedding(face2)
    print("✓ Rostro 2 procesado")
    
    # Comparar
    print("\nComparando...")
    confidence_level, similarity = comparator.verify_identity(emb1, emb2)
    
    print("\n" + "=" * 60)
    print("RESULTADO")
    print("=" * 60)
    print(f"Similitud: {similarity:.2%}")
    print(f"Distancia: {1-similarity:.3f}")
    print(f"Veredicto: {confidence_level}")
    
    if similarity > 0.85:
        print("\n✓ Las personas son la misma (Match Seguro)")
    elif similarity > 0.72:
        print("\n⚠ Posible coincidencia (requiere revisión)")
    else:
        print("\n❌ Las personas son diferentes")


async def main():
    """Menú principal de ejemplos"""
    
    print("""
╔══════════════════════════════════════════════════════════════╗
║                    ALIAH-PLUS EXAMPLES                       ║
║           Sistema Avanzado de Re-Identificación             ║
╚══════════════════════════════════════════════════════════════╝

Selecciona un ejemplo:

1. Búsqueda completa (Face detection + Search + OCR + Cross-ref)
2. Solo extracción OCR de miniatura
3. Comparación directa entre dos rostros
4. Salir
    """)
    
    choice = input("Opción (1-4): ").strip()
    
    if choice == "1":
        image_path = input("\nRuta de la imagen: ").strip()
        await example_complete_search(image_path)
        
    elif choice == "2":
        thumbnail_path = input("\nRuta de la miniatura: ").strip()
        await example_ocr_only(thumbnail_path)
        
    elif choice == "3":
        image1 = input("\nRuta imagen 1: ").strip()
        image2 = input("Ruta imagen 2: ").strip()
        await example_compare_faces(image1, image2)
        
    elif choice == "4":
        print("\nAdiós!")
        return
    
    else:
        print("\n❌ Opción inválida")


if __name__ == "__main__":
    try:
        asyncio.run(main())
    except KeyboardInterrupt:
        print("\n\n👋 Interrumpido por el usuario")
    except Exception as e:
        print(f"\n❌ Error: {e}")