File size: 7,274 Bytes
996c29a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

from utils import *


def onUpload():
    return ""


def onClick(cloth_id, pose_image, pose_id, size, request: gr.Request):
    if pose_image is None:
        return None, "no pose image found !"
    print(pose_id, cloth_id)
    # pose_id, cloth_id = pose_id['label'], cloth_id['label']
    print(pose_id, cloth_id, size, (pose_image is None), len(pose_id)>0)
    if len(pose_id)>0:
        res = get_result_example(cloth_id, pose_id)
        # print(res)
        assert os.path.exists(res), res
        return res, "Done! Use the pre-run results directly, the cloth size does not take effect "

    else:
        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
            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:
                return None, "fail to upload"
            elif isUpload==-1:
                return None, "There is a running task already, please wait and check the history tab"
            elif isUpload==-2:
                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:
                return None, "fail to public you task"

            max_try = 10
            wait_s = 30
            for i in range(max_try):
                time.sleep(wait_s)
                state = getInfRes(ApiUrl, OpenId, ApiKey, client_ip, timeId)
                if state=='stateIs-1':
                    return None, "task failed, it may be that no human was detected, or there may be illegal content, etc. "
                elif state=='stateIs0':
                    return None, "task not public success"
                elif len(state)>20:
                    return state, "task finished"
                elif (not state.startswith('stateIs')):
                    # return None, 'task is in queue, position is '+str(state)
                    pass
                else:
                    return None, state
            return None, "task has been created, you can refresh the page latter, and check the following history tab"
        except Exception as e:
            print(e)
            return None, "fail to create task"


def onLoad(request: gr.Request):
    client_ip = request.client.host
    x_forwarded_for = dict(request.headers).get('x-forwarded-for')
    if x_forwarded_for:
        client_ip = x_forwarded_for
    his_datas = [None for _ in range(10)]
    try:
        infs = getAllInfs(ApiUrl, OpenId, ApiKey, client_ip)
        print(client_ip, 'infs', len(infs))
        for i, inf in enumerate(infs):
            if i>4: continue
            his_datas[i*2] = inf['pose']
            his_datas[i*2+1] = inf['res']
    except Exception as e:
        print(e)
    return his_datas


cloth_examples = get_cloth_examples()
pose_examples = get_pose_examples()



# Description
title = r"""
<h1 align="center">Outfit Anyone in the Wild: Get rid of Annoying Restrictions for Virtual Try-on Task</h1>
"""

description = r"""
<b>Official πŸ€— Gradio demo</b> for <a href='https://github.com/selfitcamera/Outfit-Anyone-in-the-Wild' target='_blank'><b>Outfit Anyone in the Wild: Get rid of Annoying Restrictions for Virtual Try-on Task</b></a>.<br>
1. Clothing models are fixed in this demo, but you can create your own in SelfitCamera WeChat applet (for Chainese users).
2. You can upload your own pose photo, then click the run button and wait for 3~5 minutes to see the results.
3. After submitting the task, feel free to leave this page. Everytime you refresh this page, completed tasks will be displayed on the history tab page below (bind with your ip address).
4. Share your try-on photo with your friends and enjoy! 😊"""


css = """
.gradio-container {width: 85% !important}
"""
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(type="numpy", value=cloth_examples[0][1], label="")
                cloth_id = gr.Label(value=cloth_examples[0][0], label="Clothing 3D Model", visible=False)
                example = gr.Examples(inputs=[cloth_id, cloth_image],
                                      examples_per_page=3,
                                      examples = cloth_examples)
        with gr.Column():
            with gr.Column():
                # pose_image = gr.Image(source='upload', value=pose_examples[0][1], 
                #     type="numpy", label="")
                pose_image = gr.Image(value=pose_examples[0][1], 
                    type="numpy", label="")                
                pose_id = gr.Label(value=pose_examples[0][0], label="Pose Image", visible=False)
                example_pose = gr.Examples(inputs=[pose_id, pose_image],
                                          examples_per_page=3,
                                          examples=pose_examples)
                size_slider = gr.Slider(-2.5, 2.5, value=1, interactive=True, label="clothes size")
            
        with gr.Column():
            with gr.Column():
                run_button = gr.Button(value="Run")
                res_image = gr.Image(label="result image", value=None)
                info_text = gr.Textbox(value="", interactive=False, 
                    label='runtime information')
                
    with gr.Tab('history'):
        with gr.Row():
            his_pose_image1 = gr.Image(label="pose image1", interactive=False)
            his_res_image1 = gr.Image(label="result image1", interactive=False)

        with gr.Row():
            his_pose_image2 = gr.Image(label="pose image2", interactive=False)
            his_res_image2 = gr.Image(label="result image2", interactive=False)

        with gr.Row():
            his_pose_image3 = gr.Image(label="pose image3", interactive=False)
            his_res_image3 = gr.Image(label="result image3", interactive=False)

        with gr.Row():
            his_pose_image4 = gr.Image(label="pose image4", interactive=False)
            his_res_image4 = gr.Image(label="result image4", interactive=False)

        with gr.Row():
            his_pose_image5 = gr.Image(label="pose image5", interactive=False)
            his_res_image5 = gr.Image(label="result image5", interactive=False)


    run_button.click(fn=onClick, inputs=[cloth_id, pose_image, pose_id, size_slider], 
                     outputs=[res_image, info_text], concurrency_limit=60)

    pose_image.upload(fn=onUpload, inputs=[], outputs=[pose_id],)
    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,
        ])

if __name__ == "__main__":

    demo.queue(max_size=60)
    # demo.queue(concurrency_count=60)
    # demo.launch(server_name='0.0.0.0', server_port=225)
    demo.launch(server_name='0.0.0.0')