metadata
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 personclothing_image(file): Image of the clothingprompt(optional): Generation promptnegative_prompt(optional): Negative promptip_scale(optional, 0.0-1.0): IP-Adapter influence, default: 0.8strength(optional, 0.0-1.0): Inpainting strength, default: 0.99guidance_scale(optional): CFG scale, default: 7.5num_steps(optional): Inference steps, default: 50return_format(optional): "base64" or "image", default: "base64"
Response (base64 format):
{
"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 imageclothing_image_base64(string): Base64 encoded clothing image- Other parameters same as
/tryon
Response: Same as /tryon
React Native Integration Example
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
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
# 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
- Create a new Space on Hugging Face
- Choose "Docker" as the SDK
- Upload these files:
app.pyrequirements.txtDockerfileREADME.md
- 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