Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -36,18 +36,25 @@ def segment_dress(image_np):
|
|
36 |
# Apply morphological operations for better segmentation
|
37 |
kernel = np.ones((5, 5), np.uint8)
|
38 |
mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel) # Close small gaps
|
39 |
-
mask = cv2.GaussianBlur(mask, (
|
40 |
|
41 |
return mask
|
42 |
|
43 |
-
def
|
44 |
-
"""
|
45 |
-
|
46 |
-
|
47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
def change_dress_color(image_path, color):
|
50 |
-
"""Change the dress color naturally while keeping textures
|
51 |
if image_path is None:
|
52 |
return None
|
53 |
|
@@ -66,24 +73,8 @@ def change_dress_color(image_path, color):
|
|
66 |
}
|
67 |
new_color_bgr = np.array(color_map.get(color, (0, 0, 255)), dtype=np.uint8) # Default to Red
|
68 |
|
69 |
-
#
|
70 |
-
|
71 |
-
new_color_lab = cv2.cvtColor(np.uint8([[new_color_bgr]]), cv2.COLOR_BGR2LAB)[0][0]
|
72 |
-
|
73 |
-
# Adjust color to match ambient lighting
|
74 |
-
ambient_light = get_ambient_light(img_np)
|
75 |
-
img_lab[..., 0] = np.clip(img_lab[..., 0] * (ambient_light / 128), 0, 255) # Normalize lighting
|
76 |
-
|
77 |
-
# Preserve texture by modifying only A & B channels
|
78 |
-
blend_factor = 0.6 # Controls intensity of color change
|
79 |
-
img_lab[..., 1] = np.where(mask > 128, img_lab[..., 1] * (1 - blend_factor) + new_color_lab[1] * blend_factor, img_lab[..., 1])
|
80 |
-
img_lab[..., 2] = np.where(mask > 128, img_lab[..., 2] * (1 - blend_factor) + new_color_lab[2] * blend_factor, img_lab[..., 2])
|
81 |
-
|
82 |
-
# Convert back to RGB
|
83 |
-
img_recolored = cv2.cvtColor(img_lab, cv2.COLOR_LAB2RGB)
|
84 |
-
|
85 |
-
# Use Poisson blending for seamless integration with the environment
|
86 |
-
img_recolored = cv2.seamlessClone(img_recolored, img_np, mask, (img_np.shape[1]//2, img_np.shape[0]//2), cv2.MIXED_CLONE)
|
87 |
|
88 |
return Image.fromarray(img_recolored)
|
89 |
|
|
|
36 |
# Apply morphological operations for better segmentation
|
37 |
kernel = np.ones((5, 5), np.uint8)
|
38 |
mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel) # Close small gaps
|
39 |
+
mask = cv2.GaussianBlur(mask, (15, 15), 5) # Smooth edges for natural blending
|
40 |
|
41 |
return mask
|
42 |
|
43 |
+
def recolor_dress(image_np, mask, target_color):
|
44 |
+
"""Change dress color while preserving texture and shadows."""
|
45 |
+
img_lab = cv2.cvtColor(image_np, cv2.COLOR_RGB2LAB)
|
46 |
+
target_color_lab = cv2.cvtColor(np.uint8([[target_color]]), cv2.COLOR_BGR2LAB)[0][0]
|
47 |
+
|
48 |
+
# Preserve lightness (L) and change only chromatic channels (A & B)
|
49 |
+
blend_factor = 0.7
|
50 |
+
img_lab[..., 1] = np.where(mask > 128, img_lab[..., 1] * (1 - blend_factor) + target_color_lab[1] * blend_factor, img_lab[..., 1])
|
51 |
+
img_lab[..., 2] = np.where(mask > 128, img_lab[..., 2] * (1 - blend_factor) + target_color_lab[2] * blend_factor, img_lab[..., 2])
|
52 |
+
|
53 |
+
img_recolored = cv2.cvtColor(img_lab, cv2.COLOR_LAB2RGB)
|
54 |
+
return img_recolored
|
55 |
|
56 |
def change_dress_color(image_path, color):
|
57 |
+
"""Change the dress color naturally while keeping textures."""
|
58 |
if image_path is None:
|
59 |
return None
|
60 |
|
|
|
73 |
}
|
74 |
new_color_bgr = np.array(color_map.get(color, (0, 0, 255)), dtype=np.uint8) # Default to Red
|
75 |
|
76 |
+
# Recolor the dress naturally
|
77 |
+
img_recolored = recolor_dress(img_np, mask, new_color_bgr)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
|
79 |
return Image.fromarray(img_recolored)
|
80 |
|