From ab775d9f76c7487ffbf74b5dc978479a218b7341 Mon Sep 17 00:00:00 2001 From: Linas Kondrackis Date: Fri, 17 May 2024 11:45:00 +0300 Subject: [PATCH] Attempt to fix SegFault * https://github.com/opencv/opencv-python/issues/706 --- supervision/dataset/formats/coco.py | 3 ++- test/dataset/formats/test_coco.py | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/supervision/dataset/formats/coco.py b/supervision/dataset/formats/coco.py index 0e80afb5e..4f6c169e6 100644 --- a/supervision/dataset/formats/coco.py +++ b/supervision/dataset/formats/coco.py @@ -121,7 +121,8 @@ def _mask_has_multiple_segments(mask: np.ndarray) -> bool: if mask.size == 0: return False mask_uint8 = mask.astype(np.uint8) - number_of_labels, _ = cv2.connectedComponents(mask_uint8, connectivity=4) + labels = np.zeros_like(mask_uint8, dtype=np.int32) + number_of_labels, _ = cv2.connectedComponents(mask_uint8, labels, connectivity=4) return number_of_labels > 2 diff --git a/test/dataset/formats/test_coco.py b/test/dataset/formats/test_coco.py index 139c74f80..af6b31bc6 100644 --- a/test/dataset/formats/test_coco.py +++ b/test/dataset/formats/test_coco.py @@ -500,7 +500,7 @@ def test_build_coco_class_index_mapping( # area=5 * 5, # segmentation=[[0, 0, 2, 0, 2, 2, 4, 2, 4, 4, 0, 4]])], # DoesNotRaise(), - # ), # segmentation mask in single component,no holes in mask, expects polygon mask + # ), # seg mask in single component,no holes in mask, expects polygon ( Detections( xyxy=np.array([[0, 0, 5, 5]], dtype=np.float32), @@ -564,7 +564,7 @@ def test_build_coco_class_index_mapping( ) ], DoesNotRaise(), - ), # segmentation mask in single component, with holes in mask, expects RLE mask + ), # seg mask in single component, with holes in mask, expects RLE mask ], ) def test_detections_to_coco_annotations(