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

Use skimage.measure.label if failed in importing sam2._C module #216

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions sam2/utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,16 @@ def get_connected_components(mask):
- counts: A tensor of shape (N, 1, H, W) containing the area of the connected
components for foreground pixels and 0 for background pixels.
"""
from sam2 import _C
try:
from sam2 import _C

return _C.get_connected_componnets(mask.to(torch.uint8).contiguous())
except Exception:
import skimage.measure

return _C.get_connected_componnets(mask.to(torch.uint8).contiguous())
return skimage.measure.label(
mask.to(torch.uint8).contiguous().cpu().numpy(), return_num=True
)


def mask_to_box(masks: torch.Tensor):
Expand Down