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)
-
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
-
Install the package
pip install background-remover-model
-
Install FFmpeg (required for video processing)
- Ubuntu/Debian:
sudo apt-get install ffmpeg - macOS:
brew install ffmpeg - Windows: Download from ffmpeg.org
- Ubuntu/Debian:
-
Run the API server
uvicorn app.main:app --host 0.0.0.0 --port 8000
-
Access the API
- API: http://localhost:8000
- API Documentation: http://localhost:8000/docs
-
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
-
Clone or navigate to the project directory
-
Start the application
docker-compose up --build
-
Access the application
- Frontend: http://localhost
- Backend API: http://localhost:8000
- API Documentation: http://localhost:8000/docs
Local Development
Backend
-
Navigate to backend directory
cd backend
-
Create virtual environment
python3 -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install dependencies
pip install -r requirements.txt
-
Install FFmpeg (if not already installed)
- Ubuntu/Debian:
sudo apt-get install ffmpeg - macOS:
brew install ffmpeg - Windows: Download from ffmpeg.org
- Ubuntu/Debian:
-
Run the server
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000
Frontend
-
Navigate to frontend directory
cd frontend
-
Install dependencies
npm install -
Run development server
npm run dev
-
Access the app
- Frontend: http://localhost:5173
API Endpoints
POST /remove-image
Remove background from an uploaded image.
Request:
file(multipart/form-data): Image filemodel(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 sizealpha_matting_base_size(optional): Base size for alpha mattingbackground_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 filemodel(optional): Model to usetv(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 codebackground_image(optional): Background image file
Response:
- Processed video file (MP4)
GET /health
Health check endpoint.
Response:
{
"status": "healthy"
}
Usage
- Upload a file: Drag and drop or click to browse for an image or video
- Configure options: Select model, enable alpha matting, set background color/image
- Process: Click "Remove Background" to start processing
- View results: See side-by-side comparison of original and processed files
- 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
-
FFmpeg not found
- Ensure FFmpeg is installed in the Docker container or system
- Check Dockerfile includes FFmpeg installation
-
Out of memory
- Large videos may require more memory
- Consider processing smaller files or increasing Docker memory limits
-
Processing fails
- Check file format is supported
- Verify model files are downloaded (first run may download models)
Frontend Issues
-
CORS errors
- Update
CORS_ORIGINSin.envfile - Ensure backend is running and accessible
- Update
-
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 URLCELERY_RESULT_BACKEND: Celery result backend URL
License
This project uses the backgroundremover library. Please refer to the backgroundremover license for details.
Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d9c04dcd5a05d29a3c4649fe7e55c6607612a5818f30cf56bbb92afa5b798fb6
|
|
| MD5 |
41363c5ca208a3d2dfdbdb13ba0352b0
|
|
| BLAKE2b-256 |
bf52851272ba8fb7585947983f6adf7cc596e9bc7e242e73869b5b99110d67b6
|
File details
Details for the file background_remover_model-1.0.1-py3-none-any.whl.
File metadata
- Download URL: background_remover_model-1.0.1-py3-none-any.whl
- Upload date:
- Size: 14.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
318d7499b6dd4616c72f7ad1ed15fef8d47e9e81ca2c4b7c41beba48b3506ff1
|
|
| MD5 |
ca136489d9dc9ccca5a3da48b329faae
|
|
| BLAKE2b-256 |
96877e6996b0369767c1b7796d4b0a04397b63795a77fbf24de78a272e9a6b7f
|