# AI use cases for Sam2 (facebookresearch/sam2)

Repo: [facebookresearch/sam2](https://github.com/facebookresearch/sam2) | Use cases: 3 | Page: https://dedupedai.com/use-cases/facebookresearch-sam2/

SAM 2 is Meta FAIR's Segment Anything Model 2, a foundation model for promptable visual segmentation in images and videos. You give it points or boxes and it returns masks, and in video it tracks objects across frames as masklets. The repo includes SAM 2.1 checkpoints, image and video predictors, Hugging Face loading, training code and a web demo.

This file holds every use case of this repo. Each one starts with a "## " heading and ends with its URL.

- Segment an object in an image from a click: https://dedupedai.com/use-cases/facebookresearch-sam2/segment-objects-in-an-image-with-sam2/
- Track an object through a video: https://dedupedai.com/use-cases/facebookresearch-sam2/track-an-object-through-a-video-with-sam2/
- Load SAM 2 from Hugging Face without the checkpoint script: https://dedupedai.com/use-cases/facebookresearch-sam2/load-sam2-from-hugging-face/

## How to segment an object in an image from a click with Sam2

Repo: [facebookresearch/sam2](https://github.com/facebookresearch/sam2) | Difficulty: Intermediate | Time: 45 minutes | Basis: From the README

Install SAM 2, download checkpoints and get masks for an image from point or box prompts.

**Who it is for:** A developer cutting objects out of product photos.

**The problem:** Hand masking objects is slow and inconsistent.

**The outcome:** Masks for the objects you prompted.

### Steps

1. Run `git clone https://github.com/facebookresearch/sam2.git && cd sam2` then `pip install -e .`
2. Download checkpoints with `./download_ckpts.sh` inside the checkpoints folder
3. Build a SAM2ImagePredictor from configs/sam2.1/sam2.1_hiera_l.yaml and ./checkpoints/sam2.1_hiera_large.pt
4. Call predictor.set_image with your image and predictor.predict with your prompts

### Prompt to paste into your agent

```text
Using facebookresearch/sam2, install it with `pip install -e .`, download the checkpoints, and write a script that loads SAM2ImagePredictor and saves a mask PNG for the object at a point I give in <my image>.
```

### Caveat

Install compiles a CUDA extension; if it fails SAM 2 still works with some post-processing limited.

Disclaimer: Third-party repos we don't own or vouch for. AI-generated use cases can be wrong: review everything and run at your own risk. Full text: https://dedupedai.com/disclaimer/

URL: https://dedupedai.com/use-cases/facebookresearch-sam2/segment-objects-in-an-image-with-sam2/

## How to track an object through a video with Sam2

Repo: [facebookresearch/sam2](https://github.com/facebookresearch/sam2) | Difficulty: Advanced | Time: 1 to 2 hours | Basis: From the README

Prompt an object on one frame and propagate its mask through the whole video.

**Who it is for:** A video editor or analyst who needs a mask on a moving object.

**The problem:** Rotoscoping frame by frame takes hours.

**The outcome:** Masklets for the object across every frame.

### Steps

1. Install SAM 2 and download checkpoints as in the README
2. Create a predictor with build_sam2_video_predictor and call predictor.init_state on your video
3. Add a point or box with predictor.add_new_points_or_box on one frame
4. Loop over predictor.propagate_in_video(state) to get masks for every frame

### Prompt to paste into your agent

```text
With facebookresearch/sam2, write a script that uses build_sam2_video_predictor, adds a box prompt on frame 0 of <my video frames folder>, propagates with propagate_in_video, and writes an overlay video.
```

### Caveat

Setting vos_optimized=True enables full model compilation for a major speedup, per the latest updates.

Disclaimer: Third-party repos we don't own or vouch for. AI-generated use cases can be wrong: review everything and run at your own risk. Full text: https://dedupedai.com/disclaimer/

URL: https://dedupedai.com/use-cases/facebookresearch-sam2/track-an-object-through-a-video-with-sam2/

## How to load SAM 2 from Hugging Face without the checkpoint script with Sam2

Repo: [facebookresearch/sam2](https://github.com/facebookresearch/sam2) | Difficulty: Beginner | Time: 20 minutes | Basis: From the README

Use from_pretrained to pull facebook/sam2-hiera-large straight from the Hub.

**Who it is for:** A developer prototyping in a notebook who wants the shortest path.

**The problem:** Managing checkpoint files by hand is friction for a quick test.

**The outcome:** A working image or video predictor loaded from the Hub.

### Steps

1. Install SAM 2 with `pip install -e .` from the cloned repo
2. Install the Hub client with `pip install huggingface_hub`
3. Call SAM2ImagePredictor.from_pretrained("facebook/sam2-hiera-large") or SAM2VideoPredictor.from_pretrained
4. Run set_image and predict as usual

### Prompt to paste into your agent

```text
Using facebookresearch/sam2, install `pip install huggingface_hub`, load SAM2ImagePredictor.from_pretrained("facebook/sam2-hiera-large") in a notebook, and segment the main object in <my image>.
```

### Caveat

You still need the SAM 2 package installed; the Hub only supplies the weights.

Disclaimer: Third-party repos we don't own or vouch for. AI-generated use cases can be wrong: review everything and run at your own risk. Full text: https://dedupedai.com/disclaimer/

URL: https://dedupedai.com/use-cases/facebookresearch-sam2/load-sam2-from-hugging-face/
