From 28fa054fdcd84587d2c37ad8f893f98a489731b7 Mon Sep 17 00:00:00 2001 From: Soham Date: Sat, 30 Nov 2024 13:56:18 +0530 Subject: [PATCH 1/4] fix: is_empty check for filtering --- supervision/detection/core.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/supervision/detection/core.py b/supervision/detection/core.py index 74c663a89..e4320a165 100644 --- a/supervision/detection/core.py +++ b/supervision/detection/core.py @@ -1203,6 +1203,8 @@ def __getitem__( return self.data.get(index) if isinstance(index, int): index = [index] + if self.is_empty(): + return Detections.empty() return Detections( xyxy=self.xyxy[index], mask=self.mask[index] if self.mask is not None else None, From a4dfcacbfd87f0909d63df4f938cd69e2db0eaff Mon Sep 17 00:00:00 2001 From: LinasKo Date: Tue, 3 Dec 2024 13:12:14 +0200 Subject: [PATCH 2/4] detections __getitem__: earlier check for empty detections --- supervision/detection/core.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/supervision/detection/core.py b/supervision/detection/core.py index e4320a165..14f4c0ef8 100644 --- a/supervision/detection/core.py +++ b/supervision/detection/core.py @@ -1201,10 +1201,10 @@ def __getitem__( """ if isinstance(index, str): return self.data.get(index) - if isinstance(index, int): - index = [index] if self.is_empty(): return Detections.empty() + if isinstance(index, int): + index = [index] return Detections( xyxy=self.xyxy[index], mask=self.mask[index] if self.mask is not None else None, From 37eadfe3059cb8d37b0ad25fb91462ef3e7469ef Mon Sep 17 00:00:00 2001 From: Soham Date: Tue, 3 Dec 2024 19:24:41 +0530 Subject: [PATCH 3/4] test: empty detection check in getitem --- test/detection/test_core.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/detection/test_core.py b/test/detection/test_core.py index 61796bef2..402d9ef1c 100644 --- a/test/detection/test_core.py +++ b/test/detection/test_core.py @@ -223,6 +223,12 @@ None, pytest.raises(IndexError), ), + ( + Detections.empty(), + np.isin(Detections.empty()["class_name"], [0, 1, 2]), + Detections.empty(), + DoesNotRaise(), + ), ], ) def test_getitem( From 7141f11c0b2a738fb24f0c99d044df55c34df63a Mon Sep 17 00:00:00 2001 From: LinasKo Date: Wed, 4 Dec 2024 12:13:33 +0200 Subject: [PATCH 4/4] detections __getitem__ test: minor class name correction --- test/detection/test_core.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/detection/test_core.py b/test/detection/test_core.py index 402d9ef1c..dfa784fcb 100644 --- a/test/detection/test_core.py +++ b/test/detection/test_core.py @@ -225,10 +225,10 @@ ), ( Detections.empty(), - np.isin(Detections.empty()["class_name"], [0, 1, 2]), + np.isin(Detections.empty()["class_name"], ["cat", "dog"]), Detections.empty(), DoesNotRaise(), - ), + ), # Filter an empty detections by specific class names ], ) def test_getitem(