CoolFace
Apppublic

MCP-1st-Birthday/VideoBackgroundReplacer2

sourceHugging Facemitupdated 1y agoView on Hugging Face
0likes
App README

๐ŸŽฌ 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 (see data/run.log)
  • โ€”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

  1. 1.Upload a video (.mp4, .mov, .avi, .mkv).
  2. 2.Choose a Background Type: Image, Color, Blur, Professional Backgrounds, or AI Generated.
  3. 3.If using custom background, upload your image or select from professional options.
  4. 4.Click ๐Ÿš€ Process Video.
  5. 5.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 container
  • โ€”requirements.txt โ€” Python dependencies
  • โ€”app.py โ€” Main Streamlit application
  • โ€”integrated_pipeline.py โ€” Two-stage processing pipeline
  • โ€”models/sam2_loader.py โ€” SAM2 model loader with HF Hub integration
  • โ€”models/matanyone_loader.py โ€” MatAnyone model loader
  • โ€”utils/ โ€” Utility functions
  • โ€”data/ โ€” Created at runtime for logs/outputs
  • โ€”tmp/ โ€” 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.

bash
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

  1. 1.Stage 1: SAM2 generates object masks using click points
  2. 2.Stage 2: MatAnyone refines masks for temporal consistency
  3. 3.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.