Create object masks from input prompts with SAM


In this notebook, you will learn how to create object masks from input prompts using the Segment Anything Model (SAM).

Previous article: Use Segment Anything Model (SAM) for Geospatial Data

Input prompts refer to the information or instructions provided to a model to guide its generation or decision-making process. In the context of generating object masks with the Segment Anything Model (SAM), an input prompt could be a single point or multiple points that specifies the object or region for which you want to generate a mask.

This notebook has been derived from Generating object masks from input prompts with SAM, with significant simplifications to facilitate saving the segmentation results and visualising them.

Install and Import libraries

pip install segment-geospatial leafmap localtileserver
import os
import leafmap
from samgeo import SamGeo, tms_to_geotiff

Create an interactive map

m = leafmap.Map(center=[10.029939778759502, 105.76756428014754], zoom=17, height="600px", width="800px")
CTU-Vietnam. Source: Author

Select and download a sample image

To select the area of interest on the map, you can pan and zoom to the desired location. Then, utilize the draw tools to create a polygon or rectangle shape on the map.

if m.user_roi is not None:
bbox = m.user_roi_bounds()
bbox = [105.764, 10.0274, 105.7715, 10.0329]
image = "ctu.tif"
tms_to_geotiff(output=image, bbox=bbox, zoom=16, source="Satellite", overwrite=True)

To use your own image, you have the option to uncomment and run the following cell. This will allow you to incorporate your custom image into the process.

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

To showcase the downloaded image on the map, you can display it directly within the map interface. This enables you to visualize the downloaded image in conjunction with other map elements or layers.



GeoSense ✅
𝐀𝐈 𝐦𝐨𝐧𝐤𝐬.𝐢𝐨

🌏 Remote sensing | 🛰️ Geographic Information Systems (GIS) | ℹ️

Recommended from Medium


See more recommendations