Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -37,14 +37,16 @@ def apply_mask(image: Image.Image, prompt: str, color: str) -> Image.Image:
|
|
| 37 |
|
| 38 |
# Get the binary mask from predictions
|
| 39 |
mask = preds.sigmoid().detach().cpu().numpy()
|
| 40 |
-
mask = (mask
|
|
|
|
|
|
|
| 41 |
|
| 42 |
# Convert image to RGBA
|
| 43 |
image_np = np.array(image.convert("RGBA"))
|
| 44 |
|
| 45 |
# Resize mask to match image size
|
| 46 |
mask_resized = cv2.resize(mask, (image_np.shape[1], image_np.shape[0]))
|
| 47 |
-
mask_3d = np.stack([
|
| 48 |
|
| 49 |
# Convert the color string to an RGB tuple
|
| 50 |
color_rgb = parse_color(color)
|
|
@@ -52,8 +54,11 @@ def apply_mask(image: Image.Image, prompt: str, color: str) -> Image.Image:
|
|
| 52 |
|
| 53 |
# Create an overlay with the selected color
|
| 54 |
overlay = np.zeros_like(image_np, dtype=np.uint8)
|
| 55 |
-
overlay[:] = overlay_color
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
| 57 |
# Apply the mask to the image
|
| 58 |
masked_image = np.where(mask_3d == 1, overlay, image_np)
|
| 59 |
return Image.fromarray(masked_image)
|
|
|
|
| 37 |
|
| 38 |
# Get the binary mask from predictions
|
| 39 |
mask = preds.sigmoid().detach().cpu().numpy()
|
| 40 |
+
mask = cv2.resize(mask[0], (image_np.shape[1], image_np.shape[0]))
|
| 41 |
+
mask = cv2.GaussianBlur(mask, (15, 15), 0)
|
| 42 |
+
mask_bin = (mask > 0.4).astype(np.uint8)
|
| 43 |
|
| 44 |
# Convert image to RGBA
|
| 45 |
image_np = np.array(image.convert("RGBA"))
|
| 46 |
|
| 47 |
# Resize mask to match image size
|
| 48 |
mask_resized = cv2.resize(mask, (image_np.shape[1], image_np.shape[0]))
|
| 49 |
+
mask_3d = np.stack([mask_bin] * 4, axis=-1) # Extend mask to 3D
|
| 50 |
|
| 51 |
# Convert the color string to an RGB tuple
|
| 52 |
color_rgb = parse_color(color)
|
|
|
|
| 54 |
|
| 55 |
# Create an overlay with the selected color
|
| 56 |
overlay = np.zeros_like(image_np, dtype=np.uint8)
|
| 57 |
+
# overlay[:] = overlay_color
|
| 58 |
+
masked_image = image_np.copy()
|
| 59 |
+
masked_image[mask_bin == 1] = (
|
| 60 |
+
0.5 * masked_image[mask_bin == 1] + 0.5 * overlay[mask_bin == 1]).astype(np.units)
|
| 61 |
+
)
|
| 62 |
# Apply the mask to the image
|
| 63 |
masked_image = np.where(mask_3d == 1, overlay, image_np)
|
| 64 |
return Image.fromarray(masked_image)
|