Bhargavssss commited on
Commit
e7f8052
·
verified ·
1 Parent(s): 7725cde

Upload 3 files

Browse files
Files changed (3) hide show
  1. README.md +6 -5
  2. app.py +244 -0
  3. requirements.txt +8 -0
README.md CHANGED
@@ -1,12 +1,13 @@
1
  ---
2
- title: Info Try On Deep Learning
3
- emoji: 🦀
4
- colorFrom: pink
5
- colorTo: green
6
  sdk: gradio
7
- sdk_version: 4.42.0
8
  app_file: app.py
9
  pinned: false
 
10
  ---
11
 
12
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: Info-Try-on
3
+ emoji: 👕
4
+ colorFrom: purple
5
+ colorTo: gray
6
  sdk: gradio
7
+ sdk_version: 4.38.1
8
  app_file: app.py
9
  pinned: false
10
+ license: apache-2.0
11
  ---
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,244 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import cv2
3
+ from PIL import Image
4
+ import gradio as gr
5
+ import numpy as np
6
+ import random
7
+ import base64
8
+ import requests
9
+ import json
10
+ import time
11
+ from requests.adapters import HTTPAdapter
12
+
13
+ def tryon(person_img, garment_img, seed, randomize_seed):
14
+ post_start_time = time.time()
15
+ if person_img is None or garment_img is None:
16
+ return None, None, "Empty image"
17
+ if randomize_seed:
18
+ seed = random.randint(0, MAX_SEED)
19
+ encoded_person_img = cv2.imencode('.jpg', cv2.cvtColor(person_img, cv2.COLOR_RGB2BGR))[1].tobytes()
20
+ encoded_person_img = base64.b64encode(encoded_person_img).decode('utf-8')
21
+ encoded_garment_img = cv2.imencode('.jpg', cv2.cvtColor(garment_img, cv2.COLOR_RGB2BGR))[1].tobytes()
22
+ encoded_garment_img = base64.b64encode(encoded_garment_img).decode('utf-8')
23
+
24
+ url = "http://" + os.environ['tryon_url'] + "Submit"
25
+ token = os.environ['token']
26
+ cookie = os.environ['Cookie']
27
+ referer = os.environ['referer']
28
+ headers = {'Content-Type': 'application/json', 'token': token, 'Cookie': cookie, 'referer': referer}
29
+ data = {
30
+ "clothImage": encoded_garment_img,
31
+ "humanImage": encoded_person_img,
32
+ "seed": seed
33
+ }
34
+ try:
35
+ response = requests.post(url, headers=headers, data=json.dumps(data), timeout=50)
36
+ print("post response code", response.status_code)
37
+ if response.status_code == 200:
38
+ result = response.json()['result']
39
+ status = result['status']
40
+ if status == "success":
41
+ uuid = result['result']
42
+ print(uuid)
43
+ except Exception as err:
44
+ print(f"Error: {err}")
45
+ raise gr.Error("Too many users, please try again later")
46
+ post_end_time = time.time()
47
+ print(f"post time used: {post_end_time-post_start_time}")
48
+
49
+ get_start_time =time.time()
50
+ time.sleep(9)
51
+ Max_Retry = 10
52
+ result_img = None
53
+ for i in range(Max_Retry):
54
+ try:
55
+ url = "http://" + os.environ['tryon_url'] + "Query?taskId=" + uuid
56
+ response = requests.get(url, headers=headers, timeout=15)
57
+ print("get response code", response.status_code)
58
+ if response.status_code == 200:
59
+ result = response.json()['result']
60
+ status = result['status']
61
+ if status == "success":
62
+ result = base64.b64decode(result['result'])
63
+ result_np = np.frombuffer(result, np.uint8)
64
+ result_img = cv2.imdecode(result_np, cv2.IMREAD_UNCHANGED)
65
+ result_img = cv2.cvtColor(result_img, cv2.COLOR_RGB2BGR)
66
+ info = "Success"
67
+ break
68
+ elif status == "error":
69
+ raise gr.Error("Too many users, please try again later")
70
+ else:
71
+ print(response.text)
72
+ info = "URL error, pleace contact the admin"
73
+ except requests.exceptions.ReadTimeout:
74
+ print("timeout")
75
+ info = "Too many users, please try again later"
76
+ time.sleep(1)
77
+ get_end_time = time.time()
78
+ print(f"get time used: {get_end_time-get_start_time}")
79
+
80
+ return result_img, seed, info
81
+
82
+ def start_tryon(person_img, garment_img, seed, randomize_seed):
83
+ start_time = time.time()
84
+ if person_img is None or garment_img is None:
85
+ return None, None, "Empty image"
86
+ if randomize_seed:
87
+ seed = random.randint(0, MAX_SEED)
88
+ encoded_person_img = cv2.imencode('.jpg', cv2.cvtColor(person_img, cv2.COLOR_RGB2BGR))[1].tobytes()
89
+ encoded_person_img = base64.b64encode(encoded_person_img).decode('utf-8')
90
+ encoded_garment_img = cv2.imencode('.jpg', cv2.cvtColor(garment_img, cv2.COLOR_RGB2BGR))[1].tobytes()
91
+ encoded_garment_img = base64.b64encode(encoded_garment_img).decode('utf-8')
92
+
93
+ url = "http://" + os.environ['tryon_url']
94
+ token = os.environ['token']
95
+ cookie = os.environ['Cookie']
96
+ referer = os.environ['referer']
97
+
98
+ headers = {'Content-Type': 'application/json', 'token': token, 'Cookie': cookie, 'referer': referer}
99
+ data = {
100
+ "clothImage": encoded_garment_img,
101
+ "humanImage": encoded_person_img,
102
+ "seed": seed
103
+ }
104
+
105
+ result_img = None
106
+ try:
107
+ session = requests.Session()
108
+ response = session.post(url, headers=headers, data=json.dumps(data), timeout=60)
109
+ print("response code", response.status_code)
110
+ if response.status_code == 200:
111
+ result = response.json()['result']
112
+ status = result['status']
113
+ if status == "success":
114
+ result = base64.b64decode(result['result'])
115
+ result_np = np.frombuffer(result, np.uint8)
116
+ result_img = cv2.imdecode(result_np, cv2.IMREAD_UNCHANGED)
117
+ result_img = cv2.cvtColor(result_img, cv2.COLOR_RGB2BGR)
118
+ info = "Success"
119
+ else:
120
+ info = "Try again latter"
121
+ else:
122
+ print(response.text)
123
+ info = "URL error, pleace contact the admin"
124
+ except requests.exceptions.ReadTimeout:
125
+ print("timeout")
126
+ info = "Too many users, please try again later"
127
+ raise gr.Error("Too many users, please try again later")
128
+ except Exception as err:
129
+ print(f"其他错误: {err}")
130
+ info = "Error, pleace contact the admin"
131
+ end_time = time.time()
132
+ print(f"time used: {end_time-start_time}")
133
+
134
+ return result_img, seed, info
135
+
136
+ MAX_SEED = 999999
137
+
138
+ example_path = os.path.join(os.path.dirname(__file__), 'assets')
139
+
140
+ garm_list = os.listdir(os.path.join(example_path,"cloth"))
141
+ garm_list_path = [os.path.join(example_path,"cloth",garm) for garm in garm_list]
142
+
143
+ human_list = os.listdir(os.path.join(example_path,"human"))
144
+ human_list_path = [os.path.join(example_path,"human",human) for human in human_list]
145
+
146
+ css="""
147
+ #col-left {
148
+ margin: 0 auto;
149
+ max-width: 430px;
150
+ }
151
+ #col-mid {
152
+ margin: 0 auto;
153
+ max-width: 430px;
154
+ }
155
+ #col-right {
156
+ margin: 0 auto;
157
+ max-width: 430px;
158
+ }
159
+ #col-showcase {
160
+ margin: 0 auto;
161
+ max-width: 1100px;
162
+ }
163
+ #button {
164
+ color: blue;
165
+ }
166
+ """
167
+
168
+ def load_description(fp):
169
+ with open(fp, 'r', encoding='utf-8') as f:
170
+ content = f.read()
171
+ return content
172
+
173
+ def change_imgs(image1, image2):
174
+ return image1, image2
175
+
176
+ with gr.Blocks(css=css) as Tryon:
177
+ gr.HTML(load_description("assets/title.md"))
178
+ with gr.Row():
179
+ with gr.Column(elem_id = "col-left"):
180
+ gr.HTML("""
181
+ <div style="display: flex; justify-content: center; align-items: center; text-align: center; font-size: 20px;">
182
+ <div>
183
+ Step 1. Upload a person image ⬇️
184
+ </div>
185
+ </div>
186
+ """)
187
+ with gr.Column(elem_id = "col-mid"):
188
+ gr.HTML("""
189
+ <div style="display: flex; justify-content: center; align-items: center; text-align: center; font-size: 20px;">
190
+ <div>
191
+ Step 2. Upload a garment image ⬇️
192
+ </div>
193
+ </div>
194
+ """)
195
+ with gr.Column(elem_id = "col-right"):
196
+ gr.HTML("""
197
+ <div style="display: flex; justify-content: center; align-items: center; text-align: center; font-size: 20px;">
198
+ <div>
199
+ Step 3. Press “Run” to get try-on results
200
+ </div>
201
+ </div>
202
+ """)
203
+ with gr.Row():
204
+ with gr.Column(elem_id = "col-left"):
205
+ imgs = gr.Image(label="Person image", sources='upload', type="numpy")
206
+ # category = gr.Dropdown(label="Garment category", choices=['upper_body', 'lower_body', 'dresses'], value="upper_body")
207
+ example = gr.Examples(
208
+ inputs=imgs,
209
+ examples_per_page=12,
210
+ examples=human_list_path
211
+ )
212
+ with gr.Column(elem_id = "col-mid"):
213
+ garm_img = gr.Image(label="Garment image", sources='upload', type="numpy")
214
+ example = gr.Examples(
215
+ inputs=garm_img,
216
+ examples_per_page=12,
217
+ examples=garm_list_path
218
+ )
219
+ with gr.Column(elem_id = "col-right"):
220
+ image_out = gr.Image(label="Result", show_share_button=False)
221
+ with gr.Row():
222
+ seed = gr.Slider(
223
+ label="Seed",
224
+ minimum=0,
225
+ maximum=MAX_SEED,
226
+ step=1,
227
+ value=0,
228
+ )
229
+ randomize_seed = gr.Checkbox(label="Random seed", value=True)
230
+ with gr.Row():
231
+ seed_used = gr.Number(label="Seed used")
232
+ result_info = gr.Text(label="Response")
233
+ # try_button = gr.Button(value="Run", elem_id="button")
234
+ test_button = gr.Button(value="Run", elem_id="button")
235
+
236
+
237
+ # try_button.click(fn=start_tryon, inputs=[imgs, garm_img, seed, randomize_seed], outputs=[image_out, seed_used, result_info], api_name='tryon',concurrency_limit=10)
238
+ test_button.click(fn=tryon, inputs=[imgs, garm_img, seed, randomize_seed], outputs=[image_out, seed_used, result_info], api_name='tryon',concurrency_limit=10)
239
+
240
+
241
+
242
+ # ip = requests.get('http://ifconfig.me/ip', timeout=1).text.strip()
243
+ # print("ip address", ip)
244
+ Tryon.queue(max_size = 20).launch(max_threads = 5)
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ accelerate
2
+ diffusers
3
+ invisible_watermark
4
+ torch
5
+ transformers
6
+ xformers
7
+ opencv-python
8
+ requests