diff --git a/supervision/dataset/formats/coco.py b/supervision/dataset/formats/coco.py index 0489cd08..4beb1dcd 100644 --- a/supervision/dataset/formats/coco.py +++ b/supervision/dataset/formats/coco.py @@ -123,7 +123,7 @@ def detections_to_coco_annotations( )[0].flatten() ) # todo: flag for when to use RLE? - # segmentation = {"counts": mask_to_rle(binary_mask=mask), + # segmentation = {"counts": mask_to_rle(binary_mask=mask), # "size": list(mask.shape[:2])} coco_annotation = { "id": annotation_id, diff --git a/supervision/dataset/utils.py b/supervision/dataset/utils.py index 6789d17a..4fbd1230 100644 --- a/supervision/dataset/utils.py +++ b/supervision/dataset/utils.py @@ -140,17 +140,17 @@ def rle_to_mask( Converts run-length encoding (RLE) to a binary mask. Args: - rle (npt.NDArray[np.int_]): The 1D RLE array, the format used in the COCO - dataset (column-wise encoding, values of an array with even indices + rle (npt.NDArray[np.int_]): The 1D RLE array, the format used in the COCO + dataset (column-wise encoding, values of an array with even indices represent the number of pixels assigned as background, - values of an array with odd indices represent the number of pixels + values of an array with odd indices represent the number of pixels assigned as foreground object). - resolution_wh (Tuple[int, int]): The width (w) and height (h) + resolution_wh (Tuple[int, int]): The width (w) and height (h) of the desired binary mask resolution. Returns: - npt.NDArray[np.bool_]: The generated 2D Boolean mask of shape (h,w), - where the foreground object is marked with `True`'s and the rest + npt.NDArray[np.bool_]: The generated 2D Boolean mask of shape (h,w), + where the foreground object is marked with `True`'s and the rest is filled with `False`'s. Raises: @@ -163,10 +163,10 @@ def rle_to_mask( """ width, height = resolution_wh - assert ( - width * height == np.sum(rle) - ), ("the sum of the number of pixels in the RLE must be the same " - "as the number of pixels in the expected mask") + assert width * height == np.sum(rle), ( + "the sum of the number of pixels in the RLE must be the same " + "as the number of pixels in the expected mask" + ) zero_one_values = np.zeros_like(rle) zero_one_values[1::2] = 1 @@ -180,13 +180,13 @@ def mask_to_rle(mask: npt.NDArray[np.bool_]) -> List[int]: Converts a binary mask into a run-length encoding (RLE). Args: - mask (npt.NDArray[np.bool_]): 2D binary mask where `True` indicates foreground + mask (npt.NDArray[np.bool_]): 2D binary mask where `True` indicates foreground object and `False` indicates background. Returns: - List[int]: the run-length encoded mask. Values of a list with even indices - represent the number of pixels assigned as background (`False`), values - of a list with odd indices represent the number of pixels assigned + List[int]: the run-length encoded mask. Values of a list with even indices + represent the number of pixels assigned as background (`False`), values + of a list with odd indices represent the number of pixels assigned as foreground object (`True`). Raises: diff --git a/test/dataset/formats/test_coco.py b/test/dataset/formats/test_coco.py index 12ff4f00..f47e796d 100644 --- a/test/dataset/formats/test_coco.py +++ b/test/dataset/formats/test_coco.py @@ -252,7 +252,7 @@ def test_group_coco_annotations_by_image_id( ).reshape((1, 20, 20)), ), DoesNotRaise(), - ), # single image annotations with mask, segmentation mask in L-like shape, + ), # single image annotations with mask, segmentation mask in L-like shape, # like below: # 1 0 0 0 # 1 1 0 0 @@ -307,7 +307,7 @@ def test_group_coco_annotations_by_image_id( ).reshape((1, 20, 20)), ), DoesNotRaise(), - ), # single image annotations with mask, RLE segmentation mask in L-like shape, + ), # single image annotations with mask, RLE segmentation mask in L-like shape, # like below: # 1 0 0 0 # 1 1 0 0 @@ -357,7 +357,7 @@ def test_group_coco_annotations_by_image_id( ), ), DoesNotRaise(), - ), # two image annotations with mask, one mask as polygon in in L-like shape, + ), # two image annotations with mask, one mask as polygon in in L-like shape, # second as RLE in shape of square, like below (P = polygon, R = RLE): # P R 0 0 # P P 0 0 @@ -407,7 +407,7 @@ def test_group_coco_annotations_by_image_id( ), ), DoesNotRaise(), - ), # two image annotations with mask, first mask as RLE in shape of square, + ), # two image annotations with mask, first mask as RLE in shape of square, # second as polygon in in L-like shape, like below (P = polygon, R = RLE): # P R 0 0 # P P 0 0