#!/usr/bin/env python3
"""
Debug face detection to see what's happening
"""

import os
from pathlib import Path
from PIL import Image
import numpy as np

# Suppress TensorFlow warnings
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

def debug_face_detection():
    from deepface import DeepFace
    
    # Test with the reference image
    test_image = "demo_photos/testdeepface/2019-11-22_0011.jpg"
    
    if not os.path.exists(test_image):
        print(f"Test image not found: {test_image}")
        return
    
    print(f"Testing face detection on: {test_image}")
    
    # Load and display image info
    img = Image.open(test_image)
    print(f"Image size: {img.size}")
    
    # Try different detection methods
    detectors = ['opencv', 'mtcnn', 'retinaface', 'ssd']
    
    for detector in detectors:
        print(f"\n--- Testing {detector} detector ---")
        try:
            # Try extract_faces first
            faces = DeepFace.extract_faces(
                img_path=test_image,
                detector_backend=detector,
                enforce_detection=False,
                align=True
            )
            print(f"extract_faces found {len(faces)} faces")
            
            # Try represent
            results = DeepFace.represent(
                img_path=test_image,
                model_name='ArcFace',
                detector_backend=detector,
                enforce_detection=False,
                align=True
            )
            print(f"represent found {len(results)} results")
            
            if results:
                for i, result in enumerate(results):
                    region = result.get('region', {})
                    print(f"  Result {i}: region={region}")
                    
        except Exception as e:
            print(f"Error with {detector}: {e}")

if __name__ == "__main__":
    debug_face_detection()
