Segmenting remote sensing imagery with FastSAM¶
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.
# %pip install segment-geospatial segment-anything-fast
import leafmap
from samgeo import tms_to_geotiff
from samgeo.fast_sam import SamGeo
Create an interactive map¶
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
bbox = m.user_roi_bounds()
if bbox is None:
bbox = [-51.2565, -22.1777, -51.2512, -22.175]
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.
# image = '/path/to/your/own/image.tif'
Display the downloaded image on the map.
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.
from samgeo.fast_sam import SamGeo
sam = SamGeo(model="FastSAM-x.pt")
Set the image.
sam.set_image("Image.tif")
Segment the image with everything_prompt
. You can also try point_prompt
, box_prompt
, or text_prompt
.
sam.everything_prompt(output="mask.tif")
Show the annotated image.
sam.show_anns("mask.png")
Convert the segmentation results from GeoTIFF to vector.
sam.raster_to_vector("mask.tif", "mask.geojson")
Show the segmentation results on the map.
m.add_raster("mask.tif", opacity=0.5, layer_name="Mask")
m.add_vector("mask.geojson", layer_name="Mask Vector")
m