Skip to content

Commit d283045

Browse files
authored
Add notebook for using Maxar Open Data (#198)
1 parent 46f87a1 commit d283045

File tree

4 files changed

+317
-0
lines changed

4 files changed

+317
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ apt update; apt install -y libgl1
7878
- [Batch segmentation with text prompts](https://samgeo.gishub.org/examples/text_prompts_batch)
7979
- [Using segment-geospatial with ArcGIS Pro](https://samgeo.gishub.org/examples/arcgis)
8080
- [Segmenting swimming pools with text prompts](https://samgeo.gishub.org/examples/swimming_pools)
81+
- [Segmenting satellite imagery from the Maxar Open Data Program](https://samgeo.gishub.org/examples/max_open_data)
8182

8283
## Demos
8384

docs/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ The **segment-geospatial** package draws its inspiration from [segment-anything-
4444
- [Batch segmentation with text prompts](https://samgeo.gishub.org/examples/text_prompts_batch)
4545
- [Using segment-geospatial with ArcGIS Pro](https://samgeo.gishub.org/examples/arcgis)
4646
- [Segmenting swimming pools with text prompts](https://samgeo.gishub.org/examples/swimming_pools)
47+
- [Segmenting satellite imagery from the Maxar Open Data Program](https://samgeo.gishub.org/examples/max_open_data)
4748

4849
## Demos
4950

docs/maxar_open_data.ipynb

Lines changed: 314 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,314 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Segmenting satellite imagery from the Maxar Open Data Program\n",
8+
"\n",
9+
"[![image](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/opengeos/segment-geospatial/blob/main/docs/examples/maxar_open_data.ipynb)\n",
10+
"[![image](https://img.shields.io/badge/Open-Planetary%20Computer-black?style=flat&logo=microsoft)](https://pccompute.westeurope.cloudapp.azure.com/compute/hub/user-redirect/git-pull?repo=https://github.com/opengeos/segment-geospatial&urlpath=lab/tree/segment-geospatial/docs/examples/maxar_open_data.ipynb&branch=main)\n",
11+
"[![image](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/opengeos/segment-geospatial/blob/main/docs/examples/maxar_open_data.ipynb)\n",
12+
"\n",
13+
"This notebook shows how to segment satellite imagery from the [Maxar Open Data](https://github.com/opengeos/maxar-open-data) program for Libya floods. \n",
14+
"\n",
15+
"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. "
16+
]
17+
},
18+
{
19+
"cell_type": "markdown",
20+
"metadata": {},
21+
"source": [
22+
"## Install dependencies\n",
23+
"\n",
24+
"Uncomment and run the following cell to install the required dependencies."
25+
]
26+
},
27+
{
28+
"cell_type": "code",
29+
"execution_count": null,
30+
"metadata": {},
31+
"outputs": [],
32+
"source": [
33+
"# %pip install segment-geospatial"
34+
]
35+
},
36+
{
37+
"cell_type": "code",
38+
"execution_count": null,
39+
"metadata": {
40+
"tags": []
41+
},
42+
"outputs": [],
43+
"source": [
44+
"import os\n",
45+
"import leafmap\n",
46+
"from samgeo import SamGeo, raster_to_vector, overlay_images"
47+
]
48+
},
49+
{
50+
"cell_type": "markdown",
51+
"metadata": {},
52+
"source": [
53+
"## Download sample data\n",
54+
"\n",
55+
"First, let's download a sample image of Derna, Libya from [here](https://drive.google.com/file/d/1jIIC5hvSPeJEC0fbDhtxVWk2XV9AxsQD/view?usp=sharing)."
56+
]
57+
},
58+
{
59+
"cell_type": "code",
60+
"execution_count": null,
61+
"metadata": {
62+
"tags": []
63+
},
64+
"outputs": [],
65+
"source": [
66+
"url = 'https://drive.google.com/file/d/1jIIC5hvSPeJEC0fbDhtxVWk2XV9AxsQD/view?usp=sharing'"
67+
]
68+
},
69+
{
70+
"cell_type": "code",
71+
"execution_count": null,
72+
"metadata": {
73+
"tags": []
74+
},
75+
"outputs": [],
76+
"source": [
77+
"leafmap.download_file(url, output='image.tif')"
78+
]
79+
},
80+
{
81+
"cell_type": "markdown",
82+
"metadata": {},
83+
"source": [
84+
"## Create an interactive map"
85+
]
86+
},
87+
{
88+
"cell_type": "code",
89+
"execution_count": null,
90+
"metadata": {
91+
"tags": []
92+
},
93+
"outputs": [],
94+
"source": [
95+
"m = leafmap.Map(height=\"600px\")\n",
96+
"m.add_basemap(\"SATELLITE\")\n",
97+
"m.add_raster('image.tif', layer_name=\"Image\")\n",
98+
"m.add_layer_manager()\n",
99+
"m"
100+
]
101+
},
102+
{
103+
"cell_type": "markdown",
104+
"metadata": {},
105+
"source": [
106+
"## Initialize SAM class"
107+
]
108+
},
109+
{
110+
"cell_type": "markdown",
111+
"metadata": {},
112+
"source": [
113+
"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:"
114+
]
115+
},
116+
{
117+
"cell_type": "code",
118+
"execution_count": null,
119+
"metadata": {
120+
"tags": []
121+
},
122+
"outputs": [],
123+
"source": [
124+
"sam_kwargs = {\n",
125+
" \"points_per_side\": 32,\n",
126+
" \"pred_iou_thresh\": 0.86,\n",
127+
" \"stability_score_thresh\": 0.92,\n",
128+
" \"crop_n_layers\": 1,\n",
129+
" \"crop_n_points_downscale_factor\": 2,\n",
130+
" \"min_mask_region_area\": 80,\n",
131+
"}"
132+
]
133+
},
134+
{
135+
"cell_type": "code",
136+
"execution_count": null,
137+
"metadata": {
138+
"tags": []
139+
},
140+
"outputs": [],
141+
"source": [
142+
"sam = SamGeo(\n",
143+
" model_type=\"vit_h\",\n",
144+
" sam_kwargs=sam_kwargs,\n",
145+
")"
146+
]
147+
},
148+
{
149+
"cell_type": "markdown",
150+
"metadata": {},
151+
"source": [
152+
"## Segment the image"
153+
]
154+
},
155+
{
156+
"cell_type": "code",
157+
"execution_count": null,
158+
"metadata": {
159+
"tags": []
160+
},
161+
"outputs": [],
162+
"source": [
163+
"sam.generate('image.tif', output=\"mask.tif\", foreground=True)"
164+
]
165+
},
166+
{
167+
"cell_type": "markdown",
168+
"metadata": {},
169+
"source": [
170+
"## Convert raster to vector"
171+
]
172+
},
173+
{
174+
"cell_type": "code",
175+
"execution_count": null,
176+
"metadata": {
177+
"tags": []
178+
},
179+
"outputs": [],
180+
"source": [
181+
"raster_to_vector('mask.tif', output='mask.shp')"
182+
]
183+
},
184+
{
185+
"cell_type": "markdown",
186+
"metadata": {},
187+
"source": [
188+
"## Display the segmentation result\n",
189+
"\n",
190+
"First, let's show the result as a binary image."
191+
]
192+
},
193+
{
194+
"cell_type": "code",
195+
"execution_count": null,
196+
"metadata": {
197+
"tags": []
198+
},
199+
"outputs": [],
200+
"source": [
201+
"sam.show_masks(cmap=\"binary_r\")"
202+
]
203+
},
204+
{
205+
"cell_type": "markdown",
206+
"metadata": {},
207+
"source": [
208+
"Display the annotations (each mask with a random color)."
209+
]
210+
},
211+
{
212+
"cell_type": "code",
213+
"execution_count": null,
214+
"metadata": {
215+
"tags": []
216+
},
217+
"outputs": [],
218+
"source": [
219+
"sam.show_anns(axis=\"off\", opacity=1, output=\"annotation.tif\")"
220+
]
221+
},
222+
{
223+
"cell_type": "markdown",
224+
"metadata": {},
225+
"source": [
226+
"## Compare images with a slider"
227+
]
228+
},
229+
{
230+
"cell_type": "code",
231+
"execution_count": null,
232+
"metadata": {
233+
"tags": []
234+
},
235+
"outputs": [],
236+
"source": [
237+
"leafmap.image_comparison(\n",
238+
" 'image.tif',\n",
239+
" \"annotation.tif\",\n",
240+
" label1=\"Image\",\n",
241+
" label2=\"Segmentation\",\n",
242+
")"
243+
]
244+
},
245+
{
246+
"cell_type": "markdown",
247+
"metadata": {},
248+
"source": [
249+
"Overlay the annotations on the image and use the slider to change the opacity interactively."
250+
]
251+
},
252+
{
253+
"cell_type": "code",
254+
"execution_count": null,
255+
"metadata": {
256+
"tags": []
257+
},
258+
"outputs": [],
259+
"source": [
260+
"overlay_images('image.tif', \"annotation.tif\", backend=\"TkAgg\")"
261+
]
262+
},
263+
{
264+
"cell_type": "markdown",
265+
"metadata": {},
266+
"source": [
267+
"## Display images on an interactive map."
268+
]
269+
},
270+
{
271+
"cell_type": "code",
272+
"execution_count": null,
273+
"metadata": {},
274+
"outputs": [],
275+
"source": [
276+
"m.add_raster('mask.tif', layer_name='Mask', nodata=0)\n",
277+
"m.add_raster('annotation.tif', layer_name='Annotation')\n",
278+
"m"
279+
]
280+
},
281+
{
282+
"cell_type": "code",
283+
"execution_count": null,
284+
"metadata": {
285+
"tags": []
286+
},
287+
"outputs": [],
288+
"source": [
289+
"m.add_vector('mask.shp', layer_name='Vector', info_mode=None)"
290+
]
291+
}
292+
],
293+
"metadata": {
294+
"kernelspec": {
295+
"display_name": "Python 3 (ipykernel)",
296+
"language": "python",
297+
"name": "python3"
298+
},
299+
"language_info": {
300+
"codemirror_mode": {
301+
"name": "ipython",
302+
"version": 3
303+
},
304+
"file_extension": ".py",
305+
"mimetype": "text/x-python",
306+
"name": "python",
307+
"nbconvert_exporter": "python",
308+
"pygments_lexer": "ipython3",
309+
"version": "3.9.16"
310+
}
311+
},
312+
"nbformat": 4,
313+
"nbformat_minor": 4
314+
}

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ nav:
6060
- examples/fast_sam.ipynb
6161
- examples/swimming_pools.ipynb
6262
- examples/arcgis.ipynb
63+
- examples/maxar_open_data.ipynb
6364
- API Reference:
6465
- common module: common.md
6566
- samgeo module: samgeo.md

0 commit comments

Comments
 (0)