ClearBG is a production-ready web application for high-quality background removal using the MODNet deep learning model. It is optimized for portrait matting, preserving fine details like hair even on complex backgrounds. It now includes advanced refinement tools like brush editing and a precise color removal tool.
- AI-Powered Matting: Uses MODNet for professional-grade transparency.
- Advanced Refinement Tools: Use brush tools to manually restore or remove details, or the color removal tool to instantly drop out specific background colors.
- Fast Performance: Optimized for CPU inference with intelligent image scaling.
- Premium UI: Modern glassmorphism design with instantaneous previews built with React and Vite.
- Asynchronous Backend: Built with FastAPI for efficient request handling.
clearbg/
├── frontend/ # React & Vite frontend
│ ├── src/ # React components and hooks
│ ├── package.json
│ └── vite.config.js # Vite configuration with proxy to backend
├── pretrained/ # Model weights (auto-downloaded)
├── scripts/
│ └── download_weights.py # Weight downloader utility
├── src/
│ ├── models/
│ │ └── modnet.py # MODNet architecture
│ ├── inference.py # AI processing & color removal logic
│ └── main.py # FastAPI application
├── requirements.txt # Python dependencies
└── README.md
- Python 3.9+
- Node.js & npm
Install both Python and Node.js dependencies using the main installation script:
npm run install-all(Alternatively, you can manually run pip install -r requirements.txt at the root and npm install inside the frontend/ directory).
Run the following script to fetch the pretrained MODNet weights (approx. 50MB):
python scripts/download_weights.pyYou will need two terminals running simultaneously for full-stack functionality:
Terminal 1 (Backend - FastAPI):
python src/main.pyTerminal 2 (Frontend - React/Vite):
npm run devOpen your browser and navigate to the frontend dev server URL (typically http://localhost:5173).
- Model: MODNet (MobileNetV2 backbone).
- Inference: PyTorch (CPU mode by default).
- Optimization: Input images are intelligently resized to a 512px height/width (maintaining aspect ratio) for inference, and the resulting alpha mask is upscaled back to original dimensions for the final output. This ensures fast processing while maintaining output quality.
- Color Removal: Leverages HSV masking and color space transformations to target specific hues cleanly with adjustable tolerance.