DevelopmentT/background-remover
0
AI Image Processing Server
High-performance FastAPI backend for real-time image processing, OCR, and background removal.
Features
- Real-time OCR Streaming – WebSocket endpoint (
/ws/ocr) streams extracted text line-by-line - Medical Prescription Optimized – OpenCV preprocessing pipeline (grayscale → denoise → CLAHE → Otsu)
- REST Fallback – Synchronous
/api/ocrendpoint when WebSocket isn't available - Background Removal –
/api/remove-bgendpoint (rembg / u2netp) - Production Ready – Singleton models, rate limiting, structured errors, CORS
Architecture
main.py # FastAPI entrypoint, startup, middleware
config.py # Centralised configuration
services/
├── image_processing.py # OpenCV preprocessing pipeline
├── ocr_service.py # Singleton EasyOCR reader
└── connection_manager.py # WebSocket connection tracking
routes/
├── ws_ocr.py # WebSocket /ws/ocr endpoint
└── rest.py # REST endpoints (/api/ocr, /api/remove-bg, /health)API Endpoints
Health Check
GET /health
Response: { "status": "ok" }
Remove Background
POST /remove-bg
- Content-Type:
multipart/form-data - Field:
file - Supported formats:
.png,.jpg,.jpeg,.webp - Max size: 3MB
- Rate Limit: 15 requests per minute
Example Request (cURL)
curl -X POST "https://DevelopmentT-background-remover.hf.space/remove-bg" \
-F "file=@my_image.jpg" \
--output result.pngExample Request (JavaScript)
const formData = new FormData();
formData.append("file", imageFile);
const response = await fetch("https://DevelopmentT-background-remover.hf.space/remove-bg", {
method: "POST",
body: formData,
});
const imageBlob = await response.blob();
const imageUrl = URL.createObjectURL(imageBlob);
// imageUrl is the transparent output!Local Setup
python -m venv venv .\venv\Scripts\activate pip install -r requirements.txt
Run server
uvicorn main:app --host 0.0.0.0 --port 7860 --reload
Test WebSocket (requires an image file)
python testwsclient.py prescription.png
## Docker
docker build -t ai-image-server . docker run -p 7860:7860 ai-image-server
## Environment Variables
| Variable | Default | Description |
|-------------|---------|--------------------------|
| `OCR_GPU` | `false` | Enable GPU for EasyOCR |
| `SPACE_HOST`| (empty) | HuggingFace Spaces host |
## Tech Stack
- **FastAPI** – async web framework
- **EasyOCR** – optical character recognition
- **OpenCV** – image preprocessing
- **rembg** – background removal
- **WebSockets** – real-time streaming