Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -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=
|
|
|
40 |
|
41 |
return mask
|
42 |
|
@@ -66,17 +67,22 @@ def change_dress_color(image_path, color):
|
|
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 |
-
#
|
70 |
-
|
71 |
-
|
72 |
-
|
|
|
|
|
|
|
|
|
73 |
# Convert back to RGB
|
74 |
-
img_recolored = cv2.cvtColor(
|
75 |
|
76 |
-
# Apply Poisson blending for
|
77 |
-
|
|
|
78 |
|
79 |
-
return Image.fromarray(
|
80 |
|
81 |
# Gradio Interface
|
82 |
demo = gr.Interface(
|
|
|
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 the mask to reduce noise
|
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=1) # Expand the detected dress area
|
40 |
+
mask = cv2.medianBlur(mask, 5) # Reduce noise and smooth edges
|
41 |
|
42 |
return mask
|
43 |
|
|
|
67 |
img_lab = cv2.cvtColor(img_np, cv2.COLOR_RGB2LAB)
|
68 |
new_color_lab = cv2.cvtColor(np.uint8([[new_color_bgr]]), cv2.COLOR_BGR2LAB)[0][0]
|
69 |
|
70 |
+
# Adaptive color blending - preserves details and avoids over-saturation
|
71 |
+
l_channel, a_channel, b_channel = cv2.split(img_lab)
|
72 |
+
|
73 |
+
a_channel = np.where(mask == 255, new_color_lab[1], a_channel) # Modify A-channel
|
74 |
+
b_channel = np.where(mask == 255, new_color_lab[2], b_channel) # Modify B-channel
|
75 |
+
|
76 |
+
img_recolored = cv2.merge([l_channel, a_channel, b_channel])
|
77 |
+
|
78 |
# Convert back to RGB
|
79 |
+
img_recolored = cv2.cvtColor(img_recolored, cv2.COLOR_LAB2RGB)
|
80 |
|
81 |
+
# Apply Poisson blending for natural-looking color transition
|
82 |
+
center = (img_np.shape[1]//2, img_np.shape[0]//2)
|
83 |
+
img_final = cv2.seamlessClone(img_recolored, img_np, mask, center, cv2.NORMAL_CLONE)
|
84 |
|
85 |
+
return Image.fromarray(img_final)
|
86 |
|
87 |
# Gradio Interface
|
88 |
demo = gr.Interface(
|