Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -28,7 +28,7 @@ def segment_dress(image_np):
|
|
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.
|
32 |
|
33 |
return dress_mask
|
34 |
|
@@ -43,19 +43,26 @@ def recolor_dress(image_np, dress_mask, target_color):
|
|
43 |
|
44 |
# Compute mean LAB values of dress pixels
|
45 |
dress_pixels = img_lab[dress_mask > 0]
|
46 |
-
|
|
|
47 |
|
48 |
-
|
|
|
|
|
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 |
-
#
|
56 |
-
|
|
|
|
|
|
|
|
|
57 |
|
58 |
-
return
|
59 |
|
60 |
def change_dress_color(image_path, color):
|
61 |
"""Main function to change dress color naturally"""
|
|
|
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_LINEAR)
|
32 |
|
33 |
return dress_mask
|
34 |
|
|
|
43 |
|
44 |
# Compute mean LAB values of dress pixels
|
45 |
dress_pixels = img_lab[dress_mask > 0]
|
46 |
+
if len(dress_pixels) == 0:
|
47 |
+
return image_np # No dress detected
|
48 |
|
49 |
+
mean_L, mean_A, mean_B = np.mean(dress_pixels, axis=0)
|
50 |
+
|
51 |
+
# Adjust LAB channels for the new color
|
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 |
+
# Convert mask to 3 channels for seamless blending
|
59 |
+
mask_bgr = cv2.cvtColor(dress_mask, cv2.COLOR_GRAY2BGR)
|
60 |
+
|
61 |
+
# Smooth edges for natural blending using seamlessClone
|
62 |
+
center = (image_np.shape[1] // 2, image_np.shape[0] // 2)
|
63 |
+
blended = cv2.seamlessClone(img_recolored, image_np, mask_bgr, center, cv2.NORMAL_CLONE)
|
64 |
|
65 |
+
return blended
|
66 |
|
67 |
def change_dress_color(image_path, color):
|
68 |
"""Main function to change dress color naturally"""
|