rmbg-api
v0.0.1
Published
API server for removing backgrounds from images
Readme
rmbg-api
REST API server for removing backgrounds from images using AI models.
Features
- 🚀 Fast background removal using ONNX Runtime
- 🔒 Privacy-focused - all processing done server-side (no third-party uploads)
- 📦 Multiple model support (u2netp, modnet, briaai, and more)
- 🐳 Docker support for easy deployment
- 📊 Health check and model listing endpoints
Quick Start
Using Docker
# Build the Docker image
docker build -t rmbg-api -f packages/api/Dockerfile .
# Run the container
docker run -p 3000:3000 rmbg-apiUsing Node.js
# Install dependencies
pnpm install
# Build the API
pnpm --filter rmbg-api build
# Start the server
pnpm --filter rmbg-api start
# Or run in development mode
pnpm --filter rmbg-api devAPI Endpoints
Health Check
GET /healthResponse:
{
"status": "ok",
"timestamp": "2024-01-01T00:00:00.000Z"
}List Models
GET /modelsResponse:
{
"models": [
{
"name": "u2netp",
"resolution": 320,
"size": 4574861
},
...
]
}Remove Background
POST /remove-background
Content-Type: multipart/form-data
image: [file]
model: "u2netp" (optional, default: "u2netp")
maxResolution: 2048 (optional, default: 2048)Example using cURL:
curl -X POST http://localhost:3000/remove-background \
-F "[email protected]" \
-F "model=u2netp" \
-F "maxResolution=2048" \
--output output.pngExample using fetch:
const formData = new FormData()
formData.append('image', file)
formData.append('model', 'u2netp')
formData.append('maxResolution', '2048')
const response = await fetch('http://localhost:3000/remove-background', {
method: 'POST',
body: formData
})
const blob = await response.blob()Available Models
u2netp- 320px, 4.5MB (fastest, default)modnet- 512px, 25MB (medium quality)briaai- 1024px, 44MB (highest quality)isnet-anime- 1024px, 168MB (anime-optimized)silueta- 320px, 43MB (specialized)u2net-cloth- 768px, 170MB (clothing-focused)
Environment Variables
PORT- Server port (default: 3000)NODE_ENV- Environment mode (production/development)
Error Handling
The API returns appropriate HTTP status codes and JSON error messages:
400- Bad request (invalid model, missing file, etc.)413- File too large (max 10MB)500- Internal server error
Example error response:
{
"error": "Invalid model",
"message": "Model \"invalid\" not found. Available models: u2netp, modnet, briaai"
}Docker Deployment
Using docker-compose
Create docker-compose.yml:
version: '3.8'
services:
api:
build:
context: .
dockerfile: packages/api/Dockerfile
ports:
- "3000:3000"
environment:
- PORT=3000
- NODE_ENV=production
restart: unless-stoppedRun:
docker-compose up -dProduction Considerations
- Resource Limits: Set appropriate CPU/memory limits
- Load Balancing: Use multiple instances behind a load balancer
- Caching: Consider caching model downloads
- Monitoring: Implement logging and monitoring solutions
- Rate Limiting: Add rate limiting middleware for production use
License
See root LICENSE file.
