Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -52,37 +52,45 @@ def segment_dress(image_np):
|
|
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
|
63 |
-
"""
|
|
|
|
|
64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
img_lab = cv2.cvtColor(image_np, cv2.COLOR_RGB2LAB)
|
66 |
target_color_lab = cv2.cvtColor(np.uint8([[target_color]]), cv2.COLOR_BGR2LAB)[0][0]
|
67 |
|
68 |
# Preserve lightness (L) and change only chromatic channels (A & B)
|
69 |
blend_factor = 0.7
|
70 |
-
img_lab[..., 1] = np.where(mask > 128, img_lab[..., 1] * (1 - blend_factor) + target_color_lab[1] * blend_factor, img_lab[..., 1])
|
71 |
-
img_lab[..., 2] = np.where(mask > 128, img_lab[..., 2] * (1 - blend_factor) + target_color_lab[2] * blend_factor, img_lab[..., 2])
|
72 |
|
73 |
img_recolored = cv2.cvtColor(img_lab, cv2.COLOR_LAB2RGB)
|
74 |
return img_recolored
|
75 |
|
76 |
def change_dress_color(image_path, color):
|
77 |
-
"""Change the dress color naturally while keeping textures."""
|
78 |
if image_path is None:
|
79 |
return None
|
80 |
|
81 |
img = Image.open(image_path).convert("RGB")
|
82 |
img_np = np.array(img)
|
83 |
-
|
|
|
84 |
|
85 |
-
if
|
86 |
return img # No dress detected
|
87 |
|
88 |
# Convert the selected color to BGR
|
@@ -94,7 +102,7 @@ def change_dress_color(image_path, color):
|
|
94 |
new_color_bgr = np.array(color_map.get(color, (0, 0, 255)), dtype=np.uint8) # Default to Red
|
95 |
|
96 |
# Recolor the dress naturally
|
97 |
-
img_recolored = recolor_dress(img_np,
|
98 |
|
99 |
return Image.fromarray(img_recolored)
|
100 |
|
@@ -107,7 +115,7 @@ demo = gr.Interface(
|
|
107 |
],
|
108 |
outputs=gr.Image(type="pil", label="Color Changed Dress"),
|
109 |
title="Dress Color Changer",
|
110 |
-
description="Upload an image of a dress and select a new color to change its appearance naturally."
|
111 |
)
|
112 |
|
113 |
if __name__ == "__main__":
|
|
|
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, dress_mask):
|
58 |
+
"""Detect the design part of the dress and separate it from fabric."""
|
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 |
|
75 |
# Preserve lightness (L) and change only chromatic channels (A & B)
|
76 |
blend_factor = 0.7
|
77 |
+
img_lab[..., 1] = np.where((mask > 128) & (design_mask == 0), img_lab[..., 1] * (1 - blend_factor) + target_color_lab[1] * blend_factor, img_lab[..., 1])
|
78 |
+
img_lab[..., 2] = np.where((mask > 128) & (design_mask == 0), img_lab[..., 2] * (1 - blend_factor) + target_color_lab[2] * blend_factor, img_lab[..., 2])
|
79 |
|
80 |
img_recolored = cv2.cvtColor(img_lab, cv2.COLOR_LAB2RGB)
|
81 |
return img_recolored
|
82 |
|
83 |
def change_dress_color(image_path, color):
|
84 |
+
"""Change the dress color naturally while keeping textures and design."""
|
85 |
if image_path is None:
|
86 |
return None
|
87 |
|
88 |
img = Image.open(image_path).convert("RGB")
|
89 |
img_np = np.array(img)
|
90 |
+
dress_mask = segment_dress(img_np)
|
91 |
+
design_mask = detect_design(img_np, dress_mask)
|
92 |
|
93 |
+
if dress_mask is None:
|
94 |
return img # No dress detected
|
95 |
|
96 |
# Convert the selected color to BGR
|
|
|
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, dress_mask, design_mask, new_color_bgr)
|
106 |
|
107 |
return Image.fromarray(img_recolored)
|
108 |
|
|
|
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 the design."
|
119 |
)
|
120 |
|
121 |
if __name__ == "__main__":
|