Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify Mask Handling When Only One Object is Detected. #352

Merged
merged 7 commits into from
Oct 22, 2024
10 changes: 8 additions & 2 deletions samgeo/text_sam.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,12 +337,18 @@ def predict(
masks = torch.tensor([])
if len(boxes) > 0:
masks = self.predict_sam(image_pil, boxes)
if 1 in masks.shape:
# If masks have 4 dimensions and the second dimension is 1 (e.g., [boxes, 1, height, width]),
# squeeze that dimension to reduce it to 3 dimensions ([boxes, height, width]).
# If boxes = 1, the mask's shape will be [1, height, width] after squeezing.
if masks.ndim == 4 and masks.shape[1] == 1:
masks = masks.squeeze(1)

if boxes.nelement() == 0: # No "object" instances found
print("No objects found in the image.")
return
mask_overlay = np.zeros_like(
image_np[..., 0], dtype=dtype
) # Create an empty mask overlay

else:
# Create an empty image to store the mask overlays
mask_overlay = np.zeros_like(
Expand Down
Loading