Skip to content

Commit

Permalink
Bitmask input array dimension check (#454)
Browse files Browse the repository at this point in the history
* Add dimensions check for bitmask encoder
* Update error message for mismatching media and bitmask dimensions
  • Loading branch information
sergei-encord authored Nov 7, 2023
1 parent 93ce36d commit 032df93
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
43 changes: 22 additions & 21 deletions encord/objects/bitmask.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,28 +136,29 @@ def from_dict(d: Dict[str, Any]) -> BitmaskCoordinates:

@staticmethod
def _from_array(source: Any) -> BitmaskCoordinates.EncodedBitmask:
if source is not None:
if hasattr(source, "__array_interface__"):
arr = source.__array_interface__
data_type = arr["typestr"]
data = arr["data"]
shape = arr["shape"]
if source is None:
raise EncordException("Bitmask can't be created from None")

if data_type != "|b1":
raise EncordException(
"Bitmask should be an array of boolean values. For numpy array call .astype(bool)."
)
if not hasattr(source, "__array_interface__"):
raise EncordException(f"Can't import bitmask from {source.__class__}")

raw_data = data if isinstance(data, bytes) else source.tobytes()
arr = source.__array_interface__
data_type = arr["typestr"]
data = arr["data"]
shape = arr["shape"]

rle = _mask_to_rle(raw_data)
rle_string = _rle_to_string(rle)
if len(shape) != 2:
raise EncordException("Bitmask should be a 2-dimensional array.")

return BitmaskCoordinates.EncodedBitmask(
top=0, left=0, height=shape[0], width=shape[1], rle_string=rle_string
)
if data_type != "|b1":
raise EncordException("Bitmask should be an array of boolean values. For numpy array call .astype(bool).")

raise EncordException(f"Can't import bitmask from {source.__class__}")
raw_data = data if isinstance(data, bytes) else source.tobytes()

rle = _mask_to_rle(raw_data)
rle_string = _rle_to_string(rle)

return BitmaskCoordinates.EncodedBitmask(top=0, left=0, height=shape[0], width=shape[1], rle_string=rle_string)

def to_dict(self) -> Dict[str, Any]:
return {
Expand All @@ -170,14 +171,14 @@ def to_dict(self) -> Dict[str, Any]:

def to_numpy_array(self):
"""
Converts the mask to numpy array with dtype bool.
Converts the mask to a 2D numpy array with dtype bool.
Numpy needs to be installed for this call to work
Numpy needs to be installed for this call to work.
"""
try:
import numpy as np # type: ignore[missing-import]
except ImportError:
raise EncordException("Numpy is required for .to_numpy call.")
except ImportError as e:
raise EncordException("Numpy is required for .to_numpy_array call.") from e

return np.array(self)

Expand Down
2 changes: 1 addition & 1 deletion encord/objects/ontology_labels_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -1149,7 +1149,7 @@ def _add_coordinates_to_encord_object(
frame_view.height == coordinates._encoded_bitmask.height
and frame_view.width == coordinates._encoded_bitmask.width
):
raise ValueError("Bitmask resolution doesn't match the media resolution")
raise ValueError("Bitmask dimensions don't match the media dimensions")
encord_object["bitmask"] = coordinates.to_dict()
else:
raise NotImplementedError(f"adding coordinatees for this type not yet implemented {type(coordinates)}")
Expand Down

0 comments on commit 032df93

Please sign in to comment.