Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -96,31 +96,41 @@ def overlay_design(cloth_image, design_image):
|
|
96 |
# Generate T-shirt mask
|
97 |
mask = get_tshirt_mask(cloth_image)
|
98 |
|
99 |
-
# Extract bounding box
|
100 |
bbox = get_bounding_box(mask)
|
101 |
tshirt_width = bbox[2] - bbox[0]
|
102 |
tshirt_height = bbox[3] - bbox[1]
|
103 |
|
104 |
-
# Resize the design to fit the T-shirt
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
116 |
transparent_layer = Image.new("RGBA", cloth_image.size, (0, 0, 0, 0))
|
117 |
-
transparent_layer.paste(
|
118 |
-
|
119 |
-
#
|
120 |
-
|
121 |
-
|
122 |
-
# Combine the cloth image with the masked design
|
123 |
-
final_image = Image.alpha_composite(cloth_image, masked_design)
|
124 |
return final_image
|
125 |
|
126 |
def debug_intermediate_outputs(cloth_image, mask):
|
|
|
96 |
# Generate T-shirt mask
|
97 |
mask = get_tshirt_mask(cloth_image)
|
98 |
|
99 |
+
# Extract bounding box and T-shirt coordinates
|
100 |
bbox = get_bounding_box(mask)
|
101 |
tshirt_width = bbox[2] - bbox[0]
|
102 |
tshirt_height = bbox[3] - bbox[1]
|
103 |
|
104 |
+
# Resize the design to fit within the T-shirt bounding box
|
105 |
+
design_image = design_image.resize((tshirt_width, tshirt_height))
|
106 |
+
|
107 |
+
# Convert mask to NumPy array for warping
|
108 |
+
mask_np = np.array(mask)
|
109 |
+
|
110 |
+
# Identify T-shirt edges for perspective warp
|
111 |
+
y_coords, x_coords = np.where(mask_np > 0)
|
112 |
+
if len(y_coords) < 4 or len(x_coords) < 4:
|
113 |
+
raise Exception("Insufficient mask data for warping.")
|
114 |
+
|
115 |
+
# Calculate the corners of the mask region
|
116 |
+
top_left = (x_coords.min(), y_coords.min())
|
117 |
+
top_right = (x_coords.max(), y_coords.min())
|
118 |
+
bottom_left = (x_coords.min(), y_coords.max())
|
119 |
+
bottom_right = (x_coords.max(), y_coords.max())
|
120 |
+
|
121 |
+
# Warp design image to T-shirt shape
|
122 |
+
src_points = np.float32([[0, 0], [design_image.width, 0], [0, design_image.height], [design_image.width, design_image.height]])
|
123 |
+
dst_points = np.float32([top_left, top_right, bottom_left, bottom_right])
|
124 |
+
warp_matrix = cv2.getPerspectiveTransform(src_points, dst_points)
|
125 |
+
design_warped = cv2.warpPerspective(np.array(design_image), warp_matrix, (cloth_image.width, cloth_image.height))
|
126 |
+
design_warped = Image.fromarray(design_warped).convert("RGBA")
|
127 |
+
|
128 |
+
# Mask the warped design to fit the T-shirt
|
129 |
transparent_layer = Image.new("RGBA", cloth_image.size, (0, 0, 0, 0))
|
130 |
+
transparent_layer.paste(design_warped, (0, 0), mask)
|
131 |
+
|
132 |
+
# Blend the T-shirt with the design
|
133 |
+
final_image = Image.alpha_composite(cloth_image, transparent_layer)
|
|
|
|
|
|
|
134 |
return final_image
|
135 |
|
136 |
def debug_intermediate_outputs(cloth_image, mask):
|