Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -4,38 +4,18 @@ import torch
|
|
4 |
import cv2
|
5 |
from PIL import Image
|
6 |
from torchvision import transforms
|
7 |
-
from cloth_segmentation.networks.u2net import U2NET
|
8 |
|
9 |
# Load U²-Net model
|
10 |
model_path = "cloth_segmentation/networks/u2net.pth"
|
11 |
model = U2NET(3, 1)
|
12 |
state_dict = torch.load(model_path, map_location=torch.device('cpu'))
|
13 |
-
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 detect_design(image_np):
|
18 |
-
"""Detects the design on the dress using edge detection and adaptive thresholding."""
|
19 |
-
gray = cv2.cvtColor(image_np, cv2.COLOR_RGB2GRAY)
|
20 |
-
|
21 |
-
# Use adaptive thresholding to segment the design
|
22 |
-
adaptive_thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
|
23 |
-
cv2.THRESH_BINARY_INV, 11, 2)
|
24 |
-
|
25 |
-
# Detect edges using Canny
|
26 |
-
edges = cv2.Canny(gray, 50, 150)
|
27 |
-
|
28 |
-
# Combine both masks
|
29 |
-
design_mask = cv2.bitwise_or(adaptive_thresh, edges)
|
30 |
-
|
31 |
-
# Morphological operations to remove noise
|
32 |
-
kernel = np.ones((3, 3), np.uint8)
|
33 |
-
design_mask = cv2.morphologyEx(design_mask, cv2.MORPH_CLOSE, kernel)
|
34 |
-
|
35 |
-
return design_mask
|
36 |
-
|
37 |
def segment_dress(image_np):
|
38 |
-
"""Segment
|
39 |
transform_pipeline = transforms.Compose([
|
40 |
transforms.ToTensor(),
|
41 |
transforms.Resize((320, 320))
|
@@ -47,35 +27,38 @@ def segment_dress(image_np):
|
|
47 |
with torch.no_grad():
|
48 |
output = model(input_tensor)[0][0].squeeze().cpu().numpy()
|
49 |
|
50 |
-
# Convert output to mask
|
51 |
dress_mask = (output > 0.5).astype(np.uint8) * 255
|
52 |
dress_mask = cv2.resize(dress_mask, (image_np.shape[1], image_np.shape[0]), interpolation=cv2.INTER_NEAREST)
|
53 |
|
54 |
-
# Morphological operations for smoothness
|
55 |
-
kernel = np.ones((5, 5), np.uint8)
|
56 |
-
dress_mask = cv2.morphologyEx(dress_mask, cv2.MORPH_CLOSE, kernel)
|
57 |
-
|
58 |
return dress_mask
|
59 |
|
60 |
-
def recolor_dress(image_np, dress_mask,
|
61 |
-
"""Change dress color while
|
62 |
|
63 |
-
|
64 |
target_color_lab = cv2.cvtColor(np.uint8([[target_color]]), cv2.COLOR_BGR2LAB)[0][0]
|
65 |
|
66 |
-
#
|
67 |
-
|
|
|
|
|
|
|
|
|
68 |
|
69 |
-
#
|
70 |
-
|
71 |
-
img_lab[...,
|
72 |
-
img_lab[..., 2] = np.where(recolor_mask > 128, img_lab[..., 2] * (1 - blend_factor) + target_color_lab[2] * blend_factor, img_lab[..., 2])
|
73 |
|
|
|
74 |
img_recolored = cv2.cvtColor(img_lab, cv2.COLOR_LAB2RGB)
|
|
|
|
|
|
|
|
|
75 |
return img_recolored
|
76 |
|
77 |
def change_dress_color(image_path, color):
|
78 |
-
"""
|
79 |
if image_path is None:
|
80 |
return None
|
81 |
|
@@ -88,19 +71,16 @@ def change_dress_color(image_path, color):
|
|
88 |
if dress_mask is None:
|
89 |
return img # No dress detected
|
90 |
|
91 |
-
# Detect design on the dress
|
92 |
-
design_mask = detect_design(img_np)
|
93 |
-
|
94 |
# Convert the selected color to BGR
|
95 |
color_map = {
|
96 |
"Red": (0, 0, 255), "Blue": (255, 0, 0), "Green": (0, 255, 0), "Yellow": (0, 255, 255),
|
97 |
"Purple": (128, 0, 128), "Orange": (0, 165, 255), "Cyan": (255, 255, 0), "Magenta": (255, 0, 255),
|
98 |
"White": (255, 255, 255), "Black": (0, 0, 0)
|
99 |
}
|
100 |
-
new_color_bgr = np.array(color_map.get(color, (0, 0, 255)), dtype=np.uint8)
|
101 |
-
|
102 |
-
# Apply recoloring
|
103 |
-
img_recolored = recolor_dress(img_np, dress_mask,
|
104 |
|
105 |
return Image.fromarray(img_recolored)
|
106 |
|
@@ -112,8 +92,8 @@ demo = gr.Interface(
|
|
112 |
gr.Radio(["Red", "Blue", "Green", "Yellow", "Purple", "Orange", "Cyan", "Magenta", "White", "Black"], label="Choose New Dress Color")
|
113 |
],
|
114 |
outputs=gr.Image(type="pil", label="Color Changed Dress"),
|
115 |
-
title="Dress Color Changer",
|
116 |
-
description="Upload an image of a dress and select a new color
|
117 |
)
|
118 |
|
119 |
if __name__ == "__main__":
|
|
|
4 |
import cv2
|
5 |
from PIL import Image
|
6 |
from torchvision import transforms
|
7 |
+
from cloth_segmentation.networks.u2net import U2NET
|
8 |
|
9 |
# Load U²-Net model
|
10 |
model_path = "cloth_segmentation/networks/u2net.pth"
|
11 |
model = U2NET(3, 1)
|
12 |
state_dict = torch.load(model_path, map_location=torch.device('cpu'))
|
13 |
+
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([
|
20 |
transforms.ToTensor(),
|
21 |
transforms.Resize((320, 320))
|
|
|
27 |
with torch.no_grad():
|
28 |
output = model(input_tensor)[0][0].squeeze().cpu().numpy()
|
29 |
|
|
|
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_NEAREST)
|
32 |
|
|
|
|
|
|
|
|
|
33 |
return dress_mask
|
34 |
|
35 |
+
def recolor_dress(image_np, dress_mask, target_color):
|
36 |
+
"""Change dress color naturally while keeping textures intact"""
|
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 for better color control
|
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 |
+
mean_L, mean_A, mean_B = dress_pixels[:, 0].mean(), dress_pixels[:, 1].mean(), dress_pixels[:, 2].mean()
|
47 |
|
48 |
+
# Compute new color adjustment
|
49 |
+
img_lab[..., 1] = np.where(dress_mask > 128, img_lab[..., 1] - mean_A + target_color_lab[1], img_lab[..., 1])
|
50 |
+
img_lab[..., 2] = np.where(dress_mask > 128, img_lab[..., 2] - mean_B + target_color_lab[2], img_lab[..., 2])
|
|
|
51 |
|
52 |
+
# Convert back to RGB
|
53 |
img_recolored = cv2.cvtColor(img_lab, cv2.COLOR_LAB2RGB)
|
54 |
+
|
55 |
+
# Smooth edges for natural blending
|
56 |
+
img_recolored = cv2.seamlessClone(img_recolored, image_np, dress_mask, (image_np.shape[1]//2, image_np.shape[0]//2), cv2.NORMAL_CLONE)
|
57 |
+
|
58 |
return img_recolored
|
59 |
|
60 |
def change_dress_color(image_path, color):
|
61 |
+
"""Main function to change dress color naturally"""
|
62 |
if image_path is None:
|
63 |
return None
|
64 |
|
|
|
71 |
if dress_mask is None:
|
72 |
return img # No dress detected
|
73 |
|
|
|
|
|
|
|
74 |
# Convert the selected color to BGR
|
75 |
color_map = {
|
76 |
"Red": (0, 0, 255), "Blue": (255, 0, 0), "Green": (0, 255, 0), "Yellow": (0, 255, 255),
|
77 |
"Purple": (128, 0, 128), "Orange": (0, 165, 255), "Cyan": (255, 255, 0), "Magenta": (255, 0, 255),
|
78 |
"White": (255, 255, 255), "Black": (0, 0, 0)
|
79 |
}
|
80 |
+
new_color_bgr = np.array(color_map.get(color, (0, 0, 255)), dtype=np.uint8)
|
81 |
+
|
82 |
+
# Apply recoloring with blending
|
83 |
+
img_recolored = recolor_dress(img_np, dress_mask, new_color_bgr)
|
84 |
|
85 |
return Image.fromarray(img_recolored)
|
86 |
|
|
|
92 |
gr.Radio(["Red", "Blue", "Green", "Yellow", "Purple", "Orange", "Cyan", "Magenta", "White", "Black"], label="Choose New Dress Color")
|
93 |
],
|
94 |
outputs=gr.Image(type="pil", label="Color Changed Dress"),
|
95 |
+
title="Realistic Dress Color Changer",
|
96 |
+
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."
|
97 |
)
|
98 |
|
99 |
if __name__ == "__main__":
|