File size: 7,344 Bytes
5d7df65 90f5950 5d7df65 f7221f1 5d7df65 a0c0959 5d7df65 c24d322 593aefc be80e65 593aefc 8ea6dc9 90f5950 44d63f3 92dcd08 44d63f3 813e34f 686b24a 44d63f3 686b24a 593aefc 686b24a 593aefc 668f50f 593aefc 668f50f 5d7df65 44d63f3 813e34f 66e14ec 44d63f3 813e34f 5d7df65 44d63f3 92dcd08 5d7df65 44d63f3 813e34f a1232f9 44d63f3 813e34f 44d63f3 90f5950 92dcd08 90f5950 92dcd08 90f5950 44d63f3 fd0a2c1 90f5950 a17a9fc 90f5950 |
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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
import os
import requests
import json
import time
import cv2
import base64
import random
import numpy as np
import gradio as gr
MAX_SEED = 999999
# β
Pixelcut API Key
pixelcut_api_key = "sk_299d9c6e36d240cbb3dd65fcbac947a4"
# β
ImgBB API Key (for uploading images to get valid URLs)
imgbb_api_key = "03a2ddf1ffa5df33a3999cf20c2fb20f"
# π― Convert image to PNG format (keeps the blue tint glitch!)
def convert_to_png(image):
return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# π₯ Resize large images to prevent upload failures (ImgBB limit: 32MB)
def resize_image(image, max_size=1024):
height, width = image.shape[:2]
if max(height, width) > max_size:
scale = max_size / max(height, width)
return cv2.resize(image, (int(width * scale), int(height * scale)))
return image
# π οΈ Upload images to ImgBB (fixed payload!)
def upload_image_to_imgbb(image_data):
url = f"https://api.imgbb.com/1/upload?key={imgbb_api_key}"
files = {"image": image_data}
response = requests.post(url, files=files)
if response.status_code == 200:
return response.json().get("data", {}).get("url")
else:
print("β ImgBB upload failed:", response.text)
return None
# π Main try-on function (with blue tint + garment overlay)
def tryon(person_img, cloth_img, seed, random_seed):
import cv2
import numpy as np
# π² Handle seed
if random_seed:
seed = random.randint(0, 1000000)
# π΅ Convert images to RGB (keeps the blue tint glitch!)
person_img = cv2.cvtColor(person_img, cv2.COLOR_BGR2RGB)
cloth_img = cv2.cvtColor(cloth_img, cv2.COLOR_BGR2RGB)
# βοΈ Resize both images to 256x256
person_img = cv2.resize(person_img, (256, 256))
cloth_img = cv2.resize(cloth_img, (256, 256))
# π₯ Blend garment onto person (simple overlay β still has blue tint)
output_img = person_img.copy()
h, w, _ = person_img.shape
g_h, g_w, _ = cloth_img.shape
# Position the garment roughly on the torso (manual offset)
x_offset = (w - g_w) // 2
y_offset = int(h * 0.35)
# π οΈ Add garment onto person (no transparency support β raw overlay)
output_img[y_offset:y_offset + g_h, x_offset:x_offset + g_w] = cloth_img
return output_img, seed, "β
Success (Blue tint + Garment added)"
# π§ Paths for examples
example_path = os.path.join(os.path.dirname(__file__), "assets")
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]
# π§ Paths for examples
example_path = os.path.join(os.path.dirname(__file__), "assets")
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]
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
def change_imgs(image1, image2):
return image1, image2
with gr.Blocks(css=css) as Tryon:
gr.HTML(load_description("assets/new_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. Upload a garment image β¬οΈ
</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")
# category = gr.Dropdown(label="Garment category", choices=['upper_body', 'lower_body', 'dresses'], value="upper_body")
example = gr.Examples(
inputs=imgs,
examples_per_page=12,
examples=human_list_path
)
with gr.Column(elem_id = "col-mid"):
garm_img = gr.Image(label="Garment image", sources='upload', type="numpy")
example = gr.Examples(
inputs=garm_img,
examples_per_page=12,
examples=garm_list_path
)
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")
# try_button = gr.Button(value="Run", elem_id="button")
test_button = gr.Button(value="Run", elem_id="button")
# try_button.click(fn=start_tryon, inputs=[imgs, garm_img, seed, randomize_seed], outputs=[image_out, seed_used, result_info], api_name='tryon',concurrency_limit=10)
test_button.click(fn=tryon, inputs=[imgs, garm_img, seed, randomize_seed], outputs=[image_out, seed_used, result_info], api_name=False, concurrency_limit=45)
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, garm_img, image_out],
label=None
)
Tryon.queue(api_open=False).launch(show_api=False)
|