Skip to content

fix: slice numpy array values in custom_data per row in CSVSink and J…#2199

Open
farukalamai wants to merge 1 commit intoroboflow:developfrom
farukalamai:fix/csv-json-sink-custom-data-array-slicing
Open

fix: slice numpy array values in custom_data per row in CSVSink and J…#2199
farukalamai wants to merge 1 commit intoroboflow:developfrom
farukalamai:fix/csv-json-sink-custom-data-array-slicing

Conversation

@farukalamai
Copy link
Copy Markdown

Before submitting
  • Self-reviewed the code
  • Updated documentation, follow Google-style
  • Added docs entry for autogeneration (if new functions/classes)
  • Added/updated tests
  • All tests pass locally

Description

Fixes a bug in CSVSink and JSONSink where passing a numpy array as a
custom_data value wrote the entire array on every row instead of the
per-detection scalar value.

Type of Change

  • 🐛 Bug fix (non-breaking change which fixes an issue)

Motivation and Context

When users pass computed per-detection values like detections.area via
custom_data, each row should receive its own scalar — not the whole array.

# Before (broken): every row got the full array
with sv.CSVSink("out.csv") as sink:
    sink.append(detections, custom_data={"area": detections.area})
# area column: [400.0, 400.0] on every row ❌

# After (fixed): each row gets its own value
# area column: 400.0, 400.0 ✅

The root cause was row.update(custom_data) inside the per-detection loop,
which blindly wrote the whole value. The fix applies the same per-index
slicing logic that detections.data already uses correctly.

Closes #1397

Changes Made

  • src/supervision/detection/tools/csv_sink.py — slice numpy array values in custom_data per detection row
  • src/supervision/detection/tools/json_sink.py — same fix
  • tests/detection/test_csv.py — added test case for numpy array in custom_data

Testing

  • I have tested this code locally
  • I have added unit tests that prove my fix is effective or that my feature works
  • All new and existing tests pass

Google Colab (optional)

Colab link:

Screenshots/Videos (optional)

Additional Notes

The fix is backward compatible — scalar values in custom_data (e.g.
{"frame_number": 42}) continue to work as before, written as-is on every
row.

@farukalamai farukalamai requested a review from SkalskiP as a code owner April 3, 2026 20:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Save detection area with CSVSink

1 participant