File size: 4,406 Bytes
b10b0ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import requests
import base64
import json

def test_gradio_api():
    """Test the Gradio /api/predict endpoint"""
    # You would replace this with actual base64 encoded image data
    sample_image_path = "test_image.jpg"  # Replace with your test image
    
    try:
        with open(sample_image_path, "rb") as image_file:
            image_base64 = base64.b64encode(image_file.read()).decode('utf-8')
    except FileNotFoundError:
        print("Please add a test_image.jpg file to test the API")
        return
    
    url = "http://localhost:7860/api/predict"
    
    payload = {
        "data": [
            image_base64,
            "mobilenet",
            0.5,
            0.4
        ]
    }
    
    response = requests.post(url, json=payload)
    
    if response.status_code == 200:
        result = response.json()
        print("Success!")
        print(f"API Response: {json.dumps(result, indent=2)}")
        
        # Extract the actual detection data
        if "data" in result and len(result["data"]) > 0:
            detection_data = result["data"][0]
            if "faces" in detection_data:
                print(f"Detected {len(detection_data['faces'])} faces")
                print(f"Processing time: {detection_data.get('processing_time', 'N/A'):.3f} seconds")
                print(f"Model used: {detection_data.get('model_used', 'N/A')}")
                
                for i, face in enumerate(detection_data['faces']):
                    print(f"Face {i+1}:")
                    print(f"  Confidence: {face['confidence']:.3f}")
                    print(f"  Bounding box: {face['bbox']}")
                    print(f"  Landmarks: {face['landmarks']}")
            else:
                print("No face detection data in response")
        else:
            print("Unexpected response format")
    else:
        print(f"Error: {response.status_code}")
        print(response.text)

def test_health_check():
    """Test the Gradio app health"""
    url = "http://localhost:7860/"
    
    response = requests.get(url)
    
    if response.status_code == 200:
        print("Gradio app is running!")
        print("You can access the web interface at: http://localhost:7860")
    else:
        print(f"Health check failed: {response.status_code}")

def test_direct_api_call():
    """Test direct API call format that Thunkable would use"""
    sample_image_path = "test_image.jpg"  # Replace with your test image
    
    try:
        with open(sample_image_path, "rb") as image_file:
            image_base64 = base64.b64encode(image_file.read()).decode('utf-8')
    except FileNotFoundError:
        print("Please add a test_image.jpg file to test the API")
        return
    
    url = "http://localhost:7860/api/predict"
    
    # This is the format Thunkable will use
    payload = {
        "data": [image_base64, "mobilenet", 0.5, 0.4]
    }
    
    headers = {
        "Content-Type": "application/json"
    }
    
    print("Testing Thunkable-compatible API call...")
    response = requests.post(url, json=payload, headers=headers)
    
    if response.status_code == 200:
        result = response.json()
        print("βœ… Thunkable API call successful!")
        
        # Parse the response as Thunkable would
        if "data" in result and result["data"]:
            detection_result = result["data"][0]
            print(f"Faces detected: {detection_result.get('total_faces', 0)}")
            print(f"Model used: {detection_result.get('model_used', 'unknown')}")
            print(f"Processing time: {detection_result.get('processing_time', 0):.3f}s")
        else:
            print("❌ Unexpected response format")
    else:
        print(f"❌ API call failed: {response.status_code}")
        print(response.text)

if __name__ == "__main__":
    print("Testing RetinaFace Gradio API...")
    print("=" * 50)
    
    print("\n1. Health Check:")
    test_health_check()
    
    print("\n2. Gradio API Test:")
    test_gradio_api()
    
    print("\n3. Thunkable-Compatible API Test:")
    test_direct_api_call()
    
    print("\n" + "=" * 50)
    print("Testing complete!")
    print("\nFor Thunkable integration:")
    print("- Use URL: http://localhost:7860/api/predict")
    print("- Method: POST")
    print("- Body format: {\"data\": [\"base64_image\", \"mobilenet\", 0.5, 0.4]}")
    print("- Response will be in: response.data[0]")