REST API¶
segment-geospatial includes a built-in REST API powered by FastAPI that allows you to run image segmentation over HTTP. This is useful for integrating segmentation into web applications, pipelines, and non-Python clients.
Installation¶
Install the API dependencies with the api extra:
1 | |
To also install a specific SAM model backend, combine extras:
1 | |
Starting the Server¶
Use the samgeo-api command:
1 | |
Options:
1 2 3 | |
Alternatively, use uvicorn directly:
1 | |
Once running, interactive API docs (Swagger UI) are available at http://localhost:8000/docs.
Endpoints¶
Health Check¶
1 | |
Returns the server status and version.
1 | |
1 | |
List Models¶
1 | |
Returns available model versions/IDs and which models are currently loaded in memory.
1 | |
Clear Models¶
1 | |
Clears the model cache and frees GPU memory.
1 | |
Automatic Segmentation¶
1 | |
Runs automatic mask generation on an uploaded image. Supports SAM, SAM2, and SAM3.
Parameters (multipart form):
| Parameter | Type | Default | Description |
|---|---|---|---|
file |
file | required | Image file (TIFF, PNG, JPEG) |
model_version |
string | sam2 |
One of sam, sam2, sam3 |
model_id |
string | auto | Model identifier (e.g., sam2-hiera-large) |
output_format |
string | geojson |
One of geojson, geotiff, png |
foreground |
bool | true |
Extract foreground objects only |
unique |
bool | true |
Assign unique ID to each object |
min_size |
int | 0 |
Minimum mask size in pixels |
max_size |
int | none | Maximum mask size in pixels |
points_per_side |
int | 32 |
Points sampled per side (SAM/SAM2) |
pred_iou_thresh |
float | 0.8 |
IoU threshold for filtering |
stability_score_thresh |
float | 0.95 |
Stability score threshold |
Example:
1 2 3 4 | |
Prompt-based Segmentation¶
1 | |
Runs segmentation with point or bounding box prompts. Supports SAM and SAM2.
Parameters (multipart form):
| Parameter | Type | Default | Description |
|---|---|---|---|
file |
file | required | Image file (TIFF, PNG, JPEG) |
model_version |
string | sam2 |
One of sam, sam2 |
model_id |
string | auto | Model identifier |
output_format |
string | geojson |
One of geojson, geotiff, png |
point_coords |
string | none | JSON array of [[x, y], ...] |
point_labels |
string | none | JSON array of [1, 0, ...] (1=foreground, 0=background) |
boxes |
string | none | JSON array of [[xmin, ymin, xmax, ymax], ...] |
point_crs |
string | none | CRS string (e.g., EPSG:4326) |
multimask_output |
bool | false |
Return multiple masks per prompt |
Example with point prompts:
1 2 3 4 5 | |
Example with box prompts:
1 2 3 4 | |
Text-prompt Segmentation¶
1 | |
Runs text-prompt segmentation using SAM3.
Parameters (multipart form):
| Parameter | Type | Default | Description |
|---|---|---|---|
file |
file | required | Image file (TIFF, PNG, JPEG) |
prompt |
string | required | Text description (e.g., building, tree) |
model_id |
string | auto | SAM3 model identifier |
backend |
string | meta |
One of meta, transformers |
output_format |
string | geojson |
One of geojson, geotiff, png |
confidence_threshold |
float | 0.5 |
Detection confidence threshold |
min_size |
int | 0 |
Minimum mask size in pixels |
max_size |
int | none | Maximum mask size in pixels |
Example:
1 2 3 4 | |
Caching¶
The API automatically caches models and image encodings for better performance:
- Model cache: Models are loaded once and reused across requests. Use
DELETE /modelsto free GPU memory. - Image cache: When the same image is sent multiple times (e.g., with different prompts), the expensive image encoding step is skipped. This makes subsequent requests significantly faster.
Example timing with a 13 MB GeoTIFF:
| Request | Description | Time |
|---|---|---|
| 1st | Model load + image encoding | ~7s |
| 2nd | Same image, different prompt | ~0.4s |
| 3rd | Same image, another prompt | ~0.2s |
Python Client Example¶
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
API Reference¶
REST API for segment-geospatial.
Provides FastAPI endpoints for image segmentation using SAM, SAM2, and SAM3 models. Install with: pip install segment-geospatial[api]
Usage
samgeo-api # Start on default port 8000 samgeo-api --port 9000 # Custom port samgeo-api --preload sam2:sam2-hiera-large # Preload a model uvicorn samgeo.api:app # Direct uvicorn usage
clear_models()
¶
Clear the model cache and free GPU memory.
Source code in samgeo/api.py
312 313 314 315 316 317 318 319 320 321 322 323 324 | |
get_model(model_version, model_id=None, **kwargs)
¶
Get or create a cached model instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_version
|
str
|
One of "sam", "sam2", "sam3". |
required |
model_id
|
Optional[str]
|
Specific model identifier. Uses default if None. |
None
|
**kwargs
|
Additional keyword arguments for model initialization. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
(model_instance, threading.Lock) |
Raises:
| Type | Description |
|---|---|
HTTPException
|
If model_version or model_id is invalid, or dependencies are missing. |
Source code in samgeo/api.py
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 | |
health()
¶
Health check endpoint.
Source code in samgeo/api.py
299 300 301 302 | |
list_models()
¶
List available and currently loaded models.
Source code in samgeo/api.py
305 306 307 308 309 | |
main()
¶
Entry point for the samgeo-api console script.
Source code in samgeo/api.py
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 | |
segment_automatic(file=File(...), model_version=Form('sam2'), model_id=Form(None), output_format=Form('geojson'), foreground=Form(True), unique=Form(True), min_size=Form(0), max_size=Form(None), points_per_side=Form(32), pred_iou_thresh=Form(0.8), stability_score_thresh=Form(0.95))
async
¶
Run automatic mask generation on an uploaded image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file
|
UploadFile
|
Image file (TIFF, PNG, JPEG). |
File(...)
|
model_version
|
str
|
One of "sam", "sam2", "sam3". |
Form('sam2')
|
model_id
|
Optional[str]
|
Specific model identifier. |
Form(None)
|
output_format
|
str
|
One of "geojson", "geotiff", "png". |
Form('geojson')
|
foreground
|
bool
|
Whether to extract foreground objects only. |
Form(True)
|
unique
|
bool
|
Whether to assign unique IDs to each object. |
Form(True)
|
min_size
|
int
|
Minimum mask size in pixels. |
Form(0)
|
max_size
|
Optional[int]
|
Maximum mask size in pixels. |
Form(None)
|
points_per_side
|
int
|
Number of points sampled per side (SAM/SAM2). |
Form(32)
|
pred_iou_thresh
|
float
|
IoU threshold for filtering masks. |
Form(0.8)
|
stability_score_thresh
|
float
|
Stability score threshold for filtering. |
Form(0.95)
|
Returns:
| Type | Description |
|---|---|
|
Segmentation result in the requested format. |
Source code in samgeo/api.py
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 | |
segment_predict(file=File(...), model_version=Form('sam2'), model_id=Form(None), output_format=Form('geojson'), point_coords=Form(None), point_labels=Form(None), boxes=Form(None), point_crs=Form(None), multimask_output=Form(False), min_size=Form(0), max_size=Form(None))
async
¶
Run prompt-based segmentation with points or bounding boxes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file
|
UploadFile
|
Image file (TIFF, PNG, JPEG). |
File(...)
|
model_version
|
str
|
One of "sam", "sam2". |
Form('sam2')
|
model_id
|
Optional[str]
|
Specific model identifier. |
Form(None)
|
output_format
|
str
|
One of "geojson", "geotiff", "png". |
Form('geojson')
|
point_coords
|
Optional[str]
|
JSON string of [[x, y], ...] coordinate pairs. |
Form(None)
|
point_labels
|
Optional[str]
|
JSON string of [1, 0, ...] labels (1=foreground, 0=background). |
Form(None)
|
boxes
|
Optional[str]
|
JSON string of [[xmin, ymin, xmax, ymax], ...] bounding boxes. |
Form(None)
|
point_crs
|
Optional[str]
|
CRS string (e.g., "EPSG:4326") for point coordinates. |
Form(None)
|
multimask_output
|
bool
|
Whether to return multiple masks per prompt. |
Form(False)
|
min_size
|
int
|
Minimum mask size in pixels. |
Form(0)
|
max_size
|
Optional[int]
|
Maximum mask size in pixels. |
Form(None)
|
Returns:
| Type | Description |
|---|---|
|
Segmentation result in the requested format. |
Source code in samgeo/api.py
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 | |
segment_text(file=File(...), prompt=Form(...), model_id=Form(None), backend=Form('meta'), output_format=Form('geojson'), confidence_threshold=Form(0.5), min_size=Form(0), max_size=Form(None))
async
¶
Run text-prompt segmentation using SAM3.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file
|
UploadFile
|
Image file (TIFF, PNG, JPEG). |
File(...)
|
prompt
|
str
|
Text description of objects to segment (e.g., "building"). |
Form(...)
|
model_id
|
Optional[str]
|
SAM3 model identifier. |
Form(None)
|
backend
|
str
|
SAM3 backend, one of "meta" or "transformers". |
Form('meta')
|
output_format
|
str
|
One of "geojson", "geotiff", "png". |
Form('geojson')
|
confidence_threshold
|
float
|
Confidence threshold for detections. |
Form(0.5)
|
min_size
|
int
|
Minimum mask size in pixels. |
Form(0)
|
max_size
|
Optional[int]
|
Maximum mask size in pixels. |
Form(None)
|
Returns:
| Type | Description |
|---|---|
|
Segmentation result in the requested format. |
Source code in samgeo/api.py
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 | |