Skip to content

Download notebook

Segmenting remote sensing imagery with FastSAM

image image

FastSAM: https://github.com/CASIA-IVA-Lab/FastSAM

Make sure you use GPU runtime for this notebook. For Google Colab, go to Runtime -> Change runtime type and select GPU as the hardware accelerator.

Install dependencies

Uncomment and run the following cell to install the required dependencies.

1
# %pip install segment-geospatial segment-anything-fast
1
2
import leafmap
from samgeo.common import tms_to_geotiff

Create an interactive map

1
2
3
m = leafmap.Map(center=[-22.17615, -51.253043], zoom=18, height="800px")
m.add_basemap("SATELLITE")
m

Download a sample image

Pan and zoom the map to select the area of interest. Use the draw tools to draw a polygon or rectangle on the map

1
2
3
bbox = m.user_roi_bounds()
if bbox is None:
    bbox = [-51.2565, -22.1777, -51.2512, -22.175]
1
2
image = "Image.tif"
tms_to_geotiff(output=image, bbox=bbox, zoom=19, source="Satellite", overwrite=True)

You can also use your own image. Uncomment and run the following cell to use your own image.

1
# image = '/path/to/your/own/image.tif'

Display the downloaded image on the map.

1
2
3
m.layers[-1].visible = False
m.add_raster(image, layer_name="Image")
m

Initialize SamGeo class

The initialization of the SamGeo class might take a few minutes. The initialization downloads the model weights and sets up the model for inference.

1
2
3
from samgeo.fast_sam import SamGeo

sam = SamGeo(model="FastSAM-x.pt")

Set the image.

1
sam.set_image("Image.tif")

Segment the image with everything_prompt. You can also try point_prompt, box_prompt, or text_prompt.

1
sam.everything_prompt(output="mask.tif")

Show the annotated image.

1
sam.show_anns("mask.png")

Convert the segmentation results from GeoTIFF to vector.

1
sam.raster_to_vector("mask.tif", "mask.geojson")

Show the segmentation results on the map.

1
2
3
m.add_raster("mask.tif", opacity=0.5, layer_name="Mask")
m.add_vector("mask.geojson", layer_name="Mask Vector")
m