Use the run_ours_box_or_points function to generate segmentation masks. This function accepts an image path, sampled points, point labels, and the model instance.
Parameters:
img_path: Path to the input image.pts_sampled: Coordinates for the prompts (e.g., points or box corners).pts_labels: Labels corresponding to the points (e.g., 1 for positive, 0 for negative).model: The initialized EfficientSAM model.
Returns:
- A boolean numpy array representing the predicted mask.
def run_ours_box_or_points(img_path, pts_sampled, pts_labels, model):
image_np = np.array(Image.open(img_path))
img_tensor = ToTensor()(image_np)
pts_sampled = torch.reshape(torch.tensor(pts_sampled), [1, 1, -1, 2])
pts_labels = torch.reshape(torch.tensor(pts_labels), [1, 1, -1])
predicted_logits, predicted_iou = model(
img_tensor[None, ...],
pts_sampled,
pts_labels,
)
sorted_ids = torch.argsort(predicted_iou, dim=-1, descending=True)
predicted_iou = torch.take_along_dim(predicted_iou, sorted_ids, dim=2)
predicted_logits = torch.take_along_dim(
predicted_logits, sorted_ids[..., None, None], dim=2
)
return torch.ge(predicted_logits[0, 0, 0, :, :], 0).cpu().detach().numpy()