lixiang46 commited on
Commit
18f423a
1 Parent(s): 64d0526

add test button

Browse files
Files changed (1) hide show
  1. app.py +64 -0
app.py CHANGED
@@ -8,6 +8,68 @@ import base64
8
  import requests
9
  import json
10
  import time
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
 
13
  def start_tryon(person_img, garment_img, seed, randomize_seed):
@@ -162,9 +224,11 @@ with gr.Blocks(css=css) as Tryon:
162
  seed_used = gr.Number(label="Seed used")
163
  result_info = gr.Text(label="Response")
164
  try_button = gr.Button(value="Run", elem_id="button")
 
165
 
166
 
167
  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)
 
168
 
169
  with gr.Column(elem_id = "col-showcase"):
170
  gr.HTML("""
 
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
+ 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
+ response = requests.post(url, headers=headers, data=json.dumps(data), timeout=40)
35
+ print("post code", response.status_code)
36
+ if response.status_code == 200:
37
+ result = response.json()['result']
38
+ status = result['status']
39
+ if status == "success":
40
+ uuid = result['uuid']
41
+ print(uuid)
42
+
43
+ try:
44
+ url = "http://" + os.environ['tryon_url'] + "Query?" + uuid
45
+ session = requests.Session()
46
+ session.mount("http://", HTTPAdapter(max_retries=3))
47
+ response = session.get(url, headers=headers, timeout=10)
48
+ print("response code", response.status_code)
49
+ if response.status_code == 200:
50
+ result = response.json()['result']
51
+ status = result['status']
52
+ if status == "success":
53
+ result = base64.b64decode(result['result'])
54
+ result_np = np.frombuffer(result, np.uint8)
55
+ result_img = cv2.imdecode(result_np, cv2.IMREAD_UNCHANGED)
56
+ result_img = cv2.cvtColor(result_img, cv2.COLOR_RGB2BGR)
57
+ info = "Success"
58
+ else:
59
+ info = "Try again latter"
60
+ else:
61
+ print(response.text)
62
+ info = "URL error, pleace contact the admin"
63
+ except requests.exceptions.ReadTimeout:
64
+ print("timeout")
65
+ info = "Too many users, please try again later"
66
+ raise gr.Error("Too many users, please try again later")
67
+ end_time = time.time()
68
+ print(f"time used: {end_time-start_time}")
69
+
70
+ return result_img, seed, info
71
+
72
+
73
 
74
 
75
  def start_tryon(person_img, garment_img, seed, randomize_seed):
 
224
  seed_used = gr.Number(label="Seed used")
225
  result_info = gr.Text(label="Response")
226
  try_button = gr.Button(value="Run", elem_id="button")
227
+ test_button = gr.Button(value="Text", elem_id="button")
228
 
229
 
230
  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)
231
+ 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)
232
 
233
  with gr.Column(elem_id = "col-showcase"):
234
  gr.HTML("""