anycoder-6caaef46 / utils.py
MadhavRupala's picture
Upload folder using huggingface_hub
19b11aa verified
import numpy as np
import cv2
from PIL import Image, ImageEnhance, ImageFilter
import os
def enhance_image_quality(image, brightness=1.1, contrast=1.1, sharpness=1.1):
"""Enhance image quality for better virtual try-on results"""
if image is None:
return None
pil_image = Image.fromarray(image)
# Adjust brightness
if brightness != 1.0:
brightness_enhancer = ImageEnhance.Brightness(pil_image)
pil_image = brightness_enhancer.enhance(brightness)
# Adjust contrast
if contrast != 1.0:
contrast_enhancer = ImageEnhance.Contrast(pil_image)
pil_image = contrast_enhancer.enhance(contrast)
# Adjust sharpness
if sharpness != 1.0:
sharpness_enhancer = ImageEnhance.Sharpness(pil_image)
pil_image = sharpness_enhancer.enhance(sharpness)
return np.array(pil_image)
def remove_background_simple(image):
"""Simple background removal using color clustering"""
if image is None:
return None, None
# Convert to PIL for easier processing
pil_image = Image.fromarray(image)
# Convert to RGBA
if pil_image.mode != 'RGBA':
pil_image = pil_image.convert('RGBA')
# Simple approach: make white/light colors transparent
data = np.array(pil_image)
# Create mask for light colors (potential background)
light_mask = np.all(data[:, :, :3] > 200, axis=2)
# Create transparent version
data[light_mask] = [255, 255, 255, 0] # Make light colors transparent
return np.array(Image.fromarray(data, 'RGBA')), light_mask
def get_image_info(image):
"""Get basic information about an image"""
if image is None:
return "No image loaded"
height, width = image.shape[:2]
channels = image.shape[2] if len(image.shape) > 2 else 1
return f"Size: {width}x{height}, Channels: {channels}, Data type: {image.dtype}"
def resize_image_preserve_aspect(image, target_width, target_height):
"""Resize image while preserving aspect ratio"""
if image is None:
return None
height, width = image.shape[:2]
aspect_ratio = width / height
if target_width / target_height > aspect_ratio:
# Height is the limiting factor
new_height = target_height
new_width = int(target_height * aspect_ratio)
else:
# Width is the limiting factor
new_width = target_width
new_height = int(target_width / aspect_ratio)
resized = cv2.resize(image, (new_width, new_height))
# Create canvas and center the image
canvas = np.zeros((target_height, target_width, 3), dtype=np.uint8)
y_offset = (target_height - new_height) // 2
x_offset = (target_width - new_width) // 2
canvas[y_offset:y_offset+new_height, x_offset:x_offset+new_width] = resized
return canvas
This is a comprehensive virtual cloth trial room application! Here's what it includes:
## Features:
1. **Virtual Try-On System**: Upload person and clothing images to see a virtual try-on result
2. **Real-time Adjustments**: Use sliders to adjust clothing opacity and vertical position
3. **Simple AI Processing**: Uses computer vision techniques to blend clothing with person images
4. **User-Friendly Interface**: Clear instructions and status feedback
5. **Reset Functionality**: Start over with new images easily
## How It Works:
1. **Image Loading**: Upload photos of yourself and the clothing you want to try
2. **Preprocessing**: Images are resized and optimized for processing
3. **Virtual Try-On**: The system blends the clothing onto the person using mask-based blending
4. **Adjustments**: Fine-tune the result with opacity and position controls
## Key Technologies:
- **PIL/Pillow**: Image processing and enhancement
- **OpenCV**: Computer vision operations
- **NumPy**: Numerical operations for image arrays
- **Gradio**: User interface and web app framework
## Usage Tips:
- Use high-quality, well-lit images for best results
- Person should be positioned upright and clearly visible
- Clothing should have good contrast with its background
- The app works best with frontal-facing photos
The application is now ready to run! It provides a functional virtual try-on experience with intuitive controls and helpful feedback for users.