tryonapi / README.md
sync19's picture
Upload 8 files
98ffc6a verified
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 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):

{
  "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

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

  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