Update app.py
Browse files
app.py
CHANGED
@@ -20,7 +20,8 @@ def estimate_depth(image):
|
|
20 |
outputs = depth_model(**inputs)
|
21 |
depth = outputs.predicted_depth.squeeze().cpu().numpy()
|
22 |
depth = (depth - depth.min()) / (depth.max() - depth.min()) * 255
|
23 |
-
|
|
|
24 |
|
25 |
def warp_design(cloth_img, design_img):
|
26 |
"""Warp the design onto the clothing while preserving folds."""
|
@@ -36,22 +37,25 @@ def warp_design(cloth_img, design_img):
|
|
36 |
depth_map = estimate_depth(cloth_img)
|
37 |
|
38 |
# Generate displacement map based on depth
|
39 |
-
displacement_x = cv2.Sobel(depth_map, cv2.CV_32F, 1, 0, ksize=
|
40 |
-
displacement_y = cv2.Sobel(depth_map, cv2.CV_32F, 0, 1, ksize=
|
41 |
|
42 |
# Normalize displacement values
|
43 |
-
displacement_x = cv2.normalize(displacement_x, None, -
|
44 |
-
displacement_y = cv2.normalize(displacement_y, None, -
|
45 |
|
46 |
# Warp design using displacement map
|
47 |
h, w, _ = cloth_np.shape
|
48 |
map_x, map_y = np.meshgrid(np.arange(w), np.arange(h))
|
49 |
-
map_x = np.float32(map_x + displacement_x)
|
50 |
-
map_y = np.float32(map_y + displacement_y)
|
51 |
warped_design = cv2.remap(design_np, map_x, map_y, interpolation=cv2.INTER_LINEAR, borderMode=cv2.BORDER_REFLECT)
|
52 |
|
|
|
|
|
|
|
53 |
# Blend images
|
54 |
-
blended = cv2.addWeighted(cloth_np, 0.
|
55 |
return Image.fromarray(blended)
|
56 |
|
57 |
def main(cloth, design):
|
|
|
20 |
outputs = depth_model(**inputs)
|
21 |
depth = outputs.predicted_depth.squeeze().cpu().numpy()
|
22 |
depth = (depth - depth.min()) / (depth.max() - depth.min()) * 255
|
23 |
+
depth = depth.astype(np.uint8)
|
24 |
+
return cv2.GaussianBlur(depth, (5, 5), 0) # Smooth depth map to reduce noise
|
25 |
|
26 |
def warp_design(cloth_img, design_img):
|
27 |
"""Warp the design onto the clothing while preserving folds."""
|
|
|
37 |
depth_map = estimate_depth(cloth_img)
|
38 |
|
39 |
# Generate displacement map based on depth
|
40 |
+
displacement_x = cv2.Sobel(depth_map, cv2.CV_32F, 1, 0, ksize=3)
|
41 |
+
displacement_y = cv2.Sobel(depth_map, cv2.CV_32F, 0, 1, ksize=3)
|
42 |
|
43 |
# Normalize displacement values
|
44 |
+
displacement_x = cv2.normalize(displacement_x, None, -3, 3, cv2.NORM_MINMAX)
|
45 |
+
displacement_y = cv2.normalize(displacement_y, None, -3, 3, cv2.NORM_MINMAX)
|
46 |
|
47 |
# Warp design using displacement map
|
48 |
h, w, _ = cloth_np.shape
|
49 |
map_x, map_y = np.meshgrid(np.arange(w), np.arange(h))
|
50 |
+
map_x = np.clip(np.float32(map_x + displacement_x), 0, w - 1)
|
51 |
+
map_y = np.clip(np.float32(map_y + displacement_y), 0, h - 1)
|
52 |
warped_design = cv2.remap(design_np, map_x, map_y, interpolation=cv2.INTER_LINEAR, borderMode=cv2.BORDER_REFLECT)
|
53 |
|
54 |
+
# Convert warped design to match original image dtype
|
55 |
+
warped_design = warped_design.astype(np.uint8)
|
56 |
+
|
57 |
# Blend images
|
58 |
+
blended = cv2.addWeighted(cloth_np, 0.7, warped_design, 0.3, 0)
|
59 |
return Image.fromarray(blended)
|
60 |
|
61 |
def main(cloth, design):
|