From 4a068ff8d7cf71f1e32e8e168e10f5539643a20d Mon Sep 17 00:00:00 2001 From: magda skoczen Date: Fri, 3 May 2024 09:16:13 +0200 Subject: [PATCH] binary mask to RL encoding --- supervision/detection/utils.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/supervision/detection/utils.py b/supervision/detection/utils.py index 2b6f7d63..a915972a 100644 --- a/supervision/detection/utils.py +++ b/supervision/detection/utils.py @@ -1,4 +1,4 @@ -from itertools import chain +from itertools import chain, groupby from typing import Dict, List, Optional, Tuple, Union import cv2 @@ -776,3 +776,12 @@ def rle_to_mask(rle: np.ndarray, resolution_wh: Tuple[int, int]) -> np.ndarray: decoded_rle = np.repeat(zero_one_values, rle) return decoded_rle.reshape((height,width), order='F') + +def mask_to_rle(binary_mask: np.ndarray) -> list: + rle = [] + for _, group in groupby(binary_mask.ravel(order='F')): + rle.append(len(list(group))) + + if binary_mask[0][0] == 1: + rle = [0]+rle + return rle \ No newline at end of file