Spaces:
Running
Running
import os | |
import cv2 | |
import gradio as gr | |
import numpy as np | |
import random | |
MAX_SEED = 999999 | |
example_path = os.path.join(os.path.dirname(__file__), 'assets') | |
# Load example images | |
garm_list = os.listdir(os.path.join(example_path, "cloth")) | |
garm_list_path = [os.path.join(example_path, "cloth", garm) for garm in garm_list] | |
human_list = os.listdir(os.path.join(example_path, "human")) | |
human_list_path = [os.path.join(example_path, "human", human) for human in human_list] | |
def mock_tryon(person_img, garment_img, seed, randomize_seed, progress=gr.Progress()): | |
progress(0, desc="Starting mock try-on...") | |
if person_img is None or garment_img is None: | |
gr.Warning("Please upload both images!") | |
return None, None, "Error: Empty image" | |
if randomize_seed: | |
seed = random.randint(0, MAX_SEED) | |
progress(0.3, desc="Processing person image...") | |
# Convert to grayscale for demo (replace with actual try-on logic) | |
person_gray = cv2.cvtColor(person_img, cv2.COLOR_RGB2GRAY) | |
person_gray = cv2.cvtColor(person_gray, cv2.COLOR_GRAY2RGB) | |
progress(0.6, desc="Adding garment...") | |
# Resize garment to fit person (demo only) | |
garment_resized = cv2.resize(garment_img, (person_img.shape[1], person_img.shape[0])) | |
progress(0.8, desc="Blending images...") | |
# Simple alpha blending (replace with real try-on) | |
alpha = 0.7 | |
result = cv2.addWeighted(person_gray, 1-alpha, garment_resized, alpha, 0) | |
progress(1.0, desc="Done!") | |
return result, seed, "Mock try-on complete" | |
def load_description(fp): | |
with open(fp, 'r', encoding='utf-8') as f: | |
return f.read() | |
css = """ | |
#col-left { margin: 0 auto; max-width: 430px; } | |
#col-mid { margin: 0 auto; max-width: 430px; } | |
#col-right { margin: 0 auto; max-width: 430px; } | |
#col-showcase { margin: 0 auto; max-width: 1100px; } | |
#button { color: blue; } | |
""" | |
with gr.Blocks(css=css) as Tryon: | |
gr.HTML(load_description("assets/title.md")) | |
with gr.Row(): | |
with gr.Column(elem_id="col-left"): | |
gr.HTML("<div style='text-align: center; font-size: 20px;'>Step 1. Upload person ⬇️</div>") | |
imgs = gr.Image(label="Person", sources='upload', type="numpy") | |
gr.Examples(inputs=imgs, examples_per_page=12, examples=human_list_path) | |
with gr.Column(elem_id="col-mid"): | |
gr.HTML("<div style='text-align: center; font-size: 20px;'>Step 2. Upload garment ⬇️</div>") | |
garm_img = gr.Image(label="Garment", sources='upload', type="numpy") | |
gr.Examples(inputs=garm_img, examples_per_page=12, examples=garm_list_path) | |
with gr.Column(elem_id="col-right"): | |
gr.HTML("<div style='text-align: center; font-size: 20px;'>Step 3. Click Run</div>") | |
image_out = gr.Image(label="Result") | |
with gr.Row(): | |
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0) | |
randomize_seed = gr.Checkbox(label="Random seed", value=True) | |
with gr.Row(): | |
seed_used = gr.Number(label="Seed used") | |
result_info = gr.Text(label="Status") | |
test_button = gr.Button("Run", elem_id="button") | |
test_button.click( | |
fn=mock_tryon, | |
inputs=[imgs, garm_img, seed, randomize_seed], | |
outputs=[image_out, seed_used, result_info], | |
concurrency_limit=5 | |
) | |
with gr.Column(elem_id="col-showcase"): | |
gr.HTML("<div style='text-align: center; font-size: 20px;'>Examples</div>") | |
gr.Examples( | |
examples=[ | |
[human_list_path[0], garm_list_path[0]], # First human + first garment | |
[human_list_path[1], garm_list_path[1]], # Second pair | |
], | |
inputs=[imgs, garm_img], | |
outputs=[image_out] | |
) | |
Tryon.queue().launch() |