Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -38,9 +38,6 @@ def segment_dress(image_np):
|
|
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 |
-
# Apply Gaussian blur to smooth mask edges
|
42 |
-
mask = cv2.GaussianBlur(mask, (5, 5), 0)
|
43 |
-
|
44 |
return mask
|
45 |
|
46 |
def change_dress_color(image_path, color):
|
@@ -65,20 +62,23 @@ def change_dress_color(image_path, color):
|
|
65 |
}
|
66 |
new_color_bgr = np.array(color_map.get(color, (0, 0, 255)), dtype=np.uint8) # Default to Red
|
67 |
|
68 |
-
# Convert image to
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
# Replace Hue where the mask is active
|
73 |
-
img_hsv[..., 0] = np.where(mask == 255, target_hue, img_hsv[..., 0])
|
74 |
|
|
|
|
|
|
|
|
|
75 |
# Convert back to RGB
|
76 |
-
img_recolored = cv2.cvtColor(
|
77 |
-
|
78 |
-
|
|
|
79 |
|
80 |
return Image.fromarray(img_recolored)
|
81 |
|
|
|
82 |
demo = gr.Interface(
|
83 |
fn=change_dress_color,
|
84 |
inputs=[
|
|
|
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 |
|
43 |
def change_dress_color(image_path, color):
|
|
|
62 |
}
|
63 |
new_color_bgr = np.array(color_map.get(color, (0, 0, 255)), dtype=np.uint8) # Default to Red
|
64 |
|
65 |
+
# Convert image to LAB color space for better blending
|
66 |
+
img_lab = cv2.cvtColor(img_np, cv2.COLOR_RGB2LAB)
|
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(img_lab, cv2.COLOR_LAB2RGB)
|
75 |
+
|
76 |
+
# Apply Poisson blending for realistic color application
|
77 |
+
img_recolored = cv2.seamlessClone(img_recolored, img_np, mask, (img_np.shape[1]//2, img_np.shape[0]//2), cv2.NORMAL_CLONE)
|
78 |
|
79 |
return Image.fromarray(img_recolored)
|
80 |
|
81 |
+
# Gradio Interface
|
82 |
demo = gr.Interface(
|
83 |
fn=change_dress_color,
|
84 |
inputs=[
|