Save Computer Vision Model Predictions

How to Save TensorFlow Predictions to JSON

When you are working with computer vision models, you may want to save your detections to CSV or JSON for further processing. This is especially useful in testing and debugging scripts, or applications where you want to log all results from your model to a plain text file. Using the supervision Python package, you can

Save TensorFlow Predictions to JSON

predictions in a few lines of code. In this guide, we will show how to plot and visualize model predictions.

We will:

1. Install supervision
2. Load a model and save predictions with the supervision Sink API

Without further ado, let's get started!

Step #1: Install supervision

First, install the supervision pip package:

pip install supervision


Once you have installed supervision, you are ready to load your data and start writing logic to save detections.

Step #2: Save Data

First, we need to load our computer vision model. Then, we can load detections into the supervision Detections() API. With our detections in the Detections() API, we can use the

JSON

sink to save our data.

You can save data using the following code:


import supervision as sv
import tensorflow as tf
import tensorflow_hub as hub
import numpy as np
import cv2

module_handle = "https://tfhub.dev/tensorflow/centernet/hourglass_512x512_kpts/1"
model = hub.load(module_handle)

csv_sink = sv.JSONSink("RESULT_JSON_FILE_PATH")
frames_generator = sv.get_video_frames_generator("SOURCE_VIDEO_PATH")

with csv_sink as sink:
    for frame in frames_generator:
        result = model(frame)
        detections = sv.Detections.from_tensorflow(result)
        sink.append(detections, custom_data={'CUSTOM_LABEL':'CUSTOM_DATA'})

Next steps

supervision provides an extensive range of functionalities for working with computer vision models. With supervision, you can:

1. Process and filter detections and segmentation masks from a range of popular models (YOLOv5, Ultralytics YOLOv8, MMDetection, and more).
2. Process and filter classifications.
3. Compute confusion matrices.

And more! To learn about the full range of functionality in supervision, check out the supervision documentation.

Learn how to plot detections for other models

Below, you can find our guides on how to plot detections for other computer vision models.