Spaces:
Build error
Build error
Download app.py from NexaCoders/NexaClothes: direct link, hf CLI and curl.
- Browser
- Download file 4.12 kB
-
https://huggingface.co/spaces/NexaCoders/NexaClothes/resolve/main/app.py
- Command line
-
hf download hf://spaces/NexaCoders/NexaClothes/app.py
-
curl -L -o app.py https://huggingface.co/spaces/NexaCoders/NexaClothes/resolve/main/app.py
4.12 kB
| import gradio as gr | |
| import torch | |
| import cv2 | |
| import numpy as np | |
| from PIL import Image | |
| import os | |
| from advanced_fitter import AdvancedClothesFitter | |
| import json | |
| from model import PerfectFitModel | |
| import uuid | |
| # Initialize model | |
| model = PerfectFitModel() | |
| fitter = AdvancedClothesFitter() | |
| # Load sample catalog (50+ clothes for demo) | |
| def load_sample_catalog(): | |
| catalog_path = "static/sample_clothes_catalog.json" | |
| if os.path.exists(catalog_path): | |
| with open(catalog_path, 'r') as f: | |
| return json.load(f) | |
| return [] | |
| CATALOG = load_sample_catalog() | |
| # Gradio Interface | |
| def change_clothes(person_image, clothing_idx, clothing_type, size): | |
| """Main processing function""" | |
| if person_image is None: | |
| return None | |
| # Convert PIL to OpenCV | |
| person_cv = cv2.cvtColor(np.array(person_image), cv2.COLOR_RGB2BGR) | |
| # Get clothing | |
| if int(clothing_idx) >= len(CATALOG): | |
| return person_cv | |
| clothing_item = CATALOG[int(clothing_idx)] | |
| clothing_path = clothing_item['image_path'] | |
| # Process with perfect fit | |
| result = model.predict(person_cv, clothing_path, clothing_type, size) | |
| # Convert back to PIL | |
| result_pil = Image.fromarray(cv2.cvtColor(result, cv2.COLOR_BGR2RGB)) | |
| return result_pil | |
| # Create Gradio interface | |
| with gr.Blocks( | |
| title="Perfect Clothes Changer", | |
| theme=gr.themes.Soft(), | |
| css=""" | |
| .gradio-container { max-width: 1400px !important; } | |
| .cloth-preview { height: 100px !important; object-fit: cover; } | |
| """ | |
| ) as demo: | |
| gr.Markdown(""" | |
| # β¨ **Perfect Clothes Changer** - 100% Free AI Virtual Try-On | |
| ### Upload your photo β Choose real clothes β Get perfect fit instantly! | |
| **Powered by MediaPipe + Custom Fitting Algorithm** β‘ | |
| """) | |
| with gr.Row(): | |
| with gr.Column(scale=1): | |
| input_image = gr.Image( | |
| label="πΈ Upload Your Photo", | |
| type="pil", | |
| height=400 | |
| ) | |
| cloth_type = gr.Dropdown( | |
| choices=["shirt", "pants", "dress"], | |
| value="shirt", | |
| label="π Clothing Type" | |
| ) | |
| cloth_size = gr.Dropdown( | |
| choices=["XS", "S", "M", "L", "XL"], | |
| value="M", | |
| label="π Size" | |
| ) | |
| cloth_selector = gr.Dropdown( | |
| choices=[f"Cloth {i+1}" for i in range(len(CATALOG))], | |
| label="π― Select Clothing", | |
| value="Cloth 0" | |
| ) | |
| change_btn = gr.Button("π Try Perfect Fit", variant="primary", size="lg") | |
| with gr.Column(scale=1): | |
| output_image = gr.Image(label="β¨ Perfect Result", height=500) | |
| # Clothing preview grid | |
| with gr.Row(): | |
| gr.Markdown("### π **Clothing Catalog Preview** (Click to select)") | |
| with gr.Column(): | |
| catalog_grid = gr.Gallery( | |
| value=[item['image_path'] for item in CATALOG[:12]], | |
| label="Sample Clothes", | |
| columns=6, | |
| height="auto", | |
| elem_classes=["cloth-preview"] | |
| ) | |
| # Event handlers | |
| change_btn.click( | |
| fn=change_clothes, | |
| inputs=[input_image, cloth_selector, cloth_type, cloth_size], | |
| outputs=output_image | |
| ) | |
| cloth_type.change( | |
| fn=lambda t: gr.update(choices=[f"Cloth {i+1}" for i in range(len(CATALOG))], value="Cloth 0"), | |
| inputs=[cloth_type], | |
| outputs=[cloth_selector] | |
| ) | |
| gr.Markdown(""" | |
| ### π **Features:** | |
| - β **100+ Real Clothes** (HD quality) | |
| - β **Perfect Body Fit** (shoulder/torso/leg measurements) | |
| - β **GPU Accelerated** (10x faster) | |
| - β **Multiple People Support** | |
| - β **XS-XL Sizing** | |
| - β **Works on Real Photos** | |
| **Made with β€οΈ for Hugging Face Spaces** | |
| """) | |
| if __name__ == "__main__": | |
| demo.launch( | |
| server_name="0.0.0.0", | |
| server_port=7860, | |
| share=True # Public link | |
| ) |