File size: 6,488 Bytes
7645257
0ee293e
 
cd851c0
76b6368
 
0401b51
76b6368
cd851c0
b109571
76b6368
 
cd851c0
 
0ee293e
9a027a2
0ee293e
dd579a3
0ee293e
e9f466f
0ee293e
e9f466f
0ee293e
 
 
76b6368
0ee293e
 
e9f466f
76b6368
e9f466f
0ee293e
 
 
 
 
e9f466f
0ee293e
e9f466f
0ee293e
 
 
 
e9f466f
0ee293e
 
 
 
 
e9f466f
0ee293e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76b6368
0ee293e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e9f466f
0ee293e
e9f466f
0ee293e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dd579a3
76b6368
b109571
0ee293e
 
 
 
 
b109571
76b6368
b109571
76b6368
0ee293e
76b6368
0ee293e
b109571
0ee293e
dd579a3
0ee293e
 
 
 
 
 
 
dd579a3
0ee293e
 
 
 
 
 
 
 
 
e9f466f
0ee293e
 
 
 
 
 
e9f466f
0ee293e
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
---
title: BackgroundFX Pro - SAM2 + MatAnyone
emoji: 🎬
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.42.0
app_file: app.py
pinned: false
license: mit
suggested_hardware: t4-small
suggested_storage: small
---

# 🎬 BackgroundFX Pro - SAM2 + MatAnyone

**Professional AI video background replacement with state-of-the-art segmentation and alpha matting**

Powered by **SAM2** (Meta's Segment Anything Model 2) and **MatAnyone** (CVPR 2025) for Hollywood-quality video background replacement with professional alpha matting.

## ✨ Key Features

- **πŸ€– SAM2 Integration**: State-of-the-art object segmentation from Meta
- **🎭 MatAnyone Professional Matting**: CVPR 2025 video matting with memory propagation
- **⚑ Smart Pipeline**: SAM2 segmentation β†’ MatAnyone refinement β†’ Perfect compositing
- **🎨 Background Options**: 8 built-in presets + custom image upload
- **πŸ’Ύ Memory Optimized**: Lazy loading with automatic cleanup
- **πŸ“± Professional UI**: Clean interface with real-time progress tracking

## πŸš€ Quick Start

1. **Upload Video**: Drag & drop your video (MP4, AVI, MOV, MKV, WebM - max 5 minutes)
2. **Choose Background**: Select from presets or upload custom image
3. **Select Quality**: Choose SAM2 model size and MatAnyone settings
4. **Process**: Click "Create Professional Video" and wait for magic
5. **Download**: Get your Hollywood-quality result!

## 🎯 Professional Pipeline

### **Stage 1: SAM2 Segmentation**
- Meta's Segment Anything Model 2 detects and segments the main subject
- Multiple model sizes: Tiny (38MB), Small (185MB), Base (320MB)
- Smart point selection for optimal segmentation

### **Stage 2: MatAnyone Alpha Matting**
- CVPR 2025 state-of-the-art video matting
- Memory-based propagation for stable boundaries
- Professional hair and edge detail preservation
- Target-assigned matting using SAM2 masks

### **Stage 3: Professional Compositing**
- Seamless background replacement
- Advanced edge smoothing and blending
- Cinema-quality output

## πŸ”§ Technical Specifications

### **AI Models**
- **SAM2**: Meta's Segment Anything Model 2 for object detection
- **MatAnyone**: CVPR 2025 video matting with consistent memory propagation
- **Processing**: Lazy loading, CUDA optimization, memory management

### **Supported Formats**
- **Input**: MP4, AVI, MOV, MKV, WebM
- **Resolution**: Up to 1920x1080 (Full HD)
- **Duration**: Max 5 minutes on free tier
- **Output**: High-quality MP4 with professional alpha compositing

## ⚑ Performance Guide

| Hardware | SAM2 Only | SAM2 + MatAnyone | Recommended Use |
|----------|-----------|------------------|-----------------|
| CPU | 3-5 min/video min | 6-10 min/video min | Quick tests only |
| T4-small | 45-90s/video min | 90-180s/video min | ⭐ **Recommended** |
| T4-medium+ | 30-60s/video min | 60-120s/video min | **Professional** |

## 🎨 Background Presets

Choose from 8 professional presets:
- **🌊 Ocean Blue** - Clean professional gradient
- **πŸŒ… Sunset Orange** - Warm and vibrant atmosphere
- **🌲 Forest Green** - Natural and calming backdrop
- **πŸ’œ Purple Haze** - Creative and modern aesthetic
- **βšͺ Pure White** - Minimal and clean
- **⚫ Pure Black** - Dramatic high-contrast
- **πŸ’š Chroma Green** - For further video editing
- **πŸ’™ Chroma Blue** - Alternative chroma key

## 🎬 Professional Use Cases

- **🎯 Content Creation**: Remove distracting backgrounds for professional videos
- **πŸ“Ή Virtual Production**: Custom backgrounds for video calls and streaming
- **πŸŽ“ Education**: Clean, professional backgrounds for instructional content
- **πŸ“± Social Media**: Eye-catching backgrounds that increase engagement
- **πŸŽͺ Entertainment**: Creative backgrounds for artistic and commercial projects

## πŸ› οΈ Advanced Settings

### **SAM2 Model Selection**
- **Tiny**: Fastest processing, good quality (recommended for quick previews)
- **Small**: Balanced speed and quality (recommended for most use cases)
- **Base**: Best quality, slower processing (recommended for final videos)

### **MatAnyone Options**
- **Professional Mode**: Full MatAnyone processing for cinema-quality results
- **Fast Mode**: SAM2-only processing for quick turnaround

### **Edge Enhancement**
- **Edge Smoothing**: 0-5 scale for softer subject boundaries
- **Memory Propagation**: Automatic temporal consistency across frames

## πŸ—οΈ Built With

- [**SAM2**](https://github.com/facebookresearch/segment-anything-2) - Meta's Segment Anything Model 2
- [**MatAnyone**](https://github.com/pq-yang/MatAnyone) - CVPR 2025 Video Matting Framework
- [**Gradio**](https://gradio.app/) - Machine learning web interface framework
- [**OpenCV**](https://opencv.org/) - Computer vision and video processing
- [**PyTorch**](https://pytorch.org/) - Deep learning framework

## πŸ“‹ System Requirements

- **Recommended**: GPU-enabled Space (T4-small or better)
- **Minimum**: CPU-only mode supported but significantly slower
- **Memory**: Automatic management with CUDA optimization
- **Storage**: No persistent storage needed (true lazy loading)

## πŸ”¬ Research Citations

```bibtex
@InProceedings{yang2025matanyone,
    title = {{MatAnyone}: Stable Video Matting with Consistent Memory Propagation},
    author = {Yang, Peiqing and Zhou, Shangchen and Zhao, Jixin and Tao, Qingyi and Loy, Chen Change},
    booktitle = {CVPR},
    year = {2025}
}

@article{ravi2024sam2,
    title = {SAM 2: 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Γ€dle, Roman and Rolland, Chloe and Gustafson, Laura and Mintun, Eric and Pan, Junting and Alwala, Kalyan Vasudev and Carion, Nicolas and Wu, Chao-Yuan and Girshick, Ross and DollΓ‘r, Piotr and Feichtenhofer, Christoph},
    journal = {arXiv preprint arXiv:2408.00714},
    year = {2024}
}
```

## πŸ”— Links

- **Project Website**: [BackgroundFX Pro Demo](https://huggingface.co/spaces/your-username/backgroundfx-pro)
- **SAM2 Repository**: [facebookresearch/segment-anything-2](https://github.com/facebookresearch/segment-anything-2)
- **MatAnyone Repository**: [pq-yang/MatAnyone](https://github.com/pq-yang/MatAnyone)
- **Configuration Reference**: [HF Spaces Config](https://huggingface.co/docs/hub/spaces-config-reference)

---

**Ready to create professional videos with AI?** Upload your video and experience the future of background replacement! πŸš€