Spaces:
Build error
Build error
Create segment_functions.py
Browse files- segment_functions.py +69 -0
segment_functions.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import pipeline, SamModel, SamProcessor
|
| 2 |
+
import torch
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import requests
|
| 6 |
+
|
| 7 |
+
# Image Segmentation Model
|
| 8 |
+
sam_model = SamModel.from_pretrained("Zigeng/SlimSAM-uniform-77")
|
| 9 |
+
sam_processor = SamProcessor.from_pretrained("Zigeng/SlimSAM-uniform-77")
|
| 10 |
+
|
| 11 |
+
def show_colored_mask(mask, combined_mask, color):
|
| 12 |
+
"""
|
| 13 |
+
Add a single-colored mask to the combined mask.
|
| 14 |
+
Args:
|
| 15 |
+
mask (numpy.ndarray): Binary mask to overlay.
|
| 16 |
+
combined_mask (numpy.ndarray): Combined RGBA mask.
|
| 17 |
+
color (tuple): RGBA color for the mask.
|
| 18 |
+
"""
|
| 19 |
+
if mask.ndim == 3: # If mask has channels then take the first one
|
| 20 |
+
mask = mask[0]
|
| 21 |
+
mask = mask.squeeze() # Remove extra dimension
|
| 22 |
+
|
| 23 |
+
mask_binary = (mask > 0).astype(np.uint8) # Ensure the mask is binary
|
| 24 |
+
|
| 25 |
+
# Apply the color to the mask
|
| 26 |
+
for c in range(3): # RGB channels
|
| 27 |
+
combined_mask[:, :, c] = np.where(mask_binary > 0, color[c], combined_mask[:, :, c])
|
| 28 |
+
combined_mask[:, :, 3] = np.where(mask_binary > 0, color[3], combined_mask[:, :, 3]) # Alpha channel (transperency)
|
| 29 |
+
|
| 30 |
+
def segment_image(input_image, input_points):
|
| 31 |
+
"""
|
| 32 |
+
Perform image segmentation and overlay masks with a single solid color.
|
| 33 |
+
Args:
|
| 34 |
+
input_image (PIL.Image): The input image.
|
| 35 |
+
input_points (list): List of points [[x, y], ...].
|
| 36 |
+
Returns:
|
| 37 |
+
PIL.Image: Image with masks applied in one solid red color.
|
| 38 |
+
"""
|
| 39 |
+
# Convert input points to a 4D tensor
|
| 40 |
+
input_points_tensor = torch.tensor(input_points, dtype=torch.float32).unsqueeze(0).unsqueeze(1)
|
| 41 |
+
|
| 42 |
+
# Process input and run the SAM model
|
| 43 |
+
inputs = sam_processor(input_image, input_points=input_points_tensor, return_tensors="pt")
|
| 44 |
+
with torch.no_grad():
|
| 45 |
+
outputs = sam_model(**inputs)
|
| 46 |
+
|
| 47 |
+
# Post-process masks
|
| 48 |
+
predicted_masks = sam_processor.image_processor.post_process_masks(
|
| 49 |
+
outputs.pred_masks, inputs["original_sizes"], inputs["reshaped_input_sizes"]
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
# Define a solid red color with full opacity
|
| 53 |
+
single_color = (255, 0, 0, 100)
|
| 54 |
+
|
| 55 |
+
# Prepare a combined RGBA mask
|
| 56 |
+
image_size = input_image.size
|
| 57 |
+
combined_mask = np.zeros((image_size[1], image_size[0], 4), dtype=np.uint8)
|
| 58 |
+
|
| 59 |
+
# Apply all masks using the single color
|
| 60 |
+
for mask in predicted_masks[0]:
|
| 61 |
+
mask = mask.numpy()
|
| 62 |
+
show_colored_mask(mask, combined_mask, single_color)
|
| 63 |
+
|
| 64 |
+
# Combine the mask with the original image
|
| 65 |
+
input_image_rgba = input_image.convert("RGBA") # Red Green Blue Alpha
|
| 66 |
+
combined_image = Image.alpha_composite(input_image_rgba, Image.fromarray(combined_mask, "RGBA"))
|
| 67 |
+
|
| 68 |
+
return combined_image
|
| 69 |
+
|