Skip to main content

A FastAPI-based API for removing backgrounds from images and videos using backgroundremover

Project description

Background Remover - Full Stack Application

A production-ready full-stack application for removing backgrounds from images and videos using the backgroundremover library.

Installation

Prerequisites

  • Python 3.10 or higher
  • FFmpeg (for video processing)

Note: FFmpeg must be installed separately on your system:

  • Ubuntu/Debian: sudo apt-get install ffmpeg
  • macOS: brew install ffmpeg
  • Windows: Download from ffmpeg.org

Install from PyPI

The recommended way to install the Background Remover Model is using a virtual environment:

Step 1: Install Python venv (if not already installed)

Ubuntu/Debian:

sudo apt install python3-venv -y

macOS:

# venv is usually pre-installed with Python
python3 --version

Windows:

# venv is usually pre-installed with Python
python --version

Step 2: Create Virtual Environment

python3 -m venv bgremover-env

Step 3: Activate Virtual Environment

Linux/macOS:

source bgremover-env/bin/activate

Windows:

bgremover-env\Scripts\activate

Step 4: Install the Package

pip install background-remover-model

This will install the package and all its dependencies. After installation, you can import and use the API in your Python projects.

Step 5: Deactivate Virtual Environment (when done)

deactivate

Note: Always activate your virtual environment before using the package. You can run Python scripts inside the activated environment.

Quick Start (After Installation)

Important: Make sure your virtual environment is activated before running the server:

# Activate virtual environment (if not already activated)
source bgremover-env/bin/activate  # Linux/macOS
# or
bgremover-env\Scripts\activate      # Windows

Once installed and activated, you can run the API server:

# Run the FastAPI server
uvicorn app.main:app --host 0.0.0.0 --port 8000

Or use it as a Python package in your code:

from app.main import app
from app.routers import image_router, video_router

# The FastAPI app is ready to use
# Access API docs at http://localhost:8000/docs

Features

  • ๐Ÿ–ผ๏ธ Image Background Removal: Remove backgrounds from images with various models
  • ๐ŸŽฅ Video Background Removal: Process videos to remove backgrounds
  • ๐ŸŽจ Advanced Options: Alpha matting, custom background colors, background images
  • ๐Ÿ“Š Real-time Processing: Visual feedback during processing
  • ๐ŸŽฏ Before/After Preview: Side-by-side comparison of original and processed files
  • ๐Ÿ“ฅ Easy Download: One-click download of processed files

Tech Stack

Backend

  • Python 3.10+
  • FastAPI
  • backgroundremover
  • PyTorch (CPU)
  • FFmpeg
  • Celery (optional for async processing)
  • Redis (optional, for Celery)

Frontend

  • React 18
  • Vite
  • TailwindCSS
  • Axios

Project Structure

.
โ”œโ”€โ”€ backend/
โ”‚   โ”œโ”€โ”€ app/
โ”‚   โ”‚   โ”œโ”€โ”€ main.py              # FastAPI application
โ”‚   โ”‚   โ”œโ”€โ”€ routers/             # API route handlers
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ image_router.py
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ video_router.py
โ”‚   โ”‚   โ”œโ”€โ”€ services/            # Business logic
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ background_remover.py
โ”‚   โ”‚   โ”œโ”€โ”€ utils/               # Utility functions
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ file_cleanup.py
โ”‚   โ”‚   โ””โ”€โ”€ models/              # Pydantic models
โ”‚   โ”‚       โ””โ”€โ”€ schemas.py
โ”‚   โ”œโ”€โ”€ requirements.txt
โ”‚   โ””โ”€โ”€ Dockerfile
โ”œโ”€โ”€ frontend/
โ”‚   โ”œโ”€โ”€ src/
โ”‚   โ”‚   โ”œโ”€โ”€ components/          # React components
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ FileUploader.jsx
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ OptionsPanel.jsx
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ ResultViewer.jsx
โ”‚   โ”‚   โ”œโ”€โ”€ App.jsx
โ”‚   โ”‚   โ”œโ”€โ”€ main.jsx
โ”‚   โ”‚   โ””โ”€โ”€ index.css
โ”‚   โ”œโ”€โ”€ package.json
โ”‚   โ”œโ”€โ”€ vite.config.js
โ”‚   โ”œโ”€โ”€ tailwind.config.js
โ”‚   โ”œโ”€โ”€ nginx.conf
โ”‚   โ””โ”€โ”€ Dockerfile
โ”œโ”€โ”€ docker-compose.yml
โ”œโ”€โ”€ .env.example
โ””โ”€โ”€ README.md

Quick Start

Option 1: Install from PyPI (Recommended for API Usage)

  1. Set up virtual environment (see Installation section above)

    # Install venv if needed
    sudo apt install python3-venv -y  # Ubuntu/Debian
    
    # Create and activate virtual environment
    python3 -m venv bgremover-env
    source bgremover-env/bin/activate
    
  2. Install the package

    pip install background-remover-model
    
  3. Install FFmpeg (required for video processing)

    • Ubuntu/Debian: sudo apt-get install ffmpeg
    • macOS: brew install ffmpeg
    • Windows: Download from ffmpeg.org
  4. Run the API server

    uvicorn app.main:app --host 0.0.0.0 --port 8000
    
  5. Access the API

  6. Deactivate virtual environment (when done)

    deactivate
    

Option 2: Using Docker (Full Stack Application)

Prerequisites

  • Docker and Docker Compose
  • (Optional) Node.js 18+ and Python 3.10+ for local development
  1. Clone or navigate to the project directory

  2. Start the application

    docker-compose up --build
    
  3. Access the application

Local Development

Backend

  1. Navigate to backend directory

    cd backend
    
  2. Create virtual environment

    python3 -m venv venv
    source venv/bin/activate  # On Windows: venv\Scripts\activate
    
  3. Install dependencies

    pip install -r requirements.txt
    
  4. Install FFmpeg (if not already installed)

    • Ubuntu/Debian: sudo apt-get install ffmpeg
    • macOS: brew install ffmpeg
    • Windows: Download from ffmpeg.org
  5. Run the server

    uvicorn app.main:app --reload --host 0.0.0.0 --port 8000
    

Frontend

  1. Navigate to frontend directory

    cd frontend
    
  2. Install dependencies

    npm install
    
  3. Run development server

    npm run dev
    
  4. Access the app

API Endpoints

POST /remove-image

Remove background from an uploaded image.

Request:

  • file (multipart/form-data): Image file
  • model (optional): Model to use (u2net, u2netp, u2net_human_seg)
  • alpha_matting (optional): Enable alpha matting (boolean)
  • alpha_matting_foreground_threshold (optional): Foreground threshold (0-255)
  • alpha_matting_background_threshold (optional): Background threshold (0-255)
  • alpha_matting_erode_structure_size (optional): Erode structure size
  • alpha_matting_base_size (optional): Base size for alpha matting
  • background_color (optional): Hex color code (e.g., "#FF0000")
  • background_image (optional): Background image file

Response:

  • Processed image file (PNG)

POST /remove-video

Remove background from an uploaded video.

Request:

  • file (multipart/form-data): Video file
  • model (optional): Model to use
  • tv (optional): TV mode flag (boolean)
  • mk (optional): Masks only flag (boolean)
  • tov (optional): Transparent output video flag (boolean)
  • toi (optional): Transparent output images flag (boolean)
  • gb (optional): Green background flag (boolean)
  • wn (optional): White background flag (boolean)
  • fr (optional): Frame rate (float)
  • fl (optional): Frame limit (float)
  • background_color (optional): Hex color code
  • background_image (optional): Background image file

Response:

  • Processed video file (MP4)

GET /health

Health check endpoint.

Response:

{
  "status": "healthy"
}

Usage

  1. Upload a file: Drag and drop or click to browse for an image or video
  2. Configure options: Select model, enable alpha matting, set background color/image
  3. Process: Click "Remove Background" to start processing
  4. View results: See side-by-side comparison of original and processed files
  5. Download: Click "Download Result" to save the processed file

Models

  • u2net: Default model, good general-purpose performance
  • u2netp: Lightweight model, faster processing
  • u2net_human_seg: Optimized for human segmentation

Advanced Options

Alpha Matting

Improves edge quality for better results, especially for fine details like hair.

Background Replacement

  • Background Color: Replace background with a solid color (hex format)
  • Background Image: Replace background with another image

Video Options

  • TV Mode (-tv): Optimize for TV/video content
  • Masks Only (-mk): Generate masks only
  • Transparent Output: Create transparent backgrounds
  • Frame Rate/Limit: Control video processing parameters

File Cleanup

The application automatically cleans up temporary files older than 24 hours. You can manually trigger cleanup by calling the cleanup service.

Troubleshooting

Backend Issues

  1. FFmpeg not found

    • Ensure FFmpeg is installed in the Docker container or system
    • Check Dockerfile includes FFmpeg installation
  2. Out of memory

    • Large videos may require more memory
    • Consider processing smaller files or increasing Docker memory limits
  3. Processing fails

    • Check file format is supported
    • Verify model files are downloaded (first run may download models)

Frontend Issues

  1. CORS errors

    • Update CORS_ORIGINS in .env file
    • Ensure backend is running and accessible
  2. Upload fails

    • Check file size limits (configured in nginx.conf)
    • Verify backend is running

Development Scripts

Backend

# Start backend
cd backend
uvicorn app.main:app --reload

# Run cleanup
python -m app.utils.file_cleanup

Frontend

# Start dev server
cd frontend
npm run dev

# Build for production
npm run build

# Preview production build
npm run preview

Docker Commands

# Build and start all services
docker-compose up --build

# Start in background
docker-compose up -d

# View logs
docker-compose logs -f

# Stop services
docker-compose down

# Rebuild specific service
docker-compose build backend
docker-compose up -d backend

Environment Variables

Copy .env.example to .env and configure:

  • CORS_ORIGINS: Allowed CORS origins (comma-separated)
  • REDIS_URL: Redis connection URL (if using Celery)
  • CELERY_BROKER_URL: Celery broker URL
  • CELERY_RESULT_BACKEND: Celery result backend URL

License

This project uses the backgroundremover library. Please refer to the backgroundremover license for details.

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Submit a pull request

Support

For issues related to:

  • backgroundremover library: GitHub Issues
  • This application: Open an issue in this repository

Acknowledgments

  • backgroundremover - The core library for background removal
  • FastAPI - Modern Python web framework
  • React & Vite - Frontend framework and build tool

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

background_remover_model-1.0.1.tar.gz (16.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

background_remover_model-1.0.1-py3-none-any.whl (14.2 kB view details)

Uploaded Python 3

File details

Details for the file background_remover_model-1.0.1.tar.gz.

File metadata

  • Download URL: background_remover_model-1.0.1.tar.gz
  • Upload date:
  • Size: 16.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for background_remover_model-1.0.1.tar.gz
Algorithm Hash digest
SHA256 d9c04dcd5a05d29a3c4649fe7e55c6607612a5818f30cf56bbb92afa5b798fb6
MD5 41363c5ca208a3d2dfdbdb13ba0352b0
BLAKE2b-256 bf52851272ba8fb7585947983f6adf7cc596e9bc7e242e73869b5b99110d67b6

See more details on using hashes here.

File details

Details for the file background_remover_model-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for background_remover_model-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 318d7499b6dd4616c72f7ad1ed15fef8d47e9e81ca2c4b7c41beba48b3506ff1
MD5 ca136489d9dc9ccca5a3da48b329faae
BLAKE2b-256 96877e6996b0369767c1b7796d4b0a04397b63795a77fbf24de78a272e9a6b7f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page