File size: 16,704 Bytes
b10b0ba
 
 
 
 
 
 
 
 
 
 
2d98925
b10b0ba
e586088
 
 
 
 
 
 
 
 
 
 
b10b0ba
 
 
 
 
 
 
 
 
 
 
e586088
 
2d98925
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e586088
 
 
 
 
 
 
 
 
 
c4c0f29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e586088
 
b10b0ba
c4c0f29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b10b0ba
c4c0f29
 
 
 
 
 
 
b10b0ba
 
e586088
c4c0f29
e586088
2d98925
b10b0ba
2d98925
b10b0ba
 
2d98925
b10b0ba
2d98925
 
b10b0ba
2d98925
 
 
 
 
 
 
c4c0f29
 
 
 
 
 
b10b0ba
 
2d98925
 
 
 
 
 
 
c4c0f29
 
 
 
 
 
b10b0ba
2d98925
c4c0f29
2d98925
 
 
 
 
 
 
 
 
 
 
 
 
 
b10b0ba
2d98925
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b10b0ba
2d98925
b10b0ba
2d98925
 
 
 
 
 
 
 
b10b0ba
2d98925
b10b0ba
 
2d98925
b10b0ba
a0cfc96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4f8340a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b10b0ba
 
a0cfc96
 
 
 
 
 
 
 
b10b0ba
2d98925
 
 
 
 
 
 
 
4f8340a
 
 
2d98925
 
4f8340a
2d98925
b10b0ba
 
 
 
 
 
2d98925
b10b0ba
2d98925
b10b0ba
2d98925
b10b0ba
2d98925
b10b0ba
 
 
 
 
 
2d98925
 
b10b0ba
 
2d98925
 
 
b10b0ba
 
2d98925
4f8340a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b10b0ba
 
4f8340a
 
 
 
b10b0ba
 
4f8340a
 
 
 
 
 
 
 
 
 
 
 
b10b0ba
 
2d98925
b10b0ba
2d98925
 
b10b0ba
 
 
 
 
2d98925
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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
import os
import cv2
import torch
import numpy as np
import gradio as gr
import base64
from typing import List, Dict, Any
import tempfile
import time
from PIL import Image, ImageDraw
import json
import io

# Import RetinaFace model components with error handling
try:
    from models.retinaface import RetinaFace
    from utils.prior_box import PriorBox
    from utils.py_cpu_nms import py_cpu_nms
    from utils.box_utils import decode, decode_landm
    print("βœ… All imports successful!")
except ImportError as e:
    print(f"❌ Import error: {e}")
    import sys
    sys.exit(1)

# Global variables for models
mobilenet_model = None
resnet_model = None
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

def load_models():
    """Load both MobileNet and ResNet RetinaFace models"""
    global mobilenet_model, resnet_model
    
    try:
        print("Starting model loading...")
        
        # Model configurations
        mobilenet_cfg = {
            'name': 'mobilenet0.25',
            'min_sizes': [[16, 32], [64, 128], [256, 512]],
            'steps': [8, 16, 32],
            'variance': [0.1, 0.2],
            'clip': False,
            'loc_weight': 2.0,
            'gpu_train': True,
            'batch_size': 32,
            'ngpu': 1,
            'epoch': 250,
            'decay1': 190,
            'decay2': 220,
            'image_size': 640,
            'pretrain': False,
            'return_layers': {'stage1': 1, 'stage2': 2, 'stage3': 3},
            'in_channel': 32,
            'out_channel': 64
        }

        resnet_cfg = {
            'name': 'Resnet50',
            'min_sizes': [[16, 32], [64, 128], [256, 512]],
            'steps': [8, 16, 32],
            'variance': [0.1, 0.2],
            'clip': False,
            'loc_weight': 2.0,
            'gpu_train': True,
            'batch_size': 24,
            'ngpu': 4,
            'epoch': 100,
            'decay1': 70,
            'decay2': 90,
            'image_size': 840,
            'pretrain': False,
            'return_layers': {'layer2': 1, 'layer3': 2, 'layer4': 3},
            'in_channel': 256,
            'out_channel': 256
        }
        
        # Check if model files exist
        if not os.path.exists('mobilenet0.25_Final.pth'):
            print("❌ mobilenet0.25_Final.pth not found!")
            return False
        if not os.path.exists('Resnet50_Final.pth'):
            print("❌ Resnet50_Final.pth not found!")
            return False
            
        print("Model files found, loading MobileNet...")
        
        # Load MobileNet model with better error handling
        try:
            mobilenet_model = RetinaFace(cfg=mobilenet_cfg, phase='test')
            print("βœ… MobileNet model instance created")
            
            # Load state dict
            mobilenet_state = torch.load('mobilenet0.25_Final.pth', map_location=device)
            print(f"βœ… MobileNet state dict loaded with {len(mobilenet_state.keys())} keys")
            
            # Try to load state dict with strict=False to handle key mismatches
            missing_keys, unexpected_keys = mobilenet_model.load_state_dict(mobilenet_state, strict=False)
            
            if missing_keys:
                print(f"⚠️ Missing keys in MobileNet: {missing_keys[:5]}...")  # Show first 5
            if unexpected_keys:
                print(f"⚠️ Unexpected keys in MobileNet: {unexpected_keys[:5]}...")  # Show first 5
                
            mobilenet_model.eval()
            mobilenet_model = mobilenet_model.to(device)
            print("βœ… MobileNet model loaded successfully!")
            
        except Exception as e:
            print(f"❌ Error loading MobileNet: {e}")
            mobilenet_model = None
        
        print("Loading ResNet...")
        
        # Load ResNet model with better error handling
        try:
            resnet_model = RetinaFace(cfg=resnet_cfg, phase='test')
            print("βœ… ResNet model instance created")
            
            # Load state dict
            resnet_state = torch.load('Resnet50_Final.pth', map_location=device)
            print(f"βœ… ResNet state dict loaded with {len(resnet_state.keys())} keys")
            
            # Try to load state dict with strict=False to handle key mismatches
            missing_keys, unexpected_keys = resnet_model.load_state_dict(resnet_state, strict=False)
            
            if missing_keys:
                print(f"⚠️ Missing keys in ResNet: {missing_keys[:5]}...")  # Show first 5
            if unexpected_keys:
                print(f"⚠️ Unexpected keys in ResNet: {unexpected_keys[:5]}...")  # Show first 5
                
            resnet_model.eval()
            resnet_model = resnet_model.to(device)
            print("βœ… ResNet model loaded successfully!")
            
        except Exception as e:
            print(f"❌ Error loading ResNet: {e}")
            resnet_model = None
        
        # Check if at least one model loaded
        if mobilenet_model is not None or resnet_model is not None:
            print("βœ… At least one model loaded successfully!")
            return True
        else:
            print("❌ No models loaded successfully!")
            return False
        
    except Exception as e:
        import traceback
        print(f"❌ Error in load_models: {e}")
        print(f"❌ Full traceback: {traceback.format_exc()}")
        return False

def detect_faces(image, model_type="mobilenet", confidence_threshold=0.5, nms_threshold=0.4):
    """Core face detection function"""
    try:
        start_time = time.time()
        
        # Choose model
        if model_type == "resnet":
            model = resnet_model
            cfg = {
                'min_sizes': [[16, 32], [64, 128], [256, 512]],
                'steps': [8, 16, 32],
                'variance': [0.1, 0.2],
                'clip': False,
                'image_size': 840
            }
            if model is None:
                # Fallback to MobileNet if ResNet not available
                print("⚠️ ResNet not available, falling back to MobileNet")
                model = mobilenet_model
                model_type = "mobilenet"
                cfg['image_size'] = 640
        else:
            model = mobilenet_model
            cfg = {
                'min_sizes': [[16, 32], [64, 128], [256, 512]],
                'steps': [8, 16, 32],
                'variance': [0.1, 0.2],
                'clip': False,
                'image_size': 640
            }
            if model is None:
                # Fallback to ResNet if MobileNet not available
                print("⚠️ MobileNet not available, falling back to ResNet")
                model = resnet_model
                model_type = "resnet"
                cfg['image_size'] = 840
        
        if model is None:
            return None, "❌ No models are loaded. Please check the model loading logs."
            
        # Convert PIL to numpy array
        if isinstance(image, Image.Image):
            image = np.array(image)
        
        # Preprocessing
        img = np.float32(image)
        im_height, im_width, _ = img.shape
        scale = torch.Tensor([img.shape[1], img.shape[0], img.shape[1], img.shape[0]])
        img -= (104, 117, 123)
        img = img.transpose(2, 0, 1)
        img = torch.from_numpy(img).unsqueeze(0)
        img = img.to(device)
        scale = scale.to(device)

        # Forward pass
        with torch.no_grad():
            loc, conf, landms = model(img)

        # Generate priors
        priorbox = PriorBox(cfg, image_size=(im_height, im_width))
        priors = priorbox.forward()
        priors = priors.to(device)
        prior_data = priors.data
        boxes = decode(loc.data.squeeze(0), prior_data, cfg['variance'])
        boxes = boxes * scale
        boxes = boxes.cpu().numpy()
        scores = conf.squeeze(0).data.cpu().numpy()[:, 1]
        landms = decode_landm(landms.data.squeeze(0), prior_data, cfg['variance'])
        scale1 = torch.Tensor([img.shape[3], img.shape[2], img.shape[3], img.shape[2],
                               img.shape[3], img.shape[2], img.shape[3], img.shape[2],
                               img.shape[3], img.shape[2]])
        scale1 = scale1.to(device)
        landms = landms * scale1
        landms = landms.cpu().numpy()

        # Ignore low scores
        inds = np.where(scores > confidence_threshold)[0]
        boxes = boxes[inds]
        landms = landms[inds]
        scores = scores[inds]

        # Keep top-K before NMS
        order = scores.argsort()[::-1][:5000]
        boxes = boxes[order]
        landms = landms[order]
        scores = scores[order]

        # Apply NMS
        dets = np.hstack((boxes, scores[:, np.newaxis])).astype(np.float32, copy=False)
        keep = py_cpu_nms(dets, nms_threshold)
        dets = dets[keep, :]
        landms = landms[keep]

        # Draw results
        result_image = Image.fromarray(image)
        draw = ImageDraw.Draw(result_image)
        
        faces = []
        for b, landmarks in zip(dets, landms):
            if b[4] < confidence_threshold:
                continue
                
            # Draw bounding box
            draw.rectangle([b[0], b[1], b[2], b[3]], outline="red", width=2)
            
            # Draw confidence score
            draw.text((b[0], b[1] - 15), f'{b[4]:.2f}', fill="red")
            
            # Draw landmarks
            for i in range(0, 10, 2):
                draw.ellipse([landmarks[i]-2, landmarks[i+1]-2, landmarks[i]+2, landmarks[i+1]+2], fill="blue")
            
            faces.append({
                "bbox": {"x1": float(b[0]), "y1": float(b[1]), "x2": float(b[2]), "y2": float(b[3])},
                "confidence": float(b[4]),
                "landmarks": {
                    "left_eye": [float(landmarks[0]), float(landmarks[1])],
                    "right_eye": [float(landmarks[2]), float(landmarks[3])],
                    "nose": [float(landmarks[4]), float(landmarks[5])],
                    "left_mouth": [float(landmarks[6]), float(landmarks[7])],
                    "right_mouth": [float(landmarks[8]), float(landmarks[9])]
                }
            })
        
        processing_time = time.time() - start_time
        
        result_text = f"""
        **Detection Results:**
        - **Faces Detected:** {len(faces)}
        - **Model Used:** {model_type}
        - **Processing Time:** {processing_time:.3f}s
        - **Confidence Threshold:** {confidence_threshold}
        - **NMS Threshold:** {nms_threshold}
        """
        
        return result_image, result_text
        
    except Exception as e:
        return None, f"Error: {str(e)}"

# Simple test function to debug model loading
def test_model_loading():
    """Test model loading step by step"""
    try:
        print("=== Testing Model Loading ===")
        
        # Test basic imports
        print("Testing RetinaFace import...")
        test_cfg = {
            'name': 'mobilenet0.25',
            'min_sizes': [[16, 32], [64, 128], [256, 512]],
            'steps': [8, 16, 32],
            'variance': [0.1, 0.2],
            'clip': False,
            'pretrain': False,
            'return_layers': {'stage1': 1, 'stage2': 2, 'stage3': 3},
            'in_channel': 32,
            'out_channel': 64
        }
        
        print("Creating RetinaFace instance...")
        model = RetinaFace(cfg=test_cfg, phase='test')
        print(f"βœ… Model created successfully: {type(model)}")
        
        print("Checking model file...")
        if os.path.exists('mobilenet0.25_Final.pth'):
            print("βœ… Model file exists")
            
            print("Loading state dict...")
            state_dict = torch.load('mobilenet0.25_Final.pth', map_location='cpu')
            print(f"βœ… State dict loaded, keys: {len(state_dict.keys())}")
            
            print("Loading state dict into model...")
            model.load_state_dict(state_dict)
            print("βœ… State dict loaded successfully!")
            
            return True
        else:
            print("❌ Model file not found")
            return False
            
    except Exception as e:
        import traceback
        print(f"❌ Test failed: {e}")
        print(f"❌ Traceback: {traceback.format_exc()}")
        return False

# API test function
def test_api_endpoint():
    """Test function to verify API is working"""
    try:
        # Create a simple test image
        import numpy as np
        test_img = np.random.randint(0, 255, (100, 100, 3), dtype=np.uint8)
        test_pil = Image.fromarray(test_img)
        
        # Test the detect_faces function directly
        result_img, result_text = detect_faces(test_pil, "mobilenet", 0.5, 0.4)
        
        if result_img is not None:
            return "βœ… API function test passed - detection pipeline works"
        else:
            return f"❌ API function test failed: {result_text}"
            
    except Exception as e:
        return f"❌ API test error: {str(e)}"

# Load models on startup
print("Loading RetinaFace models...")
print("Running model loading test...")
test_result = test_model_loading()
if test_result:
    print("Test passed, proceeding with full model loading...")
    model_loaded = load_models()
else:
    print("Test failed, skipping model loading...")
    model_loaded = False

# Create simple Gradio interface
def create_interface():
    with gr.Blocks(title="RetinaFace Face Detection") as demo:
        gr.Markdown("# πŸ”₯ RetinaFace Face Detection API")
        gr.Markdown("Real-time face detection using RetinaFace with MobileNet and ResNet backbones")
        
        if model_loaded:
            gr.Markdown("βœ… **Status**: Models loaded successfully!")
            # Test API functionality
            api_test_result = test_api_endpoint()
            gr.Markdown(f"πŸ”§ **API Test**: {api_test_result}")
        else:
            gr.Markdown("❌ **Status**: Error loading models")
            gr.Markdown("πŸ”§ **API Test**: Cannot test API - models not loaded")
        
        with gr.Row():
            with gr.Column():
                input_image = gr.Image(type="pil", label="Upload Image")
                model_choice = gr.Dropdown(
                    choices=["mobilenet", "resnet"], 
                    value="mobilenet", 
                    label="Model"
                )
                confidence = gr.Slider(
                    minimum=0.1, maximum=1.0, value=0.5, step=0.1,
                    label="Confidence"
                )
                nms = gr.Slider(
                    minimum=0.1, maximum=1.0, value=0.4, step=0.1,
                    label="NMS Threshold"
                )
                detect_btn = gr.Button("πŸ” Detect Faces", variant="primary")
            
            with gr.Column():
                output_image = gr.Image(label="Results")
                output_text = gr.Markdown()
        
        detect_btn.click(
            fn=detect_faces,
            inputs=[input_image, model_choice, confidence, nms],
            outputs=[output_image, output_text]
        )
        
        gr.Markdown("""
        ## πŸ”— API Information
        
        **Your API is automatically available at these endpoints:**
        
        ### Main API Endpoint
        ```
        POST /api/predict
        ```
        
        **Request format:**
        ```json
        {
            "data": [
                "<image_as_PIL_or_path>",
                "mobilenet",
                0.5,
                0.4
            ]
        }
        ```
        
        **Response format:**
        ```json
        {
            "data": [
                "<processed_image>",
                "**Detection Results:**\\n- **Faces Detected:** 2\\n..."
            ]
        }
        ```
        
        ### For Thunkable Integration:
        - **URL:** `https://aditya-g07-retinaface-face-detection.hf.space/api/predict`
        - **Method:** POST
        - **Content-Type:** application/json
        
        ### API Status:
        - βœ… **Gradio auto-generates API endpoints**
        - βœ… **No additional configuration needed**
        - βœ… **"No API found" message is normal for Gradio 4.36.0**
        
        **Note:** The "No API found" error in the UI doesn't affect API functionality.
        """)
    
    return demo

# Create and launch the interface
demo = create_interface()

if __name__ == "__main__":
    demo.launch(
        server_name="0.0.0.0",
        server_port=7860,
        share=True
    )