File size: 6,755 Bytes
69f4f56
1d6ec77
3cddaf8
 
 
69f4f56
e7cbaa4
69f4f56
 
 
3cddaf8
 
 
 
 
 
 
69f4f56
 
3cddaf8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
---
title: SAM2 Video Background Remover
emoji: ๐ŸŽฅ
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 4.44.0
app_file: app.py
pinned: false
license: apache-2.0
tags:
  - computer-vision
  - video
  - segmentation
  - sam2
  - background-removal
  - object-tracking
---

# ๐ŸŽฅ SAM2 Video Background Remover

Remove backgrounds from videos by tracking objects using Meta's **Segment Anything Model 2 (SAM2)**.

## Features

โœจ **Background Removal**: Automatically remove backgrounds and keep only tracked objects  
๐ŸŽฏ **Object Tracking**: Track multiple objects across video frames  
๐Ÿ–ฅ๏ธ **Interactive UI**: Easy-to-use Gradio interface  
๐Ÿ”Œ **REST API**: Programmatic access via API endpoints  
โšก **GPU Accelerated**: Fast processing with CUDA support  

## How It Works

SAM2 is a foundation model for video segmentation that can:
1. **Segment objects** based on point or box annotations
2. **Track objects** automatically across all video frames
3. **Handle occlusions** and object reappearance
4. **Process multiple objects** simultaneously

## Usage

### ๐Ÿ–ฑ๏ธ Simple Mode (Web UI)

1. Upload your video
2. Specify X,Y coordinates of the object you want to track (from first frame)
3. Click "Process Video"
4. Download the result with background removed!

**Example**: For a 640x480 video with a person in the center, use X=320, Y=240

### ๐Ÿ”ง Advanced Mode (JSON Annotations)

For more control, use JSON annotations:

```json
[
    {
        "frame_idx": 0,
        "object_id": 1,
        "points": [[320, 240]],
        "labels": [1]
    }
]
```

**Parameters**:
- `frame_idx`: Frame number to annotate (0 = first frame)
- `object_id`: Unique ID for each object (1, 2, 3, ...)
- `points`: List of [x, y] coordinates on the object
- `labels`: `1` for foreground point, `0` for background point

### ๐Ÿ“ก API Usage

You can call this Space programmatically using the Gradio Client:

#### Python Example

```python
from gradio_client import Client
import json

# Connect to the Space
client = Client("YOUR_USERNAME/sam2-video-bg-remover")

# Define what to track
annotations = [
    {
        "frame_idx": 0,
        "object_id": 1,
        "points": [[320, 240]],  # x, y coordinates
        "labels": [1]             # 1 = foreground
    }
]

# Process video
result = client.predict(
    video_file="./input_video.mp4",
    annotations_json=json.dumps(annotations),
    remove_background=True,
    max_frames=300,  # Limit frames for faster processing
    api_name="/segment_video_api"
)

print(f"Output video saved to: {result}")
```

#### Track Multiple Objects

```python
annotations = [
    # First object (person)
    {
        "frame_idx": 0,
        "object_id": 1,
        "points": [[320, 240]],
        "labels": [1]
    },
    # Second object (ball)
    {
        "frame_idx": 0,
        "object_id": 2,
        "points": [[500, 300]],
        "labels": [1]
    }
]
```

#### Refine Segmentation with Background Points

```python
annotations = [
    {
        "frame_idx": 0,
        "object_id": 1,
        "points": [
            [320, 240],  # Point ON the object
            [100, 100]   # Point on background to exclude
        ],
        "labels": [1, 0]  # 1=foreground, 0=background
    }
]
```

### ๐ŸŒ HTTP API

You can also call the API directly via HTTP:

```bash
curl -X POST https://YOUR_USERNAME-sam2-video-bg-remover.hf.space/api/predict \
  -F "video_file=@input_video.mp4" \
  -F 'annotations_json=[{"frame_idx":0,"object_id":1,"points":[[320,240]],"labels":[1]}]' \
  -F "remove_background=true" \
  -F "max_frames=300"
```

## Parameters

| Parameter | Type | Default | Description |
|-----------|------|---------|-------------|
| `video_file` | File | - | Input video file (required) |
| `annotations_json` | String | - | JSON array of annotations (required) |
| `remove_background` | Boolean | `true` | Remove background or just highlight objects |
| `max_frames` | Integer | `null` | Limit frames for faster processing |

## Tips & Best Practices

### ๐ŸŽฏ Getting Good Results

1. **Choose Clear Points**: Click on the center/most distinctive part of your object
2. **Add Multiple Points**: For complex objects, add 2-3 points on different parts
3. **Use Background Points**: Add points with `label: 0` on areas you DON'T want
4. **Annotate Key Frames**: If object changes significantly, add annotations on multiple frames

### โšก Performance Tips

1. **Limit Frames**: Use `max_frames` parameter for long videos
2. **Use Smaller Model**: Default is `sam2.1-hiera-tiny` for speed
3. **Process Shorter Clips**: Split long videos into segments

### ๐Ÿ› Troubleshooting

| Issue | Solution |
|-------|----------|
| Object not tracked | Add more points on different parts of the object |
| Background leakage | Add background points with `label: 0` |
| Slow processing | Reduce `max_frames` or use a shorter video |
| Wrong object tracked | Be more precise with point coordinates |

## Model Information

This Space uses **facebook/sam2.1-hiera-tiny** for efficient processing. Other available models:

- `facebook/sam2.1-hiera-tiny` - Fastest, good quality โšก
- `facebook/sam2.1-hiera-small` - Balanced
- `facebook/sam2.1-hiera-base-plus` - Higher quality
- `facebook/sam2.1-hiera-large` - Best quality, slower ๐ŸŽฏ

## Use Cases

- ๐ŸŽฌ **Video Production**: Remove backgrounds for green screen effects
- ๐Ÿƒ **Sports Analysis**: Isolate athletes for motion analysis
- ๐ŸŽฎ **Content Creation**: Extract game characters or objects
- ๐Ÿ”ฌ **Research**: Track objects in scientific videos
- ๐Ÿ“ฑ **Social Media**: Create engaging content with background removal

## Limitations

- Video length affects processing time (longer = slower)
- GPU recommended for videos > 10 seconds
- Very fast-moving objects may require multiple annotations
- Extreme lighting changes can affect tracking quality

## Citation

If you use this Space, please cite the SAM2 paper:

```bibtex
@article{ravi2024sam2,
  title={Segment Anything in Images and Videos},
  author={Ravi, Nikhila and Gabeur, Valentin and Hu, Yuan-Ting and Hu, Ronghang and Ryali, Chaitanya and Ma, Tengyu and Khedr, Haitham and R{\"a}dle, Roman and Rolland, Chloe and Gustafson, Laura and others},
  journal={arXiv preprint arXiv:2408.00714},
  year={2024}
}
```

## License

Apache 2.0

## Links

- ๐Ÿ“š [SAM2 Documentation](https://huggingface.co/docs/transformers/model_doc/sam2_video)
- ๐Ÿค— [Model on Hugging Face](https://huggingface.co/facebook/sam2.1-hiera-tiny)
- ๐Ÿ“„ [Research Paper](https://arxiv.org/abs/2408.00714)
- ๐Ÿ’ป [Original Repository](https://github.com/facebookresearch/segment-anything-2)

---

Built with โค๏ธ using [Transformers](https://github.com/huggingface/transformers) and [Gradio](https://gradio.app)