Update app.py
Browse files
app.py
CHANGED
|
@@ -20,16 +20,18 @@ imgbb_api_key = "YOUR_IMGBB_API_KEY"
|
|
| 20 |
def convert_to_png(image):
|
| 21 |
return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 22 |
|
| 23 |
-
# π₯
|
| 24 |
-
def
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
| 30 |
def upload_image_to_imgbb(image_data):
|
| 31 |
url = f"https://api.imgbb.com/1/upload?key={imgbb_api_key}"
|
| 32 |
-
files = {"image":
|
| 33 |
response = requests.post(url, files=files)
|
| 34 |
|
| 35 |
if response.status_code == 200:
|
|
@@ -50,12 +52,18 @@ def tryon(person_img, garment_img, seed, randomize_seed):
|
|
| 50 |
if randomize_seed:
|
| 51 |
seed = random.randint(0, MAX_SEED)
|
| 52 |
|
| 53 |
-
# π₯ Convert images to base64 PNG
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
# β
Upload person and garment images to get valid URLs
|
|
|
|
| 58 |
person_url = upload_image_to_imgbb(person_encoded)
|
|
|
|
|
|
|
| 59 |
garment_url = upload_image_to_imgbb(garment_encoded)
|
| 60 |
|
| 61 |
if not person_url or not garment_url:
|
|
@@ -131,8 +139,6 @@ def tryon(person_img, garment_img, seed, randomize_seed):
|
|
| 131 |
return result_img, seed, info
|
| 132 |
|
| 133 |
|
| 134 |
-
|
| 135 |
-
|
| 136 |
example_path = os.path.join(os.path.dirname(__file__), 'assets')
|
| 137 |
|
| 138 |
garm_list = os.listdir(os.path.join(example_path,"cloth"))
|
|
|
|
| 20 |
def convert_to_png(image):
|
| 21 |
return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 22 |
|
| 23 |
+
# π₯ Resize large images to prevent upload failures (ImgBB limit: 32MB)
|
| 24 |
+
def resize_image(image, max_size=1024):
|
| 25 |
+
height, width = image.shape[:2]
|
| 26 |
+
if max(height, width) > max_size:
|
| 27 |
+
scale = max_size / max(height, width)
|
| 28 |
+
return cv2.resize(image, (int(width * scale), int(height * scale)))
|
| 29 |
+
return image
|
| 30 |
+
|
| 31 |
+
# π οΈ Upload images to ImgBB (fixed payload!)
|
| 32 |
def upload_image_to_imgbb(image_data):
|
| 33 |
url = f"https://api.imgbb.com/1/upload?key={imgbb_api_key}"
|
| 34 |
+
files = {"image": image_data}
|
| 35 |
response = requests.post(url, files=files)
|
| 36 |
|
| 37 |
if response.status_code == 200:
|
|
|
|
| 52 |
if randomize_seed:
|
| 53 |
seed = random.randint(0, MAX_SEED)
|
| 54 |
|
| 55 |
+
# π₯ Convert and resize images to base64 PNG
|
| 56 |
+
person_img = resize_image(convert_to_png(person_img))
|
| 57 |
+
garment_img = resize_image(convert_to_png(garment_img))
|
| 58 |
+
|
| 59 |
+
_, person_encoded = cv2.imencode('.png', person_img)
|
| 60 |
+
_, garment_encoded = cv2.imencode('.png', garment_img)
|
| 61 |
|
| 62 |
# β
Upload person and garment images to get valid URLs
|
| 63 |
+
print("π€ Uploading person image...")
|
| 64 |
person_url = upload_image_to_imgbb(person_encoded)
|
| 65 |
+
|
| 66 |
+
print("π€ Uploading garment image...")
|
| 67 |
garment_url = upload_image_to_imgbb(garment_encoded)
|
| 68 |
|
| 69 |
if not person_url or not garment_url:
|
|
|
|
| 139 |
return result_img, seed, info
|
| 140 |
|
| 141 |
|
|
|
|
|
|
|
| 142 |
example_path = os.path.join(os.path.dirname(__file__), 'assets')
|
| 143 |
|
| 144 |
garm_list = os.listdir(os.path.join(example_path,"cloth"))
|