mlbench123's picture
Update README.md
030bf29 verified

A newer version of the Gradio SDK is available: 6.6.0

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

# 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

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

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

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

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