Skip to content

Commit 741ecfe

Browse files
committed
Added geo functions
1 parent 74a2d99 commit 741ecfe

File tree

8 files changed

+1015
-23
lines changed

8 files changed

+1015
-23
lines changed

README.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
1-
# segment-geospatial
2-
1+
# segment-geospatial 🗺️
32

43
[![image](https://img.shields.io/pypi/v/segment-geospatial.svg)](https://pypi.python.org/pypi/segment-geospatial)
54
[![image](https://img.shields.io/conda/vn/conda-forge/segment-geospatial.svg)](https://anaconda.org/conda-forge/segment-geospatial)
65

7-
86
**Meta AI' Segment Anything Model (SAM) for Geospatial Data**
97

8+
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.
109

11-
- Free software: MIT license
12-
- Documentation: https://samgeo.gishub.org
13-
10+
- 🆓 Free software: MIT license
11+
- 📖 Documentation: https://samgeo.gishub.org
1412

1513
## Features
1614

17-
- TODO
15+
- 📝 TODO
16+
17+
## Acknowledgements
1818

19-
## Credits
19+
This package was made possible by the following open source projects. Credit goes to the developers of these projects.
2020

21-
This package was created with [Cookiecutter](https://github.com/cookiecutter/cookiecutter) and the [giswqs/pypackage](https://github.com/giswqs/pypackage) project template.
21+
- [segment-anything](https://github.com/facebookresearch/segment-anything) 💻
22+
- [segment-anything-eo](https://github.com/aliaksandr960/segment-anything-eo) 🛰️
23+
- [tms2geotiff](https://github.com/gumblex/tms2geotiff) 📷

docs/index.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
# Welcome to samgeo
22

3-
43
[![image](https://img.shields.io/pypi/v/segment-geospatial.svg)](https://pypi.python.org/pypi/segment-geospatial)
54
[![image](https://img.shields.io/conda/vn/conda-forge/segment-geospatial.svg)](https://anaconda.org/conda-forge/segment-geospatial)
65

6+
**Meta AI' Segment Anything Model (SAM) for Geospatial Data** 🗺️
77

8-
**Meta AI' Segment Anything Model (SAM) for Geospatial Data**
9-
8+
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.
109

11-
- Free software: MIT license
12-
- Documentation: <https://samgeo.gishub.org>
13-
10+
- 🆓 Free software: MIT license
11+
- 📖 Documentation: https://samgeo.gishub.org
1412

1513
## Features
1614

17-
- TODO
15+
- 📝 TODO
16+
17+
## Acknowledgements
1818

19-
## Credits
19+
This package was made possible by the following open source projects. Credit goes to the developers of these projects.
2020

21-
This package was created with [Cookiecutter](https://github.com/cookiecutter/cookiecutter) and the [giswqs/pypackage](https://github.com/giswqs/pypackage) project template.
21+
- [segment-anything](https://github.com/facebookresearch/segment-anything) 💻
22+
- [segment-anything-eo](https://github.com/aliaksandr960/segment-anything-eo) 🛰️
23+
- [tms2geotiff](https://github.com/gumblex/tms2geotiff) 📷

examples/basic_usage.ipynb

Lines changed: 294 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,294 @@
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+
"\n",
13+
"The source code is adapted from [segment-anything-eo](https://github.com/aliaksandr960/segment-anything-eo) repository. Credit to the author [Aliaksandr Hancharenka](https://github.com/aliaksandr960)."
14+
]
15+
},
16+
{
17+
"cell_type": "markdown",
18+
"metadata": {
19+
"id": "OeFjtBlhKxh4"
20+
},
21+
"source": [
22+
"This tool is developed to easy process spatial data (GeoTIFF and TMS) with Meta AI Segment Anything models\n",
23+
"\n",
24+
"You can:\n",
25+
"- download TMS data as GeoTIFF files\n",
26+
"- process GeoTIFF files with Meta AI Segment Anything models\n",
27+
"- save inference data as GeoTIFF raster data or GPKG vector data"
28+
]
29+
},
30+
{
31+
"cell_type": "markdown",
32+
"metadata": {
33+
"id": "KyZq008jL64p"
34+
},
35+
"source": [
36+
"## Clone repo and install dependancies"
37+
]
38+
},
39+
{
40+
"cell_type": "code",
41+
"execution_count": null,
42+
"metadata": {
43+
"colab": {
44+
"base_uri": "https://localhost:8080/"
45+
},
46+
"id": "L42UgbyJ6j8W",
47+
"outputId": "6924c5c8-39a0-4ac6-f114-9f1f8d102e88"
48+
},
49+
"outputs": [],
50+
"source": [
51+
"# !git clone https://github.com/aliaksandr960/segment-anything-eo.git"
52+
]
53+
},
54+
{
55+
"cell_type": "code",
56+
"execution_count": null,
57+
"metadata": {
58+
"id": "J-Yk9v21G9O2"
59+
},
60+
"outputs": [],
61+
"source": [
62+
"import os\n",
63+
"os.chdir('/home/qiusheng/Documents/Data/')"
64+
]
65+
},
66+
{
67+
"cell_type": "code",
68+
"execution_count": null,
69+
"metadata": {
70+
"colab": {
71+
"base_uri": "https://localhost:8080/"
72+
},
73+
"id": "pj5yjhMA6nBV",
74+
"outputId": "f23e269f-43b6-4fad-8ace-9184077b6916"
75+
},
76+
"outputs": [],
77+
"source": [
78+
"# !pip install rasterio\n",
79+
"# !pip install geopandas\n",
80+
"\n",
81+
"!wget https://dl.fbaipublicfiles.com/segment_anything/sam_vit_h_4b8939.pth\n",
82+
"!wget https://dl.fbaipublicfiles.com/segment_anything/sam_vit_l_0b3195.pth\n",
83+
"!wget https://dl.fbaipublicfiles.com/segment_anything/sam_vit_b_01ec64.pth"
84+
]
85+
},
86+
{
87+
"cell_type": "code",
88+
"execution_count": null,
89+
"metadata": {
90+
"id": "w5bDCQOV7gvZ"
91+
},
92+
"outputs": [],
93+
"source": [
94+
"import cv2\n",
95+
"import matplotlib.pyplot as plt\n",
96+
"from samgeo import SamGeo"
97+
]
98+
},
99+
{
100+
"cell_type": "markdown",
101+
"metadata": {
102+
"id": "F1YUWHeWMDet"
103+
},
104+
"source": [
105+
"## Initialize SemEO class"
106+
]
107+
},
108+
{
109+
"cell_type": "code",
110+
"execution_count": null,
111+
"metadata": {
112+
"id": "0XzAeiGW8rNk"
113+
},
114+
"outputs": [],
115+
"source": [
116+
"# Availble SamGeo arguments:\n",
117+
"# checkpoint=\"sam_vit_h_4b8939.pth\",\n",
118+
"# model_type='vit_h',\n",
119+
"# device='cpu',\n",
120+
"# erosion_kernel=(3, 3),\n",
121+
"# mask_multiplier=255,\n",
122+
"# sam_kwargs=None\n",
123+
"\n",
124+
"# Availble sam_kwargs:\n",
125+
"# points_per_side: Optional[int] = 32,\n",
126+
"# points_per_batch: int = 64,\n",
127+
"# pred_iou_thresh: float = 0.88,\n",
128+
"# stability_score_thresh: float = 0.95,\n",
129+
"# stability_score_offset: float = 1.0,\n",
130+
"# box_nms_thresh: float = 0.7,\n",
131+
"# crop_n_layers: int = 0,\n",
132+
"# crop_nms_thresh: float = 0.7,\n",
133+
"# crop_overlap_ratio: float = 512 / 1500,\n",
134+
"# crop_n_points_downscale_factor: int = 1,\n",
135+
"# point_grids: Optional[List[np.ndarray]] = None,\n",
136+
"# min_mask_region_area: int = 0,\n",
137+
"# output_mode: str = \"binary_mask\",\n",
138+
"\n",
139+
"device = 'cuda:0'\n",
140+
"\n",
141+
"sam_eo = SamGeo(checkpoint=\"sam_vit_h_4b8939.pth\",\n",
142+
" model_type='vit_h',\n",
143+
" # device=device,\n",
144+
" erosion_kernel=(3, 3),\n",
145+
" mask_multiplier=255,\n",
146+
" sam_kwargs=None)"
147+
]
148+
},
149+
{
150+
"cell_type": "markdown",
151+
"metadata": {
152+
"id": "rBYMot8KMLMk"
153+
},
154+
"source": [
155+
"## Download file from Openaerialmap and save it"
156+
]
157+
},
158+
{
159+
"cell_type": "code",
160+
"execution_count": null,
161+
"metadata": {
162+
"colab": {
163+
"base_uri": "https://localhost:8080/"
164+
},
165+
"id": "ZAggTXOR7yIk",
166+
"outputId": "78ff041f-e6bd-4389-ec68-c94b1989c9f9"
167+
},
168+
"outputs": [],
169+
"source": [
170+
"tms_source = 'https://tiles.openaerialmap.org/642385491a8878000512126c/0/642385491a8878000512126d/{z}/{x}/{y}'\n",
171+
"pt1 = (29.676840, -95.369222)\n",
172+
"pt2 = (29.678559, -95.367314)\n",
173+
"zoom = 20\n",
174+
"tms_tiff_path = 'test_tms_image.tif'\n",
175+
"\n",
176+
"image = sam_eo.download_tms_as_tiff(tms_source, pt1, pt2, zoom, tms_tiff_path)\n",
177+
"tiff_image = cv2.cvtColor(cv2.imread(tms_tiff_path), cv2.COLOR_BGR2RGB)"
178+
]
179+
},
180+
{
181+
"cell_type": "code",
182+
"execution_count": null,
183+
"metadata": {
184+
"colab": {
185+
"base_uri": "https://localhost:8080/",
186+
"height": 316
187+
},
188+
"id": "cTff_Z1I9cxi",
189+
"outputId": "d353ecec-e390-4bab-b6ed-ee48ed0adf86"
190+
},
191+
"outputs": [],
192+
"source": [
193+
"plt.figure()\n",
194+
"f, axarr = plt.subplots(1,2)\n",
195+
"axarr[0].imshow(image)\n",
196+
"axarr[1].imshow(tiff_image)\n",
197+
"plt.show()"
198+
]
199+
},
200+
{
201+
"cell_type": "markdown",
202+
"metadata": {
203+
"id": "x7UvOgzoMSGg"
204+
},
205+
"source": [
206+
"## Run inference and save result as tiff file"
207+
]
208+
},
209+
{
210+
"cell_type": "code",
211+
"execution_count": null,
212+
"metadata": {
213+
"colab": {
214+
"base_uri": "https://localhost:8080/"
215+
},
216+
"id": "mG6OdR6T-tKI",
217+
"outputId": "7335590d-d3ea-45f6-ba14-9ef75b9af0b6"
218+
},
219+
"outputs": [],
220+
"source": [
221+
"pred_tiff_path = 'pred.tiff'\n",
222+
"sam_eo.tiff_to_tiff(tms_tiff_path, pred_tiff_path)"
223+
]
224+
},
225+
{
226+
"cell_type": "code",
227+
"execution_count": null,
228+
"metadata": {
229+
"colab": {
230+
"base_uri": "https://localhost:8080/",
231+
"height": 316
232+
},
233+
"id": "h1julewLBcxw",
234+
"outputId": "f9e0a34d-86fe-4829-8667-20c2d28ea0ab"
235+
},
236+
"outputs": [],
237+
"source": [
238+
"pred_image = cv2.cvtColor(cv2.imread(pred_tiff_path), cv2.COLOR_BGR2RGB)\n",
239+
"\n",
240+
"plt.figure()\n",
241+
"f, axarr = plt.subplots(1,2)\n",
242+
"axarr[0].imshow(image)\n",
243+
"axarr[1].imshow(pred_image)\n",
244+
"plt.show()"
245+
]
246+
},
247+
{
248+
"cell_type": "markdown",
249+
"metadata": {
250+
"id": "k6caNqkQMVTz"
251+
},
252+
"source": [
253+
"## Polygonize result and save as GPKG file"
254+
]
255+
},
256+
{
257+
"cell_type": "code",
258+
"execution_count": null,
259+
"metadata": {
260+
"id": "UYnth2kzD-o8"
261+
},
262+
"outputs": [],
263+
"source": [
264+
"# Adjust simplification tolerance to reduce size (it could be bigger, than raster)\n",
265+
"sam_eo.tiff_to_gpkg(pred_tiff_path, 'pred.gpkg', simplify_tolerance=None)"
266+
]
267+
}
268+
],
269+
"metadata": {
270+
"accelerator": "GPU",
271+
"colab": {
272+
"provenance": []
273+
},
274+
"gpuClass": "standard",
275+
"kernelspec": {
276+
"display_name": "Python 3",
277+
"name": "python3"
278+
},
279+
"language_info": {
280+
"codemirror_mode": {
281+
"name": "ipython",
282+
"version": 3
283+
},
284+
"file_extension": ".py",
285+
"mimetype": "text/x-python",
286+
"name": "python",
287+
"nbconvert_exporter": "python",
288+
"pygments_lexer": "ipython3",
289+
"version": "3.9.16"
290+
}
291+
},
292+
"nbformat": 4,
293+
"nbformat_minor": 0
294+
}

requirements.txt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,10 @@
1-
segment-anything-py
1+
segment-anything-py
2+
opencv-python
3+
pycocotools
4+
matplotlib
5+
onnxruntime
6+
onnx
7+
geopandas
8+
rasterio
9+
tqdm
10+
gdal

samgeo/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@
33
__author__ = """Qiusheng Wu"""
44
__email__ = '[email protected]'
55
__version__ = '0.0.3'
6+
7+
8+
from .samgeo import *

0 commit comments

Comments
 (0)