File size: 29,509 Bytes
4fec4e4 | 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 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 | # API Documentation
This document describes the Gradio API endpoints exposed by the ROI-VAE image and video compression application. The API allows programmatic access to segmentation, compression, detection, and full pipeline processing for both images and videos.
**Live Demo:** https://biaslab2025-contextual-communication-demo.hf.space
## Table of Contents
- [Quick Start](#quick-start)
- [Important Notes](#important-notes)
- [Image API Endpoints](#image-api-endpoints)
- [/segment](#1-segment---generate-roi-mask)
- [/compress](#2-compress---compress-image)
- [/detect](#3-detect---object-detection)
- [/detect_overlay](#31-detect_overlay---detection-with-visualization)
- [/process](#4-process---full-image-pipeline)
- [Video API Endpoints](#video-api-endpoints)
- [/segment_video](#1-segment_video---segment-video)
- [/compress_video](#2-compress_video---compress-video)
- [/detect_video](#3-detect_video---video-detection)
- [/process_video](#4-process_video---full-video-pipeline)
- [Streaming Video API Endpoints](#streaming-video-api-endpoints)
- [/stream_process_video](#1-stream_process_video---full-streaming-pipeline)
- [/stream_compress_video](#2-stream_compress_video---simplified-streaming-compression)
- [Class Reference](#class-reference)
- [Error Handling](#error-handling)
- [GPU Quota Handling](#handling-gpu-quota-on-hf-spaces)
- [cURL Examples](#using-with-curl)
- [Example Scripts](#example-scripts)
---
## Quick Start
### Installation
```bash
pip install gradio_client
```
### Image Processing
```python
from gradio_client import Client, handle_file
# Connect to the API
client = Client("https://biaslab2025-contextual-communication-demo.hf.space")
# Or local: client = Client("http://localhost:7860")
# Full pipeline: segment → compress → detect
compressed, mask, bpp, ratio, coverage, detections_json = client.predict(
handle_file("path/to/image.jpg"),
"car, person", # segmentation prompt
"sam3", # segmentation method
4, # quality level (1-5)
0.3, # sigma (background compression)
True, # run detection
"yolo", # detection method
"", # detection classes (empty for closed-vocab)
api_name="/process"
)
print(f"Compression: {bpp:.4f} bpp ({ratio:.2f}x)")
```
### Video Processing
```python
from gradio_client import Client, handle_file
import json
client = Client("http://localhost:7860")
# Full pipeline with static settings
output_video, stats_json = client.predict(
handle_file("path/to/video.mp4"),
"person, car", # segmentation classes
"sam3", # segmentation method
"static", # mode: "static" or "dynamic"
4, # quality level (1-5)
0.3, # sigma
15.0, # output FPS
500, # bandwidth (dynamic mode)
5, # min_fps (dynamic mode)
30, # max_fps (dynamic mode)
False, # run detection
"yolo", # detection method
None, # mask_file_path (optional)
api_name="/process_video"
)
stats = json.loads(stats_json)
print(f"Compressed video: {output_video}")
print(f"Total frames: {stats['total_frames']}")
```
---
## Important Notes
### File Handling
Always wrap file paths with `handle_file()` when using `gradio_client`:
```python
from gradio_client import handle_file
# ✅ Correct
client.predict(handle_file("image.jpg"), ...)
# ❌ Incorrect - will fail with validation error
client.predict("image.jpg", ...)
```
### Detection Output Format
All detection endpoints return JSON strings with this structure:
```python
import json
detections = json.loads(detections_json)
# Each detection has:
# - label: str (class name)
# - score: float (confidence 0-1)
# - bbox_xyxy: list[float] (bounding box [x1, y1, x2, y2])
```
### Open-Vocabulary Detectors
The following detectors require a `classes` parameter:
- `yolo_world` - YOLO-World
- `grounding_dino` - Grounding DINO
Closed-vocabulary detectors (`yolo`, `detr`, `faster_rcnn`, etc.) use pretrained COCO classes and ignore the `classes` parameter.
---
## Image API Endpoints
### 1. `/segment` - Generate ROI Mask
Segments an image to create a Region of Interest (ROI) mask.
**Parameters:**
| Parameter | Type | Default | Description |
|-----------|------|---------|-------------|
| `image` | Image | required | Input image file |
| `prompt` | str | `"object"` | Comma-separated classes or natural language prompt |
| `method` | str | `"sam3"` | Segmentation method (see [methods](#segmentation-methods)) |
| `return_overlay` | bool | `False` | If `True`, returns image with ROI highlighted instead of mask |
**Returns:**
| Output | Type | Description |
|--------|------|-------------|
| `result_image` | Image | Grayscale mask OR image with ROI overlay (if `return_overlay=True`) |
| `roi_coverage` | float | Fraction of image covered by ROI (0.0-1.0) |
| `classes_used` | str | JSON list of classes/prompts used |
**Example:**
```python
# Get binary mask (default)
mask, coverage, classes = client.predict(
handle_file("car_scene.jpg"),
"car, road",
"sam3",
False, # return_overlay
api_name="/segment"
)
print(f"ROI covers {coverage*100:.2f}% of image")
# Get image with ROI highlighted
highlighted, coverage, classes = client.predict(
handle_file("car_scene.jpg"),
"car, road",
"sam3",
True, # return_overlay=True
api_name="/segment"
)
```
---
### 2. `/compress` - Compress Image
Compresses an image using TIC VAE, optionally with an ROI mask for variable quality.
**Parameters:**
| Parameter | Type | Default | Description |
|-----------|------|---------|-------------|
| `image` | Image | required | Input image file |
| `mask_image` | Image | `None` | ROI mask (white=ROI, black=background) |
| `quality` | int | `4` | Quality level 1-5 |
| `sigma` | float | `0.3` | Background preservation (0.01-1.0) |
**Quality Levels:**
| Level | Lambda | Description |
|-------|--------|-------------|
| 1 | 0.0035 | Smallest file |
| 2 | 0.013 | Smaller file |
| 3 | 0.025 | Balanced |
| 4 | 0.0483 | Higher quality (default) |
| 5 | 0.0932 | Best quality |
**Returns:**
| Output | Type | Description |
|--------|------|-------------|
| `compressed_image` | Image | Compressed output image |
| `bpp` | float | Bits per pixel |
| `compression_ratio` | float | Compression ratio (24/bpp) |
**Example:**
```python
# Compress without mask (uniform quality)
compressed, bpp, ratio = client.predict(
handle_file("image.jpg"),
None, # no mask
4, # quality
0.3, # sigma (ignored without mask)
api_name="/compress"
)
# Compress with ROI mask
mask, _, _ = client.predict(handle_file("image.jpg"), "person", "yolo", False, api_name="/segment")
compressed, bpp, ratio = client.predict(
handle_file("image.jpg"),
handle_file(mask),
4,
0.2, # aggressive background compression
api_name="/compress"
)
```
---
### 3. `/detect` - Object Detection
Runs object detection on an image and returns detection results as JSON.
**Parameters:**
| Parameter | Type | Default | Description |
|-----------|------|---------|-------------|
| `image` | Image | required | Input image file |
| `method` | str | `"yolo"` | Detection method (see [methods](#detection-methods)) |
| `classes` | str | `""` | Comma-separated classes (required for open-vocab detectors) |
| `confidence` | float | `0.25` | Confidence threshold (0.0-1.0) |
**Returns:**
| Output | Type | Description |
|--------|------|-------------|
| `detections_json` | str | JSON string of detection results |
**Example - Closed-Vocabulary:**
```python
import json
# YOLO detection (COCO classes)
dets_json = client.predict(
handle_file("street_scene.jpg"),
"yolo",
"", # no classes needed
0.25,
api_name="/detect"
)
detections = json.loads(dets_json)
for det in detections:
print(f"{det['label']}: {det['score']:.2f}")
```
**Example - Open-Vocabulary:**
```python
# YOLO-World with custom classes
dets_json = client.predict(
handle_file("image.jpg"),
"yolo_world",
"hat, backpack, umbrella", # custom classes required
0.25,
api_name="/detect"
)
```
---
### 3.1. `/detect_overlay` - Detection with Visualization
Runs object detection and returns the image with bounding boxes drawn.
**Parameters:**
| Parameter | Type | Default | Description |
|-----------|------|---------|-------------|
| `image` | Image | required | Input image file |
| `method` | str | `"yolo"` | Detection method (see [methods](#detection-methods)) |
| `classes` | str | `""` | Comma-separated classes (required for open-vocab detectors) |
| `confidence` | float | `0.25` | Confidence threshold (0.0-1.0) |
**Returns:**
| Output | Type | Description |
|--------|------|-------------|
| `result_image` | Image | Image with detection bounding boxes |
| `detections_json` | str | JSON string of detection results |
**Example:**
```python
import json
# Get image with detection boxes
result_img, dets_json = client.predict(
handle_file("street_scene.jpg"),
"yolo",
"",
0.25,
api_name="/detect_overlay"
)
# result_img is a file path to the image with boxes drawn
print(f"Image with boxes: {result_img}")
detections = json.loads(dets_json)
```
---
### 4. `/process` - Full Image Pipeline
Runs the complete pipeline: segmentation → compression → optional detection.
**Parameters:**
| Parameter | Type | Default | Description |
|-----------|------|---------|-------------|
| `image` | Image | required | Input image file |
| `prompt` | str | `"object"` | Segmentation prompt/classes |
| `segmentation_method` | str | `"sam3"` | ROI segmentation method |
| `quality` | int | `4` | Compression quality (1-5) |
| `sigma` | float | `0.3` | Background preservation (0.01-1.0) |
| `run_detection` | bool | `False` | Whether to run detection on output |
| `detection_method` | str | `"yolo"` | Detector to use |
| `detection_classes` | str | `""` | Classes for open-vocab detectors |
**Returns:**
| Output | Type | Description |
|--------|------|-------------|
| `compressed_image` | Image | Compressed output image |
| `mask_image` | Image | Generated ROI mask |
| `bpp` | float | Bits per pixel |
| `compression_ratio` | float | Compression ratio |
| `roi_coverage` | float | ROI coverage percentage (0-1) |
| `detections_json` | str | JSON detections (empty list if `run_detection=False`) |
**Example:**
```python
import json
compressed, mask, bpp, ratio, coverage, dets_json = client.predict(
handle_file("street.jpg"),
"car, person, road",
"sam3",
4,
0.3,
True, # run detection
"yolo",
"",
api_name="/process"
)
print(f"ROI Coverage: {coverage*100:.2f}%")
print(f"Compression: {bpp:.4f} bpp ({ratio:.2f}x)")
print(f"Detections: {len(json.loads(dets_json))}")
```
---
## Video API Endpoints
### 1. `/segment_video` - Segment Video
Segments a video to find ROI regions, returning either a mask file or overlay video.
**Parameters:**
| Parameter | Type | Default | Description |
|-----------|------|---------|-------------|
| `video_path` | Video | required | Input video file |
| `prompt` | str | `"object"` | Comma-separated classes or natural language prompt |
| `method` | str | `"sam3"` | Segmentation method |
| `return_overlay` | bool | `False` | If `True`, returns video with ROI highlighted |
| `output_fps` | float | `15.0` | Output framerate (max 30) |
**Returns:**
| Output | Type | Description |
|--------|------|-------------|
| `result_path` | File/Video | Mask file (NPZ) OR video with ROI overlay |
| `stats_json` | str | JSON with frame count, coverage, and classes |
**Example:**
```python
import json
# Get mask file for reuse in compression
mask_file, stats_json = client.predict(
handle_file("video.mp4"),
"person, car",
"sam3",
False, # return masks file
15.0, # fps
api_name="/segment_video"
)
stats = json.loads(stats_json)
print(f"Processed {stats['total_frames']} frames")
print(f"Avg ROI coverage: {stats['avg_roi_coverage']*100:.2f}%")
# Get video with ROI overlay for visualization
overlay_video, _ = client.predict(
handle_file("video.mp4"),
"person, car",
"sam3",
True, # return overlay video
15.0,
api_name="/segment_video"
)
```
---
### 2. `/compress_video` - Compress Video
Compresses a video with optional ROI mask preservation.
**Parameters:**
| Parameter | Type | Default | Description |
|-----------|------|---------|-------------|
| `video_path` | Video | required | Input video file |
| `mask_file_path` | str | `None` | Path to pre-computed masks (from `/segment_video`) |
| `quality` | int | `4` | Quality level (1-5) |
| `sigma` | float | `0.3` | Background preservation (0.01-1.0) |
| `output_fps` | float | `15.0` | Target output framerate |
**Returns:**
| Output | Type | Description |
|--------|------|-------------|
| `compressed_video` | Video | Compressed output video |
| `stats_json` | str | JSON with compression statistics |
**Example:**
```python
import json
# First, segment to get masks
mask_file, _ = client.predict(
handle_file("video.mp4"), "person", "sam3", False, 15.0,
api_name="/segment_video"
)
# Then compress with cached masks (3-5x faster!)
compressed, stats_json = client.predict(
handle_file("video.mp4"),
mask_file, # reuse masks
4, # quality
0.3, # sigma
15.0, # fps
api_name="/compress_video"
)
stats = json.loads(stats_json)
print(f"Compression ratio: {stats['compression_ratio']}x")
print(f"Total size: {stats['total_size_kb']} KB")
```
---
### 3. `/detect_video` - Video Detection
Runs object detection on each frame of a video.
**Parameters:**
| Parameter | Type | Default | Description |
|-----------|------|---------|-------------|
| `video_path` | Video | required | Input video file |
| `method` | str | `"yolo"` | Detection method |
| `classes` | str | `""` | Comma-separated classes (required for open-vocab) |
| `confidence` | float | `0.25` | Confidence threshold (0.0-1.0) |
| `return_overlay` | bool | `False` | If `True`, returns video with detection boxes |
| `output_fps` | float | `15.0` | Output framerate (max 30) |
**Returns:**
| Output | Type | Description |
|--------|------|-------------|
| `result_video` | Video | Video with detection boxes (if `return_overlay=True`), None otherwise |
| `detections_json` | str | JSON with per-frame detections |
**Example:**
```python
import json
# Get per-frame detections JSON
_, dets_json = client.predict(
handle_file("video.mp4"),
"yolo",
"",
0.25,
False, # return JSON only
15.0,
api_name="/detect_video"
)
data = json.loads(dets_json)
print(f"Total detections: {data['total_detections']}")
print(f"Avg per frame: {data['avg_detections_per_frame']}")
# Get video with detection overlays
det_video, _ = client.predict(
handle_file("video.mp4"),
"yolo",
"",
0.25,
True, # return overlay video
15.0,
api_name="/detect_video"
)
```
---
### 4. `/process_video` - Full Video Pipeline
Processes a video with ROI-based compression (segment → compress), with optional detection.
**Parameters:**
| Parameter | Type | Default | Description |
|-----------|------|---------|-------------|
| `video_path` | Video | required | Input video file |
| `prompt` | str | `"object"` | Segmentation prompt/classes |
| `segmentation_method` | str | `"sam3"` | ROI segmentation method |
| `mode` | str | `"static"` | `"static"` or `"dynamic"` |
| `quality` | int | `4` | Quality level 1-5 (static mode) |
| `sigma` | float | `0.3` | Background preservation (static mode) |
| `output_fps` | float | `15.0` | Target framerate (static mode) |
| `bandwidth_kbps` | float | `500.0` | Target bandwidth (dynamic mode) |
| `min_fps` | float | `5.0` | Minimum framerate (dynamic mode) |
| `max_fps` | float | `30.0` | Maximum framerate (dynamic mode) |
| `aggressiveness` | float | `0.5` | Bandwidth savings strategy (dynamic mode): `0.0` = use full bandwidth (high FPS always), `0.5` = moderate savings, `1.0` = maximum savings (aggressive FPS reduction for low motion) |
| `run_detection` | bool | `False` | Whether to run detection/tracking |
| `detection_method` | str | `"yolo"` | Detector to use |
| `mask_file_path` | str | `None` | Path to pre-computed masks (skips segmentation) |
**Returns:**
| Output | Type | Description |
|--------|------|-------------|
| `output_video` | Video | Compressed video |
| `stats_json` | str | JSON with detailed statistics |
**Example - Static Mode:**
```python
import json
output, stats_json = client.predict(
handle_file("video.mp4"),
"person, car",
"sam3",
"static",
4, 0.3, 15.0, # static: quality, sigma, fps
500, 5, 30, # dynamic: bandwidth, min_fps, max_fps (ignored)
False, "yolo", None,
api_name="/process_video"
)
stats = json.loads(stats_json)
print(f"Processed {stats['total_frames']} frames")
```
**Example - Dynamic Mode:**
```python
output, stats_json = client.predict(
handle_file("video.mp4"),
"person",
"yolo",
"dynamic",
4, 0.3, 15.0, # static settings (ignored)
750, # target bandwidth 750 kbps
8, # min FPS
30, # max FPS
True, "yolo", None,
api_name="/process_video"
)
```
---
## Class Reference
### Segmentation Methods
**Pixel-Perfect Segmentation:**
| Method | Description | Classes |
|--------|-------------|---------|
| `sam3` | Prompt-based (natural language) | Any text prompt |
### Segmentation Methods
| Method | Description | Classes |
|--------|-------------|---------|
| `sam3` | Prompt-based (natural language) | Any text prompt |
| `yolo` | YOLO instance segmentation | 80 COCO classes |
| `segformer` | Cityscapes semantic segmentation | 19 classes |
| `mask2former` | Swin-based panoptic/semantic | 133 COCO / 150 ADE20K |
| `maskrcnn` | ResNet50-FPN instance segmentation | 80 COCO classes |
| `fake_yolo` | Fast bbox-based (YOLO + ByteTrack) | 80 COCO classes |
| `fake_yolo_botsort` | Fast bbox-based (YOLO + BoTSORT) | 80 COCO classes |
| `fake_detr` | Fast bbox-based (DETR + ByteTrack) | 80 COCO classes |
| `fake_fasterrcnn` | Fast bbox-based (Faster R-CNN + ByteTrack) | 80 COCO classes |
| `fake_retinanet` | Fast bbox-based (RetinaNet + ByteTrack) | 80 COCO classes |
| `fake_fcos` | Fast bbox-based (FCOS + ByteTrack) | 80 COCO classes |
| `fake_deformable_detr` | Fast bbox-based (Deformable DETR + ByteTrack) | 80 COCO classes |
| `fake_grounding_dino` | Fast bbox-based (Grounding DINO + ByteTrack) | Requires prompt |
**Note:** `fake_*` methods create rectangular masks from detection bounding boxes with object tracking. Faster than pixel-perfect segmentation, suitable for video when precise boundaries aren't critical.
### Detection Methods
**Closed-Vocabulary (COCO pretrained):**
| Method | Description |
|--------|-------------|
| `yolo` | Ultralytics YOLO |
| `detr` | Facebook DETR |
| `faster_rcnn` | Faster R-CNN |
| `retinanet` | RetinaNet |
| `fcos` | FCOS |
| `ssd` | SSD300 |
| `deformable_detr` | Deformable DETR |
**Open-Vocabulary (requires `classes` parameter):**
| Method | Description |
|--------|-------------|
| `yolo_world` | YOLO-World |
| `grounding_dino` | Grounding DINO |
### COCO Classes (80)
```
person, bicycle, car, motorcycle, airplane, bus, train, truck, boat,
traffic light, fire hydrant, stop sign, parking meter, bench, bird, cat,
dog, horse, sheep, cow, elephant, bear, zebra, giraffe, backpack, umbrella,
handbag, tie, suitcase, frisbee, skis, snowboard, sports ball, kite,
baseball bat, baseball glove, skateboard, surfboard, tennis racket, bottle,
wine glass, cup, fork, knife, spoon, bowl, banana, apple, sandwich, orange,
broccoli, carrot, hot dog, pizza, donut, cake, chair, couch, potted plant,
bed, dining table, toilet, tv, laptop, mouse, remote, keyboard, cell phone,
microwave, oven, toaster, sink, refrigerator, book, clock, vase, scissors,
teddy bear, hair drier, toothbrush
```
### Cityscapes Classes (19)
```
road, sidewalk, building, wall, fence, pole, traffic light, traffic sign,
vegetation, terrain, sky, person, rider, car, truck, bus, train, motorcycle,
bicycle
```
---
## Error Handling
```python
try:
result = client.predict(
handle_file("image.jpg"),
...,
api_name="/endpoint"
)
except Exception as e:
print(f"API Error: {e}")
```
**Common Errors:**
| Error | Cause | Solution |
|-------|-------|----------|
| Validation error for ImageData | Missing `handle_file()` | Wrap file paths with `handle_file()` |
| File does not exist | Invalid path | Check file path is correct |
| Empty detection classes | Open-vocab detector without classes | Provide classes for `yolo_world`, `grounding_dino` |
| GPU quota exceeded | HF Spaces limit | Wait and retry (see below) |
---
## Handling GPU Quota on HF Spaces
When using Hugging Face Spaces with ZeroGPU, you may encounter quota limits:
```
You have exceeded your GPU quota (60s requested vs. 0s left). Try again in 0:05:30
```
### Automatic Retry with Backoff
```python
import time
import re
def extract_wait_time(error_msg):
"""Extract wait time from GPU quota error message."""
match = re.search(r'Try again in (\d+):(\d+)(?::(\d+))?', error_msg)
if match:
if match.group(3): # HH:MM:SS
return int(match.group(1)) * 3600 + int(match.group(2)) * 60 + int(match.group(3))
else: # MM:SS
return int(match.group(1)) * 60 + int(match.group(2))
return 60
def call_with_retry(client, *args, api_name, max_retries=5):
"""Call API with exponential backoff retry."""
delay = 10
for attempt in range(max_retries):
try:
return client.predict(*args, api_name=api_name)
except Exception as e:
error_msg = str(e)
if "exceeded your GPU quota" in error_msg:
wait_time = extract_wait_time(error_msg)
actual_delay = max(delay, wait_time + 5)
print(f"⏳ GPU quota exhausted. Waiting {actual_delay}s... (attempt {attempt + 1})")
time.sleep(actual_delay)
delay *= 2
else:
raise
raise Exception("Max retries reached")
# Usage
result = call_with_retry(
client,
handle_file("image.jpg"),
"car", "sam3", False, 4, 0.3, False, "yolo", "",
api_name="/process"
)
```
---
## Using with cURL
### Upload File First
```bash
# Upload image
FILE_URL=$(curl -s -X POST http://localhost:7860/upload \
-F "files=@image.jpg" | \
python3 -c "import sys, json; print(json.load(sys.stdin)[0])")
```
### Call Endpoints
```bash
# Segment
curl -X POST http://localhost:7860/api/segment \
-H "Content-Type: application/json" \
-d "{\"data\": [\"$FILE_URL\", \"car, person\", \"sam3\", false]}"
# Compress (no mask)
curl -X POST http://localhost:7860/api/compress \
-H "Content-Type: application/json" \
-d "{\"data\": [\"$FILE_URL\", null, 4, 0.3]}"
# Detect
curl -X POST http://localhost:7860/api/detect \
-H "Content-Type: application/json" \
-d "{\"data\": [\"$FILE_URL\", \"yolo\", \"\", 0.25, false]}"
# Full pipeline
curl -X POST http://localhost:7860/api/process \
-H "Content-Type: application/json" \
-d "{\"data\": [\"$FILE_URL\", \"car, person\", \"sam3\", 4, 0.3, true, \"yolo\", \"\"]}"
```
---
## Performance Guide
### Choosing Segmentation Methods
**Use Pixel-Perfect Segmentation when:**
- You need precise object boundaries
- Working with single images or small videos
- Quality is more important than speed
- Computing time/power is not constrained
**Use Fast Segmentation (fake_*) when:**
- Processing large videos or real-time streams
- Speed is critical (2-3x faster)
- Rectangular masks are acceptable
- Need temporal consistency (tracking maintains object IDs)
### Performance Benchmarks
**Video Processing (480p, 30 frames):**
| Method | Speed | Use Case |
|--------|-------|----------|
| `fake_yolo` | ~70 fps | Real-time video, fastest |
| `fake_yolo_botsort` | ~65 fps | Real-time with robust tracking |
| `fake_detr` | ~40 fps | Good speed + accuracy balance |
| `fake_fasterrcnn` | ~30 fps | Accurate detection |
| `yolo` (pixel-perfect) | ~30 fps | Instance segmentation |
| `sam3` | ~15 fps | Prompt-based, highest flexibility |
| `mask2former` | ~20 fps | Panoptic segmentation |
**Detection Performance (with batch support):**
| Detector | Single-Frame | Batch (30 frames) | Speedup |
|----------|--------------|-------------------|---------|
| YOLO26x | ~40 fps | ~70 fps | 1.75x |
| DETR | ~15 fps | ~40 fps | 2.67x |
| Faster R-CNN | ~12 fps | ~30 fps | 2.50x |
### Example: Fast Video Processing
```python
from gradio_client import Client, handle_file
import json
import time
client = Client("http://localhost:7860")
# Method 1: Fast fake segmentation (recommended for video)
start = time.time()
output1, stats1 = client.predict(
handle_file("long_video.mp4"),
"person, car",
"fake_yolo", # Fast detection + tracking
"static",
4,
0.3,
15.0,
500, 5, 30, False, "yolo", None,
api_name="/process_video"
)
fast_time = time.time() - start
# Method 2: Pixel-perfect segmentation
start = time.time()
output2, stats2 = client.predict(
handle_file("long_video.mp4"),
"person, car",
"yolo", # Pixel-perfect YOLO26x-seg
"static",
4,
0.3,
15.0,
500, 5, 30, False, "yolo", None,
api_name="/process_video"
)
perfect_time = time.time() - start
stats1_data = json.loads(stats1)
stats2_data = json.loads(stats2)
print(f"Fast segmentation: {fast_time:.2f}s")
print(f"Pixel-perfect: {perfect_time:.2f}s")
print(f"Speedup: {perfect_time/fast_time:.2f}x faster")
print(f"Compression ratio (fast): {stats1_data['compression_ratio']:.2f}x")
print(f"Compression ratio (perfect): {stats2_data['compression_ratio']:.2f}x")
```
### Example: Tracker Comparison
```python
# Test different trackers with same detector
trackers = {
"ByteTrack (default)": "fake_yolo",
"BoTSORT": "fake_yolo_botsort",
}
for name, method in trackers.items():
output, stats = client.predict(
handle_file("test_video.mp4"),
"person",
method,
"static",
4, 0.3, 15.0,
500, 5, 30, False, "yolo", None,
api_name="/process_video"
)
stats_data = json.loads(stats)
print(f"{name}: {stats_data['avg_roi_coverage']:.2f}% avg coverage")
```
---
## Example Scripts
### Batch Image Processing
```python
from gradio_client import Client, handle_file
from pathlib import Path
client = Client("http://localhost:7860")
output_dir = Path("compressed_output")
output_dir.mkdir(exist_ok=True)
for img_path in Path("images").glob("*.jpg"):
print(f"Processing {img_path.name}...")
compressed, mask, bpp, ratio, coverage, _ = client.predict(
handle_file(str(img_path)),
"car, person",
"sam3",
4, 0.3,
False, "", "",
api_name="/process"
)
# Save compressed image
output_path = output_dir / f"compressed_{img_path.name}"
with open(output_path, "wb") as f:
f.write(open(compressed, "rb").read())
print(f" BPP: {bpp:.4f}, Ratio: {ratio:.2f}x, ROI: {coverage*100:.2f}%")
```
### Video Processing with Mask Caching
```python
from gradio_client import Client, handle_file
import json
client = Client("http://localhost:7860")
video_path = "input_video.mp4"
# Step 1: Segment video (one-time cost)
mask_file, seg_stats = client.predict(
handle_file(video_path),
"person, car",
"sam3",
False, # return mask file
15.0,
api_name="/segment_video"
)
print(f"Segmented video, masks saved to: {mask_file}")
# Step 2: Compress with different settings, reusing masks
for quality in [3, 4, 5]:
compressed, comp_stats = client.predict(
handle_file(video_path),
mask_file, # reuse cached masks
quality,
0.3,
15.0,
api_name="/compress_video"
)
stats = json.loads(comp_stats)
print(f"Quality {quality}: {stats['compression_ratio']}x compression")
```
### Detection Comparison (Original vs Compressed)
```python
from gradio_client import Client, handle_file
import json
client = Client("http://localhost:7860")
image = "street_scene.jpg"
# Detect on original
_, dets_orig = client.predict(
handle_file(image), "yolo", "", 0.25, False,
api_name="/detect"
)
orig_count = len(json.loads(dets_orig))
print(f"Original: {orig_count} detections")
# Compress and detect
compressed, _, bpp, ratio, _, dets_comp = client.predict(
handle_file(image),
"car, person, road",
"sam3",
4, 0.3,
True, "yolo", "",
api_name="/process"
)
comp_count = len(json.loads(dets_comp))
retention = comp_count / orig_count * 100 if orig_count else 0
print(f"Compressed ({ratio:.2f}x): {comp_count} detections")
print(f"Detection retention: {retention:.1f}%")
```
---
## Additional Resources
- **Web UI**: Visit `http://localhost:7860` for interactive interface
- **GitHub**: See repository for source code and examples
- **Model Checkpoints**: Available in `checkpoints/` directory
- **Test Images**: Sample images in `data/images/` directory
|