Update app.py
Browse files
app.py
CHANGED
@@ -16,9 +16,11 @@ pixelcut_api_key = "sk_299d9c6e36d240cbb3dd65fcbac947a4"
|
|
16 |
# β
ImgBB API Key (for uploading images to get valid URLs)
|
17 |
imgbb_api_key = "03a2ddf1ffa5df33a3999cf20c2fb20f"
|
18 |
|
19 |
-
|
|
|
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):
|
@@ -28,6 +30,7 @@ def resize_image(image, max_size=1024):
|
|
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}"
|
@@ -40,28 +43,38 @@ def upload_image_to_imgbb(image_data):
|
|
40 |
print("β ImgBB upload failed:", response.text)
|
41 |
return None
|
42 |
|
43 |
-
|
|
|
44 |
def tryon(person_img, cloth_img, seed, random_seed):
|
45 |
import cv2
|
46 |
import numpy as np
|
47 |
|
48 |
-
# Handle seed
|
49 |
if random_seed:
|
50 |
-
import random
|
51 |
seed = random.randint(0, 1000000)
|
52 |
|
53 |
-
# Convert images to
|
54 |
person_img = cv2.cvtColor(person_img, cv2.COLOR_BGR2RGB)
|
55 |
cloth_img = cv2.cvtColor(cloth_img, cv2.COLOR_BGR2RGB)
|
56 |
|
57 |
-
# Resize to 256x256
|
58 |
person_img = cv2.resize(person_img, (256, 256))
|
59 |
cloth_img = cv2.resize(cloth_img, (256, 256))
|
60 |
|
61 |
-
# (
|
62 |
output_img = person_img.copy()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
|
64 |
-
return output_img, seed, "Success"
|
65 |
|
66 |
# π§ Paths for examples
|
67 |
example_path = os.path.join(os.path.dirname(__file__), "assets")
|
@@ -72,6 +85,7 @@ garm_list_path = [os.path.join(example_path, "cloth", garm) for garm in garm_lis
|
|
72 |
human_list = os.listdir(os.path.join(example_path, "human"))
|
73 |
human_list_path = [os.path.join(example_path, "human", human) for human in human_list]
|
74 |
|
|
|
75 |
css="""
|
76 |
#col-left {
|
77 |
margin: 0 auto;
|
|
|
16 |
# β
ImgBB API Key (for uploading images to get valid URLs)
|
17 |
imgbb_api_key = "03a2ddf1ffa5df33a3999cf20c2fb20f"
|
18 |
|
19 |
+
|
20 |
+
# π― Convert image to PNG format (keeps the blue tint glitch!)
|
21 |
def convert_to_png(image):
|
22 |
+
return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
23 |
+
|
24 |
|
25 |
# π₯ Resize large images to prevent upload failures (ImgBB limit: 32MB)
|
26 |
def resize_image(image, max_size=1024):
|
|
|
30 |
return cv2.resize(image, (int(width * scale), int(height * scale)))
|
31 |
return image
|
32 |
|
33 |
+
|
34 |
# π οΈ Upload images to ImgBB (fixed payload!)
|
35 |
def upload_image_to_imgbb(image_data):
|
36 |
url = f"https://api.imgbb.com/1/upload?key={imgbb_api_key}"
|
|
|
43 |
print("β ImgBB upload failed:", response.text)
|
44 |
return None
|
45 |
|
46 |
+
|
47 |
+
# π Main try-on function (with blue tint + garment overlay)
|
48 |
def tryon(person_img, cloth_img, seed, random_seed):
|
49 |
import cv2
|
50 |
import numpy as np
|
51 |
|
52 |
+
# π² Handle seed
|
53 |
if random_seed:
|
|
|
54 |
seed = random.randint(0, 1000000)
|
55 |
|
56 |
+
# π΅ Convert images to RGB (keeps the blue tint glitch!)
|
57 |
person_img = cv2.cvtColor(person_img, cv2.COLOR_BGR2RGB)
|
58 |
cloth_img = cv2.cvtColor(cloth_img, cv2.COLOR_BGR2RGB)
|
59 |
|
60 |
+
# βοΈ Resize both images to 256x256
|
61 |
person_img = cv2.resize(person_img, (256, 256))
|
62 |
cloth_img = cv2.resize(cloth_img, (256, 256))
|
63 |
|
64 |
+
# π₯ Blend garment onto person (simple overlay β still has blue tint)
|
65 |
output_img = person_img.copy()
|
66 |
+
h, w, _ = person_img.shape
|
67 |
+
g_h, g_w, _ = cloth_img.shape
|
68 |
+
|
69 |
+
# Position the garment roughly on the torso (manual offset)
|
70 |
+
x_offset = (w - g_w) // 2
|
71 |
+
y_offset = int(h * 0.35)
|
72 |
+
|
73 |
+
# π οΈ Add garment onto person (no transparency support β raw overlay)
|
74 |
+
output_img[y_offset:y_offset + g_h, x_offset:x_offset + g_w] = cloth_img
|
75 |
+
|
76 |
+
return output_img, seed, "β
Success (Blue tint + Garment added)"
|
77 |
|
|
|
78 |
|
79 |
# π§ Paths for examples
|
80 |
example_path = os.path.join(os.path.dirname(__file__), "assets")
|
|
|
85 |
human_list = os.listdir(os.path.join(example_path, "human"))
|
86 |
human_list_path = [os.path.join(example_path, "human", human) for human in human_list]
|
87 |
|
88 |
+
|
89 |
css="""
|
90 |
#col-left {
|
91 |
margin: 0 auto;
|