Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/supervision/detection/tools/csv_sink.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,13 @@ def parse_detection_data(
row[key] = value[i] if hasattr(value, "__getitem__") else value

if custom_data:
row.update(custom_data)
for key, value in custom_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
parsed_rows.append(row)
return parsed_rows

Expand Down
8 changes: 7 additions & 1 deletion src/supervision/detection/tools/json_sink.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,13 @@ def parse_detection_data(
)

if custom_data:
row.update(custom_data)
for key, value in custom_data.items():
if isinstance(value, np.ndarray) and value.ndim == 0:
row[key] = str(value)
elif isinstance(value, np.ndarray):
row[key] = str(value[i])
else:
row[key] = value
parsed_rows.append(row)
return parsed_rows

Expand Down
31 changes: 31 additions & 0 deletions tests/detection/test_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
from typing import Any

import numpy as np
import pytest

import supervision as sv
Expand Down Expand Up @@ -193,6 +194,36 @@
],
],
), # 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.csv",
[
[
"x_min",
"y_min",
"x_max",
"y_max",
"class_id",
"confidence",
"tracker_id",
"area",
],
["10.0", "20.0", "30.0", "40.0", "0", "0.9", "", "400.0"],
["50.0", "60.0", "70.0", "80.0", "1", "0.8", "", "400.0"],
["15.0", "25.0", "35.0", "45.0", "2", "0.7", "", "400.0"],
],
), # numpy array in custom_data sliced per detection row
],
)
def test_csv_sink(
Expand Down