Add /api/detect endpoint for JSON detections (MonitaQC compatible)
Browse files
app.py
CHANGED
|
@@ -269,7 +269,22 @@ with gr.Blocks(title="Industrial Defect Detection") as demo:
|
|
| 269 |
fn=gradio_inference,
|
| 270 |
inputs=[input_image, model_dropdown, conf_slider],
|
| 271 |
outputs=output_image,
|
| 272 |
-
api_name="predict" # Creates /api/predict endpoint
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 273 |
)
|
| 274 |
|
| 275 |
gr.Markdown("### Example Images")
|
|
@@ -285,16 +300,17 @@ with gr.Blocks(title="Industrial Defect Detection") as demo:
|
|
| 285 |
gr.Markdown("""
|
| 286 |
### API Access
|
| 287 |
|
| 288 |
-
This Space provides
|
| 289 |
|
| 290 |
-
|
| 291 |
-
- **
|
| 292 |
-
- **
|
| 293 |
-
- `data[0]`: image (base64 or file)
|
| 294 |
-
- `data[1]`: model name (e.g., "Data Matrix")
|
| 295 |
-
- `data[2]`: confidence threshold (0.0-1.0)
|
| 296 |
|
| 297 |
-
**
|
|
|
|
|
|
|
|
|
|
|
|
|
| 298 |
```python
|
| 299 |
from gradio_client import Client
|
| 300 |
|
|
@@ -307,7 +323,21 @@ with gr.Blocks(title="Industrial Defect Detection") as demo:
|
|
| 307 |
)
|
| 308 |
```
|
| 309 |
|
| 310 |
-
**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 311 |
- Data Matrix
|
| 312 |
- Dental Implant
|
| 313 |
- Ball Pen
|
|
|
|
| 269 |
fn=gradio_inference,
|
| 270 |
inputs=[input_image, model_dropdown, conf_slider],
|
| 271 |
outputs=output_image,
|
| 272 |
+
api_name="predict" # Creates /api/predict endpoint (returns image)
|
| 273 |
+
)
|
| 274 |
+
|
| 275 |
+
# Hidden interface for JSON API (for MonitaQC compatibility)
|
| 276 |
+
with gr.Row(visible=False):
|
| 277 |
+
json_image = gr.Image(type="numpy")
|
| 278 |
+
json_model = gr.Dropdown(choices=[v["name"] for v in MODELS.values()])
|
| 279 |
+
json_conf = gr.Slider(minimum=0.0, maximum=1.0, value=0.25)
|
| 280 |
+
json_output = gr.JSON()
|
| 281 |
+
json_btn = gr.Button("JSON Detect")
|
| 282 |
+
|
| 283 |
+
json_btn.click(
|
| 284 |
+
fn=api_inference,
|
| 285 |
+
inputs=[json_image, json_model, json_conf],
|
| 286 |
+
outputs=json_output,
|
| 287 |
+
api_name="detect" # Creates /api/detect endpoint (returns JSON)
|
| 288 |
)
|
| 289 |
|
| 290 |
gr.Markdown("### Example Images")
|
|
|
|
| 300 |
gr.Markdown("""
|
| 301 |
### API Access
|
| 302 |
|
| 303 |
+
This Space provides two API endpoints:
|
| 304 |
|
| 305 |
+
**1. Image API** (returns annotated image):
|
| 306 |
+
- **Endpoint**: `/api/predict`
|
| 307 |
+
- **Returns**: Annotated image with bounding boxes
|
|
|
|
|
|
|
|
|
|
| 308 |
|
| 309 |
+
**2. JSON API** (returns detection data - for MonitaQC):
|
| 310 |
+
- **Endpoint**: `/api/detect`
|
| 311 |
+
- **Returns**: JSON array of detections with bboxes and confidence
|
| 312 |
+
|
| 313 |
+
**Python Example (Image):**
|
| 314 |
```python
|
| 315 |
from gradio_client import Client
|
| 316 |
|
|
|
|
| 323 |
)
|
| 324 |
```
|
| 325 |
|
| 326 |
+
**Python Example (JSON - for MonitaQC):**
|
| 327 |
+
```python
|
| 328 |
+
from gradio_client import Client
|
| 329 |
+
|
| 330 |
+
client = Client("smartfalcon-ai/Industrial-Defect-Detection")
|
| 331 |
+
detections = client.predict(
|
| 332 |
+
"path/to/image.jpg",
|
| 333 |
+
"Data Matrix",
|
| 334 |
+
0.25,
|
| 335 |
+
api_name="/detect"
|
| 336 |
+
)
|
| 337 |
+
# Returns: [{"bbox": [x1, y1, x2, y2], "confidence": 0.85, "class_id": 0, ...}]
|
| 338 |
+
```
|
| 339 |
+
|
| 340 |
+
**Available Models:**
|
| 341 |
- Data Matrix
|
| 342 |
- Dental Implant
|
| 343 |
- Ball Pen
|