Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -14,6 +14,14 @@ state_dict = {k.replace('module.', ''): v for k, v in state_dict.items()}
|
|
14 |
model.load_state_dict(state_dict)
|
15 |
model.eval()
|
16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
def segment_dress(image_np):
|
18 |
"""Segment dress using U²-Net"""
|
19 |
transform_pipeline = transforms.Compose([
|
@@ -30,17 +38,30 @@ def segment_dress(image_np):
|
|
30 |
dress_mask = (output > 0.5).astype(np.uint8) * 255
|
31 |
dress_mask = cv2.resize(dress_mask, (image_np.shape[1], image_np.shape[0]), interpolation=cv2.INTER_LINEAR)
|
32 |
|
33 |
-
return dress_mask
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
def recolor_dress(image_np, dress_mask, target_color):
|
36 |
-
"""
|
37 |
|
38 |
# Convert target color to LAB
|
39 |
target_color_lab = cv2.cvtColor(np.uint8([[target_color]]), cv2.COLOR_BGR2LAB)[0][0]
|
40 |
|
41 |
-
# Convert image to LAB
|
42 |
img_lab = cv2.cvtColor(image_np, cv2.COLOR_RGB2LAB)
|
43 |
-
|
44 |
# Compute mean LAB values of dress pixels
|
45 |
dress_pixels = img_lab[dress_mask > 0]
|
46 |
if len(dress_pixels) == 0:
|
@@ -48,21 +69,20 @@ def recolor_dress(image_np, dress_mask, target_color):
|
|
48 |
|
49 |
mean_L, mean_A, mean_B = np.mean(dress_pixels, axis=0)
|
50 |
|
51 |
-
#
|
52 |
img_lab[..., 1] = np.where(dress_mask > 128, img_lab[..., 1] - mean_A + target_color_lab[1], img_lab[..., 1])
|
53 |
img_lab[..., 2] = np.where(dress_mask > 128, img_lab[..., 2] - mean_B + target_color_lab[2], img_lab[..., 2])
|
54 |
|
55 |
# Convert back to RGB
|
56 |
img_recolored = cv2.cvtColor(img_lab, cv2.COLOR_LAB2RGB)
|
57 |
|
58 |
-
#
|
59 |
-
|
60 |
|
61 |
-
#
|
62 |
-
|
63 |
-
blended = cv2.seamlessClone(img_recolored, image_np, mask_bgr, center, cv2.NORMAL_CLONE)
|
64 |
|
65 |
-
return
|
66 |
|
67 |
def change_dress_color(image_path, color):
|
68 |
"""Main function to change dress color naturally"""
|
@@ -78,6 +98,9 @@ def change_dress_color(image_path, color):
|
|
78 |
if dress_mask is None:
|
79 |
return img # No dress detected
|
80 |
|
|
|
|
|
|
|
81 |
# Convert the selected color to BGR
|
82 |
color_map = {
|
83 |
"Red": (0, 0, 255), "Blue": (255, 0, 0), "Green": (0, 255, 0), "Yellow": (0, 255, 255),
|
@@ -99,7 +122,7 @@ demo = gr.Interface(
|
|
99 |
gr.Radio(["Red", "Blue", "Green", "Yellow", "Purple", "Orange", "Cyan", "Magenta", "White", "Black"], label="Choose New Dress Color")
|
100 |
],
|
101 |
outputs=gr.Image(type="pil", label="Color Changed Dress"),
|
102 |
-
title="
|
103 |
description="Upload an image of a dress and select a new color. The AI will change the dress color naturally while keeping the fabric texture."
|
104 |
)
|
105 |
|
|
|
14 |
model.load_state_dict(state_dict)
|
15 |
model.eval()
|
16 |
|
17 |
+
def refine_mask(mask):
|
18 |
+
"""Refines mask using morphological operations"""
|
19 |
+
kernel = np.ones((5, 5), np.uint8)
|
20 |
+
mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)
|
21 |
+
mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)
|
22 |
+
|
23 |
+
return cv2.GaussianBlur(mask, (5, 5), 0)
|
24 |
+
|
25 |
def segment_dress(image_np):
|
26 |
"""Segment dress using U²-Net"""
|
27 |
transform_pipeline = transforms.Compose([
|
|
|
38 |
dress_mask = (output > 0.5).astype(np.uint8) * 255
|
39 |
dress_mask = cv2.resize(dress_mask, (image_np.shape[1], image_np.shape[0]), interpolation=cv2.INTER_LINEAR)
|
40 |
|
41 |
+
return refine_mask(dress_mask)
|
42 |
+
|
43 |
+
def apply_grabcut(image_np, dress_mask):
|
44 |
+
"""Refines the mask using GrabCut to avoid color bleeding"""
|
45 |
+
bgd_model = np.zeros((1, 65), np.float64)
|
46 |
+
fgd_model = np.zeros((1, 65), np.float64)
|
47 |
+
|
48 |
+
mask = np.where(dress_mask > 0, cv2.GC_FGD, cv2.GC_BGD).astype('uint8')
|
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 |
|
62 |
+
# Convert image to LAB
|
63 |
img_lab = cv2.cvtColor(image_np, cv2.COLOR_RGB2LAB)
|
64 |
+
|
65 |
# Compute mean LAB values of dress pixels
|
66 |
dress_pixels = img_lab[dress_mask > 0]
|
67 |
if len(dress_pixels) == 0:
|
|
|
69 |
|
70 |
mean_L, mean_A, mean_B = np.mean(dress_pixels, axis=0)
|
71 |
|
72 |
+
# Apply histogram-based color transfer
|
73 |
img_lab[..., 1] = np.where(dress_mask > 128, img_lab[..., 1] - mean_A + target_color_lab[1], img_lab[..., 1])
|
74 |
img_lab[..., 2] = np.where(dress_mask > 128, img_lab[..., 2] - mean_B + target_color_lab[2], img_lab[..., 2])
|
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), 10)
|
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)
|
|
|
84 |
|
85 |
+
return img_final
|
86 |
|
87 |
def change_dress_color(image_path, color):
|
88 |
"""Main function to change dress color naturally"""
|
|
|
98 |
if dress_mask is None:
|
99 |
return img # No dress detected
|
100 |
|
101 |
+
# Further refine mask with GrabCut
|
102 |
+
dress_mask = apply_grabcut(img_np, dress_mask)
|
103 |
+
|
104 |
# Convert the selected color to BGR
|
105 |
color_map = {
|
106 |
"Red": (0, 0, 255), "Blue": (255, 0, 0), "Green": (0, 255, 0), "Yellow": (0, 255, 255),
|
|
|
122 |
gr.Radio(["Red", "Blue", "Green", "Yellow", "Purple", "Orange", "Cyan", "Magenta", "White", "Black"], label="Choose New Dress Color")
|
123 |
],
|
124 |
outputs=gr.Image(type="pil", label="Color Changed Dress"),
|
125 |
+
title="AI-Powered Dress Color Changer",
|
126 |
description="Upload an image of a dress and select a new color. The AI will change the dress color naturally while keeping the fabric texture."
|
127 |
)
|
128 |
|