#!/usr/bin/env python3
"""
Simple test to isolate the recognition issue
"""

import cv2
import numpy as np
import os

def test_simple():
    """Test the basic components"""
    print("🧪 Simple Component Test...")
    
    # Test 1: Check if model exists
    model_file = "face_recognizer.yml"
    if not os.path.exists(model_file):
        print(f"❌ Model file not found: {model_file}")
        return False
    
    print(f"✅ Model file exists: {model_file}")
    
    # Test 2: Try to load the model
    try:
        recognizer = cv2.face.LBPHFaceRecognizer_create()
        recognizer.read(model_file)
        print("✅ Model loaded successfully")
    except Exception as e:
        print(f"❌ Failed to load model: {e}")
        return False
    
    # Test 3: Try to load a test image
    test_image_path = "registered_faces/332/front/0.jpg"
    if not os.path.exists(test_image_path):
        print(f"❌ Test image not found: {test_image_path}")
        return False
    
    print(f"✅ Test image exists: {test_image_path}")
    
    # Test 4: Try to read the image
    try:
        img = cv2.imread(test_image_path, cv2.IMREAD_GRAYSCALE)
        if img is None:
            print(f"❌ Failed to read image")
            return False
        print(f"✅ Image read successfully: {img.shape}")
    except Exception as e:
        print(f"❌ Failed to read image: {e}")
        return False
    
    # Test 5: Try to preprocess the image
    try:
        from livestream import preprocess_face
        processed = preprocess_face(img)
        print(f"✅ Image preprocessed successfully: {processed.shape}")
    except Exception as e:
        print(f"❌ Failed to preprocess image: {e}")
        return False
    
    # Test 6: Try to predict
    try:
        prediction_result = recognizer.predict(processed)
        print(f"✅ Prediction successful: {prediction_result}")
        print(f"   Type: {type(prediction_result)}")
        print(f"   Value: {prediction_result}")
        
        # Check the return format
        if isinstance(prediction_result, tuple):
            print(f"   Tuple length: {len(prediction_result)}")
            if len(prediction_result) == 2:
                user_id, confidence = prediction_result
                print(f"   User ID: {user_id}, Confidence: {confidence}")
            else:
                print(f"   Warning: Tuple has {len(prediction_result)} values, expected 2")
        elif isinstance(prediction_result, (list, np.ndarray)):
            print(f"   List/Array length: {len(prediction_result)}")
            if len(prediction_result) >= 2:
                user_id, confidence = prediction_result[0], prediction_result[1]
                print(f"   User ID: {user_id}, Confidence: {confidence}")
            else:
                print(f"   Warning: List/Array has {len(prediction_result)} values, expected 2")
        else:
            print(f"   Single value: {prediction_result}")
            
    except Exception as e:
        print(f"❌ Failed to predict: {e}")
        return False
    
    return True

if __name__ == "__main__":
    print("Simple Recognition Test")
    print("=" * 30)
    
    if test_simple():
        print("\n✅ All tests passed!")
    else:
        print("\n❌ Some tests failed!")



