--- 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