Update app.py
Browse files
app.py
CHANGED
@@ -53,32 +53,41 @@ def tryon(person_img, cloth_img, seed, random_seed):
|
|
53 |
if random_seed:
|
54 |
seed = random.randint(0, 1000000)
|
55 |
|
56 |
-
|
57 |
-
|
58 |
-
|
|
|
59 |
|
60 |
-
|
61 |
-
|
62 |
-
cloth_img = cv2.resize(cloth_img, (256, 256))
|
63 |
|
64 |
-
|
65 |
-
|
|
|
|
|
|
|
66 |
output_img = person_img.copy()
|
67 |
h, w, _ = person_img.shape
|
68 |
-
g_h, g_w, _ = cloth_img.shape
|
69 |
|
70 |
-
|
71 |
x_offset = (w - g_w) // 2
|
72 |
y_offset = int(h * 0.35)
|
73 |
|
74 |
-
|
75 |
if y_offset + g_h > h or x_offset + g_w > w:
|
76 |
raise ValueError("β Garment image is too large for the person image!")
|
77 |
|
78 |
-
|
79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
|
81 |
-
return output_img, seed, "β
Success (Blue tint + Garment added)"
|
82 |
|
83 |
except Exception as e:
|
84 |
print(f"β Error in tryon function: {e}")
|
@@ -86,6 +95,7 @@ def tryon(person_img, cloth_img, seed, random_seed):
|
|
86 |
|
87 |
|
88 |
|
|
|
89 |
# π§ Paths for examples
|
90 |
example_path = os.path.join(os.path.dirname(__file__), "assets")
|
91 |
|
|
|
53 |
if random_seed:
|
54 |
seed = random.randint(0, 1000000)
|
55 |
|
56 |
+
try:
|
57 |
+
# β
Convert images to RGB (fixing blue tint)
|
58 |
+
person_img = cv2.cvtColor(person_img, cv2.COLOR_BGR2RGB)
|
59 |
+
cloth_img = cv2.cvtColor(cloth_img, cv2.COLOR_BGR2RGB)
|
60 |
|
61 |
+
# βοΈ Resize both images to 256x256 (keep consistent sizes)
|
62 |
+
person_img = cv2.resize(person_img, (256, 256))
|
|
|
63 |
|
64 |
+
# β
Resize garment to fit person size
|
65 |
+
g_h, g_w = person_img.shape[:2]
|
66 |
+
cloth_img = cv2.resize(cloth_img, (g_w, g_h))
|
67 |
+
|
68 |
+
# π₯ Blend garment onto person (centered overlay)
|
69 |
output_img = person_img.copy()
|
70 |
h, w, _ = person_img.shape
|
|
|
71 |
|
72 |
+
# Center garment on the torso (adjust the offset if needed)
|
73 |
x_offset = (w - g_w) // 2
|
74 |
y_offset = int(h * 0.35)
|
75 |
|
76 |
+
# β
Ensure garment fits the person image
|
77 |
if y_offset + g_h > h or x_offset + g_w > w:
|
78 |
raise ValueError("β Garment image is too large for the person image!")
|
79 |
|
80 |
+
# π οΈ Overlay garment on the person (with transparency support)
|
81 |
+
alpha = 0.7 # Adjust transparency level (0 = invisible, 1 = solid)
|
82 |
+
output_img[y_offset:y_offset + g_h, x_offset:x_offset + g_w] = cv2.addWeighted(
|
83 |
+
person_img[y_offset:y_offset + g_h, x_offset:x_offset + g_w],
|
84 |
+
1 - alpha,
|
85 |
+
cloth_img,
|
86 |
+
alpha,
|
87 |
+
0
|
88 |
+
)
|
89 |
|
90 |
+
return output_img, seed, "β
Success (Blue tint removed + Garment added)"
|
91 |
|
92 |
except Exception as e:
|
93 |
print(f"β Error in tryon function: {e}")
|
|
|
95 |
|
96 |
|
97 |
|
98 |
+
|
99 |
# π§ Paths for examples
|
100 |
example_path = os.path.join(os.path.dirname(__file__), "assets")
|
101 |
|