File size: 4,696 Bytes
98ffc6a |
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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
---
title: Virtual Try-On API
emoji: ๐
colorFrom: blue
colorTo: purple
sdk: docker
pinned: false
license: apache-2.0
---
# Virtual Try-On API ๐
A FastAPI-based virtual try-on service using Stable Diffusion XL with IP-Adapters for realistic clothing transfer.
## Features
- โ
REST API for virtual clothing try-on
- โ
CPU-optimized for Hugging Face Spaces
- โ
Support for file upload and base64 encoding
- โ
Automatic body segmentation
- โ
Customizable generation parameters
- โ
CORS enabled for mobile app integration
## API Endpoints
### 1. Health Check
```
GET /
GET /health
```
Returns the status of the API and loaded models.
### 2. Virtual Try-On (File Upload)
```
POST /tryon
```
**Parameters:**
- `person_image` (file): Image of the person
- `clothing_image` (file): Image of the clothing
- `prompt` (optional): Generation prompt
- `negative_prompt` (optional): Negative prompt
- `ip_scale` (optional, 0.0-1.0): IP-Adapter influence, default: 0.8
- `strength` (optional, 0.0-1.0): Inpainting strength, default: 0.99
- `guidance_scale` (optional): CFG scale, default: 7.5
- `num_steps` (optional): Inference steps, default: 50
- `return_format` (optional): "base64" or "image", default: "base64"
**Response (base64 format):**
```json
{
"success": true,
"image": "base64_encoded_image_string",
"processing_time": 45.23,
"parameters": {
"prompt": "...",
"ip_scale": 0.8,
"strength": 0.99,
"guidance_scale": 7.5,
"num_steps": 50
}
}
```
### 3. Virtual Try-On (Base64)
```
POST /tryon-base64
```
**Parameters:**
- `person_image_base64` (string): Base64 encoded person image
- `clothing_image_base64` (string): Base64 encoded clothing image
- Other parameters same as `/tryon`
**Response:** Same as `/tryon`
## React Native Integration Example
```javascript
import * as ImagePicker from 'expo-image-picker';
import * as FileSystem from 'expo-file-system';
const API_URL = 'https://your-space-name.hf.space';
async function virtualTryOn(personUri, clothingUri) {
try {
// Convert images to base64
const personBase64 = await FileSystem.readAsStringAsync(personUri, {
encoding: FileSystem.EncodingType.Base64,
});
const clothingBase64 = await FileSystem.readAsStringAsync(clothingUri, {
encoding: FileSystem.EncodingType.Base64,
});
// Create form data
const formData = new FormData();
formData.append('person_image_base64', personBase64);
formData.append('clothing_image_base64', clothingBase64);
// Make API request
const response = await fetch(`${API_URL}/tryon-base64`, {
method: 'POST',
body: formData,
});
const result = await response.json();
if (result.success) {
// Display the generated image
const imageUri = `data:image/png;base64,${result.image}`;
console.log('Processing time:', result.processing_time);
return imageUri;
}
} catch (error) {
console.error('Error:', error);
}
}
```
## Alternative: File Upload Method
```javascript
async function virtualTryOnWithFiles(personUri, clothingUri) {
const formData = new FormData();
formData.append('person_image', {
uri: personUri,
type: 'image/jpeg',
name: 'person.jpg',
});
formData.append('clothing_image', {
uri: clothingUri,
type: 'image/jpeg',
name: 'clothing.jpg',
});
formData.append('return_format', 'base64');
const response = await fetch(`${API_URL}/tryon`, {
method: 'POST',
body: formData,
headers: {
'Content-Type': 'multipart/form-data',
},
});
const result = await response.json();
return `data:image/png;base64,${result.image}`;
}
```
## Local Development
```bash
# Install dependencies
pip install -r requirements.txt
# Clone body segmentation tool
git clone https://github.com/TonyAssi/Segment-Body.git
cp Segment-Body/SegBody.py .
# Run the API
python app.py
```
The API will be available at `http://localhost:7860`
## Deployment to Hugging Face Spaces
1. Create a new Space on Hugging Face
2. Choose "Docker" as the SDK
3. Upload these files:
- `app.py`
- `requirements.txt`
- `Dockerfile`
- `README.md`
4. Wait for the Space to build and deploy
## Notes
- First request may take longer as models are loaded
- CPU inference is slower than GPU (expect 30-60 seconds per generation)
- Recommended image size: 512x512 pixels
- For production use, consider adding authentication and rate limiting
## License
Apache 2.0
|