File size: 6,021 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
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
# RetinaFace Face Detection API

A Gradio-based face detection service using RetinaFace models (MobileNet and ResNet backbones) deployed on Hugging Face Spaces.

## Features

- πŸ”₯ **Dual Model Support**: MobileNet (fast) and ResNet (accurate) backbones
- πŸ“± **Thunkable Compatible**: API endpoints for mobile app integration
- ⚑ **Real-time Detection**: Web interface and API endpoints
- 🎨 **Interactive UI**: Gradio web interface for easy testing
- πŸš€ **Serverless**: Deployed on Hugging Face Spaces for free

## Web Interface

Access the interactive web interface at your Hugging Face Space URL:
- Image upload and detection
- Model selection (MobileNet/ResNet)
- Confidence threshold adjustment
- Real-time results visualization
- API testing interface

## API Endpoints

### 1. Gradio API Endpoint
```
POST /api/predict
```
Main API endpoint compatible with Thunkable and other applications.

**Request Body:**
```json
{
  "data": [
    "base64_encoded_image_string",
    "mobilenet",
    0.5,
    0.4
  ]
}
```

**Response:**
```json
{
  "data": [
    {
      "faces": [
        {
          "bbox": {"x1": 100, "y1": 120, "x2": 200, "y2": 220},
          "confidence": 0.95,
          "landmarks": {
            "right_eye": [130, 150],
            "left_eye": [170, 150],
            "nose": [150, 170],
            "right_mouth": [135, 190],
            "left_mouth": [165, 190]
          }
        }
      ],
      "processing_time": 0.1,
      "model_used": "mobilenet",
      "total_faces": 1
    }
  ]
}
```

## Deployment Instructions

### 1. Hugging Face Spaces Deployment

1. **Create a new Space on Hugging Face:**
   - Go to https://huggingface.co/spaces
   - Click "Create new Space"
   - Choose "Gradio" as SDK
   - Set SDK version to 4.44.0
   - Set visibility to "Public"

2. **Upload your files:**
   ```
   β”œβ”€β”€ app.py                     # Main Gradio application
   β”œβ”€β”€ requirements.txt           # Python dependencies
   β”œβ”€β”€ README.md                  # HF Spaces configuration
   β”œβ”€β”€ mobilenet0.25_Final.pth    # MobileNet model weights
   β”œβ”€β”€ Resnet50_Final.pth         # ResNet model weights
   β”œβ”€β”€ models/
   β”‚   └── retinaface.py         # RetinaFace model architecture
   └── utils/
       β”œβ”€β”€ box_utils.py          # Bounding box utilities
       β”œβ”€β”€ prior_box.py          # Anchor box generation
       └── py_cpu_nms.py         # Non-maximum suppression
   ```

3. **Your Space will automatically build and deploy!**

### 2. Local Testing

1. **Install dependencies:**
   ```bash
   pip install -r requirements.txt
   ```

2. **Run locally:**
   ```bash
   python app.py
   ```

3. **Test the API:**
   ```bash
   python test_api.py
   ```

4. **Access the web interface:**
   Open http://localhost:7860 in your browser

## Thunkable Integration

### 1. Web API Component Setup
```
URL: https://your-username-retinaface-api.hf.space/api/predict
Method: POST
Headers: Content-Type: application/json
Body: {
  "data": [
    "{{base64_image}}",
    "mobilenet",
    0.5,
    0.4
  ]
}
```

### 2. Response Handling in Thunkable
```
When Web API receives data:
  Set app variable "apiResponse" to response body
  Set app variable "detectionData" to get property "data" of apiResponse
  Set app variable "faces" to get property "faces" of detectionData[0]
  Set app variable "faceCount" to get property "total_faces" of detectionData[0]
  
  If faceCount > 0:
    For each face in faces:
      // Process face data (bbox, confidence, landmarks)
```

### 3. Base64 Image Conversion
```
// In Thunkable, convert camera image to base64
Set app variable "imageBase64" to 
  call CloudinaryAPI.convertToBase64 
    mediaDB = Camera1.Picture
```

## Model Performance

| Model | Speed | Accuracy | Use Case |
|-------|-------|----------|----------|
| MobileNet | Fast | Good | Real-time mobile apps |
| ResNet50 | Slower | High | High-accuracy applications |

## API Testing

Use the built-in API testing interface in the Gradio app:
1. Go to the "πŸ“Š API Testing" tab
2. Paste your base64 encoded image
3. Select model and parameters
4. Click "πŸ§ͺ Test API"
5. View the JSON response

## Error Handling

The API includes comprehensive error handling:
- Invalid image data validation
- Model loading verification
- Detailed error responses in JSON format

## Advantages of Gradio SDK

βœ… **Web Interface**: Built-in UI for testing and demonstration
βœ… **API Endpoints**: Automatic API generation at `/api/predict`
βœ… **Easy Deployment**: No Docker configuration needed
βœ… **Real-time Testing**: Interactive interface for immediate feedback
βœ… **Documentation**: Built-in API documentation
βœ… **Mobile Friendly**: Responsive web interface

## Limitations

- **File Size**: Max upload size determined by Hugging Face Spaces
- **Concurrent Requests**: Subject to Hugging Face Spaces limits
- **Cold Starts**: First request may take longer due to model loading
- **Processing Time**: Heavy models may timeout on free tier

## Example Integration Code

### JavaScript/Thunkable
```javascript
const response = await fetch('https://your-space.hf.space/api/predict', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({
    data: [base64Image, "mobilenet", 0.5, 0.4]
  })
});

const result = await response.json();
const faces = result.data[0].faces;
```

### Python
```python
import requests
import base64

# Convert image to base64
with open('image.jpg', 'rb') as f:
    image_b64 = base64.b64encode(f.read()).decode()

# Make API call
response = requests.post(
    'https://your-space.hf.space/api/predict',
    json={"data": [image_b64, "mobilenet", 0.5, 0.4]}
)

result = response.json()
faces = result["data"][0]["faces"]
```

## Support

For issues or questions:
1. Check the web interface at your Space URL
2. Test locally using the provided test script
3. Use the built-in API testing tab in Gradio
4. Verify model files are correctly uploaded

## License

Apache 2.0