Skip to content

Commit c0956ad

Browse files
authored
Added fastsam module (#167)
* Add fastsam module * Add fastsam functions and notebook * Add show_anns function * Update dep
1 parent d77bc91 commit c0956ad

File tree

7 files changed

+590
-3
lines changed

7 files changed

+590
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ private/
1313
**/*.jpg
1414
**/*.png
1515
**/*.csv
16+
**/*.pt
1617
docs/examples/*.geojson
1718

1819
# C extensions

docs/examples/fast_sam.ipynb

Lines changed: 267 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,267 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Segmenting remote sensing imagery with FastSAM\n",
8+
"\n",
9+
"[![image](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/opengeos/segment-geospatial/blob/main/docs/examples/fast_sam.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/fast_sam.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/fast_sam.ipynb)\n",
12+
"\n",
13+
"FastSAM: https://github.com/CASIA-IVA-Lab/FastSAM\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 segment-anything-fast"
34+
]
35+
},
36+
{
37+
"cell_type": "code",
38+
"execution_count": null,
39+
"metadata": {},
40+
"outputs": [],
41+
"source": [
42+
"import leafmap\n",
43+
"from samgeo import tms_to_geotiff\n",
44+
"from samgeo.fast_sam import SamGeo"
45+
]
46+
},
47+
{
48+
"cell_type": "markdown",
49+
"metadata": {},
50+
"source": [
51+
"## Create an interactive map"
52+
]
53+
},
54+
{
55+
"cell_type": "code",
56+
"execution_count": null,
57+
"metadata": {},
58+
"outputs": [],
59+
"source": [
60+
"m = leafmap.Map(center=[-22.17615, -51.253043], zoom=18, height=\"800px\")\n",
61+
"m.add_basemap(\"SATELLITE\")\n",
62+
"m"
63+
]
64+
},
65+
{
66+
"cell_type": "markdown",
67+
"metadata": {},
68+
"source": [
69+
"## Download a sample image\n",
70+
"\n",
71+
"Pan and zoom the map to select the area of interest. Use the draw tools to draw a polygon or rectangle on the map"
72+
]
73+
},
74+
{
75+
"cell_type": "code",
76+
"execution_count": null,
77+
"metadata": {},
78+
"outputs": [],
79+
"source": [
80+
"bbox = m.user_roi_bounds()\n",
81+
"if bbox is None:\n",
82+
" bbox = [-51.2565, -22.1777, -51.2512, -22.175]"
83+
]
84+
},
85+
{
86+
"cell_type": "code",
87+
"execution_count": null,
88+
"metadata": {},
89+
"outputs": [],
90+
"source": [
91+
"image = \"Image.tif\"\n",
92+
"tms_to_geotiff(output=image, bbox=bbox, zoom=19, source=\"Satellite\", overwrite=True)"
93+
]
94+
},
95+
{
96+
"cell_type": "markdown",
97+
"metadata": {},
98+
"source": [
99+
"You can also use your own image. Uncomment and run the following cell to use your own image."
100+
]
101+
},
102+
{
103+
"cell_type": "code",
104+
"execution_count": null,
105+
"metadata": {},
106+
"outputs": [],
107+
"source": [
108+
"# image = '/path/to/your/own/image.tif'"
109+
]
110+
},
111+
{
112+
"cell_type": "markdown",
113+
"metadata": {},
114+
"source": [
115+
"Display the downloaded image on the map."
116+
]
117+
},
118+
{
119+
"cell_type": "code",
120+
"execution_count": null,
121+
"metadata": {},
122+
"outputs": [],
123+
"source": [
124+
"m.layers[-1].visible = False\n",
125+
"m.add_raster(image, layer_name=\"Image\")\n",
126+
"m"
127+
]
128+
},
129+
{
130+
"cell_type": "markdown",
131+
"metadata": {},
132+
"source": [
133+
"## Initialize SamGeo class\n",
134+
"\n",
135+
"The initialization of the SamGeo class might take a few minutes. The initialization downloads the model weights and sets up the model for inference."
136+
]
137+
},
138+
{
139+
"cell_type": "code",
140+
"execution_count": null,
141+
"metadata": {},
142+
"outputs": [],
143+
"source": [
144+
"from samgeo.fast_sam import SamGeo\n",
145+
"sam = SamGeo(model=\"FastSAM-x.pt\")"
146+
]
147+
},
148+
{
149+
"cell_type": "markdown",
150+
"metadata": {},
151+
"source": [
152+
"Set the image."
153+
]
154+
},
155+
{
156+
"cell_type": "code",
157+
"execution_count": null,
158+
"metadata": {},
159+
"outputs": [],
160+
"source": [
161+
"sam.set_image(\"Image.tif\")"
162+
]
163+
},
164+
{
165+
"cell_type": "markdown",
166+
"metadata": {},
167+
"source": [
168+
"Segment the image with `everything_prompt`. You can also try `point_prompt`, `box_prompt`, or `text_prompt`."
169+
]
170+
},
171+
{
172+
"cell_type": "code",
173+
"execution_count": null,
174+
"metadata": {},
175+
"outputs": [],
176+
"source": [
177+
"sam.everything_prompt(output=\"mask.tif\")"
178+
]
179+
},
180+
{
181+
"cell_type": "markdown",
182+
"metadata": {},
183+
"source": [
184+
"Show the annotated image."
185+
]
186+
},
187+
{
188+
"cell_type": "code",
189+
"execution_count": null,
190+
"metadata": {},
191+
"outputs": [],
192+
"source": [
193+
"sam.show_anns(\"mask.png\")"
194+
]
195+
},
196+
{
197+
"cell_type": "markdown",
198+
"metadata": {},
199+
"source": [
200+
"![](https://i.imgur.com/af4bj7O.png)"
201+
]
202+
},
203+
{
204+
"cell_type": "markdown",
205+
"metadata": {},
206+
"source": [
207+
"Convert the segmentation results from GeoTIFF to vector."
208+
]
209+
},
210+
{
211+
"cell_type": "code",
212+
"execution_count": null,
213+
"metadata": {},
214+
"outputs": [],
215+
"source": [
216+
"sam.raster_to_vector(\"mask.tif\", \"mask.geojson\")"
217+
]
218+
},
219+
{
220+
"cell_type": "markdown",
221+
"metadata": {},
222+
"source": [
223+
"Show the segmentation results on the map."
224+
]
225+
},
226+
{
227+
"cell_type": "code",
228+
"execution_count": null,
229+
"metadata": {},
230+
"outputs": [],
231+
"source": [
232+
"m.add_raster(\"mask.tif\", opacity=0.5, layer_name=\"Mask\")\n",
233+
"m.add_vector(\"mask.geojson\", layer_name=\"Mask Vector\")\n",
234+
"m"
235+
]
236+
},
237+
{
238+
"cell_type": "markdown",
239+
"metadata": {},
240+
"source": [
241+
"![](https://i.imgur.com/LvEAMSl.png)"
242+
]
243+
}
244+
],
245+
"metadata": {
246+
"kernelspec": {
247+
"display_name": "sam",
248+
"language": "python",
249+
"name": "python3"
250+
},
251+
"language_info": {
252+
"codemirror_mode": {
253+
"name": "ipython",
254+
"version": 3
255+
},
256+
"file_extension": ".py",
257+
"mimetype": "text/x-python",
258+
"name": "python",
259+
"nbconvert_exporter": "python",
260+
"pygments_lexer": "ipython3",
261+
"version": "3.9.16"
262+
},
263+
"orig_nbformat": 4
264+
},
265+
"nbformat": 4,
266+
"nbformat_minor": 2
267+
}

docs/installation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ mamba install -c conda-forge segment-geospatial
2323
Samgeo-geospatial has some optional dependencies that are not included in the default conda environment. To install these dependencies, run the following command:
2424

2525
```bash
26-
mamba install -c conda-forge groundingdino-py
26+
mamba install -c conda-forge groundingdino-py segment-anything-fast
2727
```
2828

2929
As of July 9th, 2023 Linux systems have also required that `libgl1` be installed for segment-geospatial to work. The following command will install that dependency

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,6 @@ nav:
6262
- API Reference:
6363
- common module: common.md
6464
- samgeo module: samgeo.md
65+
- fast_sam module: fast_sam.md
6566
- hq_sam module: hq_sam.md
6667
- text_sam module: text_sam.md

requirements_dev.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
rio-cogeo
1+
rio-cogeo
2+
groundingdino-py
3+
segment-anything-fast

samgeo/common.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1228,7 +1228,7 @@ def draw_tile(source, lat0, lon0, lat1, lon1, zoom, filename, **kwargs):
12281228
return image
12291229

12301230

1231-
def raster_to_vector(source, output, simplify_tolerance=None, **kwargs):
1231+
def raster_to_vector(source, output, simplify_tolerance=None, dst_crs=None, **kwargs):
12321232
"""Vectorize a raster dataset.
12331233
12341234
Args:
@@ -1256,6 +1256,10 @@ def raster_to_vector(source, output, simplify_tolerance=None, **kwargs):
12561256
gdf = gpd.GeoDataFrame.from_features(fc)
12571257
if src.crs is not None:
12581258
gdf.set_crs(crs=src.crs, inplace=True)
1259+
1260+
if dst_crs is not None:
1261+
gdf = gdf.to_crs(dst_crs)
1262+
12591263
gdf.to_file(output, **kwargs)
12601264

12611265

0 commit comments

Comments
 (0)