MogensR commited on
Commit
84ac472
·
1 Parent(s): 867be90
Files changed (2) hide show
  1. README.md +101 -40
  2. requirements.txt +1 -1
README.md CHANGED
@@ -1,4 +1,3 @@
1
- ---
2
  title: 🎬 BackgroundFX Pro - SAM2 + MatAnyone
3
  emoji: 🎥
4
  colorFrom: indigo
@@ -7,61 +6,123 @@ sdk: docker
7
  app_port: 7860
8
  license: mit
9
  tags:
10
- - video
11
- - background-removal
12
- - segmentation
13
- - matting
14
- - SAM2
15
- - MatAnyOne
16
- ---
17
 
18
- # 🎬 BackgroundFX Pro — Professional Video Background Replacement
19
 
20
- **BackgroundFX Pro** is an advanced video background replacement app built for **Hugging Face Spaces (GPU)**.
21
- It combines cutting-edge AI segmentation and matting models with a simple Gradio UI:
22
 
23
- - **[SAM2](https://huggingface.co/facebook/sam2)** → high-quality object segmentation (first frame or image prompts).
24
- - **[MatAnyone](https://huggingface.co/PeiqingYang/MatAnyone)** → temporal video matting with consistent alpha masks.
25
- - **Rembg fallback** → ensures every video can be processed, even if models fail.
26
 
27
- 👉 The result: **fast, stable, professional-grade background replacement** — ideal for content creators, streamers, and marketing.
28
 
29
- ---
30
 
31
- ## ✨ Features
32
 
33
- - 🎥 Upload any video and replace its background with:
34
- - Solid colors
35
- - AI-generated images
36
- - Custom uploaded backgrounds
37
- - ⚡ GPU-optimized (Tesla T4 / CUDA 12.x)
38
- - 🧩 Model pre-warm for faster startup
39
- - 🛡️ Fallback pipeline so you *never lose frames*
40
- - 📂 Persistent caching in `/data/.cache` for Hugging Face Spaces
41
 
42
- ---
 
 
43
 
44
- ## 🚀 Try it on Hugging Face Spaces
45
 
46
- [**▶ Open BackgroundFX Pro on Hugging Face**](https://huggingface.co/spaces/MogensR/VideoBackgroundReplacer2)
47
 
48
- No installation required runs directly in your browser with GPU acceleration.
 
 
 
 
 
49
 
50
- ---
51
 
52
- ## 🛠️ Developer Notes
 
53
 
54
- ### Local Development (with Docker)
55
 
56
- Clone the repo and build locally:
57
 
58
- ```bash
59
- git clone https://huggingface.co/spaces/MogensR/VideoBackgroundReplacer2
60
- cd VideoBackgroundReplacer2
61
 
62
- # Build container (CUDA 12.3 base, Python 3.10, Torch 2.3.1+cu121)
63
- docker build -t backgroundfx-pro .
64
 
65
- # Run locally
66
- docker run --gpus all -p 7860:7860 backgroundfx-pro
67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  title: 🎬 BackgroundFX Pro - SAM2 + MatAnyone
2
  emoji: 🎥
3
  colorFrom: indigo
 
6
  app_port: 7860
7
  license: mit
8
  tags:
 
 
 
 
 
 
 
9
 
10
+ video
11
 
12
+ background-removal
 
13
 
14
+ segmentation
 
 
15
 
16
+ matting
17
 
18
+ SAM2
19
 
20
+ MatAnyone
21
 
22
+ 🎬 BackgroundFX Pro Professional Video Background Replacement
 
 
 
 
 
 
 
23
 
24
+ BackgroundFX Pro is a GPU-accelerated app for Hugging Face Spaces (Docker) that replaces video backgrounds using:
25
+ • SAM2 — high-quality object segmentation
26
+ • MatAnyone — temporal video matting for stable alpha over time
27
 
28
+ Built on: CUDA 12.1.1, PyTorch 2.5.1 (cu121), torchvision 0.20.1, Gradio 4.41.0.
29
 
30
+ Features
31
 
32
+ Upload a video and replace its background with Solid color, AI-generated image, your own uploaded image, or an Unsplash search.
33
+ • Optimized for T4 GPUs (HF Spaces).
34
+ • Caching & logs stored in the repository volume for reliability:
35
+ – HF cache: ./.hf
36
+ – Torch cache: ./.torch
37
+ – App data & logs: ./data (see data/run.log)
38
 
39
+ 🚀 Try It
40
 
41
+ Open the Space in your browser (GPU required):
42
+ https://huggingface.co/spaces/MogensR/VideoBackgroundReplacer2
43
 
44
+ 🖱️ How to Use
45
 
46
+ Upload a video (.mp4, .avi, .mov, .mkv).
47
 
48
+ Choose a Background Type: Upload Image, AI Generate, Gradient, Solid, or Unsplash.
 
 
49
 
50
+ If not uploading, enter a prompt and click Generate Background.
 
51
 
52
+ Click Process Video.
 
53
 
54
+ Preview and download the result.
55
+
56
+ Tip: Start with 720p/1080p on T4; 4K can exceed memory.
57
+
58
+ 🗂️ Project Structure (key files)
59
+
60
+ • Dockerfile
61
+ • requirements.txt
62
+ • ui.py
63
+ • ui_core_interface.py
64
+ • ui_core_functionality.py
65
+ • two_stage_pipeline.py
66
+ • models/sam2_loader.py
67
+ • models/matanyone_loader.py
68
+ • utils/init.py
69
+ • data/ (created at runtime for logs and outputs)
70
+ • tmp/ (created at runtime for jobs and temp files)
71
+
72
+ ⚙️ Runtime Notes
73
+
74
+ • The app binds to the port from PORT or GRADIO_SERVER_PORT (defaults to 7860).
75
+ • A heartbeat writes every ~2s to logs with memory and disk stats.
76
+ • If the process dies without a final “PROCESS EXITING” line, it was likely an OOM or hard kill.
77
+
78
+ 🧪 Local Development (Docker)
79
+
80
+ Requires an NVIDIA GPU with CUDA drivers installed.
81
+
82
+ Steps:
83
+
84
+ Clone the Space repo.
85
+
86
+ Build the image (base: Ubuntu 22.04, CUDA 12.1.1; installs Torch 2.5.1+cu121).
87
+
88
+ Run the container and map port 7860.
89
+
90
+ 🔧 Space Settings
91
+
92
+ • SDK: docker
93
+ • App port: 7860
94
+ • Hardware: T4 GPU (or better)
95
+
96
+ 📦 Key Versions
97
+
98
+ • Gradio: 4.41.0
99
+ • PyTorch: 2.5.1 (cu121)
100
+ • torchvision: 0.20.1
101
+ • SAM2: installed from source at build time in third_party/sam2
102
+ • MatAnyone: installed from GitHub
103
+
104
+ 🧯 Troubleshooting
105
+
106
+ Container exits after ~20s:
107
+ • Check data/run.log — heartbeat shows RSS and cgroup memory limit.
108
+ • If OOM is suspected: use lower-resolution videos, keep concurrency low (already set to max_threads=1 and queue(max_size=2)), and ensure GPU hardware is selected.
109
+
110
+ “Could not open video” or empty output:
111
+ • Verify the uploaded file codec/container. Re-encode to H.264/AAC in .mp4 if needed.
112
+
113
+ Weights not found / slow first run:
114
+ • First run downloads model assets into ./.hf and ./.torch. Subsequent runs are faster.
115
+
116
+ 🔐 Privacy
117
+
118
+ All processing happens inside the Space container. Uploaded files are copied into ./data/uploads within the Space’s sandboxed storage.
119
+
120
+ 📄 License
121
+
122
+ MIT — see LICENSE or the Space’s license field.
123
+
124
+ 🙌 Credits
125
+
126
+ • SAM2 by Meta FAIR
127
+ • MatAnyone by Peiqing Yang et al.
128
+ • Built with Gradio and PyTorch
requirements.txt CHANGED
@@ -36,7 +36,7 @@ kornia>=0.7.0,<0.8.0
36
  tqdm>=4.60.0,<5.0.0
37
 
38
  # ===== UI and API =====
39
- gradio==4.41.3
40
 
41
  # ===== Helpers and Utilities =====
42
  huggingface-hub>=0.20.0,<1.0.0
 
36
  tqdm>=4.60.0,<5.0.0
37
 
38
  # ===== UI and API =====
39
+ gradio==4.41.0
40
 
41
  # ===== Helpers and Utilities =====
42
  huggingface-hub>=0.20.0,<1.0.0