Spaces:
Running
Running
from utils import * | |
cloth_examples = get_cloth_examples() | |
pose_examples = get_pose_examples() | |
tip1, tip2 = get_tips() | |
# Description | |
title = r""" | |
<h1 align="center">Outfit Anyone in the Wild: Best Virtual Try-on Task Ever</h1> | |
""" | |
description = r""" | |
<b>Test results on </b> <a href='https://heybeautify.online/ClothData/Publics/Shows/shows/man_v2/man_v2.html' target='_blank'><b>man01</b></a>.<br> | |
<b>Test results on </b> <a href='https://heybeautify.online/ClothData/Publics/Shows/shows/cider/cider_0403.html' target='_blank'><b>woman01</b></a>.<br> | |
""" | |
mk_guide = "" | |
def onClick(cloth_image, pose_image, size): | |
if pose_image is None: | |
yield None, "no pose image found !", "" | |
return None, "no pose image found !", "" | |
if cloth_image is None: | |
yield None, "no cloth image found !", "" | |
return None, "no cloth image found !", "" | |
pose_id = os.path.basename(pose_image).split(".")[0] | |
cloth_id = int(os.path.basename(cloth_image).split(".")[0]) | |
if len(pose_id)>0: | |
res = get_result_example(cloth_id, pose_id) | |
print(res) | |
if os.path.exists(res): | |
yield res, "Done! Use the pre-run results directly, the cloth size does not take effect ", mk_guide | |
return res, "Done! Use the pre-run results directly, the cloth size does not take effect ", mk_guide | |
try: | |
client_ip = '0' | |
timeId = int( str(time.time()).replace(".", "") )+random.randint(1000, 9999) | |
isUpload = upload_pose_img(ApiUrl, OpenId, ApiKey, client_ip, timeId, pose_image) | |
if isUpload==0: | |
yield None, "fail to upload", "" | |
return None, "fail to upload", "" | |
elif isUpload==-1: | |
yield None, "There is a running task already, please wait and check the history tab. Please remember to give us a star on github, thx~", "" | |
return None, "There is a running task already, please wait and check the history tab. Please remember to give us a star on github, thx~", "" | |
elif isUpload==-2: | |
yield None, "can not creat task, you have exhausted free trial quota", "" | |
return None, "can not creat task, you have exhausted free trial quota", "" | |
taskId = publicClothSwap(ApiUrl, OpenId, ApiKey, client_ip, cloth_id, timeId, size) | |
if taskId==0: | |
yield None, "fail to public you task", "" | |
return None, "fail to public you task", "" | |
max_try = 100 | |
wait_s = 1 | |
for i in range(max_try): | |
time.sleep(wait_s) | |
state = getInfRes(ApiUrl, OpenId, ApiKey, client_ip, timeId) | |
if state=='stateIs-1': | |
yield None, "task failed, it may be that no human was detected, or there may be illegal content, etc. ", "" | |
return None, "task failed, it may be that no human was detected, or there may be illegal content, etc. ", "" | |
elif state=='stateIs0': | |
yield None, "task not public success", "" | |
return None, "task not public success", "" | |
elif len(state)>20: | |
yield state, "task finished", "" | |
return state, "task finished", "" | |
elif (not state.startswith('stateIs')): | |
yield None, f"task is on processing, query {i}", "" | |
pass | |
else: | |
return None, state, "" | |
return None, "task has been created successfully, you can refresh the page 5~15 mins latter, and check the following history tab", "" | |
except Exception as e: | |
print(e) | |
raise e | |
return None, "fail to create task", "" | |
def onLoad(request: gr.Request): | |
client_ip = '8.8.8.8' | |
his_datas = [None for _ in range(10)] | |
info = '' | |
try: | |
infs = getAllInfs(ApiUrl, OpenId, ApiKey, client_ip) | |
print(client_ip, 'history infs: ', len(infs)) | |
cnt = 0 | |
finish_n, fail_n, queue_n = 0, 0, 0 | |
for i, inf in enumerate(infs): | |
if inf['state']==2: | |
if cnt>4: continue | |
pose, res = inf['pose'], inf['res'] | |
his_datas[cnt*2] = f"<img src=\"{pose}\" >" | |
his_datas[cnt*2+1] = f"<img src=\"{res}\" >" | |
finish_n += 1 | |
cnt += 1 | |
elif inf['state'] in [-1, -2, 0]: | |
fail_n += 1 | |
elif inf['state'] in [1]: | |
queue_n += 1 | |
info = f"{client_ip}, you have {finish_n} successed tasks, {queue_n} running tasks, {fail_n} failed tasks." | |
if fail_n>0: | |
info = info+" Please upload a half/full-body human image, not just a clothing image!!!" | |
if queue_n>0: | |
info = info+" Wait for 1 min and refresh this page, successed results will display in the history tab at the bottom" | |
except Exception as e: | |
print(e) | |
his_datas = his_datas + [info] | |
return his_datas | |
with gr.Blocks() as demo: | |
gr.Markdown(title) | |
gr.Markdown(description) | |
with gr.Accordion('upload tips', open=False): | |
with gr.Row(): | |
gr.HTML(f"<img src=\"{tip1}\" >") | |
gr.HTML(f"<img src=\"{tip2}\" >") | |
with gr.Row(): | |
with gr.Column(): | |
cloth_image = gr.Image(value=None, sources='clipboard', type="filepath", label="choose a outfit") | |
example = gr.Examples(inputs=cloth_image, | |
examples_per_page=18, | |
examples=cloth_examples) | |
with gr.Column(): | |
pose_image = gr.Image(value=None, type="filepath", label="choose/upload a photo") | |
example_pose = gr.Examples(inputs=pose_image, | |
examples_per_page=18, | |
examples=pose_examples) | |
with gr.Column(): | |
with gr.Column(): | |
size_slider = gr.Slider(-3, 3, value=1, interactive=True, label="clothes size") | |
run_button = gr.Button(value="Run") | |
info_text = gr.Textbox(value="", interactive=False, | |
label='runtime information') | |
res_image = gr.Image(label="result image", value=None, type="filepath") | |
MK01 = gr.Markdown() | |
with gr.Tab('history'): | |
with gr.Row(): | |
MK02 = gr.Markdown() | |
with gr.Row(): | |
his_pose_image1 = gr.HTML() | |
his_res_image1 = gr.HTML() | |
with gr.Row(): | |
his_pose_image2 = gr.HTML() | |
his_res_image2 = gr.HTML() | |
with gr.Row(): | |
his_pose_image3 = gr.HTML() | |
his_res_image3 = gr.HTML() | |
with gr.Row(): | |
his_pose_image4 = gr.HTML() | |
his_res_image4 = gr.HTML() | |
with gr.Row(): | |
his_pose_image5 = gr.HTML() | |
his_res_image5 = gr.HTML() | |
run_button.click(fn=onClick, inputs=[cloth_image, pose_image, size_slider], | |
outputs=[res_image, info_text, MK01]) | |
demo.load(onLoad, inputs=[], outputs=[his_pose_image1, his_res_image1, | |
his_pose_image2, his_res_image2, his_pose_image3, his_res_image3, | |
his_pose_image4, his_res_image4, his_pose_image5, his_res_image5, | |
MK02]) | |
if __name__ == "__main__": | |
demo.queue(max_size=50) | |
# demo.queue(concurrency_count=60) | |
# demo.launch(server_name='0.0.0.0', server_port=225) | |
demo.launch(server_name='0.0.0.0') | |