Skip to content

Commit

Permalink
Added fastsam module (#167)
Browse files Browse the repository at this point in the history
* Add fastsam module

* Add fastsam functions and notebook

* Add show_anns function

* Update dep
  • Loading branch information
giswqs authored Aug 24, 2023
1 parent d77bc91 commit c0956ad
Show file tree
Hide file tree
Showing 7 changed files with 590 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ private/
**/*.jpg
**/*.png
**/*.csv
**/*.pt
docs/examples/*.geojson

# C extensions
Expand Down
267 changes: 267 additions & 0 deletions docs/examples/fast_sam.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,267 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Segmenting remote sensing imagery with FastSAM\n",
"\n",
"[![image](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/opengeos/segment-geospatial/blob/main/docs/examples/fast_sam.ipynb)\n",
"[![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",
"[![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",
"\n",
"FastSAM: https://github.com/CASIA-IVA-Lab/FastSAM\n",
"\n",
"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. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Install dependencies\n",
"\n",
"Uncomment and run the following cell to install the required dependencies."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# %pip install segment-geospatial segment-anything-fast"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import leafmap\n",
"from samgeo import tms_to_geotiff\n",
"from samgeo.fast_sam import SamGeo"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Create an interactive map"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"m = leafmap.Map(center=[-22.17615, -51.253043], zoom=18, height=\"800px\")\n",
"m.add_basemap(\"SATELLITE\")\n",
"m"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Download a sample image\n",
"\n",
"Pan and zoom the map to select the area of interest. Use the draw tools to draw a polygon or rectangle on the map"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"bbox = m.user_roi_bounds()\n",
"if bbox is None:\n",
" bbox = [-51.2565, -22.1777, -51.2512, -22.175]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"image = \"Image.tif\"\n",
"tms_to_geotiff(output=image, bbox=bbox, zoom=19, source=\"Satellite\", overwrite=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can also use your own image. Uncomment and run the following cell to use your own image."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# image = '/path/to/your/own/image.tif'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Display the downloaded image on the map."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"m.layers[-1].visible = False\n",
"m.add_raster(image, layer_name=\"Image\")\n",
"m"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Initialize SamGeo class\n",
"\n",
"The initialization of the SamGeo class might take a few minutes. The initialization downloads the model weights and sets up the model for inference."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from samgeo.fast_sam import SamGeo\n",
"sam = SamGeo(model=\"FastSAM-x.pt\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Set the image."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"sam.set_image(\"Image.tif\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Segment the image with `everything_prompt`. You can also try `point_prompt`, `box_prompt`, or `text_prompt`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"sam.everything_prompt(output=\"mask.tif\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Show the annotated image."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"sam.show_anns(\"mask.png\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![](https://i.imgur.com/af4bj7O.png)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Convert the segmentation results from GeoTIFF to vector."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"sam.raster_to_vector(\"mask.tif\", \"mask.geojson\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Show the segmentation results on the map."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"m.add_raster(\"mask.tif\", opacity=0.5, layer_name=\"Mask\")\n",
"m.add_vector(\"mask.geojson\", layer_name=\"Mask Vector\")\n",
"m"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![](https://i.imgur.com/LvEAMSl.png)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "sam",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.16"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
2 changes: 1 addition & 1 deletion docs/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ mamba install -c conda-forge segment-geospatial
Samgeo-geospatial has some optional dependencies that are not included in the default conda environment. To install these dependencies, run the following command:

```bash
mamba install -c conda-forge groundingdino-py
mamba install -c conda-forge groundingdino-py segment-anything-fast
```

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
Expand Down
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,6 @@ nav:
- API Reference:
- common module: common.md
- samgeo module: samgeo.md
- fast_sam module: fast_sam.md
- hq_sam module: hq_sam.md
- text_sam module: text_sam.md
4 changes: 3 additions & 1 deletion requirements_dev.txt
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
rio-cogeo
rio-cogeo
groundingdino-py
segment-anything-fast
6 changes: 5 additions & 1 deletion samgeo/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1228,7 +1228,7 @@ def draw_tile(source, lat0, lon0, lat1, lon1, zoom, filename, **kwargs):
return image


def raster_to_vector(source, output, simplify_tolerance=None, **kwargs):
def raster_to_vector(source, output, simplify_tolerance=None, dst_crs=None, **kwargs):
"""Vectorize a raster dataset.
Args:
Expand Down Expand Up @@ -1256,6 +1256,10 @@ def raster_to_vector(source, output, simplify_tolerance=None, **kwargs):
gdf = gpd.GeoDataFrame.from_features(fc)
if src.crs is not None:
gdf.set_crs(crs=src.crs, inplace=True)

if dst_crs is not None:
gdf = gdf.to_crs(dst_crs)

gdf.to_file(output, **kwargs)


Expand Down
Loading

0 comments on commit c0956ad

Please sign in to comment.