Skip to content

Instantly share code, notes, and snippets.

@giswqs
Last active September 19, 2024 16:07
Show Gist options
  • Save giswqs/8225768307a2aa503144ae7be2ec5a3a to your computer and use it in GitHub Desktop.
Save giswqs/8225768307a2aa503144ae7be2ec5a3a to your computer and use it in GitHub Desktop.
Aral Sea
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Segmenting objects from timeseries images with SAM 2\n",
"\n",
"[![image](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/gist/giswqs/8225768307a2aa503144ae7be2ec5a3a)\n",
"\n",
"\n",
"This notebook shows how to segment objects from timeseries with the [samgeo](https://github.com/opengeos/segment-geospatial) package.\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 -U segment-geospatial"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Import libraries"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"from samgeo import SamGeo2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Segment the objects from a video"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"predictor = SamGeo2(\n",
" model_id=\"sam2-hiera-large\",\n",
" video=True,\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"url = \"https://github.com/opengeos/datasets/releases/download/videos/aral_sea.mp4\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"video_path = url\n",
"predictor.set_video(video_path)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"predictor.show_images()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"prompts = {\n",
" 1: {\n",
" \"points\": [[213, 350], [530, 382], [325, 260]],\n",
" \"labels\": [1, 1, 0],\n",
" \"frame_idx\": 0,\n",
" },\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"predictor.show_prompts(prompts, frame_idx=0)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"predictor.predict_video(prompts)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"predictor.save_video_segments_blended(\"sea\", output_video=\"aral_sea_blended.mp4\", fps=5)"
]
}
],
"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.11.8"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment