Update app.py
Browse files
app.py
CHANGED
@@ -15,13 +15,14 @@ depth_model.eval()
|
|
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 |
-
|
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."""
|
@@ -30,19 +31,20 @@ def warp_design(cloth_img, design_img):
|
|
30 |
cloth_np = np.array(cloth_img)
|
31 |
design_np = np.array(design_img)
|
32 |
|
33 |
-
#
|
34 |
design_np = cv2.resize(design_np, (cloth_np.shape[1], cloth_np.shape[0]))
|
35 |
|
36 |
# Estimate depth for fold detection
|
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=
|
41 |
-
displacement_y = cv2.Sobel(depth_map, cv2.CV_32F, 0, 1, ksize=
|
42 |
|
43 |
# Normalize displacement values
|
44 |
-
displacement_x = cv2.normalize(displacement_x, None, -
|
45 |
-
displacement_y = cv2.normalize(displacement_y, None, -
|
46 |
|
47 |
# Warp design using displacement map
|
48 |
h, w, _ = cloth_np.shape
|
@@ -51,11 +53,8 @@ def warp_design(cloth_img, design_img):
|
|
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.
|
59 |
return Image.fromarray(blended)
|
60 |
|
61 |
def main(cloth, design):
|
@@ -70,4 +69,4 @@ iface = gr.Interface(
|
|
70 |
)
|
71 |
|
72 |
if __name__ == "__main__":
|
73 |
-
iface.launch()
|
|
|
15 |
def estimate_depth(image):
|
16 |
"""Estimate depth map from image."""
|
17 |
image = image.convert("RGB")
|
18 |
+
image = image.resize((384, 384)) # Resize for model input
|
19 |
inputs = feature_extractor(images=image, return_tensors="pt")
|
20 |
with torch.no_grad():
|
21 |
outputs = depth_model(**inputs)
|
22 |
depth = outputs.predicted_depth.squeeze().cpu().numpy()
|
23 |
+
depth = cv2.resize(depth, (image.width, image.height)) # Resize back to original
|
24 |
depth = (depth - depth.min()) / (depth.max() - depth.min()) * 255
|
25 |
+
return depth.astype(np.uint8)
|
|
|
26 |
|
27 |
def warp_design(cloth_img, design_img):
|
28 |
"""Warp the design onto the clothing while preserving folds."""
|
|
|
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)
|
44 |
|
45 |
# Normalize displacement values
|
46 |
+
displacement_x = cv2.normalize(displacement_x, None, -5, 5, cv2.NORM_MINMAX)
|
47 |
+
displacement_y = cv2.normalize(displacement_y, None, -5, 5, cv2.NORM_MINMAX)
|
48 |
|
49 |
# Warp design using displacement map
|
50 |
h, w, _ = cloth_np.shape
|
|
|
53 |
map_y = np.clip(np.float32(map_y + displacement_y), 0, h - 1)
|
54 |
warped_design = cv2.remap(design_np, map_x, map_y, interpolation=cv2.INTER_LINEAR, borderMode=cv2.BORDER_REFLECT)
|
55 |
|
|
|
|
|
|
|
56 |
# Blend images
|
57 |
+
blended = cv2.addWeighted(cloth_np, 0.6, warped_design, 0.4, 0)
|
58 |
return Image.fromarray(blended)
|
59 |
|
60 |
def main(cloth, design):
|
|
|
69 |
)
|
70 |
|
71 |
if __name__ == "__main__":
|
72 |
+
iface.launch(share=True)
|