4.41.0
Browse files- README.md +101 -40
- 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 |
-
|
| 19 |
|
| 20 |
-
|
| 21 |
-
It combines cutting-edge AI segmentation and matting models with a simple Gradio UI:
|
| 22 |
|
| 23 |
-
|
| 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 |
-
|
| 28 |
|
| 29 |
-
|
| 30 |
|
| 31 |
-
|
| 32 |
|
| 33 |
-
|
| 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 |
-
|
| 45 |
|
| 46 |
-
|
| 47 |
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
|
| 51 |
|
| 52 |
-
|
|
|
|
| 53 |
|
| 54 |
-
|
| 55 |
|
| 56 |
-
|
| 57 |
|
| 58 |
-
|
| 59 |
-
git clone https://huggingface.co/spaces/MogensR/VideoBackgroundReplacer2
|
| 60 |
-
cd VideoBackgroundReplacer2
|
| 61 |
|
| 62 |
-
|
| 63 |
-
docker build -t backgroundfx-pro .
|
| 64 |
|
| 65 |
-
|
| 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.
|
| 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
|