File size: 3,385 Bytes
030bf29
 
 
 
 
 
 
492772b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
030bf29
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
---

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