selfitcamera commited on
Commit
e4c1f44
Β·
1 Parent(s): 18526bc
Files changed (2) hide show
  1. app.py +4 -2
  2. app2.py +174 -0
app.py CHANGED
@@ -136,8 +136,10 @@ with gr.Blocks(css=css) as demo:
136
 
137
  with gr.Tab('history'):
138
  with gr.Row():
139
- his_pose_image1 = gr.Image(label="pose image1", interactive=False)
140
- his_res_image1 = gr.Image(label="result image1", interactive=False)
 
 
141
 
142
  with gr.Row():
143
  his_pose_image2 = gr.Image(label="pose image2", interactive=False)
 
136
 
137
  with gr.Tab('history'):
138
  with gr.Row():
139
+ his_pose_image1 = gr.HTML()
140
+ his_res_image1 = gr.HTML()
141
+ # his_pose_image1 = gr.Image(label="pose image1", interactive=False)
142
+ # his_res_image1 = gr.Image(label="result image1", interactive=False)
143
 
144
  with gr.Row():
145
  his_pose_image2 = gr.Image(label="pose image2", interactive=False)
app2.py ADDED
@@ -0,0 +1,174 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ from utils import *
3
+
4
+
5
+ def onUpload():
6
+ return ""
7
+
8
+
9
+ def onClick(cloth_id, pose_image, pose_id, size, request: gr.Request):
10
+ if pose_image is None:
11
+ return None, "no pose image found !"
12
+ # pose_id, cloth_id = pose_id['label'], cloth_id['label']
13
+ # print(pose_id, cloth_id, size, (pose_image is None), len(pose_id)>0)
14
+ if len(pose_id)>0:
15
+ res = get_result_example(cloth_id, pose_id)
16
+ print(res)
17
+ assert os.path.exists(res), res
18
+ return res, "Done! Use the pre-run results directly, the cloth size does not take effect "
19
+ else:
20
+ try:
21
+ client_ip = request.client.host
22
+ x_forwarded_for = dict(request.headers).get('x-forwarded-for')
23
+ if x_forwarded_for:
24
+ client_ip = x_forwarded_for
25
+ timeId = int( str(time.time()).replace(".", "") )+random.randint(1000, 9999)
26
+ isUpload = upload_pose_img(ApiUrl, OpenId, ApiKey, client_ip, timeId, pose_image)
27
+ if isUpload==0:
28
+ return None, "fail to upload"
29
+ elif isUpload==-1:
30
+ return None, "There is a running task already, please wait and check the history tab"
31
+ elif isUpload==-2:
32
+ return None, "can not creat task, you have exhausted free trial quota"
33
+
34
+ taskId = publicClothSwap(ApiUrl, OpenId, ApiKey, client_ip, cloth_id, timeId, size)
35
+ if taskId==0:
36
+ return None, "fail to public you task"
37
+
38
+ max_try = 1
39
+ wait_s = 30
40
+ for i in range(max_try):
41
+ time.sleep(wait_s)
42
+ state = getInfRes(ApiUrl, OpenId, ApiKey, client_ip, timeId)
43
+ if state=='stateIs-1':
44
+ return None, "task failed, it may be that no human was detected, or there may be illegal content, etc. "
45
+ elif state=='stateIs0':
46
+ return None, "task not public success"
47
+ elif len(state)>20:
48
+ return state, "task finished"
49
+ elif (not state.startswith('stateIs')):
50
+ # return None, 'task is in queue, position is '+str(state)
51
+ pass
52
+ else:
53
+ return None, state
54
+ return None, "task has been created successfully, you can refresh the page 5~15 mins latter, and check the following history tab"
55
+ except Exception as e:
56
+ print(e)
57
+ return None, "fail to create task"
58
+
59
+
60
+ def onLoad(request: gr.Request):
61
+ client_ip = request.client.host
62
+ x_forwarded_for = dict(request.headers).get('x-forwarded-for')
63
+ if x_forwarded_for:
64
+ client_ip = x_forwarded_for
65
+ his_datas = [None for _ in range(10)]
66
+ try:
67
+ infs = getAllInfs(ApiUrl, OpenId, ApiKey, client_ip)
68
+ print(client_ip, 'history infs: ', len(infs))
69
+ for i, inf in enumerate(infs):
70
+ if i>4: continue
71
+ his_datas[i*2] = inf['pose']
72
+ his_datas[i*2+1] = inf['res']
73
+ time.sleep(3)
74
+ except Exception as e:
75
+ print(e)
76
+ return his_datas
77
+
78
+
79
+ cloth_examples = get_cloth_examples()
80
+ pose_examples = get_pose_examples()
81
+
82
+
83
+
84
+ # Description
85
+ title = r"""
86
+ <h1 align="center">Outfit Anyone in the Wild: Get rid of Annoying Restrictions for Virtual Try-on Task</h1>
87
+ """
88
+
89
+ description = r"""
90
+ <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>
91
+ 1. Clothing models are fixed in this demo, but you can create your own in SelfitCamera WeChat applet (for Chainese users).
92
+ 2. You can upload your own pose photo, then click the run button and wait for 3~5 minutes to see the results.
93
+ 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 (bind with your ip address).
94
+ 4. Share your try-on photo with your friends and enjoy! 😊"""
95
+
96
+
97
+ css = """
98
+ .gradio-container {width: 85% !important}
99
+ """
100
+ with gr.Blocks(css=css) as demo:
101
+ # description
102
+ gr.Markdown(title)
103
+ gr.Markdown(description)
104
+
105
+ with gr.Row():
106
+ with gr.Column():
107
+ with gr.Column():
108
+ # cloth_image = gr.Image(type="numpy", value=cloth_examples[0][1], label="")
109
+ cloth_image = gr.Image(sources='clipboard', type="filepath", label="",
110
+ value=cloth_examples[0][1])
111
+ cloth_id = gr.Label(value=cloth_examples[0][0], label="Clothing 3D Model", visible=False)
112
+ example = gr.Examples(inputs=[cloth_id, cloth_image],
113
+ examples_per_page=3,
114
+ examples = cloth_examples)
115
+ with gr.Column():
116
+ with gr.Column():
117
+ # pose_image = gr.Image(source='upload', value=pose_examples[0][1],
118
+ # type="numpy", label="")
119
+ pose_image = gr.Image(value=pose_examples[0][1],
120
+ type="numpy", label="")
121
+ pose_id = gr.Label(value=pose_examples[0][0], label="Pose Image", visible=False)
122
+ example_pose = gr.Examples(inputs=[pose_id, pose_image],
123
+ examples_per_page=3,
124
+ examples=pose_examples)
125
+ size_slider = gr.Slider(-2.5, 2.5, value=1, interactive=True, label="clothes size")
126
+
127
+ with gr.Column():
128
+ with gr.Column():
129
+ run_button = gr.Button(value="Run")
130
+ init_res = get_result_example(cloth_examples[0][0], pose_examples[0][0])
131
+ # res_image = gr.Image(label="result image", value=init_res, type="filepath")
132
+ res_image = gr.Image(label="result image", value=cv2.imread(init_res),
133
+ type="numpy")
134
+ info_text = gr.Textbox(value="", interactive=False,
135
+ label='runtime information')
136
+
137
+ with gr.Tab('history'):
138
+ with gr.Row():
139
+ his_pose_image1 = gr.Image(label="pose image1", interactive=False)
140
+ his_res_image1 = gr.Image(label="result image1", interactive=False)
141
+
142
+ with gr.Row():
143
+ his_pose_image2 = gr.Image(label="pose image2", interactive=False)
144
+ his_res_image2 = gr.Image(label="result image2", interactive=False)
145
+
146
+ with gr.Row():
147
+ his_pose_image3 = gr.Image(label="pose image3", interactive=False)
148
+ his_res_image3 = gr.Image(label="result image3", interactive=False)
149
+
150
+ with gr.Row():
151
+ his_pose_image4 = gr.Image(label="pose image4", interactive=False)
152
+ his_res_image4 = gr.Image(label="result image4", interactive=False)
153
+
154
+ with gr.Row():
155
+ his_pose_image5 = gr.Image(label="pose image5", interactive=False)
156
+ his_res_image5 = gr.Image(label="result image5", interactive=False)
157
+
158
+
159
+ run_button.click(fn=onClick, inputs=[cloth_id, pose_image, pose_id, size_slider],
160
+ outputs=[res_image, info_text], concurrency_limit=50)
161
+
162
+ pose_image.upload(fn=onUpload, inputs=[], outputs=[pose_id],)
163
+ demo.load(onLoad, inputs=[], outputs=[his_pose_image1, his_res_image1,
164
+ his_pose_image2, his_res_image2, his_pose_image3, his_res_image3,
165
+ his_pose_image4, his_res_image4, his_pose_image5, his_res_image5,
166
+ ])
167
+
168
+ if __name__ == "__main__":
169
+
170
+ demo.queue(max_size=50)
171
+ # demo.queue(concurrency_count=60)
172
+ # demo.launch(server_name='0.0.0.0', server_port=225)
173
+ demo.launch(server_name='0.0.0.0')
174
+