Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -7,12 +7,12 @@ from torchvision import transforms
|
|
7 |
from cloth_segmentation.networks.u2net import U2NET # Import U²-Net
|
8 |
|
9 |
# Load U²-Net model
|
10 |
-
model_path = "cloth_segmentation/networks/u2net.pth"
|
11 |
model = U2NET(3, 1)
|
12 |
|
13 |
# Load the state dictionary
|
14 |
state_dict = torch.load(model_path, map_location=torch.device('cpu'))
|
15 |
-
state_dict = {k.replace('module.', ''): v for k, v in state_dict.items()}
|
16 |
model.load_state_dict(state_dict)
|
17 |
model.eval()
|
18 |
|
@@ -33,10 +33,11 @@ def segment_dress(image_np):
|
|
33 |
# Resize mask to original image size
|
34 |
mask = cv2.resize(mask, (image_np.shape[1], image_np.shape[0]), interpolation=cv2.INTER_NEAREST)
|
35 |
|
36 |
-
#
|
37 |
-
kernel = np.ones((
|
38 |
mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel) # Close small gaps
|
39 |
mask = cv2.dilate(mask, kernel, iterations=2) # Expand the detected dress area
|
|
|
40 |
|
41 |
return mask
|
42 |
|
@@ -52,29 +53,28 @@ def change_dress_color(image_path, color):
|
|
52 |
if mask is None:
|
53 |
return img # No dress detected
|
54 |
|
55 |
-
# Convert
|
|
|
|
|
|
|
56 |
color_map = {
|
57 |
-
"Red":
|
58 |
-
"Blue":
|
59 |
-
"Green":
|
60 |
-
"Yellow":
|
61 |
-
"Purple":
|
62 |
}
|
63 |
-
|
64 |
-
|
65 |
-
#
|
66 |
-
|
67 |
-
new_color_lab = cv2.cvtColor(np.uint8([[new_color_bgr]]), cv2.COLOR_BGR2LAB)[0][0]
|
68 |
-
|
69 |
-
# Preserve texture by only modifying the A & B channels
|
70 |
-
img_lab[..., 1] = np.where(mask == 255, new_color_lab[1], img_lab[..., 1]) # Modify A-channel
|
71 |
-
img_lab[..., 2] = np.where(mask == 255, new_color_lab[2], img_lab[..., 2]) # Modify B-channel
|
72 |
|
73 |
# Convert back to RGB
|
74 |
-
img_recolored = cv2.cvtColor(
|
75 |
|
76 |
-
# Apply Poisson blending for
|
77 |
-
|
|
|
78 |
|
79 |
return Image.fromarray(img_recolored)
|
80 |
|
|
|
7 |
from cloth_segmentation.networks.u2net import U2NET # Import U²-Net
|
8 |
|
9 |
# Load U²-Net model
|
10 |
+
model_path = "cloth_segmentation/networks/u2net.pth"
|
11 |
model = U2NET(3, 1)
|
12 |
|
13 |
# Load the state dictionary
|
14 |
state_dict = torch.load(model_path, map_location=torch.device('cpu'))
|
15 |
+
state_dict = {k.replace('module.', ''): v for k, v in state_dict.items()}
|
16 |
model.load_state_dict(state_dict)
|
17 |
model.eval()
|
18 |
|
|
|
33 |
# Resize mask to original image size
|
34 |
mask = cv2.resize(mask, (image_np.shape[1], image_np.shape[0]), interpolation=cv2.INTER_NEAREST)
|
35 |
|
36 |
+
# Refine mask using morphological operations
|
37 |
+
kernel = np.ones((5, 5), np.uint8)
|
38 |
mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel) # Close small gaps
|
39 |
mask = cv2.dilate(mask, kernel, iterations=2) # Expand the detected dress area
|
40 |
+
mask = cv2.GaussianBlur(mask, (5, 5), 0) # Smooth edges
|
41 |
|
42 |
return mask
|
43 |
|
|
|
53 |
if mask is None:
|
54 |
return img # No dress detected
|
55 |
|
56 |
+
# Convert image to HSV for color modification
|
57 |
+
img_hsv = cv2.cvtColor(img_np, cv2.COLOR_RGB2HSV)
|
58 |
+
|
59 |
+
# Define new color in HSV (only modifying the Hue)
|
60 |
color_map = {
|
61 |
+
"Red": 0, # Hue value for Red
|
62 |
+
"Blue": 120, # Hue value for Blue
|
63 |
+
"Green": 60, # Hue value for Green
|
64 |
+
"Yellow": 30, # Hue value for Yellow
|
65 |
+
"Purple": 150 # Hue value for Purple
|
66 |
}
|
67 |
+
new_hue = color_map.get(color, 0)
|
68 |
+
|
69 |
+
# Modify only the Hue channel where the mask is applied
|
70 |
+
img_hsv[..., 0] = np.where(mask > 128, new_hue, img_hsv[..., 0])
|
|
|
|
|
|
|
|
|
|
|
71 |
|
72 |
# Convert back to RGB
|
73 |
+
img_recolored = cv2.cvtColor(img_hsv, cv2.COLOR_HSV2RGB)
|
74 |
|
75 |
+
# Apply Poisson blending for natural integration
|
76 |
+
center = (img_np.shape[1] // 2, img_np.shape[0] // 2)
|
77 |
+
img_recolored = cv2.seamlessClone(img_recolored, img_np, mask, center, cv2.MIXED_CLONE)
|
78 |
|
79 |
return Image.fromarray(img_recolored)
|
80 |
|