Skip to content

Commit 2b87cea

Browse files
committed
Added notebook
1 parent 54d8174 commit 2b87cea

File tree

9 files changed

+309
-308
lines changed

9 files changed

+309
-308
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ __pycache__/
33
private/
44
*.py[cod]
55
*$py.class
6+
**/*.tiff
7+
**/*.tif
68

79
# C extensions
810
*.so

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# segment-geospatial
22

3-
[![image](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/opengeos/segment-geospatial/blob/main/examples/basic_usage.ipynb)
4-
[![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/examples/basic_usage.ipynb&branch=main)
5-
[![image](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/opengeos/segment-geospatial/blob/main/examples/basic_usage.ipynb)
3+
[![image](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/opengeos/segment-geospatial/blob/main/docs/examples/satellite.ipynb)
4+
[![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/satellite.ipynb&branch=main)
5+
[![image](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/opengeos/segment-geospatial/blob/main/docs/examples/satellite.ipynb)
66
[![image](https://img.shields.io/pypi/v/segment-geospatial.svg)](https://pypi.python.org/pypi/segment-geospatial)
77
[![image](https://img.shields.io/conda/vn/conda-forge/segment-geospatial.svg)](https://anaconda.org/conda-forge/segment-geospatial)
88

9-
**Meta AI' Segment Anything Model (SAM) for Geospatial Data**
9+
**A Python package for segmenting geospatial data with the Segment Anything Model (SAM)**
1010

1111
The **segment-geospatial** package draws its inspiration from [segment-anything-eo](https://github.com/aliaksandr960/segment-anything-eo) repository authored by [Aliaksandr Hancharenka](https://github.com/aliaksandr960). To facilitate the use of the Segment Anything Model (SAM) for geospatial data, I have developed the [segment-anything-py](https://github.com/opengeos/segment-anything) and [segment-geospatial](https://github.com/opengeos/segment-geospatial) Python packages, which are now available on PyPI and conda-forge. My primary objective is to simplify the process of leveraging SAM for geospatial data analysis by enabling users to achieve this with minimal coding effort. I have adapted the source code of segment-geospatial from the [segment-anything-eo](https://github.com/aliaksandr960/segment-anything-eo) repository, and credit for its original version goes to Aliaksandr Hancharenka.
1212

@@ -15,7 +15,8 @@ The **segment-geospatial** package draws its inspiration from [segment-anything-
1515

1616
## Features
1717

18-
- TODO
18+
- Download map tiles from Tile Map Service (TMS) servers and create GeoTIFF files
19+
- Segment GeoTIFF files using the Segment Anything Model (SAM)
1920

2021
## Acknowledgements
2122

docs/common.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# common module
2+
3+
::: samgeo.common

docs/examples/satellite.ipynb

Lines changed: 281 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,281 @@
1+
{
2+
"cells": [
3+
{
4+
"attachments": {},
5+
"cell_type": "markdown",
6+
"metadata": {
7+
"id": "W-IgodFVKrQO"
8+
},
9+
"source": [
10+
"# Segment Anything Model for Geospatial Data \n",
11+
"\n",
12+
"[![image](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/opengeos/segment-geospatial/blob/main/docs/examples/satellite.ipynb)\n",
13+
"[![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/satellite.ipynb&branch=main)\n",
14+
"[![image](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/opengeos/segment-geospatial/blob/main/docs/examples/satellite.ipynb)\n",
15+
"\n",
16+
"This notebook shows how to use segment satellite imagery using the Segment Anything Model (SAM) with a few lines of code. "
17+
]
18+
},
19+
{
20+
"attachments": {},
21+
"cell_type": "markdown",
22+
"metadata": {
23+
"id": "KyZq008jL64p"
24+
},
25+
"source": [
26+
"## Install dependencies\n",
27+
"\n",
28+
"Uncomment and run the following cell to install the required dependencies.\n"
29+
]
30+
},
31+
{
32+
"cell_type": "code",
33+
"execution_count": null,
34+
"metadata": {
35+
"colab": {
36+
"base_uri": "https://localhost:8080/"
37+
},
38+
"id": "L42UgbyJ6j8W",
39+
"outputId": "6924c5c8-39a0-4ac6-f114-9f1f8d102e88"
40+
},
41+
"outputs": [],
42+
"source": [
43+
"# %pip install segment-geospatial leafmap localtileserver"
44+
]
45+
},
46+
{
47+
"attachments": {},
48+
"cell_type": "markdown",
49+
"metadata": {},
50+
"source": [
51+
"## Import libraries"
52+
]
53+
},
54+
{
55+
"cell_type": "code",
56+
"execution_count": null,
57+
"metadata": {},
58+
"outputs": [],
59+
"source": [
60+
"import os\n",
61+
"import leafmap\n",
62+
"import torch\n",
63+
"from samgeo import SamGeo, tms_to_geotiff"
64+
]
65+
},
66+
{
67+
"attachments": {},
68+
"cell_type": "markdown",
69+
"metadata": {},
70+
"source": [
71+
"## Create an interactive map"
72+
]
73+
},
74+
{
75+
"cell_type": "code",
76+
"execution_count": null,
77+
"metadata": {},
78+
"outputs": [],
79+
"source": [
80+
"m = leafmap.Map(center=[29.676840, -95.369222], zoom=19)\n",
81+
"m.add_basemap('SATELLITE')\n",
82+
"m"
83+
]
84+
},
85+
{
86+
"attachments": {},
87+
"cell_type": "markdown",
88+
"metadata": {},
89+
"source": [
90+
"Pan and zoom the map to select the area of interest. Use the draw tools to draw a polygon or rectangle on the map"
91+
]
92+
},
93+
{
94+
"cell_type": "code",
95+
"execution_count": null,
96+
"metadata": {},
97+
"outputs": [],
98+
"source": [
99+
"if m.user_roi_bounds() is not None:\n",
100+
" bbox = m.user_roi_bounds()\n",
101+
"else:\n",
102+
" bbox = [-95.3704, 29.6762, -95.368, 29.6775]"
103+
]
104+
},
105+
{
106+
"attachments": {},
107+
"cell_type": "markdown",
108+
"metadata": {},
109+
"source": [
110+
"## Download map tiles\n",
111+
"\n",
112+
"Download maps tiles and mosaic them into a single GeoTIFF file"
113+
]
114+
},
115+
{
116+
"cell_type": "code",
117+
"execution_count": null,
118+
"metadata": {},
119+
"outputs": [],
120+
"source": [
121+
"out_dir = os.path.join(os.path.expanduser('~'), 'Downloads')\n",
122+
"if not os.path.exists(out_dir):\n",
123+
" os.makedirs(out_dir)"
124+
]
125+
},
126+
{
127+
"cell_type": "code",
128+
"execution_count": null,
129+
"metadata": {},
130+
"outputs": [],
131+
"source": [
132+
"image = os.path.join(out_dir, 'satellite.tif')\n",
133+
"# image = '/path/to/your/own/image.tif'"
134+
]
135+
},
136+
{
137+
"cell_type": "code",
138+
"execution_count": null,
139+
"metadata": {},
140+
"outputs": [],
141+
"source": [
142+
"tms_to_geotiff(output=image, bbox=bbox, zoom=20, source='Satellite')"
143+
]
144+
},
145+
{
146+
"cell_type": "code",
147+
"execution_count": null,
148+
"metadata": {},
149+
"outputs": [],
150+
"source": [
151+
"m.add_raster(image, layer_name='Image')\n",
152+
"m"
153+
]
154+
},
155+
{
156+
"attachments": {},
157+
"cell_type": "markdown",
158+
"metadata": {},
159+
"source": [
160+
"![](https://i.imgur.com/KAm84IY.png)"
161+
]
162+
},
163+
{
164+
"attachments": {},
165+
"cell_type": "markdown",
166+
"metadata": {},
167+
"source": [
168+
"## Initialize SAM class"
169+
]
170+
},
171+
{
172+
"cell_type": "code",
173+
"execution_count": null,
174+
"metadata": {},
175+
"outputs": [],
176+
"source": [
177+
"checkpoint = os.path.join(out_dir, 'sam_vit_h_4b8939.pth')\n",
178+
"device = 'cuda' if torch.cuda.is_available() else 'cpu'\n",
179+
"sam = SamGeo(checkpoint=checkpoint,\n",
180+
" model_type='vit_h',\n",
181+
" device=device,\n",
182+
" erosion_kernel=(3, 3),\n",
183+
" mask_multiplier=255,\n",
184+
" sam_kwargs=None)"
185+
]
186+
},
187+
{
188+
"attachments": {},
189+
"cell_type": "markdown",
190+
"metadata": {},
191+
"source": [
192+
"## Segment the image"
193+
]
194+
},
195+
{
196+
"cell_type": "code",
197+
"execution_count": null,
198+
"metadata": {},
199+
"outputs": [],
200+
"source": [
201+
"mask = 'segment.tiff'\n",
202+
"sam.generate(image, mask)"
203+
]
204+
},
205+
{
206+
"attachments": {},
207+
"cell_type": "markdown",
208+
"metadata": {},
209+
"source": [
210+
"## Polygonize the raster data"
211+
]
212+
},
213+
{
214+
"cell_type": "code",
215+
"execution_count": null,
216+
"metadata": {},
217+
"outputs": [],
218+
"source": [
219+
"vector = os.path.join(out_dir, 'segment.gpkg')\n",
220+
"sam.tiff_to_gpkg(mask, vector, simplify_tolerance=None)"
221+
]
222+
},
223+
{
224+
"attachments": {},
225+
"cell_type": "markdown",
226+
"metadata": {},
227+
"source": [
228+
"## Visualize the results"
229+
]
230+
},
231+
{
232+
"cell_type": "code",
233+
"execution_count": null,
234+
"metadata": {},
235+
"outputs": [],
236+
"source": [
237+
"style = {\n",
238+
" 'color': '#3388ff',\n",
239+
" 'weight': 2,\n",
240+
" 'fillColor': '#7c4185',\n",
241+
" 'fillOpacity': 0.5,\n",
242+
"}\n",
243+
"m.add_vector(vector, layer_name='Vector', style=style)\n",
244+
"m"
245+
]
246+
},
247+
{
248+
"attachments": {},
249+
"cell_type": "markdown",
250+
"metadata": {},
251+
"source": [
252+
"![](https://i.imgur.com/Ysq3u7E.png)"
253+
]
254+
}
255+
],
256+
"metadata": {
257+
"accelerator": "GPU",
258+
"colab": {
259+
"provenance": []
260+
},
261+
"gpuClass": "standard",
262+
"kernelspec": {
263+
"display_name": "Python 3",
264+
"name": "python3"
265+
},
266+
"language_info": {
267+
"codemirror_mode": {
268+
"name": "ipython",
269+
"version": 3
270+
},
271+
"file_extension": ".py",
272+
"mimetype": "text/x-python",
273+
"name": "python",
274+
"nbconvert_exporter": "python",
275+
"pygments_lexer": "ipython3",
276+
"version": "3.9.16"
277+
}
278+
},
279+
"nbformat": 4,
280+
"nbformat_minor": 0
281+
}

docs/index.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# Welcome to samgeo
22

3-
[![image](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/opengeos/segment-geospatial/blob/main/examples/basic_usage.ipynb)
4-
[![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/examples/basic_usage.ipynb&branch=main)
5-
[![image](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/opengeos/segment-geospatial/blob/main/examples/basic_usage.ipynb)
3+
[![image](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/opengeos/segment-geospatial/blob/main/docs/examples/satellite.ipynb)
4+
[![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/satellite.ipynb&branch=main)
5+
[![image](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/opengeos/segment-geospatial/blob/main/docs/examples/satellite.ipynb)
66
[![image](https://img.shields.io/pypi/v/segment-geospatial.svg)](https://pypi.python.org/pypi/segment-geospatial)
77
[![image](https://img.shields.io/conda/vn/conda-forge/segment-geospatial.svg)](https://anaconda.org/conda-forge/segment-geospatial)
88

9-
**Meta AI' Segment Anything Model (SAM) for Geospatial Data** 🗺️
9+
**A Python package for segmenting geospatial data with the Segment Anything Model (SAM)** 🗺️
1010

1111
The **segment-geospatial** package draws its inspiration from [segment-anything-eo](https://github.com/aliaksandr960/segment-anything-eo) repository authored by [Aliaksandr Hancharenka](https://github.com/aliaksandr960). To facilitate the use of the Segment Anything Model (SAM) for geospatial data, I have developed the [segment-anything-py](https://github.com/opengeos/segment-anything) and [segment-geospatial](https://github.com/opengeos/segment-geospatial) Python packages, which are now available on PyPI and conda-forge. My primary objective is to simplify the process of leveraging SAM for geospatial data analysis by enabling users to achieve this with minimal coding effort. I have adapted the source code of segment-geospatial from the [segment-anything-eo](https://github.com/aliaksandr960/segment-anything-eo) repository, and credit for its original version goes to Aliaksandr Hancharenka.
1212

@@ -15,7 +15,8 @@ The **segment-geospatial** package draws its inspiration from [segment-anything-
1515

1616
## Features
1717

18-
- 📝 TODO
18+
- Download map tiles from Tile Map Service (TMS) servers and create GeoTIFF files
19+
- Segment GeoTIFF files using the Segment Anything Model (SAM)
1920

2021
## Acknowledgements
2122

0 commit comments

Comments
 (0)