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

Fix mask_annotate for int dtypes #1445

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
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
24 changes: 20 additions & 4 deletions supervision/validators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import numpy as np

from supervision.utils.internal import warn_deprecated


def validate_xyxy(xyxy: Any) -> None:
expected_shape = "(_, 4)"
Expand All @@ -15,15 +17,29 @@ def validate_xyxy(xyxy: Any) -> None:


def validate_mask(mask: Any, n: int) -> None:
if mask is None:
return

expected_shape = f"({n}, H, W)"
actual_shape = str(getattr(mask, "shape", None))
is_valid = mask is None or (
actual_dtype = getattr(mask, "dtype", None)

is_valid_shape = (
isinstance(mask, np.ndarray) and len(mask.shape) == 3 and mask.shape[0] == n
)
if not is_valid:
if not is_valid_shape:
raise ValueError(
f"mask must be a 3D np.ndarray with shape {expected_shape}, but got shape "
f"{actual_shape}"
"Mask must be a 3D np.ndarray with shape "
+ f"{expected_shape}, but got shape {actual_shape}"
)
if not np.issubdtype(actual_dtype, bool):
warn_deprecated(
f"A `Detections` object was created with a mask of type {actual_dtype}."
" Masks of type other than `bool` are deprecated and may produce unexpected"
" behavior. Stricter type checking will be introduced in"
" `supervision-0.28.0`. Please use `mask = np.array(..., dtype=bool)` when"
" creating the mask manually. If you did not create the mask manually,"
" please report the issue to the `supervision` team."
)


Expand Down
21 changes: 14 additions & 7 deletions test/dataset/formats/test_coco.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,8 @@ def test_group_coco_annotations_by_image_id(
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
]
]
],
dtype=bool,
),
),
DoesNotRaise(),
Expand Down Expand Up @@ -259,7 +260,8 @@ def test_group_coco_annotations_by_image_id(
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
]
]
],
dtype=bool,
),
),
DoesNotRaise(),
Expand Down Expand Up @@ -304,7 +306,8 @@ def test_group_coco_annotations_by_image_id(
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
],
]
],
dtype=bool,
),
),
DoesNotRaise(),
Expand Down Expand Up @@ -349,7 +352,8 @@ def test_group_coco_annotations_by_image_id(
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
],
]
],
dtype=bool,
),
),
DoesNotRaise(),
Expand Down Expand Up @@ -461,7 +465,8 @@ def test_build_coco_class_index_mapping(
[1, 1, 1, 1, 0],
[1, 1, 1, 1, 0],
]
]
],
dtype=bool,
),
),
0,
Expand Down Expand Up @@ -490,7 +495,8 @@ def test_build_coco_class_index_mapping(
[0, 0, 0, 1, 1],
[0, 0, 0, 1, 1],
]
]
],
dtype=bool,
),
),
0,
Expand Down Expand Up @@ -522,7 +528,8 @@ def test_build_coco_class_index_mapping(
[1, 1, 0, 0, 1],
[1, 1, 1, 1, 1],
]
]
],
dtype=bool,
),
),
0,
Expand Down
2 changes: 1 addition & 1 deletion test/utils/test_internal.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def __private_property(self):
xyxy=np.array([[1, 2, 3, 4], [5, 6, 7, 8]]),
class_id=np.array([1, 2]),
confidence=np.array([0.1, 0.2]),
mask=np.array([[[1]], [[2]]]),
mask=np.array([[[1]], [[2]]], dtype=bool),
tracker_id=np.array([1, 2]),
data={"key_1": [1, 2], "key_2": [3, 4]},
),
Expand Down