Skip to content

Commit c938d37

Browse files
committed
Add show_anns function
1 parent c1b1072 commit c938d37

File tree

4 files changed

+80
-2
lines changed

4 files changed

+80
-2
lines changed

docs/examples/fast_sam.ipynb

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
"[![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",
1111
"[![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",
1212
"\n",
13+
"FastSAM: https://github.com/CASIA-IVA-Lab/FastSAM\n",
14+
"\n",
1315
"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. "
1416
]
1517
},
@@ -143,6 +145,13 @@
143145
"sam = SamGeo(model=\"FastSAM-x.pt\")"
144146
]
145147
},
148+
{
149+
"cell_type": "markdown",
150+
"metadata": {},
151+
"source": [
152+
"Set the image."
153+
]
154+
},
146155
{
147156
"cell_type": "code",
148157
"execution_count": null,
@@ -152,6 +161,13 @@
152161
"sam.set_image(\"Image.tif\")"
153162
]
154163
},
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+
},
155171
{
156172
"cell_type": "code",
157173
"execution_count": null,
@@ -161,6 +177,36 @@
161177
"sam.everything_prompt(output=\"mask.tif\")"
162178
]
163179
},
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+
},
164210
{
165211
"cell_type": "code",
166212
"execution_count": null,
@@ -170,6 +216,13 @@
170216
"sam.raster_to_vector(\"mask.tif\", \"mask.geojson\")"
171217
]
172218
},
219+
{
220+
"cell_type": "markdown",
221+
"metadata": {},
222+
"source": [
223+
"Show the segmentation results on the map."
224+
]
225+
},
173226
{
174227
"cell_type": "code",
175228
"execution_count": null,

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

requirements_dev.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
rio-cogeo
2-
segment-anything-hq
2+
segment-anything-fast

samgeo/fast_sam.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,3 +285,28 @@ def raster_to_vector(
285285
dst_crs=dst_crs,
286286
**kwargs,
287287
)
288+
289+
def show_anns(
290+
self,
291+
output=None,
292+
**kwargs,
293+
):
294+
"""Show the annotations (objects with random color) on the input image.
295+
296+
Args:
297+
figsize (tuple, optional): The figure size. Defaults to (12, 10).
298+
axis (str, optional): Whether to show the axis. Defaults to "off".
299+
alpha (float, optional): The alpha value for the annotations. Defaults to 0.35.
300+
output (str, optional): The path to the output image. Defaults to None.
301+
blend (bool, optional): Whether to show the input image. Defaults to True.
302+
"""
303+
304+
annotations = self.annotations
305+
prompt_process = self.prompt_process
306+
307+
if output is None:
308+
output = temp_file_path(".png")
309+
310+
prompt_process.plot(annotations, output, **kwargs)
311+
312+
show_image(output)

0 commit comments

Comments
 (0)