In [1]:
Copied!
# %pip install geoai-py
# %pip install geoai-py
Import libraries¶
In [2]:
Copied!
import geoai
import geoai
Download sample data¶
In [3]:
Copied!
raster_url = (
"https://huggingface.co/datasets/giswqs/geospatial/resolve/main/trees_brazil.tif"
)
raster_path = geoai.download_file(raster_url)
raster_url = (
"https://huggingface.co/datasets/giswqs/geospatial/resolve/main/trees_brazil.tif"
)
raster_path = geoai.download_file(raster_url)
trees_brazil.tif: 0%| | 0.00/3.93M [00:00<?, ?B/s]
trees_brazil.tif: 100%|██████████| 3.93M/3.93M [00:00<00:00, 97.8MB/s]
Visualize data¶
In [4]:
Copied!
geoai.view_raster(raster_url)
geoai.view_raster(raster_url)
Out[4]:
Initialize model¶
In [5]:
Copied!
segmenter = geoai.CLIPSegmentation(tile_size=512, overlap=32)
segmenter = geoai.CLIPSegmentation(tile_size=512, overlap=32)
Using a slow image processor as `use_fast` is unset and a slow processor was saved with this model. `use_fast=True` will be the default behavior in v4.52, even if the model was saved with a slow processor. This will result in minor differences in outputs. You'll still be able to use a slow processor with `use_fast=False`.
WARNING:transformers.processing_utils:Using a slow image processor as `use_fast` is unset and a slow processor was saved with this model. `use_fast=True` will be the default behavior in v4.52, even if the model was saved with a slow processor. This will result in minor differences in outputs. You'll still be able to use a slow processor with `use_fast=False`.
Model loaded on cpu
Run inference¶
In [6]:
Copied!
output_path = "tree_masks.tif"
text_prompt = "trees"
output_path = "tree_masks.tif"
text_prompt = "trees"
In [7]:
Copied!
segmenter.segment_image(
raster_path,
output_path=output_path,
text_prompt=text_prompt,
threshold=0.5,
smoothing_sigma=1.0,
)
segmenter.segment_image(
raster_path,
output_path=output_path,
text_prompt=text_prompt,
threshold=0.5,
smoothing_sigma=1.0,
)
Processing tiles: 0%| | 0/15 [00:00<?, ?it/s]
Processing tiles: 7%|▋ | 1/15 [00:00<00:10, 1.37it/s]
Processing tiles: 13%|█▎ | 2/15 [00:01<00:09, 1.41it/s]
Processing tiles: 20%|██ | 3/15 [00:02<00:08, 1.46it/s]
Processing tiles: 27%|██▋ | 4/15 [00:02<00:07, 1.46it/s]
Processing tiles: 33%|███▎ | 5/15 [00:03<00:06, 1.46it/s]
Processing tiles: 40%|████ | 6/15 [00:04<00:06, 1.45it/s]
Processing tiles: 47%|████▋ | 7/15 [00:04<00:05, 1.48it/s]
Processing tiles: 53%|█████▎ | 8/15 [00:05<00:04, 1.47it/s]
Processing tiles: 60%|██████ | 9/15 [00:06<00:04, 1.48it/s]
Processing tiles: 67%|██████▋ | 10/15 [00:06<00:03, 1.51it/s]
Processing tiles: 73%|███████▎ | 11/15 [00:07<00:02, 1.52it/s]
Processing tiles: 80%|████████ | 12/15 [00:08<00:01, 1.53it/s]
Processing tiles: 87%|████████▋ | 13/15 [00:08<00:01, 1.53it/s]
Processing tiles: 93%|█████████▎| 14/15 [00:09<00:00, 1.51it/s]
Processing tiles: 100%|██████████| 15/15 [00:10<00:00, 1.52it/s]
Processing tiles: 100%|██████████| 15/15 [00:10<00:00, 1.49it/s]
Segmentation saved to tree_masks.tif
Out[7]:
'tree_masks.tif'
Visualize results¶
In [8]:
Copied!
geoai.view_raster(
output_path,
nodata=0,
opacity=0.8,
colormap="greens",
layer_name="Trees",
basemap=raster_url,
)
geoai.view_raster(
output_path,
nodata=0,
opacity=0.8,
colormap="greens",
layer_name="Trees",
basemap=raster_url,
)
Out[8]:
In [9]:
Copied!
geoai.create_split_map(
left_layer=output_path,
right_layer=raster_url,
left_label="Trees",
right_label="Satellite Image",
left_args={"nodata": 0, "opacity": 0.8, "colormap": "greens"},
basemap=raster_url,
)
geoai.create_split_map(
left_layer=output_path,
right_layer=raster_url,
left_label="Trees",
right_label="Satellite Image",
left_args={"nodata": 0, "opacity": 0.8, "colormap": "greens"},
basemap=raster_url,
)
Out[9]: