From 4e669b6e198261f86aacec5d90839b3083b64d24 Mon Sep 17 00:00:00 2001 From: 0xD4rky Date: Mon, 16 Dec 2024 23:16:59 +0530 Subject: [PATCH 1/6] changing polygon format conversion --- supervision/dataset/formats/yolo.py | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/supervision/dataset/formats/yolo.py b/supervision/dataset/formats/yolo.py index 1f9033fc5..9bd5af8b8 100644 --- a/supervision/dataset/formats/yolo.py +++ b/supervision/dataset/formats/yolo.py @@ -44,16 +44,14 @@ def _parse_polygon(values: List[str]) -> np.ndarray: return np.array(values, dtype=np.float32).reshape(-1, 2) -def _polygons_to_masks( - polygons: List[np.ndarray], resolution_wh: Tuple[int, int] -) -> np.ndarray: - return np.array( - [ - polygon_to_mask(polygon=polygon, resolution_wh=resolution_wh) - for polygon in polygons - ], - dtype=bool, - ) +def _polygons_to_masks(polygon: list[np.ndarray], resolution_wh: Tuple[int, int]) -> np.ndarray: + polygon_int = np.round(polygon).astype(np.int32) + mask = np.zeros((resolution_wh[1], resolution_wh[0]), dtype=np.uint8) + + cv2.fillPoly(mask, [polygon_int], 1) + + return mask.astype(bool) + def _with_mask(lines: List[str]) -> bool: @@ -115,9 +113,9 @@ def yolo_annotations_to_detections( return Detections(class_id=class_id, xyxy=xyxy, data=data) polygons = [ - (polygon * np.array(resolution_wh)).astype(int) for polygon in relative_polygon + (polygon * np.array(resolution_wh)) for polygon in relative_polygon ] - mask = _polygons_to_masks(polygons=polygons, resolution_wh=resolution_wh) + mask = _polygons_to_masks(polygon=polygons, resolution_wh=resolution_wh) return Detections(class_id=class_id, xyxy=xyxy, data=data, mask=mask) From fd4e84e968954f2e2a6ad982475692d621a10397 Mon Sep 17 00:00:00 2001 From: 0xD4rky Date: Mon, 16 Dec 2024 23:18:29 +0530 Subject: [PATCH 2/6] reformating polygon type conversion --- supervision/dataset/formats/yolo.py | 1 + 1 file changed, 1 insertion(+) diff --git a/supervision/dataset/formats/yolo.py b/supervision/dataset/formats/yolo.py index 9bd5af8b8..ba050c7ec 100644 --- a/supervision/dataset/formats/yolo.py +++ b/supervision/dataset/formats/yolo.py @@ -143,6 +143,7 @@ def load_yolo_annotations( where pairs of [x, y] are box corners. Returns: + Tuple[List[str], List[str], Dict[str, Detections]]: A tuple containing a list of class names, a dictionary with image names as keys and images as values, and a dictionary From b593c1ed151a9016caf8910e84661b1c989c8199 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 16 Dec 2024 17:56:49 +0000 Subject: [PATCH 3/6] =?UTF-8?q?fix(pre=5Fcommit):=20=F0=9F=8E=A8=20auto=20?= =?UTF-8?q?format=20pre-commit=20hooks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- supervision/dataset/formats/yolo.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/supervision/dataset/formats/yolo.py b/supervision/dataset/formats/yolo.py index ba050c7ec..185154348 100644 --- a/supervision/dataset/formats/yolo.py +++ b/supervision/dataset/formats/yolo.py @@ -8,7 +8,7 @@ from supervision.config import ORIENTED_BOX_COORDINATES from supervision.dataset.utils import approximate_mask_with_polygons from supervision.detection.core import Detections -from supervision.detection.utils import polygon_to_mask, polygon_to_xyxy +from supervision.detection.utils import polygon_to_xyxy from supervision.utils.file import ( list_files_with_extensions, read_txt_file, @@ -44,16 +44,17 @@ def _parse_polygon(values: List[str]) -> np.ndarray: return np.array(values, dtype=np.float32).reshape(-1, 2) -def _polygons_to_masks(polygon: list[np.ndarray], resolution_wh: Tuple[int, int]) -> np.ndarray: +def _polygons_to_masks( + polygon: list[np.ndarray], resolution_wh: Tuple[int, int] +) -> np.ndarray: polygon_int = np.round(polygon).astype(np.int32) mask = np.zeros((resolution_wh[1], resolution_wh[0]), dtype=np.uint8) - + cv2.fillPoly(mask, [polygon_int], 1) return mask.astype(bool) - def _with_mask(lines: List[str]) -> bool: return any([len(line.split()) > 5 for line in lines]) @@ -112,9 +113,7 @@ def yolo_annotations_to_detections( if not with_masks: return Detections(class_id=class_id, xyxy=xyxy, data=data) - polygons = [ - (polygon * np.array(resolution_wh)) for polygon in relative_polygon - ] + polygons = [(polygon * np.array(resolution_wh)) for polygon in relative_polygon] mask = _polygons_to_masks(polygon=polygons, resolution_wh=resolution_wh) return Detections(class_id=class_id, xyxy=xyxy, data=data, mask=mask) @@ -143,7 +142,7 @@ def load_yolo_annotations( where pairs of [x, y] are box corners. Returns: - + Tuple[List[str], List[str], Dict[str, Detections]]: A tuple containing a list of class names, a dictionary with image names as keys and images as values, and a dictionary From 45e025949522e0b9b471d68f9c793979b0de7493 Mon Sep 17 00:00:00 2001 From: 0xD4rky Date: Tue, 17 Dec 2024 23:33:55 +0530 Subject: [PATCH 4/6] handling mask dimension --- supervision/dataset/formats/yolo.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/supervision/dataset/formats/yolo.py b/supervision/dataset/formats/yolo.py index 185154348..93f8cb4c5 100644 --- a/supervision/dataset/formats/yolo.py +++ b/supervision/dataset/formats/yolo.py @@ -51,7 +51,7 @@ def _polygons_to_masks( mask = np.zeros((resolution_wh[1], resolution_wh[0]), dtype=np.uint8) cv2.fillPoly(mask, [polygon_int], 1) - + mask = mask[None, ...] return mask.astype(bool) From d93e2e8ff3d409769b0c9693c939bd59a501baa5 Mon Sep 17 00:00:00 2001 From: 0xD4rky Date: Tue, 17 Dec 2024 23:38:35 +0530 Subject: [PATCH 5/6] handling mask dimension --- supervision/dataset/formats/yolo.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/supervision/dataset/formats/yolo.py b/supervision/dataset/formats/yolo.py index 93f8cb4c5..aee30cb42 100644 --- a/supervision/dataset/formats/yolo.py +++ b/supervision/dataset/formats/yolo.py @@ -51,7 +51,9 @@ def _polygons_to_masks( mask = np.zeros((resolution_wh[1], resolution_wh[0]), dtype=np.uint8) cv2.fillPoly(mask, [polygon_int], 1) + mask = mask[None, ...] + return mask.astype(bool) From 373b661582d5c1012ac36eaab12d5b7fce402e8c Mon Sep 17 00:00:00 2001 From: 0xD4rky Date: Tue, 17 Dec 2024 23:41:14 +0530 Subject: [PATCH 6/6] handling mask dimensions --- supervision/dataset/formats/yolo.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/supervision/dataset/formats/yolo.py b/supervision/dataset/formats/yolo.py index aee30cb42..93f8cb4c5 100644 --- a/supervision/dataset/formats/yolo.py +++ b/supervision/dataset/formats/yolo.py @@ -51,9 +51,7 @@ def _polygons_to_masks( mask = np.zeros((resolution_wh[1], resolution_wh[0]), dtype=np.uint8) cv2.fillPoly(mask, [polygon_int], 1) - mask = mask[None, ...] - return mask.astype(bool)