Spaces:
Sleeping
Sleeping
rogerjager
commited on
Update app.py
Browse filesremoved the something new it makes the images worse
app.py
CHANGED
@@ -117,60 +117,18 @@ net = build_model(hypar, device)
|
|
117 |
|
118 |
def refine_mask(mask):
|
119 |
"""
|
120 |
-
|
121 |
"""
|
122 |
-
#
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
# Use morphological operations to clean up the mask
|
130 |
-
kernel = np.ones((5,5), np.uint8)
|
131 |
-
cleaned_mask = cv2.morphologyEx(binary_mask, cv2.MORPH_CLOSE, kernel)
|
132 |
-
cleaned_mask = cv2.morphologyEx(cleaned_mask, cv2.MORPH_OPEN, kernel)
|
133 |
-
|
134 |
-
# Edge detection
|
135 |
-
edges = cv2.Canny(cleaned_mask, 100, 200)
|
136 |
-
|
137 |
-
# Dilate edges to create a transition area
|
138 |
-
dilated_edges = cv2.dilate(edges, kernel, iterations=2)
|
139 |
-
|
140 |
-
# Create a gradient in the transition area
|
141 |
-
distance = cv2.distanceTransform(255 - dilated_edges, cv2.DIST_L2, 5)
|
142 |
-
distance = cv2.normalize(distance, None, 0, 1, cv2.NORM_MINMAX)
|
143 |
-
|
144 |
-
# Combine the cleaned mask with the gradient transition
|
145 |
-
refined_mask = cleaned_mask.astype(float) / 255.0
|
146 |
-
refined_mask = np.maximum(refined_mask, distance)
|
147 |
-
refined_mask = (refined_mask * 255).astype(np.uint8)
|
148 |
-
|
149 |
-
# Final smoothing
|
150 |
-
refined_mask = cv2.GaussianBlur(refined_mask, (5, 5), 0)
|
151 |
|
152 |
return refined_mask
|
153 |
-
|
154 |
-
def remove_background(image_path):
|
155 |
-
# Load the image
|
156 |
-
image = Image.open(image_path).convert("RGBA")
|
157 |
-
|
158 |
-
# Create a mask (this is where you'd use the DIS model)
|
159 |
-
# For demonstration, let's create a simple circular mask
|
160 |
-
mask = Image.new("L", image.size, 0)
|
161 |
-
draw = ImageDraw.Draw(mask)
|
162 |
-
draw.ellipse((0, 0) + image.size, fill=255)
|
163 |
-
|
164 |
-
# Refine the mask
|
165 |
-
refined_mask = refine_mask(mask)
|
166 |
-
|
167 |
-
# Convert refined mask back to PIL Image
|
168 |
-
refined_mask_pil = Image.fromarray(refined_mask)
|
169 |
-
|
170 |
-
# Apply the refined mask
|
171 |
-
image.putalpha(refined_mask_pil)
|
172 |
-
|
173 |
-
return image
|
174 |
def inference(image):
|
175 |
image_path = image
|
176 |
|
|
|
117 |
|
118 |
def refine_mask(mask):
|
119 |
"""
|
120 |
+
Softly refine the mask using Gaussian Blur and feathering for smooth transitions.
|
121 |
"""
|
122 |
+
# Apply Gaussian Blur to soften edges and make the mask more continuous
|
123 |
+
refined_mask = cv2.GaussianBlur(mask, (5, 5), 0)
|
124 |
+
|
125 |
+
# Feather the edges for a smoother transition between foreground and background
|
126 |
+
feathered_mask = cv2.copyMakeBorder(refined_mask, 10, 10, 10, 10, cv2.BORDER_CONSTANT, value=[255])
|
127 |
+
feathered_mask = cv2.GaussianBlur(feathered_mask, (21, 21), 0)
|
128 |
+
refined_mask = feathered_mask[10:-10, 10:-10] # Remove border
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
129 |
|
130 |
return refined_mask
|
131 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
132 |
def inference(image):
|
133 |
image_path = image
|
134 |
|