Zuii commited on
Commit
66e14ec
Β·
verified Β·
1 Parent(s): 72040ed

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -72
app.py CHANGED
@@ -85,86 +85,38 @@ def tryon(person_img, garment_img, seed, randomize_seed):
85
  max_retries = 5
86
  retry_delay = 3 # Faster retries now!
87
 
88
- try:
89
- session = requests.Session()
90
-
91
- # πŸ› οΈ Ensure both images are resized to 256x256
92
- person_img = cv2.resize(person_img, (256, 256))
93
- garment_img = cv2.resize(garment_img, (256, 256))
94
-
95
- # 🎯 Handle transparency if the garment has an alpha channel
96
- if garment_img.shape[-1] == 4:
97
- alpha = garment_img[:, :, 3] / 255.0
98
- for c in range(0, 3):
99
- person_img[:, :, c] = (1 - alpha) * person_img[:, :, c] + alpha * garment_img[:, :, c]
100
-
101
- # πŸ”§ Optional: Center the garment on the torso area
102
- h, w, _ = person_img.shape
103
- g_h, g_w, _ = garment_img.shape
104
- x_offset = (w - g_w) // 2
105
- y_offset = int(h * 0.35) # Adjust this value for higher/lower garment placement
106
- person_img[y_offset:y_offset + g_h, x_offset:x_offset + g_w] = garment_img
107
-
108
- # πŸ” Retry loop β€” smarter handling of "please try again later"
109
- for attempt in range(max_retries):
110
- # 🌐 Debugging payload, URL, and headers
111
- print("πŸ“© Sending data:", seed)
112
- print("🌐 URL:", url)
113
- print("πŸ› οΈ Headers:", headers)
114
-
115
- response = session.post(url, headers=headers, json={"person_data": seed}, timeout=60)
116
- print("Response code:", response.status_code)
117
-
118
- # βœ… Success β€” process the result
119
- if response.status_code == 200:
120
- result_url = response.json().get('result_url', '')
121
- print("βœ… Success:", result_url)
122
-
123
- # Fetch the final image
124
- if result_url:
125
- result_img_data = requests.get(result_url).content
126
- result_np = np.frombuffer(result_img_data, np.uint8)
127
- result_img = cv2.imdecode(result_np, cv2.IMREAD_UNCHANGED)
128
- info = "βœ… Success"
129
- # πŸ”΅ Blue tint fix: Ensure correct color space (BGR to RGB)
130
- if result_img is not None and len(result_img.shape) == 3:
131
- result_img = cv2.cvtColor(result_img, cv2.COLOR_BGR2RGB)
132
-
133
- info = "βœ… Success"
134
- break
135
-
136
- else:
137
- # πŸ”₯ Handle "please try again later" or other errors
138
- error_response = response.json().get('error', '')
139
-
140
- if "please try again later" in error_response.lower():
141
- print(f"Attempt {attempt + 1}/{max_retries}: API said 'please try again later'. Retrying in {retry_delay} seconds...")
142
- time.sleep(retry_delay)
143
- retry_delay += 2 # Exponential backoff β€” retries longer each time
144
- else:
145
- info = f"❌ Error: {response.status_code} - {response.text}"
146
- break
147
-
148
- else:
149
- # ❌ If all retries fail
150
- info = "API still says 'please try again later' β€” waited too long."
151
-
152
- # πŸ›‘ Handle timeouts specifically
153
  except requests.exceptions.ReadTimeout:
154
  print("Timeout!")
155
  info = "⚑ Timeout β€” please try again later"
156
  raise gr.Error("Too many users, please try again later")
157
 
158
- # ❗ Handle any other unexpected errors
159
  except Exception as err:
160
- print(f"❌ Detailed error: {repr(err)}") # This now prints the complete error traceback
161
  info = "Error, please contact the admin"
162
 
163
- end_time = time.time()
164
- print(f"⏳ Time used: {end_time - start_time:.2f} seconds")
165
-
166
- return result_img, seed, info
167
-
168
  example_path = os.path.join(os.path.dirname(__file__), 'assets')
169
 
170
  garm_list = os.listdir(os.path.join(example_path,"cloth"))
 
85
  max_retries = 5
86
  retry_delay = 3 # Faster retries now!
87
 
88
+ try:
89
+ session = requests.Session()
90
+
91
+ # πŸ› οΈ Ensure both images are resized to 256x256
92
+ person_img = cv2.resize(person_img, (256, 256))
93
+ garment_img = cv2.resize(garment_img, (256, 256))
94
+
95
+ # 🎯 Handle transparency if the garment has an alpha channel
96
+ if garment_img.shape[-1] == 4:
97
+ alpha = garment_img[:, :, 3] / 255.0
98
+ for c in range(0, 3):
99
+ person_img[:, :, c] = (1 - alpha) * person_img[:, :, c] + alpha * garment_img[:, :, c]
100
+
101
+ # πŸ”§ Optional: Center the garment on the torso area
102
+ h, w, _ = person_img.shape
103
+ g_h, g_w, _ = garment_img.shape
104
+
105
+ x_offset = (w - g_w) // 2
106
+ y_offset = int(h * 0.35) # Adjust this value for higher/lower garment placement
107
+ person_img[y_offset:y_offset + g_h, x_offset:x_offset + g_w] = garment_img
108
+
109
+ # βœ… Now the rest of your code continues here (retry loop, API call, etc.)
110
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111
  except requests.exceptions.ReadTimeout:
112
  print("Timeout!")
113
  info = "⚑ Timeout β€” please try again later"
114
  raise gr.Error("Too many users, please try again later")
115
 
 
116
  except Exception as err:
117
+ print(f"❌ Other error: {err}")
118
  info = "Error, please contact the admin"
119
 
 
 
 
 
 
120
  example_path = os.path.join(os.path.dirname(__file__), 'assets')
121
 
122
  garm_list = os.listdir(os.path.join(example_path,"cloth"))