Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -32,7 +32,7 @@ def segment_dress(image_np):
|
|
32 |
with torch.no_grad():
|
33 |
output = model(input_tensor)[0][0].squeeze().cpu().numpy()
|
34 |
|
35 |
-
mask = (output > 0.5).astype(np.uint8) # Thresholding for binary mask
|
36 |
mask = cv2.resize(mask, (image_np.shape[1], image_np.shape[0])) # Resize mask to original
|
37 |
return mask
|
38 |
|
@@ -48,25 +48,27 @@ def change_dress_color(image_path, color):
|
|
48 |
if mask is None:
|
49 |
return img # No dress detected
|
50 |
|
51 |
-
# Convert the selected color to
|
52 |
color_map = {
|
53 |
-
"Red": (0,
|
54 |
-
"Blue": (
|
55 |
-
"Green": (
|
56 |
-
"Yellow": (
|
57 |
-
"Purple": (
|
58 |
}
|
59 |
-
|
60 |
|
61 |
-
# Convert to
|
62 |
-
new_color_bgr = cv2.cvtColor(hsv_color, cv2.COLOR_HSV2BGR)[0][0]
|
63 |
-
|
64 |
-
# Apply the color change
|
65 |
img_hsv = cv2.cvtColor(img_np, cv2.COLOR_RGB2HSV)
|
66 |
-
img_hsv[..., 0] = mask * new_color_bgr[0] + (1 - mask) * img_hsv[..., 0] # Adjust hue
|
67 |
-
img_hsv[..., 1] = mask * new_color_bgr[1] + (1 - mask) * img_hsv[..., 1] # Adjust saturation
|
68 |
-
img_hsv[..., 2] = mask * new_color_bgr[2] + (1 - mask) * img_hsv[..., 2] # Adjust value
|
69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
img_recolored = cv2.cvtColor(img_hsv, cv2.COLOR_HSV2RGB)
|
71 |
|
72 |
return Image.fromarray(img_recolored)
|
|
|
32 |
with torch.no_grad():
|
33 |
output = model(input_tensor)[0][0].squeeze().cpu().numpy()
|
34 |
|
35 |
+
mask = (output > 0.5).astype(np.uint8) * 255 # Thresholding for binary mask
|
36 |
mask = cv2.resize(mask, (image_np.shape[1], image_np.shape[0])) # Resize mask to original
|
37 |
return mask
|
38 |
|
|
|
48 |
if mask is None:
|
49 |
return img # No dress detected
|
50 |
|
51 |
+
# Convert the selected color to BGR
|
52 |
color_map = {
|
53 |
+
"Red": (0, 0, 255),
|
54 |
+
"Blue": (255, 0, 0),
|
55 |
+
"Green": (0, 255, 0),
|
56 |
+
"Yellow": (0, 255, 255),
|
57 |
+
"Purple": (128, 0, 128)
|
58 |
}
|
59 |
+
new_color_bgr = np.array(color_map.get(color, (0, 0, 255)), dtype=np.uint8) # Default to Red
|
60 |
|
61 |
+
# Convert image to HSV
|
|
|
|
|
|
|
62 |
img_hsv = cv2.cvtColor(img_np, cv2.COLOR_RGB2HSV)
|
|
|
|
|
|
|
63 |
|
64 |
+
# Convert new color to HSV
|
65 |
+
new_color_hsv = cv2.cvtColor(np.uint8([[new_color_bgr]]), cv2.COLOR_BGR2HSV)[0][0]
|
66 |
+
|
67 |
+
# Apply the new color only to the masked dress area
|
68 |
+
img_hsv[..., 0] = np.where(mask == 255, new_color_hsv[0], img_hsv[..., 0]) # Hue
|
69 |
+
img_hsv[..., 1] = np.where(mask == 255, new_color_hsv[1], img_hsv[..., 1]) # Saturation
|
70 |
+
|
71 |
+
# Convert back to RGB
|
72 |
img_recolored = cv2.cvtColor(img_hsv, cv2.COLOR_HSV2RGB)
|
73 |
|
74 |
return Image.fromarray(img_recolored)
|