Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,171 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import cv2
|
3 |
+
import spaces
|
4 |
+
from PIL import Image
|
5 |
+
import gradio as gr
|
6 |
+
import numpy as np
|
7 |
+
import random
|
8 |
+
import base64
|
9 |
+
import requests
|
10 |
+
import json
|
11 |
+
import time
|
12 |
+
|
13 |
+
# Add a new function for text-to-image generation
|
14 |
+
def generate_garment_image(prompt):
|
15 |
+
# This is a placeholder function. You'll need to implement actual text-to-image generation here.
|
16 |
+
# For example, you might use a service like DALL-E, Stable Diffusion, or any other text-to-image model.
|
17 |
+
# For now, we'll just return a placeholder image.
|
18 |
+
placeholder_image = np.zeros((256, 256, 3), dtype=np.uint8)
|
19 |
+
cv2.putText(placeholder_image, prompt, (10, 128), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1)
|
20 |
+
return placeholder_image
|
21 |
+
|
22 |
+
def tryon(person_img, garment_prompt, seed, randomize_seed):
|
23 |
+
post_start_time = time.time()
|
24 |
+
if person_img is None or garment_prompt == "":
|
25 |
+
return None, None, "Empty image or prompt"
|
26 |
+
if randomize_seed:
|
27 |
+
seed = random.randint(0, MAX_SEED)
|
28 |
+
|
29 |
+
# Generate garment image from prompt
|
30 |
+
garment_img = generate_garment_image(garment_prompt)
|
31 |
+
|
32 |
+
encoded_person_img = cv2.imencode('.jpg', cv2.cvtColor(person_img, cv2.COLOR_RGB2BGR))[1].tobytes()
|
33 |
+
encoded_person_img = base64.b64encode(encoded_person_img).decode('utf-8')
|
34 |
+
encoded_garment_img = cv2.imencode('.jpg', cv2.cvtColor(garment_img, cv2.COLOR_RGB2BGR))[1].tobytes()
|
35 |
+
encoded_garment_img = base64.b64encode(encoded_garment_img).decode('utf-8')
|
36 |
+
|
37 |
+
# Rest of the function remains the same
|
38 |
+
# ...
|
39 |
+
|
40 |
+
def start_tryon(person_img, garment_prompt, seed, randomize_seed):
|
41 |
+
start_time = time.time()
|
42 |
+
if person_img is None or garment_prompt == "":
|
43 |
+
return None, None, "Empty image or prompt"
|
44 |
+
if randomize_seed:
|
45 |
+
seed = random.randint(0, MAX_SEED)
|
46 |
+
|
47 |
+
# Generate garment image from prompt
|
48 |
+
garment_img = generate_garment_image(garment_prompt)
|
49 |
+
|
50 |
+
encoded_person_img = cv2.imencode('.jpg', cv2.cvtColor(person_img, cv2.COLOR_RGB2BGR))[1].tobytes()
|
51 |
+
encoded_person_img = base64.b64encode(encoded_person_img).decode('utf-8')
|
52 |
+
encoded_garment_img = cv2.imencode('.jpg', cv2.cvtColor(garment_img, cv2.COLOR_RGB2BGR))[1].tobytes()
|
53 |
+
encoded_garment_img = base64.b64encode(encoded_garment_img).decode('utf-8')
|
54 |
+
|
55 |
+
# Rest of the function remains the same
|
56 |
+
# ...
|
57 |
+
|
58 |
+
MAX_SEED = 999999
|
59 |
+
|
60 |
+
example_path = os.path.join(os.path.dirname(__file__), 'assets')
|
61 |
+
|
62 |
+
human_list = os.listdir(os.path.join(example_path,"human"))
|
63 |
+
human_list_path = [os.path.join(example_path,"human",human) for human in human_list]
|
64 |
+
|
65 |
+
css="""
|
66 |
+
#col-left {
|
67 |
+
margin: 0 auto;
|
68 |
+
max-width: 430px;
|
69 |
+
}
|
70 |
+
#col-mid {
|
71 |
+
margin: 0 auto;
|
72 |
+
max-width: 430px;
|
73 |
+
}
|
74 |
+
#col-right {
|
75 |
+
margin: 0 auto;
|
76 |
+
max-width: 430px;
|
77 |
+
}
|
78 |
+
#col-showcase {
|
79 |
+
margin: 0 auto;
|
80 |
+
max-width: 1100px;
|
81 |
+
}
|
82 |
+
#button {
|
83 |
+
color: blue;
|
84 |
+
}
|
85 |
+
"""
|
86 |
+
|
87 |
+
def load_description(fp):
|
88 |
+
with open(fp, 'r', encoding='utf-8') as f:
|
89 |
+
content = f.read()
|
90 |
+
return content
|
91 |
+
|
92 |
+
with gr.Blocks(css=css) as Tryon:
|
93 |
+
gr.HTML(load_description("assets/title.md"))
|
94 |
+
with gr.Row():
|
95 |
+
with gr.Column(elem_id = "col-left"):
|
96 |
+
gr.HTML("""
|
97 |
+
<div style="display: flex; justify-content: center; align-items: center; text-align: center; font-size: 20px;">
|
98 |
+
<div>
|
99 |
+
Step 1. Upload a person image ⬇️
|
100 |
+
</div>
|
101 |
+
</div>
|
102 |
+
""")
|
103 |
+
with gr.Column(elem_id = "col-mid"):
|
104 |
+
gr.HTML("""
|
105 |
+
<div style="display: flex; justify-content: center; align-items: center; text-align: center; font-size: 20px;">
|
106 |
+
<div>
|
107 |
+
Step 2. Enter a garment description ⬇️
|
108 |
+
</div>
|
109 |
+
</div>
|
110 |
+
""")
|
111 |
+
with gr.Column(elem_id = "col-right"):
|
112 |
+
gr.HTML("""
|
113 |
+
<div style="display: flex; justify-content: center; align-items: center; text-align: center; font-size: 20px;">
|
114 |
+
<div>
|
115 |
+
Step 3. Press "Run" to get try-on results
|
116 |
+
</div>
|
117 |
+
</div>
|
118 |
+
""")
|
119 |
+
with gr.Row():
|
120 |
+
with gr.Column(elem_id = "col-left"):
|
121 |
+
imgs = gr.Image(label="Person image", sources='upload', type="numpy")
|
122 |
+
example = gr.Examples(
|
123 |
+
inputs=imgs,
|
124 |
+
examples_per_page=12,
|
125 |
+
examples=human_list_path
|
126 |
+
)
|
127 |
+
with gr.Column(elem_id = "col-mid"):
|
128 |
+
garm_prompt = gr.Textbox(label="Garment description", placeholder="Enter a description of the garment...")
|
129 |
+
example_prompts = gr.Examples(
|
130 |
+
inputs=garm_prompt,
|
131 |
+
examples=["A red t-shirt", "Blue jeans", "A floral summer dress", "A black leather jacket"]
|
132 |
+
)
|
133 |
+
with gr.Column(elem_id = "col-right"):
|
134 |
+
image_out = gr.Image(label="Result", show_share_button=False)
|
135 |
+
with gr.Row():
|
136 |
+
seed = gr.Slider(
|
137 |
+
label="Seed",
|
138 |
+
minimum=0,
|
139 |
+
maximum=MAX_SEED,
|
140 |
+
step=1,
|
141 |
+
value=0,
|
142 |
+
)
|
143 |
+
randomize_seed = gr.Checkbox(label="Random seed", value=True)
|
144 |
+
with gr.Row():
|
145 |
+
seed_used = gr.Number(label="Seed used")
|
146 |
+
result_info = gr.Text(label="Response")
|
147 |
+
test_button = gr.Button(value="Run", elem_id="button")
|
148 |
+
|
149 |
+
test_button.click(fn=tryon, inputs=[imgs, garm_prompt, seed, randomize_seed], outputs=[image_out, seed_used, result_info], api_name='tryon', concurrency_limit=40)
|
150 |
+
|
151 |
+
with gr.Column(elem_id = "col-showcase"):
|
152 |
+
gr.HTML("""
|
153 |
+
<div style="display: flex; justify-content: center; align-items: center; text-align: center; font-size: 20px;">
|
154 |
+
<div> </div>
|
155 |
+
<br>
|
156 |
+
<div>
|
157 |
+
Virtual try-on examples in pairs of person images and garment descriptions
|
158 |
+
</div>
|
159 |
+
</div>
|
160 |
+
""")
|
161 |
+
show_case = gr.Examples(
|
162 |
+
examples=[
|
163 |
+
["assets/examples/model2.png", "A blue t-shirt", "assets/examples/result2.png"],
|
164 |
+
["assets/examples/model3.png", "A red dress", "assets/examples/result3.png"],
|
165 |
+
["assets/examples/model1.png", "A black suit", "assets/examples/result1.png"],
|
166 |
+
],
|
167 |
+
inputs=[imgs, garm_prompt, image_out],
|
168 |
+
label=None
|
169 |
+
)
|
170 |
+
|
171 |
+
Tryon.launch()
|