Repository: roboflow/supervision
Issue category: Bug Report

Issue:
Crash when filtering empty detections: xyxy shape (0, 0, 4).
Reproduction code:

```python
import supervision as sv
import numpy as np

CLASSES = [0, 1, 2]

prediction = sv.Detections.empty()
prediction = prediction[np.isin(prediction["class_name"], CLASSES)]
```

Error:

```
Traceback (most recent call last):
  File "/Users/linasko/.settler_workspace/pr/supervision-fresh/run_detections.py", line 7, in <module>
    prediction = prediction[np.isin(prediction["class_name"], CLASSES)]
  File "/Users/linasko/.settler_workspace/pr/supervision-fresh/supervision/detection/core.py", line 1206, in __getitem__
    return Detections(
  File "<string>", line 10, in __init__
  File "/Users/linasko/.settler_workspace/pr/supervision-fresh/supervision/detection/core.py", line 144, in __post_init__
    validate_detections_fields(
  File "/Users/linasko/.settler_workspace/pr/supervision-fresh/supervision/validators/__init__.py", line 120, in validate_detections_fields
    validate_xyxy(xyxy)
  File "/Users/linasko/.settler_workspace/pr/supervision-fresh/supervision/validators/__init__.py", line 11, in validate_xyxy
    raise ValueError(
ValueError: xyxy must be a 2D np.ndarray with shape (_, 4), but got shape (0, 0, 4)
```



Hints:

Patch:
diff --git a/supervision/detection/core.py b/supervision/detection/core.py
index 74c663a89..14f4c0ef8 100644
--- a/supervision/detection/core.py
+++ b/supervision/detection/core.py
@@ -1201,6 +1201,8 @@ def __getitem__(
         """
         if isinstance(index, str):
             return self.data.get(index)
+        if self.is_empty():
+            return Detections.empty()
         if isinstance(index, int):
             index = [index]
         return Detections(


Test patch:
diff --git a/test/detection/test_core.py b/test/detection/test_core.py
index 61796bef2..dfa784fcb 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"], ["cat", "dog"]),
+            Detections.empty(),
+            DoesNotRaise(),
+        ),  # Filter an empty detections by specific class names
     ],
 )
 def test_getitem(