File size: 4,262 Bytes
5d333f5
 
 
 
 
 
 
 
82b445a
ca30ad2
 
 
82b445a
 
 
 
 
 
ca30ad2
 
82b445a
5d333f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ca30ad2
 
5d333f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

from mtcnn.mtcnn import MTCNN
from utils import *


face_detector = MTCNN()

# Description
# title = r"""
# 
#align="center">IDM-VTON + Outfit Anyone in the Wild 

# """

# description = r"""
# This demo combines <b>IDM-VTON </b> and <b>Outfit Anyone in the Wild </b>
# 1. Human pose detection and reconstruction using large human model from Outfit Anyone in the Wild.
# 2. Use IDM-VTON for training-free try-on.
# 3. Applying the refine network from Outfit Anyone in the Wild.

# """

css = """
.gradio-container {width: 85% !important}
"""


def onClick(cloth_image, pose_image, category, 
        caption, request: gr.Request):
    if pose_image is None:
        yield None, f"no user image found !"
        return None, "no user image found !"
    elif cloth_image is None:
        yield None, f"no cloth image found !"
        return None, "no cloth image found !"
    try:
        faces = face_detector.detect_faces(pose_image[:,:,::-1])
        if len(faces)==0:
            print(client_ip, 'faces num is 0! ', flush=True)
            yield None, "Fatal Error !!! No face detected in pose image !!! "
            return None, "Fatal Error !!! No face detected in pose image !!! "
        else:
            x, y, w, h = faces[0]["box"]
            H, W = pose_image.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 in pose image!!!"
                return None, "Fatal Error !!! Headshot is not allowed in pose image!!!"
        
        uploads = upload_imgs(ApiUrl, UploadToken, cloth_image, pose_image)
        if uploads is None:
            yield None, "fail to upload"
            return None, "fail to upload"

        infId = publicFastSwap(ApiUrl, OpenId, ApiKey, uploads, category, caption)
        if not infId:
            yield None, "fail to public you task"
            return None, "fail to public you task"

        max_try = 30
        wait_s = 3
        yield None, "start to process, please wait..."
        for i in range(max_try):
            time.sleep(wait_s)
            taskStatus = getTaskRes(ApiUrl, infId)
            if taskStatus is None: continue

            status = taskStatus['status']
            if status in ['FAILED', 'CANCELLED', 'TIMED_OUT', ]:
                yield None, f"task failed, query {i}, status {status}"
                return None, f"task failed, query {i}, status {status}"
            elif status in ['IN_QUEUE', 'IN_PROGRESS', 'IN_QUEUE', ]:
                pass
                yield None, f"task is on processing, query {i}, status {status}, please do not exit !!!"
            elif status=='COMPLETED':
                out = taskStatus['output']['job_results']['output1']
                yield out, f"task is COMPLETED"
                return out, f"{i} task COMPLETED"
        yield None, "fail to query task.."
        return None, "fail to query task.."


    except Exception as e:
        print(e)
        return None, "fail to create task"


with gr.Blocks(css=css) as demo:
    # description
    #gr.Markdown(title)
    #gr.Markdown(description)
                    
    with gr.Row():
        with gr.Column():
            with gr.Column():
                cloth_image = gr.Image(value=None, type="numpy", label="cloth")
        with gr.Column():
            with gr.Column():
                pose_image = gr.Image(value=None, type="numpy", label="user photo")
        with gr.Column():
            with gr.Column():
                category = gr.Dropdown(value="upper_cloth", choices=["upper_cloth", 
                    "lower_cloth", "full_body", "dresses"], interactive=True)
                caption = gr.Textbox(value="", interactive=True, label='cloth caption')
                
                info_text = gr.Textbox(value="", interactive=False, label='runtime information')
                run_button = gr.Button(value="Run")
                res_image = gr.Image(label="result image", value=None, type="filepath")

    run_button.click(fn=onClick, inputs=[cloth_image, pose_image, 
        category, caption, ], 
        outputs=[res_image, info_text, ])

if __name__ == "__main__":

    demo.queue(max_size=50)
    demo.launch(server_name='0.0.0.0')