diff --git a/src/supervision/detection/tools/csv_sink.py b/src/supervision/detection/tools/csv_sink.py index d06d8966..d7932b80 100644 --- a/src/supervision/detection/tools/csv_sink.py +++ b/src/supervision/detection/tools/csv_sink.py @@ -112,6 +112,12 @@ class CSVSink: if self.file: self.file.close() + @staticmethod + def _slice_value(value: Any, i: int) -> Any: + if isinstance(value, np.ndarray): + return value if value.ndim == 0 else value[i] + return value + @staticmethod def parse_detection_data( detections: Detections, custom_data: dict[str, Any] | None = None @@ -136,15 +142,12 @@ class CSVSink: if hasattr(detections, "data"): for key, value in detections.data.items(): - if isinstance(value, np.ndarray) and value.ndim == 0: - row[key] = value - elif isinstance(value, np.ndarray): - row[key] = value[i] - else: - row[key] = value[i] if hasattr(value, "__getitem__") else value + row[key] = CSVSink._slice_value(value, i) if custom_data: - row.update(custom_data) + for key, value in custom_data.items(): + row[key] = CSVSink._slice_value(value, i) + parsed_rows.append(row) return parsed_rows diff --git a/src/supervision/detection/tools/json_sink.py b/src/supervision/detection/tools/json_sink.py index 5a34e16e..32b0c36c 100644 --- a/src/supervision/detection/tools/json_sink.py +++ b/src/supervision/detection/tools/json_sink.py @@ -84,6 +84,12 @@ class JSONSink: json.dump(self.data, self.file, indent=4) self.file.close() + @staticmethod + def _slice_value(value: Any, i: int) -> Any: + if isinstance(value, np.ndarray): + return value if value.ndim == 0 else value[i] + return value + @staticmethod def parse_detection_data( detections: Detections, custom_data: dict[str, Any] | None = None @@ -108,17 +114,13 @@ class JSONSink: if hasattr(detections, "data"): for key, value in detections.data.items(): - if isinstance(value, np.ndarray): - row[key] = str(value[i]) if value.ndim != 0 else str(value) - else: - row[key] = ( - str(value[i]) - if hasattr(value, "__getitem__") - else str(value) - ) + row[key] = str(JSONSink._slice_value(value, i)) if custom_data: - row.update(custom_data) + for key, value in custom_data.items(): + v = JSONSink._slice_value(value, i) + row[key] = str(v) if isinstance(value, np.ndarray) else v + parsed_rows.append(row) return parsed_rows diff --git a/tests/detection/test_csv.py b/tests/detection/test_csv.py index 12d52058..4f8953e5 100644 --- a/tests/detection/test_csv.py +++ b/tests/detection/test_csv.py @@ -2,6 +2,7 @@ import csv import os from typing import Any +import numpy as np import pytest import supervision as sv @@ -193,6 +194,37 @@ from tests.helpers import _create_detections ], ], ), # Complex Data + ( + _create_detections( + xyxy=[[10, 20, 30, 40], [50, 60, 70, 80]], + confidence=[0.9, 0.8], + class_id=[0, 1], + ), + {"area": np.array([400.0, 400.0]), "frame": 5}, + _create_detections( + xyxy=[[15, 25, 35, 45]], + confidence=[0.7], + class_id=[2], + ), + {"area": np.array([100.0]), "frame": 6}, + "test_detections_mixed_custom_data.csv", + [ + [ + "x_min", + "y_min", + "x_max", + "y_max", + "class_id", + "confidence", + "tracker_id", + "area", + "frame", + ], + ["10.0", "20.0", "30.0", "40.0", "0", "0.9", "", "400.0", "5"], + ["50.0", "60.0", "70.0", "80.0", "1", "0.8", "", "400.0", "5"], + ["15.0", "25.0", "35.0", "45.0", "2", "0.7", "", "100.0", "6"], + ], + ), # mixed custom_data: ndarray sliced per row, scalar broadcast to all rows ], ) def test_csv_sink( diff --git a/tests/detection/test_json.py b/tests/detection/test_json.py index 0d1218aa..06988cd5 100644 --- a/tests/detection/test_json.py +++ b/tests/detection/test_json.py @@ -2,6 +2,7 @@ import json import os from typing import Any +import numpy as np import pytest import supervision as sv @@ -227,6 +228,53 @@ from tests.helpers import _create_detections }, ], ), # Complex Data + ( + _create_detections( + xyxy=[[10, 20, 30, 40], [50, 60, 70, 80]], + confidence=[0.9, 0.8], + class_id=[0, 1], + ), + {"area": np.array([400.0, 400.0])}, + _create_detections( + xyxy=[[15, 25, 35, 45]], + confidence=[0.7], + class_id=[2], + ), + {"area": np.array([400.0])}, + "test_detections_array_custom_data.json", + [ + { + "x_min": 10, + "y_min": 20, + "x_max": 30, + "y_max": 40, + "class_id": 0, + "confidence": 0.8999999761581421, + "tracker_id": "", + "area": "400.0", + }, + { + "x_min": 50, + "y_min": 60, + "x_max": 70, + "y_max": 80, + "class_id": 1, + "confidence": 0.800000011920929, + "tracker_id": "", + "area": "400.0", + }, + { + "x_min": 15, + "y_min": 25, + "x_max": 35, + "y_max": 45, + "class_id": 2, + "confidence": 0.699999988079071, + "tracker_id": "", + "area": "400.0", + }, + ], + ), # numpy array in custom_data sliced per detection row ], ) def test_json_sink(