Recording ZED2 Camera Footage - Carleton-SRL/SPOT GitHub Wiki

There are really two ways to accomplish this -- one is to simply use the terminal, and the other is to use Python and the associated ZED2 API.

Using the Jetson Terminal

The terminal approach is far simpler but limited in terms of what you configure. To record a video through the terminal, ensure that ffmpeg is installed using the following terminal command:

sudo apt install ffmpeg

Also ensure that this package is installed:

sudo apt install v4l2-ctl

Then, in the terminal window, all you need to do is:

ffmpeg -f v4l2 -video_size 1280x720 -framerate 15 -i /dev/video0 output.mp4

Note that you need to ensure that the ZED2 camera is /dev/video0 -- you can check this using the command v4l2-ctl --list-devices, per the screenshot below. In this screenshot, the ZED2 is in fact assigned to video8, so to save the output your command would be /dev/video8.

image

You can set the file name by changing output to anything you desire.

Sample ZED Python Script

With the camera SDK installed, we can run a quick Python script to confirm it is working as expected. Before we can begin, you need to set the following on the Jetson. In the terminal, type:

export LC_ALL="C"

It is now up to you how you want to edit and run your Python code. My recommendation is a remove Visual Studio Code connection from the Ground Station computer. But, you can also edit the code using the terminal and nano . For testing purposes, we will go through the terminal. From the home directory, navigate to cd Documents, and create a new directory using mkdir ZED_Example. Navigate to this directory via cd ZED_Example . Inside this new directory, we will create a virtual environment so as to not cause problems with other code on the Orin. This is done using the following command:

python3.8 -m venv .venv

Note the use of python3.8 ; consider this a requirement, as it will ensure the default examples work. If you want a newer version of Python, you are on your own! Enter the new environment using:

source ./.venv/bin/activate

We need to install some libraries for most ZED applications. These are:

pip3 install opencv-python
pip3 install matplotlib

We also need to install the ZED API in this environment using the command:

python3 /usr/local/zed/get_python_api.py 

Then create a new Python script by typing nano Sample_ZED.py . Inside this file you will place the following code:

import os
import cv2
import pyzed.sl as sl

def main():

    # Create a Camera object
    zed = sl.Camera()

    # Create a InitParameters object and set configuration parameters
    init_params = sl.InitParameters()
    init_params.camera_resolution = sl.RESOLUTION.AUTO # Use HD720 opr HD1200 video mode, depending on camera type.
    init_params.camera_fps = 30  # Set fps at 30

    # Open the camera
    err = zed.open(init_params)
    if err != sl.ERROR_CODE.SUCCESS:
        print("Camera Open : "+repr(err)+". Exit program.")
        exit()

    runtime_parameters = sl.RuntimeParameters()

    # Prepare Mat objects to store images
    image_left = sl.Mat()
    image_right = sl.Mat()

    # Create output directory
    output_dir = "zed_frames"
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

    print("Recording... Press Ctrl-C to stop.")
    frame_number = 0

    try:
        while True:
            # Grab a new frame
            if zed.grab(runtime_parameters) == sl.ERROR_CODE.SUCCESS:
                
                # Retrieve the left and right images
                zed.retrieve_image(image_left, sl.VIEW.LEFT)
                zed.retrieve_image(image_right, sl.VIEW.RIGHT)
                
                # Extract data
                left_frame = image_left.get_data()
                right_frame = image_right.get_data()
                
                # Construct file names
                left_filename = os.path.join(output_dir, f"left_{frame_number:04d}.png")
                right_filename = os.path.join(output_dir, f"right_{frame_number:04d}.png")
                
                # Save the images
                image_left.write(left_filename)
                image_right.write(right_filename)
                
                frame_number += 1
    except KeyboardInterrupt:
        print("\nRecording stopped by user.")
    finally:
        # Clean up and close the camera
        zed.close()

if __name__ == "__main__":
    main()

Hit Ctrl+o to save the file and then Ctrl+x to exit the nano session. To run the script, in the terminal type the following command:

python3 Sampled_ZED.py

If successful you should see the following, as well as a printed message stating Recording... Press Ctrl-C to stop. , which indicates it is working.

spot-vision@ubuntu:~/Documents/ZED_Example$ python3 Sample_ZED.py
[2025-02-23 16:23:30 UTC][ZED][INFO] Logging level INFO
[2025-02-23 16:23:30 UTC][ZED][INFO] Logging level INFO
[2025-02-23 16:23:30 UTC][ZED][INFO] Logging level INFO
[2025-02-23 16:23:31 UTC][ZED][INFO] Using USB input... Switched to default resolution HD720
[2025-02-23 16:23:31 UTC][ZED][INFO] [Init]  Depth mode: PERFORMANCE
[2025-02-23 16:23:31 UTC][ZED][INFO] [Init]  Camera successfully opened.
[2025-02-23 16:23:31 UTC][ZED][INFO] [Init]  Camera FW version: 1523
[2025-02-23 16:23:31 UTC][ZED][INFO] [Init]  Video mode: HD720@30
[2025-02-23 16:23:31 UTC][ZED][INFO] [Init]  Serial Number: S/N 18234

This code will also save images to a folder called zed_frames for further processing if required.