Skip to content

Commit f0cecf9

Browse files
authored
Improved text prompt notebook (#79)
1 parent 8656f93 commit f0cecf9

File tree

3 files changed

+129
-2
lines changed

3 files changed

+129
-2
lines changed

docs/examples/text_prompts.ipynb

Lines changed: 101 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,9 @@
189189
"cell_type": "markdown",
190190
"metadata": {},
191191
"source": [
192-
"## Visualize the results"
192+
"## Visualize the results\n",
193+
"\n",
194+
"Show the result with bounding boxes on the map."
193195
]
194196
},
195197
{
@@ -202,6 +204,63 @@
202204
" cmap='Greens', \n",
203205
" box_color='red', \n",
204206
" title='Automatic Segmentation of Trees', \n",
207+
" blend=True,\n",
208+
")"
209+
]
210+
},
211+
{
212+
"cell_type": "markdown",
213+
"metadata": {},
214+
"source": [
215+
"![](https://i.imgur.com/ytKMTlA.png)"
216+
]
217+
},
218+
{
219+
"cell_type": "markdown",
220+
"metadata": {},
221+
"source": [
222+
"Show the result without bounding boxes on the map."
223+
]
224+
},
225+
{
226+
"cell_type": "code",
227+
"execution_count": null,
228+
"metadata": {},
229+
"outputs": [],
230+
"source": [
231+
"sam.show_anns(\n",
232+
" cmap='Greens', \n",
233+
" add_boxes=False,\n",
234+
" alpha=0.5,\n",
235+
" title='Automatic Segmentation of Trees', \n",
236+
")"
237+
]
238+
},
239+
{
240+
"cell_type": "markdown",
241+
"metadata": {},
242+
"source": [
243+
"![](https://i.imgur.com/3Iq2kt1.png)"
244+
]
245+
},
246+
{
247+
"cell_type": "markdown",
248+
"metadata": {},
249+
"source": [
250+
"Show the result as a grayscale image."
251+
]
252+
},
253+
{
254+
"cell_type": "code",
255+
"execution_count": null,
256+
"metadata": {},
257+
"outputs": [],
258+
"source": [
259+
"sam.show_anns(\n",
260+
" cmap='Greys_r', \n",
261+
" add_boxes=False,\n",
262+
" alpha=1,\n",
263+
" title='Automatic Segmentation of Trees', \n",
205264
" blend=False,\n",
206265
" output='trees.tif'\n",
207266
")"
@@ -211,7 +270,47 @@
211270
"cell_type": "markdown",
212271
"metadata": {},
213272
"source": [
214-
"![](https://i.imgur.com/ytKMTlA.png)"
273+
"![](https://i.imgur.com/KtHwFbF.png)"
274+
]
275+
},
276+
{
277+
"cell_type": "markdown",
278+
"metadata": {},
279+
"source": [
280+
"Convert the result to a vector format. "
281+
]
282+
},
283+
{
284+
"cell_type": "code",
285+
"execution_count": null,
286+
"metadata": {},
287+
"outputs": [],
288+
"source": [
289+
"sam.raster_to_vector(\"trees.tif\", \"trees.shp\")"
290+
]
291+
},
292+
{
293+
"cell_type": "markdown",
294+
"metadata": {},
295+
"source": [
296+
"Show the results on the interactive map."
297+
]
298+
},
299+
{
300+
"cell_type": "code",
301+
"execution_count": null,
302+
"metadata": {},
303+
"outputs": [],
304+
"source": [
305+
"m.add_raster(\"trees.tif\", layer_name=\"Trees\", palette=\"Greens\", opacity=0.5, nodata=0)\n",
306+
"style = {\n",
307+
" \"color\": \"#3388ff\",\n",
308+
" \"weight\": 2,\n",
309+
" \"fillColor\": \"#7c4185\",\n",
310+
" \"fillOpacity\": 0.5,\n",
311+
"}\n",
312+
"m.add_vector(\"trees.shp\", layer_name=\"Vector\", style=style)\n",
313+
"m"
215314
]
216315
}
217316
],

samgeo/samgeo.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,20 @@ def download_tms_as_tiff(self, source, pt1, pt2, zoom, dist):
604604
image = draw_tile(source, pt1[0], pt1[1], pt2[0], pt2[1], zoom, dist)
605605
return image
606606

607+
def raster_to_vector(self, image, output, simplify_tolerance=None, **kwargs):
608+
"""Save the result to a vector file.
609+
610+
Args:
611+
image (str): The path to the image file.
612+
output (str): The path to the vector file.
613+
simplify_tolerance (float, optional): The maximum allowed geometry displacement.
614+
The higher this value, the smaller the number of vertices in the resulting geometry.
615+
"""
616+
617+
raster_to_vector(
618+
image, output, simplify_tolerance=simplify_tolerance, **kwargs
619+
)
620+
607621
def tiff_to_vector(self, tiff_path, output, simplify_tolerance=None, **kwargs):
608622
"""Convert a tiff file to a gpkg file.
609623

samgeo/text_sam.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,20 @@ def show_anns(
376376
else:
377377
array_to_image(self.prediction, output, self.source)
378378

379+
def raster_to_vector(self, image, output, simplify_tolerance=None, **kwargs):
380+
"""Save the result to a vector file.
381+
382+
Args:
383+
image (str): The path to the image file.
384+
output (str): The path to the vector file.
385+
simplify_tolerance (float, optional): The maximum allowed geometry displacement.
386+
The higher this value, the smaller the number of vertices in the resulting geometry.
387+
"""
388+
389+
raster_to_vector(
390+
image, output, simplify_tolerance=simplify_tolerance, **kwargs
391+
)
392+
379393

380394
def main():
381395
parser = argparse.ArgumentParser(description="LangSAM")

0 commit comments

Comments
 (0)