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