Update app.py
Browse files
app.py
CHANGED
@@ -14,19 +14,24 @@ depth_model.eval()
|
|
14 |
|
15 |
def estimate_depth(image):
|
16 |
"""Estimate depth map from image."""
|
|
|
17 |
inputs = feature_extractor(images=image, return_tensors="pt")
|
18 |
with torch.no_grad():
|
19 |
outputs = depth_model(**inputs)
|
20 |
-
depth = outputs.predicted_depth.squeeze().numpy()
|
21 |
depth = (depth - depth.min()) / (depth.max() - depth.min()) * 255
|
22 |
return depth.astype(np.uint8)
|
23 |
|
24 |
def warp_design(cloth_img, design_img):
|
25 |
"""Warp the design onto the clothing while preserving folds."""
|
26 |
-
|
|
|
27 |
cloth_np = np.array(cloth_img)
|
28 |
design_np = np.array(design_img)
|
29 |
|
|
|
|
|
|
|
30 |
# Estimate depth for fold detection
|
31 |
depth_map = estimate_depth(cloth_img)
|
32 |
|
@@ -35,15 +40,15 @@ def warp_design(cloth_img, design_img):
|
|
35 |
displacement_y = cv2.Sobel(depth_map, cv2.CV_32F, 0, 1, ksize=5)
|
36 |
|
37 |
# Normalize displacement values
|
38 |
-
displacement_x = cv2.normalize(displacement_x, None, -
|
39 |
-
displacement_y = cv2.normalize(displacement_y, None, -
|
40 |
|
41 |
# Warp design using displacement map
|
42 |
h, w, _ = cloth_np.shape
|
43 |
map_x, map_y = np.meshgrid(np.arange(w), np.arange(h))
|
44 |
map_x = np.float32(map_x + displacement_x)
|
45 |
map_y = np.float32(map_y + displacement_y)
|
46 |
-
warped_design = cv2.remap(design_np, map_x, map_y, interpolation=cv2.INTER_LINEAR, borderMode=cv2.
|
47 |
|
48 |
# Blend images
|
49 |
blended = cv2.addWeighted(cloth_np, 0.6, warped_design, 0.4, 0)
|
|
|
14 |
|
15 |
def estimate_depth(image):
|
16 |
"""Estimate depth map from image."""
|
17 |
+
image = image.convert("RGB")
|
18 |
inputs = feature_extractor(images=image, return_tensors="pt")
|
19 |
with torch.no_grad():
|
20 |
outputs = depth_model(**inputs)
|
21 |
+
depth = outputs.predicted_depth.squeeze().cpu().numpy()
|
22 |
depth = (depth - depth.min()) / (depth.max() - depth.min()) * 255
|
23 |
return depth.astype(np.uint8)
|
24 |
|
25 |
def warp_design(cloth_img, design_img):
|
26 |
"""Warp the design onto the clothing while preserving folds."""
|
27 |
+
cloth_img = cloth_img.convert("RGB")
|
28 |
+
design_img = design_img.convert("RGB")
|
29 |
cloth_np = np.array(cloth_img)
|
30 |
design_np = np.array(design_img)
|
31 |
|
32 |
+
# Ensure both images have the same dimensions
|
33 |
+
design_np = cv2.resize(design_np, (cloth_np.shape[1], cloth_np.shape[0]))
|
34 |
+
|
35 |
# Estimate depth for fold detection
|
36 |
depth_map = estimate_depth(cloth_img)
|
37 |
|
|
|
40 |
displacement_y = cv2.Sobel(depth_map, cv2.CV_32F, 0, 1, ksize=5)
|
41 |
|
42 |
# Normalize displacement values
|
43 |
+
displacement_x = cv2.normalize(displacement_x, None, -5, 5, cv2.NORM_MINMAX)
|
44 |
+
displacement_y = cv2.normalize(displacement_y, None, -5, 5, cv2.NORM_MINMAX)
|
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.6, warped_design, 0.4, 0)
|