https://github.com/brownag/rsamgeo
R 'reticulate' wrapper for `segment-geospatial` 'Python' package for segmenting geospatial data with the ‘Segment Anything Model’ (SAM).
https://github.com/brownag/rsamgeo
python r segment-anything-model segment-geospatial
Last synced: 2 months ago
JSON representation
R 'reticulate' wrapper for `segment-geospatial` 'Python' package for segmenting geospatial data with the ‘Segment Anything Model’ (SAM).
- Host: GitHub
- URL: https://github.com/brownag/rsamgeo
- Owner: brownag
- License: other
- Created: 2023-05-07T01:53:06.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2023-05-08T00:51:59.000Z (about 2 years ago)
- Last Synced: 2025-03-01T00:19:01.931Z (3 months ago)
- Topics: python, r, segment-anything-model, segment-geospatial
- Language: R
- Homepage: https://humus.rocks/rsamgeo
- Size: 9.77 KB
- Stars: 13
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.Rmd
- License: LICENSE
Awesome Lists containing this project
README
---
output: github_document
---```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```# {rsamgeo}
[](https://colab.research.google.com/drive/1DwHUc1Vpgg1dRTSKB7AY5puDM_2uB8MY?usp=sharing)
The goal of {rsamgeo} is to provide a basic R wrapper around the [`segment-geospatial`](https://github.com/opengeos/segment-geospatial) 'Python' package by [Dr. Qiusheng Wu](https://github.com/giswqs). Uses R {reticulate} package to call tools for segmenting geospatial data with the [Meta 'Segment Anything Model' (SAM)](https://github.com/facebookresearch/segment-anything). The 'segment-geospatial' package draws its inspiration from 'segment-anything-eo' repository authored by [Aliaksandr Hancharenka](https://github.com/aliaksandr960).
## Installation
You can install the development version of {rsamgeo} like so:
``` r
remotes::install_github("brownag/rsamgeo")
rsamgeo::sg_install()
```## Example
After installing the package and the 'Python' dependencies, we can download some sample data using `tms_to_geotiff()`
```{r example1, eval = FALSE}
library(rsamgeo)samgeo()$tms_to_geotiff(
output = "satellite.tif",
bbox = c(-120.3704, 37.6762, -120.368, 37.6775),
zoom = 20L,
source = 'Satellite',
overwrite = TRUE
)
```The SAM `model_type` specifies the SAM model you wish to use. Trained model data used for segmentation are downloaded if the file `checkpoint` is not found. Downloading this for the first time may take a while. Create an instance of your desired model with `sg_samgeo()`
```{r example2, eval = FALSE}
out_dir <- path.expand(file.path('~', 'Downloads'))
checkpoint <- file.path(out_dir, 'sam_vit_h_4b8939.pth')sam <- sg_samgeo(
model_type = 'vit_h',
checkpoint = checkpoint,
erosion_kernel = c(3L, 3L),
mask_multiplier = 255L,
sam_kwargs = NULL
)
```Finally, generate a segmented image from the input, processing the input in chunks as needed with `batch=TRUE`. Note that you want this processing to run on the GPU with CUDA enabled. For the [Google Colab Notebook]((https://colab.research.google.com/drive/1DwHUc1Vpgg1dRTSKB7AY5puDM_2uB8MY?usp=sharing)) example remember to set the notebook runtime to 'GPU'.
```{r example3, eval = FALSE}
sg_generate(sam,
"satellite.tif",
"segment.tif",
batch = TRUE)
```Now that we have processed the input data, we can convert the segmented image to vectors and write them out as a layer in a GeoPackage for subsequent use.
```{r example4, eval = FALSE}
sam$tiff_to_gpkg("segment.tif",
"segment.gpkg",
simplify_tolerance=NULL)
```It is then fairly easy to overlay our segment polygons on the original satellite image with {terra}:
```{r example-inspect, eval=FALSE}
library(terra)
r <- rast("satellite.tif")
v <- vect("segment.gpkg")
v$ID <- seq_len(nrow(v))
plotRGB(r)
plot(v, col = v$ID, alpha = 0.25, add = TRUE)
```