Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for SAM 2 #312

Merged
merged 16 commits into from
Sep 16, 2024
Merged

Add support for SAM 2 #312

merged 16 commits into from
Sep 16, 2024

Conversation

giswqs
Copy link
Member

@giswqs giswqs commented Sep 12, 2024

@giswqs giswqs added the Feature request New feature or request label Sep 12, 2024
Copy link

github-actions bot commented Sep 12, 2024

@github-actions github-actions bot temporarily deployed to pull request September 12, 2024 04:30 Inactive
@giswqs
Copy link
Member Author

giswqs commented Sep 13, 2024

from samgeo import SamGeo2

sam2 = SamGeo2(model_id="sam2-hiera-large", automatic=True)
image = "images/cars.jpg"
masks = sam2.generate(image)

image

image

@github-actions github-actions bot temporarily deployed to pull request September 13, 2024 19:03 Inactive
@giswqs
Copy link
Member Author

giswqs commented Sep 13, 2024

from samgeo import SamGeo2

sam2 = SamGeo2(
    model_id="sam2-hiera-large",
    apply_postprocessing=False,
    points_per_side=32,
    points_per_batch=64,
    pred_iou_thresh=0.7,
    stability_score_thresh=0.92,
    stability_score_offset=0.7,
    crop_n_layers=1,
    box_nms_thresh=0.7,
    crop_n_points_downscale_factor=2,
    min_mask_region_area=25.0,
    use_m2m=True,
)
image = "images/satellite.tif"
masks = sam2.generate(image)

satellite

image

@github-actions github-actions bot temporarily deployed to pull request September 13, 2024 19:55 Inactive
@giswqs
Copy link
Member Author

giswqs commented Sep 13, 2024

Peek.2024-09-13.15-56.mp4

@twoldemariam
Copy link

What an exciting time we are in!

@github-actions github-actions bot temporarily deployed to pull request September 14, 2024 02:46 Inactive
@github-actions github-actions bot temporarily deployed to pull request September 14, 2024 04:09 Inactive
@github-actions github-actions bot temporarily deployed to pull request September 14, 2024 04:49 Inactive
@giswqs giswqs linked an issue Sep 14, 2024 that may be closed by this pull request
@github-actions github-actions bot temporarily deployed to pull request September 14, 2024 15:27 Inactive
@github-actions github-actions bot temporarily deployed to pull request September 14, 2024 17:05 Inactive
@brendancol
Copy link
Collaborator

@giswqs
Copy link
Member Author

giswqs commented Sep 15, 2024

@brendancol Yes, we should be able to add support for LangSAM.
The Grounded SAM2 is also very interesting.

@github-actions github-actions bot temporarily deployed to pull request September 15, 2024 05:00 Inactive
@github-actions github-actions bot temporarily deployed to pull request September 15, 2024 14:22 Inactive
@github-actions github-actions bot temporarily deployed to pull request September 15, 2024 14:47 Inactive
@giswqs
Copy link
Member Author

giswqs commented Sep 15, 2024

from samgeo import SamGeo2

predictor = SamGeo2(video=True)
video_path = "input_video.mp4"
predictor.set_video(video_path)
prompts = {
    1: {
        "points": [[200, 300], [275, 175]],
        "labels": [1, 0],
        "frame_idx": 0,
    },
    2: {
        "points": [[400, 150]],
        "labels": [1],
        "frame_idx": 0,
    },
}
predictor.predict_video(prompts)
predictor.save_video_segments_blended(
    "blended", output_video="output_video.mp4", fps=30
)

input video

input_video.mp4

output video

output_video.mp4

@github-actions github-actions bot temporarily deployed to pull request September 15, 2024 22:50 Inactive
@giswqs
Copy link
Member Author

giswqs commented Sep 16, 2024

from samgeo import SamGeo2

predictor = SamGeo2(video=True)
video_path = "input_video.mp4"
predictor.set_video(video_path, frame_rate=3)
prompts = {
    1: {
        "points": [[200, 300], [275, 175]],
        "labels": [1, 0],
        "frame_idx": 0,
    },
    2: {
        "points": [[400, 150]],
        "labels": [1],
        "frame_idx": 0,
    },
}
predictor.show_prompts(prompts, frame_idx=0)

image

@giswqs
Copy link
Member Author

giswqs commented Sep 16, 2024

from samgeo import SamGeo2

predictor = SamGeo2(video=True)
video_path = "input_video.mp4"
predictor.set_video(video_path, frame_rate=3)
predictor.show_images()

image

@github-actions github-actions bot temporarily deployed to pull request September 16, 2024 00:34 Inactive
@giswqs
Copy link
Member Author

giswqs commented Sep 16, 2024

Input video

landsat.mp4

Point prompts

image

Output video

output_video2.mp4

@github-actions github-actions bot temporarily deployed to pull request September 16, 2024 04:19 Inactive
@giswqs
Copy link
Member Author

giswqs commented Sep 16, 2024

from samgeo import SamGeo2

predictor = SamGeo2(video=True)
video_path = "landsat_ts"
predictor.set_video(video_path)
prompts = {
    1: {
        "points": [[1582, 933], [1287, 905], [1473, 998]],
        "labels": [1, 1, 1],
        "frame_idx": 0,
    },
}
predictor.show_prompts(prompts, frame_idx=0)
predictor.predict_video(prompts)
predictor.save_video_segments("segments")

Input GeoTIFFs

landsat_images.mp4

Point prompts

image

Output GeoTIFFs

landsat_water.mp4

Blended mode

landsat_water_blended.mp4

@github-actions github-actions bot temporarily deployed to pull request September 16, 2024 04:44 Inactive
@giswqs
Copy link
Member Author

giswqs commented Sep 16, 2024

Input points can be either XY or lon/lat pairs.

from samgeo import SamGeo2

predictor = SamGeo2(video=True)
video_path = "landsat_ts"
predictor.set_video(video_path)
prompts = {
    1: {
        "points": [[-74.3713, -8.5218], [-74.2973, -8.5306], [-74.3230, -8.5495]],
        "labels": [1, 1, 1],
        "frame_idx": 0,
    },
}
predictor.show_prompts(prompts, frame_idx=0, point_crs="EPSG:4326")
predictor.predict_video()
predictor.save_video_segments_blended(
    "blended", fps=5, output_video="landsat_water_blended.mp4"
)

@github-actions github-actions bot temporarily deployed to pull request September 16, 2024 17:52 Inactive
@giswqs giswqs merged commit 79f9a31 into main Sep 16, 2024
9 checks passed
@giswqs giswqs deleted the sam2 branch September 16, 2024 17:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature request New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for SAM 2
3 participants