Spaces:
Runtime error
Runtime error
File size: 5,099 Bytes
493e780 ca30234 493e780 ca30234 493e780 b5eb0cf 493e780 b5eb0cf 493e780 b5eb0cf 493e780 b5eb0cf 493e780 50ab3df b5eb0cf 493e780 ca30234 493e780 ca30234 493e780 ca30234 50ab3df ca30234 493e780 ca30234 493e780 ca30234 493e780 ca30234 50ab3df 493e780 50ab3df 493e780 50ab3df 493e780 ca30234 50ab3df |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
import os
import cv2
import gradio as gr
import numpy as np
import random
import time
def tryon(person_img, garment_prompt, seed, randomize_seed):
post_start_time = time.time()
if person_img is None or garment_prompt.strip() == "":
return None, None, "Empty image or prompt"
if randomize_seed:
seed = random.randint(0, MAX_SEED)
# Create a copy of the person image to overlay text
result_img = person_img.copy()
# Convert the image to OpenCV format (if needed)
if len(result_img.shape) == 2: # Convert grayscale to RGB
result_img = cv2.cvtColor(result_img, cv2.COLOR_GRAY2RGB)
# Set text position and properties
text_position = (10, 30)
font = cv2.FONT_HERSHEY_SIMPLEX
font_scale = 1
font_color = (0, 255, 0) # Green color for the text
thickness = 2
# Overlay the garment description text on the image
cv2.putText(result_img, f'Garment: {garment_prompt}', text_position, font, font_scale, font_color, thickness, cv2.LINE_AA)
post_end_time = time.time()
print(f"post time used: {post_end_time - post_start_time}")
# Return the resulting image, used seed, and success message
return result_img, seed, "Success"
MAX_SEED = 999999
example_path = os.path.join(os.path.dirname(__file__), 'assets')
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]
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;
}
"""
def load_description(fp):
with open(fp, 'r', encoding='utf-8') as f:
content = f.read()
return content
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="display: flex; justify-content: center; align-items: center; text-align: center; font-size: 20px;">
<div>
Step 1. Upload a person image ⬇️
</div>
</div>
""")
with gr.Column(elem_id="col-mid"):
gr.HTML("""
<div style="display: flex; justify-content: center; align-items: center; text-align: center; font-size: 20px;">
<div>
Step 2. Enter a text prompt for the garment ⬇️
</div>
</div>
""")
with gr.Column(elem_id="col-right"):
gr.HTML("""
<div style="display: flex; justify-content: center; align-items: center; text-align: center; font-size: 20px;">
<div>
Step 3. Press “Run” to get try-on results
</div>
</div>
""")
with gr.Row():
with gr.Column(elem_id="col-left"):
imgs = gr.Image(label="Person image", sources='upload', type="numpy")
example = gr.Examples(
inputs=imgs,
examples_per_page=12,
examples=human_list_path
)
with gr.Column(elem_id="col-mid"):
garment_prompt = gr.Textbox(label="Garment text prompt", placeholder="Describe the garment...")
with gr.Column(elem_id="col-right"):
image_out = gr.Image(label="Result", show_share_button=False)
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="Response")
test_button = gr.Button(value="Run", elem_id="button")
test_button.click(fn=tryon, inputs=[imgs, garment_prompt, seed, randomize_seed], outputs=[image_out, seed_used, result_info], concurrency_limit=40)
with gr.Column(elem_id="col-showcase"):
gr.HTML("""
<div style="display: flex; justify-content: center; align-items: center; text-align: center; font-size: 20px;">
<div> </div>
<br>
<div>
Virtual try-on examples in pairs of person and garment images
</div>
</div>
""")
show_case = gr.Examples(
examples=[
["assets/examples/model2.png", "assets/examples/garment2.png", "assets/examples/result2.png"],
["assets/examples/model3.png", "assets/examples/garment3.png", "assets/examples/result3.png"],
["assets/examples/model1.png", "assets/examples/garment1.png", "assets/examples/result1.png"],
],
inputs=[imgs, garment_prompt, image_out],
label=None
)
Tryon.launch()
|