fix: slice numpy array values in custom_data per row in CSVSink (#2199)

* fix: slice numpy array values in custom_data per row in CSVSink and JSONSink
* fix: restore non-ndarray custom_data passthrough in CSVSink
* test: add JSONSink test for numpy array custom_data slicing
* test: add mixed-type custom_data test (ndarray + scalar together)
* refactor: extract _slice_value helper; drop redundant ndarray test

---------

Co-authored-by: Jirka Borovec <6035284+Borda@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Claude Code <noreply@anthropic.com>
This commit is contained in:
Md Faruk Alam
2026-04-14 00:51:52 +02:00
committed by GitHub
co-authored by Jirka Borovec Copilot Claude Code
parent 72fc49f124
commit e514142b3d
4 changed files with 101 additions and 16 deletions
+10 -7
View File
@@ -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
+11 -9
View File
@@ -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
+32
View File
@@ -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(
+48
View File
@@ -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(