Update app.py
Browse files
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 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 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"β
|
| 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"))
|