Spaces:
Running
Running
File size: 5,340 Bytes
0173443 |
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 |
import cv2
from mtcnn.mtcnn import MTCNN
from utils import *
cloth_examples = get_cloth_examples(hr=0)
cloth_hr_examples = get_cloth_examples(hr=1)
pose_examples = get_pose_examples()
face_detector = MTCNN()
# Description
title = r"""
<h1 align="center">VTON Test Demo</h1>
"""
def onClick(cloth_image, pose_image, request: gr.Request):
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])
try:
client_ip = request.client.host
x_forwarded_for = dict(request.headers).get('x-forwarded-for')
if x_forwarded_for:
client_ip = x_forwarded_for
pose_np = cv2.imread(pose_image)
faces = face_detector.detect_faces(pose_np[:,:,::-1])
if len(faces)==0:
print(client_ip, 'faces num is 0! ', flush=True)
yield None, "Fatal Error !!! No face detected !!! You must upload a human photo!!! Not clothing photo!!!", ""
return None, "Fatal Error !!! No face detected !!! You must upload a human photo!!! Not clothing photo!!!", ""
else:
x, y, w, h = faces[0]["box"]
H, W = pose_np.shape[:2]
max_face_ratio = 1/3.3
if w/W>max_face_ratio or h/H>max_face_ratio:
yield None, "Fatal Error !!! Headshot is not allowed !!! You must upload a full-body or half-body photo!!!", ""
return None, "Fatal Error !!! Headshot is not allowed !!! You must upload a full-body or half-body photo!!!", ""
if not check_region_warp(client_ip):
yield None, "Failed !!! Our server is under maintenance, please try again later", ""
return None, "Failed !!! Our server is under maintenance, please try again later", ""
# client_ip = '8.8.8.8'
yield None, "begin to upload ", ""
timeId = int( str(time.time()).replace(".", "") )+random.randint(1000, 9999)
upload_url = upload_pose_img(client_ip, timeId, pose_image)
# exit(0)
yield None, "begin to public task ", ""
# return None, "begin to public task ", ""
if len(upload_url)==0:
yield None, "fail to upload", ""
return None, "fail to upload", ""
public_res = publicClothSwap(upload_url, cloth_id, is_hr=0)
if public_res is None:
yield None, "fail to public you task", ""
return None, "fail to public you task", ""
print(client_ip, public_res['mid_result'])
yield public_res['mid_result'], f"task is processing", ""
max_try = 120*3
wait_s = 0.5
for i in range(max_try):
time.sleep(wait_s)
state = getInfRes(public_res['id'])
timestamp = int(time.time() * 1000)
if state is None:
yield None, "task query failed,", ""
elif state['status']=='PROCESSING':
yield None, f"wait {i}", ""
elif state['status']=='SUCCEED':
yield state['output1'] + f"?t={timestamp}", f"task finished, {state['msg']}", ""
return state['output1'] + f"?t={timestamp}", f"task finished, {state['msg']}", ""
elif state['status']=='FAILED':
yield None, f"task failed, {state['msg']}", ""
return None, f"task failed, {state['msg']}", ""
else:
yield None, f"{i}", ""
return None, "no machine...", ""
except Exception as e:
print(e)
raise e
return None, "fail to create task", ""
with gr.Blocks() as demo:
gr.Markdown(title)
with gr.Row():
with gr.Column():
cloth_image = gr.Image(value=None, interactive=False, type="filepath", label="choose a clothing")
example = gr.Examples(inputs=cloth_image,examples_per_page=20,examples=cloth_examples, label="clothing")
hr_example = gr.Examples(inputs=cloth_image,examples_per_page=9,examples=cloth_hr_examples, label="invalid clothing")
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=20,
examples=pose_examples)
with gr.Column():
with gr.Column():
run_button = gr.Button(value="Submit")
info_text = gr.Textbox(value="", interactive=False,
label='log')
res_image = gr.Image(label="result image", value=None, type="filepath")
MK01 = gr.Markdown()
run_button.click(fn=onClick, inputs=[cloth_image, pose_image],
outputs=[res_image, info_text, MK01])
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')
|