Update app.py
Browse files
app.py
CHANGED
@@ -34,28 +34,40 @@ def blend_design(cloth_img, design_img):
|
|
34 |
# Resize design to fit within clothing
|
35 |
h, w, _ = cloth_np.shape
|
36 |
dh, dw, _ = design_np.shape
|
37 |
-
scale_factor = min(w / dw, h / dh) * 0.
|
38 |
new_w, new_h = int(dw * scale_factor), int(dh * scale_factor)
|
39 |
design_np = cv2.resize(design_np, (new_w, new_h))
|
40 |
|
41 |
-
# Convert design to grayscale and darken
|
42 |
design_gray = cv2.cvtColor(design_np, cv2.COLOR_RGB2GRAY)
|
43 |
design_np = cv2.cvtColor(design_gray, cv2.COLOR_GRAY2RGB)
|
44 |
-
design_np = cv2.convertScaleAbs(design_np, alpha=1.
|
45 |
|
46 |
# Create a blank canvas and paste the resized design at the center
|
47 |
design_canvas = np.zeros_like(cloth_np)
|
48 |
x_offset = (w - new_w) // 2
|
49 |
-
y_offset = (h
|
50 |
design_canvas[y_offset:y_offset+new_h, x_offset:x_offset+new_w] = design_np
|
51 |
|
52 |
# Estimate depth for fold detection
|
53 |
depth_map = estimate_depth(cloth_img)
|
54 |
depth_map = cv2.resize(depth_map, (w, h))
|
55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
# Use Poisson blending for seamless integration
|
57 |
-
mask = (
|
58 |
-
blended = cv2.seamlessClone(
|
59 |
|
60 |
return Image.fromarray(blended)
|
61 |
|
|
|
34 |
# Resize design to fit within clothing
|
35 |
h, w, _ = cloth_np.shape
|
36 |
dh, dw, _ = design_np.shape
|
37 |
+
scale_factor = min(w / dw, h / dh) * 0.4 # Scale to 40% of clothing area
|
38 |
new_w, new_h = int(dw * scale_factor), int(dh * scale_factor)
|
39 |
design_np = cv2.resize(design_np, (new_w, new_h))
|
40 |
|
41 |
+
# Convert design to grayscale and darken for print effect
|
42 |
design_gray = cv2.cvtColor(design_np, cv2.COLOR_RGB2GRAY)
|
43 |
design_np = cv2.cvtColor(design_gray, cv2.COLOR_GRAY2RGB)
|
44 |
+
design_np = cv2.convertScaleAbs(design_np, alpha=1.5, beta=-40) # Increase contrast
|
45 |
|
46 |
# Create a blank canvas and paste the resized design at the center
|
47 |
design_canvas = np.zeros_like(cloth_np)
|
48 |
x_offset = (w - new_w) // 2
|
49 |
+
y_offset = int(h * 0.35) # Move slightly upward for a natural position
|
50 |
design_canvas[y_offset:y_offset+new_h, x_offset:x_offset+new_w] = design_np
|
51 |
|
52 |
# Estimate depth for fold detection
|
53 |
depth_map = estimate_depth(cloth_img)
|
54 |
depth_map = cv2.resize(depth_map, (w, h))
|
55 |
|
56 |
+
# Generate displacement map based on depth
|
57 |
+
displacement_x = cv2.Sobel(depth_map, cv2.CV_32F, 1, 0, ksize=5)
|
58 |
+
displacement_y = cv2.Sobel(depth_map, cv2.CV_32F, 0, 1, ksize=5)
|
59 |
+
displacement_x = cv2.normalize(displacement_x, None, -3, 3, cv2.NORM_MINMAX)
|
60 |
+
displacement_y = cv2.normalize(displacement_y, None, -3, 3, cv2.NORM_MINMAX)
|
61 |
+
|
62 |
+
# Warp design using displacement map
|
63 |
+
map_x, map_y = np.meshgrid(np.arange(w), np.arange(h))
|
64 |
+
map_x = np.clip(np.float32(map_x + displacement_x), 0, w - 1)
|
65 |
+
map_y = np.clip(np.float32(map_y + displacement_y), 0, h - 1)
|
66 |
+
warped_design = cv2.remap(design_canvas, map_x, map_y, interpolation=cv2.INTER_LINEAR, borderMode=cv2.BORDER_REFLECT)
|
67 |
+
|
68 |
# Use Poisson blending for seamless integration
|
69 |
+
mask = (warped_design > 0).astype(np.uint8) * 255
|
70 |
+
blended = cv2.seamlessClone(warped_design, cloth_np, mask, (w//2, int(h * 0.35 + new_h//2)), cv2.NORMAL_CLONE)
|
71 |
|
72 |
return Image.fromarray(blended)
|
73 |
|