DevelopmentT/background-remover
0
1---2title: AI Image Processing Server3emoji: ๐ผ๏ธ4colorFrom: indigo5colorTo: purple6sdk: docker7pinned: false8---9 10# AI Image Processing Server11 12> High-performance FastAPI backend for real-time image processing, OCR, and background removal.13 14## Features15 16- **Real-time OCR Streaming** โ WebSocket endpoint (`/ws/ocr`) streams extracted text line-by-line17- **Medical Prescription Optimized** โ OpenCV preprocessing pipeline (grayscale โ denoise โ CLAHE โ Otsu)18- **REST Fallback** โ Synchronous `/api/ocr` endpoint when WebSocket isn't available19- **Background Removal** โ `/api/remove-bg` endpoint (rembg / u2netp)20- **Production Ready** โ Singleton models, rate limiting, structured errors, CORS21 22## Architecture23 24```25main.py # FastAPI entrypoint, startup, middleware26config.py # Centralised configuration27services/28 โโโ image_processing.py # OpenCV preprocessing pipeline29 โโโ ocr_service.py # Singleton EasyOCR reader30 โโโ connection_manager.py # WebSocket connection tracking31routes/32 โโโ ws_ocr.py # WebSocket /ws/ocr endpoint33 โโโ rest.py # REST endpoints (/api/ocr, /api/remove-bg, /health)34```35 36## API Endpoints37 38### Health Check39 40GET /health41 42Response:43{44 "status": "ok"45}46 47---48 49### Remove Background50 51POST `/remove-bg`52 53- **Content-Type:** `multipart/form-data`54- **Field:** `file`55- **Supported formats:** `.png`, `.jpg`, `.jpeg`, `.webp`56- **Max size:** 3MB 57- **Rate Limit:** 15 requests per minute58 59#### Example Request (cURL)60 61```bash62curl -X POST "https://DevelopmentT-background-remover.hf.space/remove-bg" \63 -F "file=@my_image.jpg" \64 --output result.png65```66 67#### Example Request (JavaScript)68 69```javascript70const formData = new FormData();71formData.append("file", imageFile);72 73const response = await fetch("https://DevelopmentT-background-remover.hf.space/remove-bg", {74 method: "POST",75 body: formData,76});77 78const imageBlob = await response.blob();79const imageUrl = URL.createObjectURL(imageBlob);80// imageUrl is the transparent output!81```82 83---84 85## Local Setup86 87python -m venv venv88.\venv\Scripts\activate89pip install -r requirements.txt90 91# Run server92uvicorn main:app --host 0.0.0.0 --port 7860 --reload93 94# Test WebSocket (requires an image file)95python test_ws_client.py prescription.png96```97 98## Docker99 100```bash101docker build -t ai-image-server .102docker run -p 7860:7860 ai-image-server103```104 105## Environment Variables106 107| Variable | Default | Description |108|-------------|---------|--------------------------|109| `OCR_GPU` | `false` | Enable GPU for EasyOCR |110| `SPACE_HOST`| (empty) | HuggingFace Spaces host |111 112## Tech Stack113 114- **FastAPI** โ async web framework115- **EasyOCR** โ optical character recognition116- **OpenCV** โ image preprocessing117- **rembg** โ background removal118- **WebSockets** โ real-time streaming