A newer version of the Streamlit SDK is available:
1.52.1
metadata
title: π¬ BackgroundFX Pro - SAM2 + MatAnyone
emoji: π₯
colorFrom: indigo
colorTo: purple
sdk: streamlit
sdk_version: 1.32.0
app_file: streamlit_app.py
pinned: false
license: mit
tags:
- video
- background-removal
- segmentation
- matting
- SAM2
- MatAnyone
π¬ BackgroundFX Pro β Professional Video Background Replacement
BackgroundFX Pro is a GPU-accelerated app for Hugging Face Spaces (Docker) that replaces video backgrounds using:
- SAM2 β high-quality object segmentation
- MatAnyone β temporal video matting for stable alpha over time
Built on: CUDA 12.1.1, PyTorch 2.5.1 (cu121), torchvision 0.20.1, Streamlit 1.49.1.
β¨ Features
- Replace backgrounds with: solid color, AI-generated image (procedural), custom uploaded image, or professional backgrounds
- Optimized for T4 GPUs on Hugging Face
- Two-stage pipeline: SAM2 segmentation β MatAnyone refinement β compositing
- Caching & logs stored in the repo volume:
- HF cache β
./.hf - Torch cache β
./.torch - App data & logs β
./data(seedata/run.log)
- HF cache β
- FFmpeg β video format conversion and frame extraction
π Try It
Open the Space in your browser (GPU required):
https://huggingface.co/spaces/MogensR/VideoBackgroundReplacer2
π±οΈ How to Use
- Upload a video (
.mp4,.mov,.avi,.mkv). - Choose a Background Type: Image, Color, Blur, Professional Backgrounds, or AI Generated.
- If using custom background, upload your image or select from professional options.
- Click π Process Video.
- Preview and πΎ Download Result.
Tip: Start with 720p/1080p on T4; 4K can exceed memory limits.
ποΈ Project Structure (key files)
Dockerfileβ CUDA 12.1.1 + PyTorch 2.5.1 containerrequirements.txtβ Python dependenciesapp.pyβ Main Streamlit applicationintegrated_pipeline.pyβ Two-stage processing pipelinemodels/sam2_loader.pyβ SAM2 model loader with HF Hub integrationmodels/matanyone_loader.pyβ MatAnyone model loaderutils/β Utility functionsdata/β Created at runtime for logs/outputstmp/β Created at runtime for processing jobs -video_pipeline.pyβ Core video processing logic (SAM2 + MatAnyone integration)video_pipeline.pyβ Core video processing logic (SAM2 + MatAnyone integration)
βοΈ Runtime Notes
- Binds to
PORT/STREAMLIT_SERVER_PORT(defaults to 7860) - File upload limit: 200MB via
--server.maxUploadSize=200 - CORS disabled for Docker compatibility:
--server.enableCORS=false - Memory management with automatic cleanup between stages
- If processing fails, check Space logs for detailed error information
π§ͺ Local Development (Docker)
Requires an NVIDIA GPU with CUDA drivers.
git clone https://huggingface.co/spaces/MogensR/VideoBackgroundReplacer2
cd VideoBackgroundReplacer2
# Build (Ubuntu 22.04, CUDA 12.1.1; installs Torch 2.5.1+cu121)
docker build -t backgroundfx-pro .
# Run
docker run --gpus all -p 7860:7860 backgroundfx-pro
Access at: http://localhost:7860
π§ Technical Details
Pipeline Architecture
- Stage 1: SAM2 generates object masks using click points
- Stage 2: MatAnyone refines masks for temporal consistency
- Stage 3: Composite foreground with new background
Model Loading
- SAM2 models downloaded from Hugging Face Hub automatically
- Supports small/base/large variants (small recommended for T4)
- MatAnyone loaded from official repository
Performance Optimizations
- T4-specific optimizations (fp16, channels_last)
- Memory pruning during long video processing
- Automatic model unloading between stages
π License
MIT License - See LICENSE file for details.