π A FastAPI service that removes backgrounds from images using deep learning models, optimized for CPU and low-memory environments.
- High-quality background removal using CarveKit
- FastAPI-based REST API
- Docker support
- CPU-optimized processing
- Batch processing support
- Easy integration
This API uses CarveKit's Tracer-B7 model which provides:
- 90% accuracy (mean F1-Score, DUTS-TE)
- Optimized for general objects (people, animals, products etc.)
- CPU-friendly processing
- High-quality edge detection
| Networks | Target | Accuracy |
|---|---|---|
| Tracer-B7 (default) | General (objects, animals, etc) | 90% (mean F1-Score, DUTS-TE) |
| U^2-net | Hairs (hairs, people, animals, objects) | 80.4% (mean F1-Score, DUTS-TE) |
| BASNet | General (people, objects) | 80.3% (mean F1-Score, DUTS-TE) |
| DeepLabV3 | People, Animals, Cars, etc | 67.4% (mean IoU, COCO val2017) |
Note: This API currently uses Tracer-B7 as it provides the best general-purpose performance.
- Python 3.10+
- Docker (optional)
- Clone the repository
git clone <repository-url>
cd image-processing-cpu- Create virtual environment
python -m venv .venv
source .venv/bin/activate # Linux/Mac
# or
.venv\Scripts\activate # Windows- Install dependencies
pip install -r requirements.txt- Setup environment variables
cp .env.example .env- Run the application
python main.py- Build the image
docker build -t background-remover .- Run the container
docker run -p 8000:8000 background-removerGET /POST /remove-bg
Content-Type: multipart/form-data
file: <image-file>Example using curl:
curl -X POST -F "[email protected]" http://localhost:8000/remove-bgThe service uses CarveKit with the following optimized settings:
- Segmentation Network: Tracer-B7
- Segmentation Mask Size: 640
- Trimap Parameters:
- Dilation: 30
- Erosion: 5
- Post-processing: FBA Matting
- Device: CPU optimized
.
βββ app/
β βββ api/
β β βββ __init__.py
β β βββ routes.py
β βββ core/
β β βββ __init__.py
β β βββ config.py
β βββ services/
β β βββ __init__.py
β β βββ background_remover.py
β βββ utils/
β βββ __init__.py
β βββ file_handler.py
βββ script/
β βββ download-weights
βββ .env.example
βββ .gitignore
βββ Dockerfile
βββ README.md
βββ RemoveBackground.py
βββ main.py
βββ requirements.txt
| Variable | Description | Default |
|---|---|---|
| API_HOST | API host address | 0.0.0.0 |
| API_PORT | API port number | 8000 |
| TEMP_DIR | Temporary files directory | temp |
| OUTPUT_DIR | Output files directory | output |
| CLOUDINARY_CLOUD_NAME | Cloudinary cloud name | - |
| CLOUDINARY_API_KEY | Cloudinary API key | - |
| CLOUDINARY_API_SECRET | Cloudinary API secret | - |
- Create and activate virtual environment
- Install dependencies
- Copy .env.example to .env and adjust as needed
- Run the application in development mode:
uvicorn main:app --reloadFor production deployment, it's recommended to:
- Use Docker
- Set appropriate environment variables
- Use a reverse proxy (e.g., Nginx)
- Implement proper security measures
This project uses CarveKit for background removal.