|
|
""" |
|
|
Ejemplo de uso de Aliah-Plus |
|
|
Demuestra cómo usar las funcionalidades principales del sistema |
|
|
""" |
|
|
|
|
|
import asyncio |
|
|
import sys |
|
|
from pathlib import 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) |
|
|
|
|
|
|
|
|
print("\n[1/7] Inicializando componentes...") |
|
|
face_processor = FaceProcessor() |
|
|
embedding_engine = EmbeddingEngine(model="ArcFace") |
|
|
stealth_search = StealthSearch(headless=True) |
|
|
ocr_extractor = OCRExtractor(gpu=False) |
|
|
cross_referencer = CrossReferencer() |
|
|
comparator = FaceComparator(threshold=0.75) |
|
|
|
|
|
|
|
|
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) |
|
|
|
|
|
|
|
|
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") |
|
|
|
|
|
|
|
|
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") |
|
|
|
|
|
|
|
|
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") |
|
|
|
|
|
|
|
|
print("\n[6/7] Extrayendo dominios con OCR...") |
|
|
ocr_domains = [] |
|
|
|
|
|
if 'pimeyes' in search_results: |
|
|
for pim_result in search_results['pimeyes'][:5]: |
|
|
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]: |
|
|
print(f" • {dom['domain']} (confianza: {dom['confidence']:.2%})") |
|
|
|
|
|
|
|
|
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)}") |
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
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() |
|
|
|
|
|
|
|
|
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") |
|
|
|
|
|
|
|
|
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") |
|
|
|
|
|
|
|
|
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}") |
|
|
|