Spaces:
Sleeping
Sleeping
| license: mit | |
| title: InspectechSegmentation | |
| sdk: gradio | |
| emoji: π | |
| colorFrom: blue | |
| # Binary Image Segmentation - FastAPI Service | |
| Professional background removal service with web interface and REST API, ready for Hugging Face Spaces deployment. | |
| ## π Quick Start | |
| ### Local Development | |
| ```bash | |
| # 1. Install dependencies | |
| pip install -r requirements.txt | |
| # 2. Download U2NETP model weights | |
| mkdir -p .model_cache | |
| wget https://github.com/xuebinqin/U-2-Net/raw/master/saved_models/u2netp/u2netp.pth -O .model_cache/u2netp.pth | |
| # 3. Run the server | |
| uvicorn app:app --host 0.0.0.0 --port 7860 | |
| # 4. Open browser | |
| # Visit: http://localhost:7860 | |
| ``` | |
| ### Test the API | |
| ```bash | |
| python test_api.py | |
| ``` | |
| ## π Project Structure | |
| ``` | |
| . | |
| βββ app.py # FastAPI application (main entry point) | |
| βββ binary_segmentation.py # Core segmentation module | |
| βββ requirements.txt # Python dependencies | |
| βββ Dockerfile # Docker configuration for deployment | |
| βββ README_HF.md # Hugging Face Space README | |
| βββ DEPLOYMENT.md # Detailed deployment guide | |
| βββ client_examples.py # API usage examples (Python, JS, curl) | |
| βββ test_api.py # Test script | |
| βββ .gitignore # Git ignore file | |
| βββ static/ | |
| βββ index.html # Web interface | |
| ``` | |
| ## π¨ Features | |
| ### Web Interface | |
| - Drag & drop image upload | |
| - 3 AI model options (U2NETP, BiRefNet, RMBG) | |
| - Adjustable threshold | |
| - Multiple output formats (transparent PNG, binary mask, or both) | |
| - Real-time preview | |
| - Download results | |
| ### REST API | |
| - **POST /segment** - Segment image β transparent PNG | |
| - **POST /segment/mask** - Get binary mask only | |
| - **POST /segment/base64** - Get base64 encoded results | |
| - **POST /segment/batch** - Process multiple images | |
| - **GET /models** - List available models | |
| - **GET /health** - Health check | |
| ### Supported Models | |
| | Model | Speed | Accuracy | Size | Best For | | |
| |-------|-------|----------|------|----------| | |
| | **U2NETP** | β‘β‘β‘ | ββ | 4.7 MB | Speed, simple objects | | |
| | **BiRefNet** | β‘ | βββ | ~400 MB | Best quality | | |
| | **RMBG** | β‘β‘ | βββ | ~200 MB | Balanced | | |
| ## π§ API Usage Examples | |
| ### Python | |
| ```python | |
| import requests | |
| # Segment image | |
| with open('input.jpg', 'rb') as f: | |
| response = requests.post( | |
| 'http://localhost:7860/segment', | |
| files={'file': f}, | |
| data={'model': 'u2netp', 'threshold': 0.5} | |
| ) | |
| # Save result | |
| with open('output.png', 'wb') as out: | |
| out.write(response.content) | |
| ``` | |
| ### JavaScript | |
| ```javascript | |
| async function removeBackground(file) { | |
| const formData = new FormData(); | |
| formData.append('file', file); | |
| formData.append('model', 'u2netp'); | |
| formData.append('threshold', '0.5'); | |
| const response = await fetch('/segment', { | |
| method: 'POST', | |
| body: formData | |
| }); | |
| const blob = await response.blob(); | |
| return URL.createObjectURL(blob); | |
| } | |
| ``` | |
| ### cURL | |
| ```bash | |
| curl -X POST "http://localhost:7860/segment" \ | |
| -F "file=@input.jpg" \ | |
| -F "model=u2netp" \ | |
| -F "threshold=0.5" \ | |
| --output result.png | |
| ``` | |
| See `client_examples.py` for more! | |
| ## π Deploy to Hugging Face Spaces | |
| See `DEPLOYMENT.md` for complete guide! | |
| ## π License | |
| Apache 2.0 | |
| ## π Credits | |
| - U2-Net, BiRefNet, RMBG models | |
| - FastAPI framework |