This project has been refactored with a modern, modular architecture that separates concerns and improves maintainability, testability, and scalability.
removals-background/
├── app/ # Main application package
│ ├── __init__.py # Package initialization
│ ├── main.py # FastAPI application entry point
│ ├── config.py # Configuration management
│ ├── api/ # API layer
│ │ ├── __init__.py
│ │ └── routes.py # API route handlers
│ ├── core/ # Core business logic
│ │ ├── __init__.py
│ │ └── model_manager.py # AI model lifecycle management
│ ├── services/ # Business services
│ │ ├── __init__.py
│ │ ├── background_removal.py # Main background removal service
│ │ └── image_utils.py # Image processing utilities
│ ├── models/ # AI model architectures
│ │ ├── __init__.py
│ │ └── u2net.py # U²-Net model definition
│ └── utils/ # Utility modules
│ ├── __init__.py
│ ├── logger.py # Logging configuration
│ ├── exceptions.py # Custom exceptions
│ └── download_model.py # Model download utility
├── client/ # Frontend (unchanged)
│ ├── index.html
│ ├── script.js
│ └── styles.css
├── models/ # Model weights directory
│ └── u2net.pth # Pre-trained model weights
├── main.py # Application entry point
├── requirements.txt # Python dependencies
├── .env.example # Environment variables template
├── start.bat # Windows startup script
├── start.sh # Linux/Mac startup script
└── README.md # Project documentation
- Purpose: Handle HTTP requests and responses
- Responsibilities:
- Request validation
- Response formatting
- Error handling
- Route definitions
Key Files:
routes.py: Contains all API endpoints
- Purpose: Business logic and domain services
- Responsibilities:
- Image processing orchestration
- Background removal logic
- Image utilities
Key Files:
background_removal.py: Main service for removing backgroundsimage_utils.py: Helper functions for image manipulation
- Purpose: Core infrastructure and model management
- Responsibilities:
- Model loading and lifecycle
- Device management (CPU/GPU)
- Model optimization
Key Files:
model_manager.py: Manages AI model loading and inference
- Purpose: AI model architectures
- Responsibilities:
- Model definitions
- Neural network architectures
Key Files:
u2net.py: U²-Net model implementation
- Purpose: Shared utilities
- Responsibilities:
- Logging configuration
- Custom exceptions
- Helper functions
- Centralized configuration using
pydantic-settings - Environment variable support
- Type-safe configuration
- Easy to override for different environments
- Multi-scale inference: Processes image at multiple scales and averages results for better quality
- Mask smoothing: Applies Gaussian smoothing for cleaner edges
- Edge refinement: Uses image gradients to refine mask boundaries
- Adaptive resizing: Maintains aspect ratio while optimizing for model input
- Custom exception hierarchy
- Proper HTTP status codes
- Detailed error messages
- Comprehensive logging
- Automatic device detection (CPU/GPU)
- Model compilation for faster inference (PyTorch 2.0+)
- Better error handling for model loading
- Model information endpoints
- Separation of concerns
- Single Responsibility Principle
- Dependency injection ready
- Easy to test and extend
Configuration is managed through app/config.py using environment variables. See .env.example for all available options.
MODEL_TYPE: Choose between "u2net" (full) or "u2netp" (portable)DEVICE: "auto" (detect), "cpu", or "cuda"USE_MULTI_SCALE: Enable multi-scale inference for better qualityMASK_SMOOTHING: Apply Gaussian smoothing to masksEDGE_REFINEMENT: Use edge detection to refine mask boundaries
Health check endpoint
Detailed health check with model information
Remove background from uploaded image
- Input: multipart/form-data with image file
- Output: PNG image with transparent background
Same as /remove-background (for preview purposes)
- Adaptive image resizing maintaining aspect ratio
- Proper normalization for model input
- Support for large images (with size limits)
- Multi-scale inference (3 scales: 0.75x, 1.0x, 1.25x)
- Averaged predictions for better quality
- Optimized tensor operations
- Mask quality enhancement (morphological operations)
- Gaussian smoothing for cleaner edges
- Edge refinement using image gradients
- High-quality upsampling (LANCZOS4)
- Model Compilation: Uses
torch.compilewhen available (PyTorch 2.0+) - GPU Support: Automatic CUDA detection and usage
- Efficient Resizing: Maintains aspect ratio to reduce artifacts
- Optimized Inference: Multi-scale inference can be disabled for speed
- Add model architecture to
app/models/ - Update
model_manager.pyto support the new model - Update configuration in
app/config.py
- Add route handlers to
app/api/routes.py - Create service methods in
app/services/if needed - Update API documentation
- Add utilities to
app/services/image_utils.py - Integrate into
background_removal.pyservice - Add configuration options if needed
The modular architecture makes testing easier:
- Unit tests for services
- Integration tests for API endpoints
- Mock model for faster testing
The architecture supports:
- Docker containerization
- Environment-based configuration
- Horizontal scaling (stateless design)
- Health check endpoints for load balancers
The old service/ directory structure is maintained for backward compatibility, but new code uses the app/ structure. The model weights location has changed from service/models/ to models/ at the root level.