Spaces:
Sleeping
Sleeping
Update README.md
Browse files
README.md
CHANGED
|
@@ -1,326 +1,10 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
- **CORS Enabled**: Ready for cross-origin requests from Flutter app
|
| 12 |
-
|
| 13 |
-
## Prerequisites
|
| 14 |
-
|
| 15 |
-
- Python 3.8 or higher
|
| 16 |
-
- CUDA-capable GPU (optional, for faster processing)
|
| 17 |
-
- At least 8GB RAM
|
| 18 |
-
- 5GB free disk space (for SAM model)
|
| 19 |
-
|
| 20 |
-
## Installation
|
| 21 |
-
|
| 22 |
-
### 1. Clone or Navigate to Backend Directory
|
| 23 |
-
|
| 24 |
-
```bash
|
| 25 |
-
cd /media/aliroohan/hello/MAD/project/backend
|
| 26 |
-
```
|
| 27 |
-
|
| 28 |
-
### 2. Create Virtual Environment
|
| 29 |
-
|
| 30 |
-
```bash
|
| 31 |
-
python3 -m venv venv
|
| 32 |
-
source venv/bin/activate # On Windows: venv\Scripts\activate
|
| 33 |
-
```
|
| 34 |
-
|
| 35 |
-
### 3. Install Dependencies
|
| 36 |
-
|
| 37 |
-
```bash
|
| 38 |
-
pip install --upgrade pip
|
| 39 |
-
pip install -r requirements.txt
|
| 40 |
-
```
|
| 41 |
-
|
| 42 |
-
### 4. Download SAM Model
|
| 43 |
-
|
| 44 |
-
Download the SAM model checkpoint (choose one based on your needs):
|
| 45 |
-
|
| 46 |
-
**Option 1: Largest and Most Accurate (vit_h - 2.4GB)**
|
| 47 |
-
```bash
|
| 48 |
-
wget https://dl.fbaipublicfiles.com/segment_anything/sam_vit_h_4b8939.pth
|
| 49 |
-
```
|
| 50 |
-
|
| 51 |
-
**Option 2: Medium (vit_l - 1.2GB)**
|
| 52 |
-
```bash
|
| 53 |
-
wget https://dl.fbaipublicfiles.com/segment_anything/sam_vit_l_0b3195.pth
|
| 54 |
-
```
|
| 55 |
-
|
| 56 |
-
**Option 3: Smallest and Fastest (vit_b - 375MB)**
|
| 57 |
-
```bash
|
| 58 |
-
wget https://dl.fbaipublicfiles.com/segment_anything/sam_vit_b_01ec64.pth
|
| 59 |
-
```
|
| 60 |
-
|
| 61 |
-
If using a different model, update `sam_checkpoint` and `model_type` in `main.py`.
|
| 62 |
-
|
| 63 |
-
### 5. Quick Setup (All-in-One)
|
| 64 |
-
|
| 65 |
-
Alternatively, run the setup script:
|
| 66 |
-
|
| 67 |
-
```bash
|
| 68 |
-
chmod +x setup.sh
|
| 69 |
-
./setup.sh
|
| 70 |
-
```
|
| 71 |
-
|
| 72 |
-
## Running the Server
|
| 73 |
-
|
| 74 |
-
### Development Mode
|
| 75 |
-
|
| 76 |
-
```bash
|
| 77 |
-
uvicorn main:app --reload --host 0.0.0.0 --port 8000
|
| 78 |
-
```
|
| 79 |
-
|
| 80 |
-
### Production Mode
|
| 81 |
-
|
| 82 |
-
```bash
|
| 83 |
-
uvicorn main:app --host 0.0.0.0 --port 8000 --workers 4
|
| 84 |
-
```
|
| 85 |
-
|
| 86 |
-
The server will start at `http://localhost:8000`
|
| 87 |
-
|
| 88 |
-
## API Endpoints
|
| 89 |
-
|
| 90 |
-
### Health Check
|
| 91 |
-
|
| 92 |
-
**GET** `/health`
|
| 93 |
-
|
| 94 |
-
Check if the server and SAM model are loaded.
|
| 95 |
-
|
| 96 |
-
**Response:**
|
| 97 |
-
```json
|
| 98 |
-
{
|
| 99 |
-
"status": "healthy",
|
| 100 |
-
"device": "cuda",
|
| 101 |
-
"sam_model_loaded": true
|
| 102 |
-
}
|
| 103 |
-
```
|
| 104 |
-
|
| 105 |
-
### Automatic Segmentation
|
| 106 |
-
|
| 107 |
-
**POST** `/segment-automatic`
|
| 108 |
-
|
| 109 |
-
Automatically segments all objects in the image.
|
| 110 |
-
|
| 111 |
-
**Request:**
|
| 112 |
-
- Content-Type: `multipart/form-data`
|
| 113 |
-
- Body: `file` (image file)
|
| 114 |
-
|
| 115 |
-
**Response:**
|
| 116 |
-
```json
|
| 117 |
-
{
|
| 118 |
-
"success": true,
|
| 119 |
-
"num_masks": 5,
|
| 120 |
-
"masks": [
|
| 121 |
-
{
|
| 122 |
-
"id": 0,
|
| 123 |
-
"mask_base64": "...",
|
| 124 |
-
"area": 50000,
|
| 125 |
-
"bbox": [x, y, width, height]
|
| 126 |
-
}
|
| 127 |
-
],
|
| 128 |
-
"image_base64": "..."
|
| 129 |
-
}
|
| 130 |
-
```
|
| 131 |
-
|
| 132 |
-
### Simple Segmentation (Fallback)
|
| 133 |
-
|
| 134 |
-
**POST** `/simple-segment`
|
| 135 |
-
|
| 136 |
-
Uses traditional CV methods for segmentation.
|
| 137 |
-
|
| 138 |
-
Same request/response format as `/segment-automatic`.
|
| 139 |
-
|
| 140 |
-
### Point-Based Segmentation
|
| 141 |
-
|
| 142 |
-
**POST** `/segment-point`
|
| 143 |
-
|
| 144 |
-
Segments object at a specific point in the image.
|
| 145 |
-
|
| 146 |
-
**Request:**
|
| 147 |
-
```json
|
| 148 |
-
{
|
| 149 |
-
"image_base64": "...",
|
| 150 |
-
"point_x": 100.5,
|
| 151 |
-
"point_y": 200.5
|
| 152 |
-
}
|
| 153 |
-
```
|
| 154 |
-
|
| 155 |
-
**Response:**
|
| 156 |
-
```json
|
| 157 |
-
{
|
| 158 |
-
"success": true,
|
| 159 |
-
"mask_base64": "...",
|
| 160 |
-
"score": 0.95
|
| 161 |
-
}
|
| 162 |
-
```
|
| 163 |
-
|
| 164 |
-
### Apply Color
|
| 165 |
-
|
| 166 |
-
**POST** `/apply-color`
|
| 167 |
-
|
| 168 |
-
Applies color to a masked region.
|
| 169 |
-
|
| 170 |
-
**Request:**
|
| 171 |
-
```json
|
| 172 |
-
{
|
| 173 |
-
"image_base64": "...",
|
| 174 |
-
"mask_base64": "...",
|
| 175 |
-
"color_hex": "#FF5733",
|
| 176 |
-
"opacity": 0.8
|
| 177 |
-
}
|
| 178 |
-
```
|
| 179 |
-
|
| 180 |
-
**Response:**
|
| 181 |
-
```json
|
| 182 |
-
{
|
| 183 |
-
"success": true,
|
| 184 |
-
"result_base64": "..."
|
| 185 |
-
}
|
| 186 |
-
```
|
| 187 |
-
|
| 188 |
-
## Configuration
|
| 189 |
-
|
| 190 |
-
### Environment Variables
|
| 191 |
-
|
| 192 |
-
Create a `.env` file (see `.env.example`):
|
| 193 |
-
|
| 194 |
-
```env
|
| 195 |
-
HOST=0.0.0.0
|
| 196 |
-
PORT=8000
|
| 197 |
-
SAM_CHECKPOINT=sam_vit_h_4b8939.pth
|
| 198 |
-
MODEL_TYPE=vit_h
|
| 199 |
-
DEVICE=cuda # or 'cpu'
|
| 200 |
-
```
|
| 201 |
-
|
| 202 |
-
### Model Selection
|
| 203 |
-
|
| 204 |
-
In `main.py`, modify:
|
| 205 |
-
|
| 206 |
-
```python
|
| 207 |
-
sam_checkpoint = "sam_vit_h_4b8939.pth" # Path to model
|
| 208 |
-
model_type = "vit_h" # vit_h, vit_l, or vit_b
|
| 209 |
-
device = "cuda" # cuda or cpu
|
| 210 |
-
```
|
| 211 |
-
|
| 212 |
-
## Troubleshooting
|
| 213 |
-
|
| 214 |
-
### CUDA Out of Memory
|
| 215 |
-
|
| 216 |
-
If you encounter CUDA memory errors:
|
| 217 |
-
|
| 218 |
-
1. Use a smaller model (vit_b)
|
| 219 |
-
2. Reduce image size in Flutter app
|
| 220 |
-
3. Use CPU instead: `device = "cpu"`
|
| 221 |
-
|
| 222 |
-
### Model Not Loading
|
| 223 |
-
|
| 224 |
-
1. Verify checkpoint file exists in the correct location
|
| 225 |
-
2. Check file integrity (download again if needed)
|
| 226 |
-
3. Ensure sufficient RAM/VRAM
|
| 227 |
-
|
| 228 |
-
### Slow Performance
|
| 229 |
-
|
| 230 |
-
- Use GPU (CUDA) instead of CPU
|
| 231 |
-
- Reduce image resolution
|
| 232 |
-
- Use smaller model (vit_b)
|
| 233 |
-
|
| 234 |
-
## Testing
|
| 235 |
-
|
| 236 |
-
### Test with cURL
|
| 237 |
-
|
| 238 |
-
```bash
|
| 239 |
-
# Health check
|
| 240 |
-
curl http://localhost:8000/health
|
| 241 |
-
|
| 242 |
-
# Upload and segment
|
| 243 |
-
curl -X POST -F "file=@test_image.jpg" \
|
| 244 |
-
http://localhost:8000/segment-automatic
|
| 245 |
-
```
|
| 246 |
-
|
| 247 |
-
### Test with Python
|
| 248 |
-
|
| 249 |
-
```python
|
| 250 |
-
import requests
|
| 251 |
-
|
| 252 |
-
# Health check
|
| 253 |
-
response = requests.get('http://localhost:8000/health')
|
| 254 |
-
print(response.json())
|
| 255 |
-
|
| 256 |
-
# Segment image
|
| 257 |
-
with open('test_image.jpg', 'rb') as f:
|
| 258 |
-
files = {'file': f}
|
| 259 |
-
response = requests.post(
|
| 260 |
-
'http://localhost:8000/segment-automatic',
|
| 261 |
-
files=files
|
| 262 |
-
)
|
| 263 |
-
print(response.json())
|
| 264 |
-
```
|
| 265 |
-
|
| 266 |
-
## Performance
|
| 267 |
-
|
| 268 |
-
### With CUDA (GPU)
|
| 269 |
-
- Segmentation: 2-5 seconds per image
|
| 270 |
-
- Color application: < 1 second
|
| 271 |
-
|
| 272 |
-
### Without CUDA (CPU)
|
| 273 |
-
- Segmentation: 10-30 seconds per image
|
| 274 |
-
- Color application: < 1 second
|
| 275 |
-
|
| 276 |
-
## Network Configuration
|
| 277 |
-
|
| 278 |
-
### For Android Emulator
|
| 279 |
-
Use: `http://10.0.2.2:8000`
|
| 280 |
-
|
| 281 |
-
### For iOS Simulator
|
| 282 |
-
Use: `http://localhost:8000`
|
| 283 |
-
|
| 284 |
-
### For Real Devices
|
| 285 |
-
1. Find your computer's IP address:
|
| 286 |
-
```bash
|
| 287 |
-
# Linux/Mac
|
| 288 |
-
ip addr show
|
| 289 |
-
# or
|
| 290 |
-
ifconfig
|
| 291 |
-
|
| 292 |
-
# Windows
|
| 293 |
-
ipconfig
|
| 294 |
-
```
|
| 295 |
-
|
| 296 |
-
2. Use: `http://YOUR_IP:8000`
|
| 297 |
-
3. Ensure firewall allows port 8000
|
| 298 |
-
|
| 299 |
-
## Dependencies
|
| 300 |
-
|
| 301 |
-
- **fastapi**: Web framework
|
| 302 |
-
- **uvicorn**: ASGI server
|
| 303 |
-
- **segment-anything**: Meta's SAM model
|
| 304 |
-
- **torch**: PyTorch for deep learning
|
| 305 |
-
- **opencv-python**: Image processing
|
| 306 |
-
- **pillow**: Image manipulation
|
| 307 |
-
- **numpy**: Numerical operations
|
| 308 |
-
|
| 309 |
-
## License
|
| 310 |
-
|
| 311 |
-
This project uses Meta's Segment Anything Model. See SAM's license for details.
|
| 312 |
-
|
| 313 |
-
## Support
|
| 314 |
-
|
| 315 |
-
For issues or questions:
|
| 316 |
-
1. Check the troubleshooting section
|
| 317 |
-
2. Verify all dependencies are installed
|
| 318 |
-
3. Ensure the model is downloaded correctly
|
| 319 |
-
4. Check server logs for detailed error messages
|
| 320 |
-
|
| 321 |
-
## Credits
|
| 322 |
-
|
| 323 |
-
- Meta AI for the Segment Anything Model
|
| 324 |
-
- FastAPI framework
|
| 325 |
-
- OpenCV community
|
| 326 |
-
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Wallpaint
|
| 3 |
+
emoji: 🌖
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: purple
|
| 6 |
+
sdk: docker
|
| 7 |
+
pinned: false
|
| 8 |
+
---
|
| 9 |
+
|
| 10 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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