Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -15,12 +15,11 @@ model.load_state_dict(state_dict)
|
|
15 |
model.eval()
|
16 |
|
17 |
def refine_mask(mask):
|
18 |
-
"""Refines mask using morphological
|
19 |
-
kernel = np.ones((
|
20 |
-
mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)
|
21 |
-
mask = cv2.
|
22 |
-
|
23 |
-
return cv2.GaussianBlur(mask, (5, 5), 0)
|
24 |
|
25 |
def segment_dress(image_np):
|
26 |
"""Segment dress using U²-Net"""
|
@@ -28,7 +27,7 @@ def segment_dress(image_np):
|
|
28 |
transforms.ToTensor(),
|
29 |
transforms.Resize((320, 320))
|
30 |
])
|
31 |
-
|
32 |
image = Image.fromarray(image_np).convert("RGB")
|
33 |
input_tensor = transform_pipeline(image).unsqueeze(0)
|
34 |
|
@@ -49,13 +48,13 @@ def apply_grabcut(image_np, dress_mask):
|
|
49 |
rect = (10, 10, image_np.shape[1] - 10, image_np.shape[0] - 10)
|
50 |
|
51 |
cv2.grabCut(image_np, mask, rect, bgd_model, fgd_model, 5, cv2.GC_INIT_WITH_MASK)
|
52 |
-
|
53 |
refined_mask = np.where((mask == 2) | (mask == 0), 0, 255).astype("uint8")
|
54 |
return refine_mask(refined_mask)
|
55 |
|
56 |
def recolor_dress(image_np, dress_mask, target_color):
|
57 |
"""Changes dress color while keeping texture & lighting intact"""
|
58 |
-
|
59 |
# Convert target color to LAB
|
60 |
target_color_lab = cv2.cvtColor(np.uint8([[target_color]]), cv2.COLOR_BGR2LAB)[0][0]
|
61 |
|
@@ -69,15 +68,17 @@ def recolor_dress(image_np, dress_mask, target_color):
|
|
69 |
|
70 |
mean_L, mean_A, mean_B = np.mean(dress_pixels, axis=0)
|
71 |
|
72 |
-
# Apply histogram-based color transfer
|
73 |
-
|
74 |
-
|
|
|
|
|
75 |
|
76 |
# Convert back to RGB
|
77 |
-
img_recolored = cv2.cvtColor(img_lab, cv2.COLOR_LAB2RGB)
|
78 |
|
79 |
# Create feathered mask for smooth blending
|
80 |
-
feathered_mask = cv2.GaussianBlur(dress_mask, (15, 15),
|
81 |
|
82 |
# Blend the recolored dress with the original image
|
83 |
img_final = (image_np * (1 - feathered_mask[..., None] / 255) + img_recolored * (feathered_mask[..., None] / 255)).astype(np.uint8)
|
@@ -94,7 +95,7 @@ def change_dress_color(image_path, color):
|
|
94 |
|
95 |
# Get dress segmentation mask
|
96 |
dress_mask = segment_dress(img_np)
|
97 |
-
|
98 |
if dress_mask is None:
|
99 |
return img # No dress detected
|
100 |
|
|
|
15 |
model.eval()
|
16 |
|
17 |
def refine_mask(mask):
|
18 |
+
"""Refines mask using morphological closing followed by Gaussian blur"""
|
19 |
+
kernel = np.ones((7, 7), np.uint8)
|
20 |
+
mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel) # Close holes inside dress
|
21 |
+
mask = cv2.GaussianBlur(mask, (7, 7), 1.5)
|
22 |
+
return mask
|
|
|
23 |
|
24 |
def segment_dress(image_np):
|
25 |
"""Segment dress using U²-Net"""
|
|
|
27 |
transforms.ToTensor(),
|
28 |
transforms.Resize((320, 320))
|
29 |
])
|
30 |
+
|
31 |
image = Image.fromarray(image_np).convert("RGB")
|
32 |
input_tensor = transform_pipeline(image).unsqueeze(0)
|
33 |
|
|
|
48 |
rect = (10, 10, image_np.shape[1] - 10, image_np.shape[0] - 10)
|
49 |
|
50 |
cv2.grabCut(image_np, mask, rect, bgd_model, fgd_model, 5, cv2.GC_INIT_WITH_MASK)
|
51 |
+
|
52 |
refined_mask = np.where((mask == 2) | (mask == 0), 0, 255).astype("uint8")
|
53 |
return refine_mask(refined_mask)
|
54 |
|
55 |
def recolor_dress(image_np, dress_mask, target_color):
|
56 |
"""Changes dress color while keeping texture & lighting intact"""
|
57 |
+
|
58 |
# Convert target color to LAB
|
59 |
target_color_lab = cv2.cvtColor(np.uint8([[target_color]]), cv2.COLOR_BGR2LAB)[0][0]
|
60 |
|
|
|
68 |
|
69 |
mean_L, mean_A, mean_B = np.mean(dress_pixels, axis=0)
|
70 |
|
71 |
+
# Apply histogram-based color transfer with topological adjustment
|
72 |
+
a_shift = target_color_lab[1] - mean_A
|
73 |
+
b_shift = target_color_lab[2] - mean_B
|
74 |
+
img_lab[..., 1] = np.clip(img_lab[..., 1] + (dress_mask / 255.0) * a_shift, 0, 255)
|
75 |
+
img_lab[..., 2] = np.clip(img_lab[..., 2] + (dress_mask / 255.0) * b_shift, 0, 255)
|
76 |
|
77 |
# Convert back to RGB
|
78 |
+
img_recolored = cv2.cvtColor(img_lab.astype(np.uint8), cv2.COLOR_LAB2RGB)
|
79 |
|
80 |
# Create feathered mask for smooth blending
|
81 |
+
feathered_mask = cv2.GaussianBlur(dress_mask, (15, 15), 5)
|
82 |
|
83 |
# Blend the recolored dress with the original image
|
84 |
img_final = (image_np * (1 - feathered_mask[..., None] / 255) + img_recolored * (feathered_mask[..., None] / 255)).astype(np.uint8)
|
|
|
95 |
|
96 |
# Get dress segmentation mask
|
97 |
dress_mask = segment_dress(img_np)
|
98 |
+
|
99 |
if dress_mask is None:
|
100 |
return img # No dress detected
|
101 |
|