selfitcamera commited on
Commit
8c56efb
β€’
1 Parent(s): 7da95c2
Files changed (3) hide show
  1. app.py +12 -14
  2. app3.py +185 -0
  3. utils.py +0 -1
app.py CHANGED
@@ -56,7 +56,6 @@ def onClick(cloth_id, pose_image, pose_id, size, request: gr.Request):
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')
@@ -71,8 +70,8 @@ def onLoad(request: gr.Request):
71
  if i>4: continue
72
  pose = inf['pose']
73
  res = inf['res']
74
- his_datas[i*2] = f"<img src=\"{pose}\" >"
75
- his_datas[i*2+1] = f"<img src=\"{res}\" >"
76
  time.sleep(3)
77
  except Exception as e:
78
  print(e)
@@ -83,7 +82,6 @@ cloth_examples = get_cloth_examples()
83
  pose_examples = get_pose_examples()
84
 
85
 
86
-
87
  # Description
88
  title = r"""
89
  <h1 align="center">Outfit Anyone in the Wild: Get rid of Annoying Restrictions for Virtual Try-on Task</h1>
@@ -139,32 +137,32 @@ with gr.Blocks(css=css) as demo:
139
 
140
  with gr.Tab('history'):
141
  with gr.Row():
142
- his_pose_image1 = gr.HTML()
143
- his_res_image1 = gr.HTML()
144
  # his_pose_image1 = gr.Image(label="pose image1", interactive=False)
145
  # his_res_image1 = gr.Image(label="result image1", interactive=False)
146
 
147
  with gr.Row():
148
- his_pose_image2 = gr.HTML()
149
- his_res_image2 = gr.HTML()
150
  # his_pose_image2 = gr.Image(label="pose image2", interactive=False)
151
  # his_res_image2 = gr.Image(label="result image2", interactive=False)
152
 
153
  with gr.Row():
154
- his_pose_image3 = gr.HTML()
155
- his_res_image3 = gr.HTML()
156
  # his_pose_image3 = gr.Image(label="pose image3", interactive=False)
157
  # his_res_image3 = gr.Image(label="result image3", interactive=False)
158
 
159
  with gr.Row():
160
- his_pose_image4 = gr.HTML()
161
- his_res_image4 = gr.HTML()
162
  # his_pose_image4 = gr.Image(label="pose image4", interactive=False)
163
  # his_res_image4 = gr.Image(label="result image4", interactive=False)
164
 
165
  with gr.Row():
166
- his_pose_image5 = gr.HTML()
167
- his_res_image5 = gr.HTML()
168
  # his_pose_image5 = gr.Image(label="pose image5", interactive=False)
169
  # his_res_image5 = gr.Image(label="result image5", interactive=False)
170
 
 
56
  print(e)
57
  return None, "fail to create task"
58
 
 
59
  def onLoad(request: gr.Request):
60
  client_ip = request.client.host
61
  x_forwarded_for = dict(request.headers).get('x-forwarded-for')
 
70
  if i>4: continue
71
  pose = inf['pose']
72
  res = inf['res']
73
+ his_datas[i*2] = f"![pose]({pose})"
74
+ his_datas[i*2+1] = f"![pose]({res})"
75
  time.sleep(3)
76
  except Exception as e:
77
  print(e)
 
82
  pose_examples = get_pose_examples()
83
 
84
 
 
85
  # Description
86
  title = r"""
87
  <h1 align="center">Outfit Anyone in the Wild: Get rid of Annoying Restrictions for Virtual Try-on Task</h1>
 
137
 
138
  with gr.Tab('history'):
139
  with gr.Row():
140
+ his_pose_image1 = gr.Markdown()
141
+ his_res_image1 = gr.Markdown()
142
  # his_pose_image1 = gr.Image(label="pose image1", interactive=False)
143
  # his_res_image1 = gr.Image(label="result image1", interactive=False)
144
 
145
  with gr.Row():
146
+ his_pose_image2 = gr.Markdown()
147
+ his_res_image2 = gr.Markdown()
148
  # his_pose_image2 = gr.Image(label="pose image2", interactive=False)
149
  # his_res_image2 = gr.Image(label="result image2", interactive=False)
150
 
151
  with gr.Row():
152
+ his_pose_image3 = gr.Markdown()
153
+ his_res_image3 = gr.Markdown()
154
  # his_pose_image3 = gr.Image(label="pose image3", interactive=False)
155
  # his_res_image3 = gr.Image(label="result image3", interactive=False)
156
 
157
  with gr.Row():
158
+ his_pose_image4 = gr.Markdown()
159
+ his_res_image4 = gr.Markdown()
160
  # his_pose_image4 = gr.Image(label="pose image4", interactive=False)
161
  # his_res_image4 = gr.Image(label="result image4", interactive=False)
162
 
163
  with gr.Row():
164
+ his_pose_image5 = gr.Markdown()
165
+ his_res_image5 = gr.Markdown()
166
  # his_pose_image5 = gr.Image(label="pose image5", interactive=False)
167
  # his_res_image5 = gr.Image(label="result image5", interactive=False)
168
 
app3.py ADDED
@@ -0,0 +1,185 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ def onLoad(request: gr.Request):
60
+ client_ip = request.client.host
61
+ x_forwarded_for = dict(request.headers).get('x-forwarded-for')
62
+ if x_forwarded_for:
63
+ client_ip = x_forwarded_for
64
+ his_datas = [None for _ in range(10)]
65
+ try:
66
+ infs = getAllInfs(ApiUrl, OpenId, ApiKey, client_ip)
67
+ print(client_ip, 'history infs: ', len(infs))
68
+ print(infs)
69
+ for i, inf in enumerate(infs):
70
+ if i>4: continue
71
+ pose = inf['pose']
72
+ res = inf['res']
73
+ his_datas[i*2] = f"<img src=\"{pose}\" >"
74
+ his_datas[i*2+1] = f"<img src=\"{res}\" >"
75
+ time.sleep(3)
76
+ except Exception as e:
77
+ print(e)
78
+ return his_datas
79
+
80
+
81
+ cloth_examples = get_cloth_examples()
82
+ pose_examples = get_pose_examples()
83
+
84
+
85
+ # Description
86
+ title = r"""
87
+ <h1 align="center">Outfit Anyone in the Wild: Get rid of Annoying Restrictions for Virtual Try-on Task</h1>
88
+ """
89
+
90
+ description = r"""
91
+ <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>
92
+ 1. Clothing models are fixed in this demo, but you can create your own in SelfitCamera WeChat applet (for Chainese users).
93
+ 2. You can upload your own pose photo, then click the run button and wait for 3~5 minutes to see the results.
94
+ 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).
95
+ 4. Share your try-on photo with your friends and enjoy! 😊"""
96
+
97
+
98
+ css = """
99
+ .gradio-container {width: 85% !important}
100
+ """
101
+ with gr.Blocks(css=css) as demo:
102
+ # description
103
+ gr.Markdown(title)
104
+ gr.Markdown(description)
105
+
106
+ with gr.Row():
107
+ with gr.Column():
108
+ with gr.Column():
109
+ # cloth_image = gr.Image(type="numpy", value=cloth_examples[0][1], label="")
110
+ cloth_image = gr.Image(sources='clipboard', type="filepath", label="",
111
+ value=cloth_examples[0][1])
112
+ cloth_id = gr.Label(value=cloth_examples[0][0], label="Clothing 3D Model", visible=False)
113
+ example = gr.Examples(inputs=[cloth_id, cloth_image],
114
+ examples_per_page=3,
115
+ examples = cloth_examples)
116
+ with gr.Column():
117
+ with gr.Column():
118
+ # pose_image = gr.Image(source='upload', value=pose_examples[0][1],
119
+ # type="numpy", label="")
120
+ pose_image = gr.Image(value=pose_examples[0][1],
121
+ type="numpy", label="")
122
+ pose_id = gr.Label(value=pose_examples[0][0], label="Pose Image", visible=False)
123
+ example_pose = gr.Examples(inputs=[pose_id, pose_image],
124
+ examples_per_page=3,
125
+ examples=pose_examples)
126
+ size_slider = gr.Slider(-2.5, 2.5, value=1, interactive=True, label="clothes size")
127
+
128
+ with gr.Column():
129
+ with gr.Column():
130
+ run_button = gr.Button(value="Run")
131
+ init_res = get_result_example(cloth_examples[0][0], pose_examples[0][0])
132
+ # res_image = gr.Image(label="result image", value=init_res, type="filepath")
133
+ res_image = gr.Image(label="result image", value=cv2.imread(init_res),
134
+ type="numpy")
135
+ info_text = gr.Textbox(value="", interactive=False,
136
+ label='runtime information')
137
+
138
+ with gr.Tab('history'):
139
+ with gr.Row():
140
+ his_pose_image1 = gr.HTML()
141
+ his_res_image1 = gr.HTML()
142
+ # his_pose_image1 = gr.Image(label="pose image1", interactive=False)
143
+ # his_res_image1 = gr.Image(label="result image1", interactive=False)
144
+
145
+ with gr.Row():
146
+ his_pose_image2 = gr.HTML()
147
+ his_res_image2 = gr.HTML()
148
+ # his_pose_image2 = gr.Image(label="pose image2", interactive=False)
149
+ # his_res_image2 = gr.Image(label="result image2", interactive=False)
150
+
151
+ with gr.Row():
152
+ his_pose_image3 = gr.HTML()
153
+ his_res_image3 = gr.HTML()
154
+ # his_pose_image3 = gr.Image(label="pose image3", interactive=False)
155
+ # his_res_image3 = gr.Image(label="result image3", interactive=False)
156
+
157
+ with gr.Row():
158
+ his_pose_image4 = gr.HTML()
159
+ his_res_image4 = gr.HTML()
160
+ # his_pose_image4 = gr.Image(label="pose image4", interactive=False)
161
+ # his_res_image4 = gr.Image(label="result image4", interactive=False)
162
+
163
+ with gr.Row():
164
+ his_pose_image5 = gr.HTML()
165
+ his_res_image5 = gr.HTML()
166
+ # his_pose_image5 = gr.Image(label="pose image5", interactive=False)
167
+ # his_res_image5 = gr.Image(label="result image5", interactive=False)
168
+
169
+
170
+ run_button.click(fn=onClick, inputs=[cloth_id, pose_image, pose_id, size_slider],
171
+ outputs=[res_image, info_text], concurrency_limit=50)
172
+
173
+ pose_image.upload(fn=onUpload, inputs=[], outputs=[pose_id],)
174
+ demo.load(onLoad, inputs=[], outputs=[his_pose_image1, his_res_image1,
175
+ his_pose_image2, his_res_image2, his_pose_image3, his_res_image3,
176
+ his_pose_image4, his_res_image4, his_pose_image5, his_res_image5,
177
+ ])
178
+
179
+ if __name__ == "__main__":
180
+
181
+ demo.queue(max_size=50)
182
+ # demo.queue(concurrency_count=60)
183
+ # demo.launch(server_name='0.0.0.0', server_port=225)
184
+ demo.launch(server_name='0.0.0.0')
185
+
utils.py CHANGED
@@ -60,7 +60,6 @@ def getAllInfs(apiUrl, openId, apiKey, clientIp):
60
  'res':OssUrl+record['showUrl']+"?thumbnail/768x768>"})
61
  # res.append({'pose':OssUrl+record['body_url'],
62
  # 'res':OssUrl+record['showUrl']})
63
-
64
  return res
65
 
66
 
 
60
  'res':OssUrl+record['showUrl']+"?thumbnail/768x768>"})
61
  # res.append({'pose':OssUrl+record['body_url'],
62
  # 'res':OssUrl+record['showUrl']})
 
63
  return res
64
 
65