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, facebook/sam3.1) |
output_format |
string | geojson |
One of geojson, geotiff, png, json, detections |
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, SAM2, and SAM3.
For SAM3 with bounding box prompts, the model finds all similar objects in the image (not just the object inside the box). Point prompts with SAM3 segment the specific object at the point location.
Parameters (multipart form):
| Parameter | Type | Default | Description |
|---|---|---|---|
file |
file | required | Image file (TIFF, PNG, JPEG) |
model_version |
string | sam3 |
One of sam, sam2, sam3 |
model_id |
string | auto | Model identifier (e.g., facebook/sam3.1) |
output_format |
string | geojson |
One of geojson, geotiff, png, json, detections |
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) for point/box coordinates |
multimask_output |
bool | false |
Return multiple masks per prompt |
min_size |
int | 0 |
Minimum mask size in pixels |
max_size |
int | none | Maximum mask size in pixels |
Example with point prompts:
1 2 3 4 5 | |
Example with box prompts (finds all similar objects):
1 2 3 4 | |
Example with JSON output (pixel-coordinate bounding boxes):
1 2 3 4 | |
1 2 3 4 5 6 7 8 9 | |
Example with detections output (geographic-coordinate bounding boxes):
1 2 3 4 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
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 (facebook/sam3 or facebook/sam3.1) |
backend |
string | meta |
One of meta, transformers; SAM 3.1 uses meta |
output_format |
string | geojson |
One of geojson, geotiff, png, json, detections |
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 (GeoJSON mask polygons):
1 2 3 4 | |
Example with JSON output (pixel-coordinate bounding boxes):
1 2 3 4 | |
1 2 3 4 5 6 7 8 9 | |
Example with detections output (geographic-coordinate bounding boxes):
1 2 3 4 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
GeoJSON output¶
With output_format=geojson, every segmentation endpoint returns one polygon
feature per mask. Each feature's properties carry the mask's raster value
and, when the model reports one, its confidence score, so a single request
yields both geometry and confidence:
1 | |
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 14 | |
1 2 3 4 5 6 7 8 9 10 11 | |
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
620 621 622 623 624 625 626 627 628 629 630 631 632 | |
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, cache_key). The cache key
includes the model-shaping kwargs (e.g. |
Raises:
| Type | Description |
|---|---|
HTTPException
|
If model_version or model_id is invalid, or dependencies are missing. |
Source code in samgeo/api.py
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 | |
health()
¶
Health check endpoint.
Source code in samgeo/api.py
607 608 609 610 | |
list_models()
¶
List available and currently loaded models.
Source code in samgeo/api.py
613 614 615 616 617 | |
main()
¶
Entry point for the samgeo-api console script.
Source code in samgeo/api.py
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 | |
segment_automatic(file=File(...), model_version=Form('sam3'), 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('sam3')
|
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
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 | |
segment_predict(file=File(...), model_version=Form('sam3'), 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.
For SAM3 with bounding box prompts, the model finds all similar objects in the image (not just the object inside the box). Point prompts with SAM3 segment the specific object at the point location.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file
|
UploadFile
|
Image file (TIFF, PNG, JPEG). |
File(...)
|
model_version
|
str
|
One of "sam", "sam2", "sam3". |
Form('sam3')
|
model_id
|
Optional[str]
|
Specific model identifier. |
Form(None)
|
output_format
|
str
|
One of "geojson", "geotiff", "png", "json", "detections". |
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/box 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
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 | |
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", "detections", "json". Use "detections" to get a GeoJSON FeatureCollection of bounding box polygons in geographic coordinates with confidence scores. Use "json" for a plain JSON array of bounding boxes in pixel coordinates, suitable for non-georeferenced images. |
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
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 | |