lixiang46 commited on
Commit
0c1a8d3
1 Parent(s): f8801dc

add try except

Browse files
Files changed (1) hide show
  1. app.py +22 -16
app.py CHANGED
@@ -33,24 +33,30 @@ def start_tryon(person_img, garment_img, seed, randomize_seed):
33
  "seed": seed
34
  }
35
 
36
- print("post")
37
- response = requests.post(url, headers=headers, data=json.dumps(data), timeout=50)
38
- print("response code", response.status_code)
39
  result_img = None
40
- if response.status_code == 200:
41
- result = response.json()['result']
42
- status = result['status']
43
- if status == "success":
44
- result = base64.b64decode(result['result'])
45
- result_np = np.frombuffer(result, np.uint8)
46
- result_img = cv2.imdecode(result_np, cv2.IMREAD_UNCHANGED)
47
- result_img = cv2.cvtColor(result_img, cv2.COLOR_RGB2BGR)
48
- info = "Success"
 
 
 
 
 
49
  else:
50
- info = "Try again latter"
51
- else:
52
- print(response.text)
53
- info = "URL error, pleace contact the admin"
 
 
 
 
54
  end_time = time.time()
55
  print(f"time used: {end_time-start_time}")
56
 
 
33
  "seed": seed
34
  }
35
 
 
 
 
36
  result_img = None
37
+ try:
38
+ response = requests.post(url, headers=headers, data=json.dumps(data), timeout=50)
39
+ print("response code", response.status_code)
40
+ if response.status_code == 200:
41
+ result = response.json()['result']
42
+ status = result['status']
43
+ if status == "success":
44
+ result = base64.b64decode(result['result'])
45
+ result_np = np.frombuffer(result, np.uint8)
46
+ result_img = cv2.imdecode(result_np, cv2.IMREAD_UNCHANGED)
47
+ result_img = cv2.cvtColor(result_img, cv2.COLOR_RGB2BGR)
48
+ info = "Success"
49
+ else:
50
+ info = "Try again latter"
51
  else:
52
+ print(response.text)
53
+ info = "URL error, pleace contact the admin"
54
+ except requests.exceptions.ReadTimeout:
55
+ print("timeout")
56
+ info = "Too many users, please try again later"
57
+ except Exception as err:
58
+ print(f"其他错误: {err}")
59
+ info = "Error, pleace contact the admin"
60
  end_time = time.time()
61
  print(f"time used: {end_time-start_time}")
62