A fast background removal service written in Rust, using the U2Net model and ONNX Runtime for CPU inference.
- Fast background removal using U2Net model
- CPU-based inference using ONNX Runtime
- RESTful API endpoint for image processing
- Docker support for easy deployment
- Handles images of any size while maintaining aspect ratio
- Returns PNG images with transparency
- Rust 1.70 or higher
- Docker (optional, for containerized deployment)
- wget or curl (for downloading the model)
- Clone the repository:
git clone https://github.com/yourusername/rembg-cpu-rust
cd rembg-cpu-rust- Download the U2Net model:
./scripts/download-model.sh- Build and run locally:
cargo run --releaseThe server will start at http://localhost:8000
Build and run using Docker:
docker build -t rembg-cpu-rust .
docker run -p 8000:8000 rembg-cpu-rustRemove background from an image:
curl -X POST -F "image=@/path/to/your/image.jpg" http://localhost:8000/api/rem-bg -o output.pngOr use the provided api.http file with REST Client extensions in VS Code/IntelliJ.
- Default port: 8000
- Max file size: 10MB
- Model path:
models/u2net.onnx
├── src/
│ ├── application/ # Application logic
│ ├── domain/ # Domain models and errors
│ ├── infrastructure/ # Server setup and configuration
│ └── presentation/ # API handlers
├── models/ # Model storage
├── scripts/ # Utility scripts
└── Dockerfile # Docker configuration
- Images are processed at 320x320 resolution for inference
- Original image dimensions are preserved in the output
- CPU-optimized for broad compatibility
The service handles various error cases:
- Invalid image format
- Missing image file
- Model loading errors
- Processing errors
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- U2Net - Original U2Net model
- ONNX Runtime - Machine learning inference engine
- Axum - Web framework