Image Segmentation with Grounded SAM¶
This notebook demonstrates how to use the GroundedSAM
class to segment images using Grounded SAM. To learn more about Grounded SAM, please refer to the Grounded SAM documentation.
Install package¶
To use the geoai-py
package, ensure it is installed in your environment. Uncomment the command below if needed.
In [ ]:
Copied!
# %pip install geoai-py
# %pip install geoai-py
Import library¶
In [ ]:
Copied!
import geoai
from geoai.segment import GroundedSAM
import geoai
from geoai.segment import GroundedSAM
Download sample data¶
The sample data is from OpenAerialMap. Credits to the provider Jason McMinn.
In [ ]:
Copied!
raster_url = "https://huggingface.co/datasets/giswqs/geospatial/resolve/main/aerial.tif"
raster_url = "https://huggingface.co/datasets/giswqs/geospatial/resolve/main/aerial.tif"
In [ ]:
Copied!
raster_path = geoai.download_file(raster_url)
raster_path = geoai.download_file(raster_url)
Visualize sample data¶
In [ ]:
Copied!
geoai.view_raster(raster_url)
geoai.view_raster(raster_url)
Image Segmentation¶
In [ ]:
Copied!
grounded_sam = GroundedSAM(
detector_id="IDEA-Research/grounding-dino-tiny",
segmenter_id="facebook/sam-vit-base",
device=None, # Will auto-detect CUDA if available
tile_size=1024,
overlap=128,
threshold=0.3,
)
grounded_sam = GroundedSAM(
detector_id="IDEA-Research/grounding-dino-tiny",
segmenter_id="facebook/sam-vit-base",
device=None, # Will auto-detect CUDA if available
tile_size=1024,
overlap=128,
threshold=0.3,
)
In [ ]:
Copied!
text_prompts = ["building", "car", "tree"]
output_file = "segmented_objects.tif"
text_prompts = ["building", "car", "tree"]
output_file = "segmented_objects.tif"
In [ ]:
Copied!
result_files = grounded_sam.segment_image(
input_path=raster_path,
output_path=output_file,
text_prompts=text_prompts,
polygon_refinement=True,
export_boxes=True,
export_polygons=True,
smoothing_sigma=1.0,
nms_threshold=0.5,
min_polygon_area=50,
simplify_tolerance=1.0,
)
result_files = grounded_sam.segment_image(
input_path=raster_path,
output_path=output_file,
text_prompts=text_prompts,
polygon_refinement=True,
export_boxes=True,
export_polygons=True,
smoothing_sigma=1.0,
nms_threshold=0.5,
min_polygon_area=50,
simplify_tolerance=1.0,
)
In [ ]:
Copied!
result_files
result_files
Visualize results¶
In [ ]:
Copied!
polygons = result_files["polygons"]
boxes = result_files["boxes"]
polygons = result_files["polygons"]
boxes = result_files["boxes"]
In [ ]:
Copied!
geoai.view_vector_interactive(
polygons, basemap=raster_url, column="label", cmap="tab20"
)
geoai.view_vector_interactive(
polygons, basemap=raster_url, column="label", cmap="tab20"
)
In [ ]:
Copied!
geoai.view_raster(output_file, indexes=[2], basemap=raster_url, layer_name="Building")
geoai.view_raster(output_file, indexes=[2], basemap=raster_url, layer_name="Building")
In [ ]:
Copied!
geoai.view_raster(output_file, indexes=[3], basemap=raster_url, layer_name="Car")
geoai.view_raster(output_file, indexes=[3], basemap=raster_url, layer_name="Car")
In [ ]:
Copied!
geoai.view_raster(output_file, indexes=[4], basemap=raster_url, layer_name="Tree")
geoai.view_raster(output_file, indexes=[4], basemap=raster_url, layer_name="Tree")