Cloud Computing with Google Earth Engine and GeoAI
This notebook is designed for the workshop presented at the Symposium on Spatiotemporal Data Science.
The packages used in this notebook include:
Prerequisites
To use geemap and the Earth Engine Python API, you must register for an Earth Engine account and follow the instructions here to create a Cloud Project. Earth Engine is free for noncommercial and research use. To test whether you can use authenticate the Earth Engine Python API, please run this notebook on Google Colab.
Use Colab GPU runtime
Runtime -> Change runtime type -> Hardware accelerator -> T4GPU

Install dependencies
Uncomment and run the following cell to install the required dependencies.
Import libraries
Authenticate and initialize Earth Engine
Visualize Earth Engine data
Download Earth Engine data
Automatic segmentation
Visualize segmentation results
Download sample data
Create an interactive map
Pan and zoom the map to select the area of interest. Use the draw tools to draw a polygon or rectangle on the map
Download map tiles
Download maps tiles and mosaic them into a single GeoTIFF file
Specify the basemap as the source.
You can also use your own image. Uncomment and run the following cell to use your own image.
Display the downloaded image on the map.

Automatic mask generation
Initialize SAM class
Automatic mask generation
Segment the image and save the results to a GeoTIFF file. Set unique=True to assign a unique ID to each object.

Show the object annotations (objects with random color) on the map.

Compare images with a slider.

Add image to the map.

Convert the object annotations to vector format, such as GeoPackage, Shapefile, or GeoJSON.

Automatic mask generation options
There are several tunable parameters in automatic mask generation that control how densely points are sampled and what the thresholds are for removing low quality or duplicate masks. Additionally, generation can be automatically run on crops of the image to get improved performance on smaller objects, and post-processing can remove stray pixels and holes. Here is an example configuration that samples more masks:


Compare images with a slider.
Use points as input prompts
Initialize SAM class
Set automatic=False to disable the SamAutomaticMaskGenerator and enable the SamPredictor.
Specify the image to segment.
Image segmentation with input points
A single point can be used to segment an object. The point can be specified as a tuple of (x, y), such as (col, row) or (lon, lat). The points can also be specified as a file path to a vector dataset. For non (col, row) input points, specify the point_crs parameter, which will automatically transform the points to the image column and row coordinates.
Try a single point input:

Try multiple points input:

Interactive segmentation
Display the interactive map and use the marker tool to draw points on the map. Then click on the Segment button to segment the objects. The results will be added to the map automatically. Click on the Reset button to clear the points and the results.

Bounding box input prompts
Create an interactive map
Specify the image to segment.
Create bounding boxes
If no rectangles are drawn, the default bounding boxes will be used as follows:
Segment the image
Use the predict() method to segment the image with specified bounding boxes. The boxes parameter accepts a list of bounding box coordinates in the format of [[left, bottom, right, top], [left, bottom, right, top], ...], a GeoJSON dictionary, or a file path to a GeoJSON file.
Display the result
Add the segmented image to the map.

Text promots
Initialize LangSAM class
The initialization of the LangSAM class might take a few minutes. The initialization downloads the model weights and sets up the model for inference.
Specify text prompts
Segment the image
Part of the model prediction includes setting appropriate thresholds for object detection and text association with the detected objects. These threshold values range from 0 to 1 and are set while calling the predict method of the LangSAM class.
box_threshold: This value is used for object detection in the image. A higher value makes the model more selective, identifying only the most confident object instances, leading to fewer overall detections. A lower value, conversely, makes the model more tolerant, leading to increased detections, including potentially less confident ones.
text_threshold: This value is used to associate the detected objects with the provided text prompt. A higher value requires a stronger association between the object and the text prompt, leading to more precise but potentially fewer associations. A lower value allows for looser associations, which could increase the number of associations but also introduce less precise matches.
Remember to test different threshold values on your specific data. The optimal threshold can vary depending on the quality and nature of your images, as well as the specificity of your text prompts. Make sure to choose a balance that suits your requirements, whether that's precision or recall.
Visualize the results
Show the result with bounding boxes on the map.

Show the result without bounding boxes on the map.

Show the result as a grayscale image.
Convert the result to a vector format.
Show the results on the interactive map.

Interactive segmentation
