Update app.py
Browse files
app.py
CHANGED
@@ -31,13 +31,22 @@ def warp_design(cloth_img, design_img):
|
|
31 |
cloth_np = np.array(cloth_img)
|
32 |
design_np = np.array(design_img)
|
33 |
|
34 |
-
# Resize design to match clothing dimensions
|
35 |
-
design_np = cv2.resize(design_np, (cloth_np.shape[1], cloth_np.shape[0]))
|
36 |
-
|
37 |
# Estimate depth for fold detection
|
38 |
depth_map = estimate_depth(cloth_img)
|
39 |
depth_map = cv2.resize(depth_map, (cloth_np.shape[1], cloth_np.shape[0])) # Ensure matching dimensions
|
40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
# Generate displacement map based on depth
|
42 |
displacement_x = cv2.Sobel(depth_map, cv2.CV_32F, 1, 0, ksize=5)
|
43 |
displacement_y = cv2.Sobel(depth_map, cv2.CV_32F, 0, 1, ksize=5)
|
@@ -51,7 +60,7 @@ def warp_design(cloth_img, design_img):
|
|
51 |
map_x, map_y = np.meshgrid(np.arange(w), np.arange(h))
|
52 |
map_x = np.clip(np.float32(map_x + displacement_x), 0, w - 1)
|
53 |
map_y = np.clip(np.float32(map_y + displacement_y), 0, h - 1)
|
54 |
-
warped_design = cv2.remap(
|
55 |
|
56 |
# Blend images
|
57 |
blended = cv2.addWeighted(cloth_np, 0.6, warped_design, 0.4, 0)
|
@@ -65,7 +74,7 @@ iface = gr.Interface(
|
|
65 |
inputs=[gr.Image(type="pil"), gr.Image(type="pil")],
|
66 |
outputs=gr.Image(type="pil"),
|
67 |
title="AI Cloth Design Warping",
|
68 |
-
description="Upload a clothing image and a design to blend it naturally, considering fabric folds."
|
69 |
)
|
70 |
|
71 |
if __name__ == "__main__":
|
|
|
31 |
cloth_np = np.array(cloth_img)
|
32 |
design_np = np.array(design_img)
|
33 |
|
|
|
|
|
|
|
34 |
# Estimate depth for fold detection
|
35 |
depth_map = estimate_depth(cloth_img)
|
36 |
depth_map = cv2.resize(depth_map, (cloth_np.shape[1], cloth_np.shape[0])) # Ensure matching dimensions
|
37 |
|
38 |
+
# Resize design to fit centrally on clothing
|
39 |
+
design_h, design_w = cloth_np.shape[:2]
|
40 |
+
center_x, center_y = design_w // 2, design_h // 2
|
41 |
+
resized_design = cv2.resize(design_np, (design_w // 2, design_h // 2))
|
42 |
+
|
43 |
+
# Place design at the center of the clothing image
|
44 |
+
y_offset = center_y - resized_design.shape[0] // 2
|
45 |
+
x_offset = center_x - resized_design.shape[1] // 2
|
46 |
+
|
47 |
+
blended_design = cloth_np.copy()
|
48 |
+
blended_design[y_offset:y_offset + resized_design.shape[0], x_offset:x_offset + resized_design.shape[1]] = resized_design
|
49 |
+
|
50 |
# Generate displacement map based on depth
|
51 |
displacement_x = cv2.Sobel(depth_map, cv2.CV_32F, 1, 0, ksize=5)
|
52 |
displacement_y = cv2.Sobel(depth_map, cv2.CV_32F, 0, 1, ksize=5)
|
|
|
60 |
map_x, map_y = np.meshgrid(np.arange(w), np.arange(h))
|
61 |
map_x = np.clip(np.float32(map_x + displacement_x), 0, w - 1)
|
62 |
map_y = np.clip(np.float32(map_y + displacement_y), 0, h - 1)
|
63 |
+
warped_design = cv2.remap(blended_design, map_x, map_y, interpolation=cv2.INTER_LINEAR, borderMode=cv2.BORDER_REFLECT)
|
64 |
|
65 |
# Blend images
|
66 |
blended = cv2.addWeighted(cloth_np, 0.6, warped_design, 0.4, 0)
|
|
|
74 |
inputs=[gr.Image(type="pil"), gr.Image(type="pil")],
|
75 |
outputs=gr.Image(type="pil"),
|
76 |
title="AI Cloth Design Warping",
|
77 |
+
description="Upload a clothing image and a design to blend it naturally at the center, considering fabric folds."
|
78 |
)
|
79 |
|
80 |
if __name__ == "__main__":
|