How to save model predictions to JSON or CSV

Save the detections from RF-DETR, YOLO, Transformers, Detectron2 or any other model to a CSV or JSON file with supervision.

Overview

Saving a model's predictions to a file lets you analyze them in a spreadsheet or pandas, compare two runs, or hand them to another system. supervision (opens in a new tab), Roboflow's open source computer vision library, has two sinks that write detections to a file: sv.CSVSink and sv.JSONSink. Both take an sv.Detections object, so they work with any model supervision can read.

This guide covers:

  • Saving RF-DETR predictions to CSV and JSON in Python.
  • Saving predictions from other models: Ultralytics YOLO, YOLOv5, Transformers, Detectron2, MMDetection, YOLO-NAS, PaddleDetection, TensorFlow and DeepSparse.
  • Saving predictions for every frame of a video.
  • Asking a coding agent to do it through the Roboflow MCP server.

Step 1: Install supervision and RF-DETR

RF-DETR (opens in a new tab) is Roboflow's open source real-time model for object detection and segmentation. Its Nano, Small, Medium and Large checkpoints are licensed under Apache 2.0, so you can use them in commercial products. Install both packages:

Shell
pip install rfdetr supervision

Step 2: Save predictions to CSV

This script runs RF-DETR on every image in a folder and writes one row per detected object to predictions.csv. custom_data adds a column with the file name, so you can tell which image each row came from:

save_csv.pyPython
from pathlib import Path

import supervision as sv
from rfdetr import RFDETRSmall

model = RFDETRSmall()

with sv.CSVSink("predictions.csv") as sink:
    for path in sorted(Path("images").glob("*.jpg")):
        detections = model.predict(str(path), threshold=0.5)
        sink.append(detections, custom_data={"image": path.name})

Every row starts with the box corners in pixels (x_min, y_min, x_max, y_max), then class_id, confidence and tracker_id, which stays empty unless you track objects. The extra columns follow in alphabetical order: here class_name, image and source_shape. RF-DETR adds class_name and source_shape (the image height and width), and custom_data adds image. An image with no detections writes no rows.

RFDETRSmall() is pre-trained on the 80 COCO classes. To save predictions from your own model, load the checkpoint you fine-tuned with the same size class, for example RFDETRSmall(pretrain_weights="checkpoint_best_total.pth").

Step 3: Save predictions to JSON

Swap the sink for sv.JSONSink to write the same fields as JSON:

save_json.pyPython
from pathlib import Path

import supervision as sv
from rfdetr import RFDETRSmall

model = RFDETRSmall()

with sv.JSONSink("predictions.json") as sink:
    for path in sorted(Path("images").glob("*.jpg")):
        detections = model.predict(str(path), threshold=0.5)
        sink.append(detections, custom_data={"image": path.name})

The file is one list with an object per detection, keyed like the CSV columns. Coordinates and confidence are numbers, and class_id is an integer. JSONSink keeps the rows in memory and writes the file when the with block ends.

Save predictions from other models

The sinks only need an sv.Detections object. For any other model, convert its output with the matching loader, then call sink.append() the same way:

  • Ultralytics YOLO (YOLOv8, YOLO11, YOLO26): sv.Detections.from_ultralytics()
  • YOLOv5: sv.Detections.from_yolov5()
  • Hugging Face Transformers: sv.Detections.from_transformers()
  • Detectron2: sv.Detections.from_detectron2()
  • MMDetection: sv.Detections.from_mmdetection()
  • YOLO-NAS: sv.Detections.from_yolo_nas()
  • PaddleDetection: sv.Detections.from_paddledet()
  • TensorFlow Hub: sv.Detections.from_tensorflow()
  • DeepSparse: sv.Detections.from_deepsparse()
  • Roboflow Inference and the hosted API: sv.Detections.from_inference()

For example, with an Ultralytics model:

save_yolo.pyPython
import supervision as sv
from ultralytics import YOLO

model = YOLO("yolo11n.pt")

with sv.CSVSink("predictions.csv") as sink:
    result = model("image.jpg")[0]
    detections = sv.Detections.from_ultralytics(result)
    sink.append(detections, custom_data={"image": "image.jpg"})

Save predictions from a video

For a video, read it frame by frame and record the frame number with each row:

save_video.pyPython
import supervision as sv
from rfdetr import RFDETRSmall

model = RFDETRSmall()
frames = sv.get_video_frames_generator("video.mp4")

with sv.CSVSink("video-predictions.csv") as sink:
    for index, frame in enumerate(frames):
        detections = model.predict(frame[:, :, ::-1], threshold=0.5)
        sink.append(detections, custom_data={"frame": index})

Frames come out of OpenCV in BGR order and RF-DETR expects RGB, so frame[:, :, ::-1] flips the channels. How to run a computer vision model on video covers drawing the predictions on the video as well.

Save predictions with a coding agent

The Roboflow MCP server connects a coding agent, such as Claude Code, Cursor or Codex, to your Roboflow workspace. Describe the file you want and the agent writes and runs the script above with your model. For example:

Run RF-DETR Small on every image in ./images and save the detections to predictions.csv, one row per box with the image file name.
Start where you are

Speak with an AI expert

Our team will help you start solving business problems on the first call.

  • Solution architecting
  • Live demonstration
  • Pricing and specifications
  • Feasibility assessment

Over 80,000 organizations build with Roboflow.

Rivian
Pella
Chobani
USG Corporation
BNSF Railway
American Woodmark
Outcomes
Patrick Industries
Peer Robotics