Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -52,23 +52,24 @@ def segment_dress(image_np):
|
|
52 |
# Combine K-means and U²-Net masks
|
53 |
refined_mask = cv2.bitwise_and(mask, u2net_mask)
|
54 |
|
|
|
|
|
|
|
|
|
|
|
55 |
return refined_mask
|
56 |
|
57 |
-
def detect_design(image_np
|
58 |
-
"""
|
59 |
gray = cv2.cvtColor(image_np, cv2.COLOR_RGB2GRAY)
|
60 |
edges = cv2.Canny(gray, 50, 150)
|
61 |
-
|
62 |
-
# Expand detected edges to mask the design area
|
63 |
-
kernel = np.ones((5, 5), np.uint8)
|
64 |
design_mask = cv2.dilate(edges, kernel, iterations=2)
|
65 |
-
|
66 |
-
# Keep only the design within the dress area
|
67 |
-
design_mask = cv2.bitwise_and(design_mask, dress_mask)
|
68 |
return design_mask
|
69 |
|
70 |
def recolor_dress(image_np, mask, design_mask, target_color):
|
71 |
"""Change dress color while preserving texture and design."""
|
|
|
72 |
img_lab = cv2.cvtColor(image_np, cv2.COLOR_RGB2LAB)
|
73 |
target_color_lab = cv2.cvtColor(np.uint8([[target_color]]), cv2.COLOR_BGR2LAB)[0][0]
|
74 |
|
@@ -87,10 +88,10 @@ def change_dress_color(image_path, color):
|
|
87 |
|
88 |
img = Image.open(image_path).convert("RGB")
|
89 |
img_np = np.array(img)
|
90 |
-
|
91 |
-
design_mask = detect_design(img_np
|
92 |
|
93 |
-
if
|
94 |
return img # No dress detected
|
95 |
|
96 |
# Convert the selected color to BGR
|
@@ -101,8 +102,8 @@ def change_dress_color(image_path, color):
|
|
101 |
}
|
102 |
new_color_bgr = np.array(color_map.get(color, (0, 0, 255)), dtype=np.uint8) # Default to Red
|
103 |
|
104 |
-
# Recolor the dress naturally
|
105 |
-
img_recolored = recolor_dress(img_np,
|
106 |
|
107 |
return Image.fromarray(img_recolored)
|
108 |
|
@@ -115,7 +116,7 @@ demo = gr.Interface(
|
|
115 |
],
|
116 |
outputs=gr.Image(type="pil", label="Color Changed Dress"),
|
117 |
title="Dress Color Changer",
|
118 |
-
description="Upload an image of a dress and select a new color to change its appearance naturally while preserving
|
119 |
)
|
120 |
|
121 |
if __name__ == "__main__":
|
|
|
52 |
# Combine K-means and U²-Net masks
|
53 |
refined_mask = cv2.bitwise_and(mask, u2net_mask)
|
54 |
|
55 |
+
# Morphological operations for smoothness
|
56 |
+
kernel = np.ones((5, 5), np.uint8)
|
57 |
+
refined_mask = cv2.morphologyEx(refined_mask, cv2.MORPH_CLOSE, kernel)
|
58 |
+
refined_mask = cv2.GaussianBlur(refined_mask, (15, 15), 5)
|
59 |
+
|
60 |
return refined_mask
|
61 |
|
62 |
+
def detect_design(image_np):
|
63 |
+
"""Detects design patterns on the dress using edge detection."""
|
64 |
gray = cv2.cvtColor(image_np, cv2.COLOR_RGB2GRAY)
|
65 |
edges = cv2.Canny(gray, 50, 150)
|
66 |
+
kernel = np.ones((3, 3), np.uint8)
|
|
|
|
|
67 |
design_mask = cv2.dilate(edges, kernel, iterations=2)
|
|
|
|
|
|
|
68 |
return design_mask
|
69 |
|
70 |
def recolor_dress(image_np, mask, design_mask, target_color):
|
71 |
"""Change dress color while preserving texture and design."""
|
72 |
+
|
73 |
img_lab = cv2.cvtColor(image_np, cv2.COLOR_RGB2LAB)
|
74 |
target_color_lab = cv2.cvtColor(np.uint8([[target_color]]), cv2.COLOR_BGR2LAB)[0][0]
|
75 |
|
|
|
88 |
|
89 |
img = Image.open(image_path).convert("RGB")
|
90 |
img_np = np.array(img)
|
91 |
+
mask = segment_dress(img_np)
|
92 |
+
design_mask = detect_design(img_np)
|
93 |
|
94 |
+
if mask is None:
|
95 |
return img # No dress detected
|
96 |
|
97 |
# Convert the selected color to BGR
|
|
|
102 |
}
|
103 |
new_color_bgr = np.array(color_map.get(color, (0, 0, 255)), dtype=np.uint8) # Default to Red
|
104 |
|
105 |
+
# Recolor the dress naturally while preserving design
|
106 |
+
img_recolored = recolor_dress(img_np, mask, design_mask, new_color_bgr)
|
107 |
|
108 |
return Image.fromarray(img_recolored)
|
109 |
|
|
|
116 |
],
|
117 |
outputs=gr.Image(type="pil", label="Color Changed Dress"),
|
118 |
title="Dress Color Changer",
|
119 |
+
description="Upload an image of a dress and select a new color to change its appearance naturally while preserving any design patterns."
|
120 |
)
|
121 |
|
122 |
if __name__ == "__main__":
|