Skip to content

Recording ZED Footage

Alexander Crain edited this page Jan 26, 2024 · 2 revisions

There are many ways to record footage from the ZED cameras. This Wiki entry will detail one method in particular. The method described in this Wiki can slow down the TX2 if other algorithms are running in the same While loop - as such, implementing this method and recording during experiments is generally not recommended, unless you have enough processing overhead. Simply put - be aware that this method may degrade performance slightly. This Wiki entry will also assume users are familiar with the general use of a ZED camera.

The Python code to save images is as follows:

# Import any relevant libraries
import cv2

# Set up a save directory where you can save the images
save_dir = os.path.join(os.path.expanduser('~/sdcard'),'Combined_Experiment_Saved_ZED_Images')

# Check if the directory exists, and if not create the directory
if not os.path.exists(save_dir):
    os.makedirs(save_dir)

# Initialize the ZED camera (can be replaced with any initialization)
zedCamera = recordStereo.StereoCamera(1, 'MJPG', 30.0, 2560, 720)
zedCamera.initImageRecord()

# Set up a frame counter
frame_counter = 0

# Start a loop
while True:

    # Grab the image and the corresponding timestamp
    image, timestamp = zedCamera.getImageAndTimeStamp()

    # Check that the image is valid
    if len(image.shape) == 2:
        continue

    # Set up the filename for the frame, which is based on the frame counter so as to not overwrite previous filenames
    frame_filename = f"{save_dir}/frame_{frame_counter:06d}.png"

    # Save the image to the filename and increment the counter
    cv2.imwrite(frame_filename, image)
    frame_counter += 1

To move the saved files from the TX2 to the ground station PC, the easiest method is to use FileZilla. This Secure File Transfer Protocol (SFTP) tool is already installed on the ground station PC. Assuming the TX2 is turned ON, simply open FileZilla, and then using the QuickConnect dropdown menu, select the first option (which should be 192.168.1.114 - however, there are two options in the dropdown. You may have to try the other option to connect):

image

Once connected, users should see the file directory of the Remote Site, which in this case is the TX2. Users can then navigate to the directory where the images were saved, and then drag and drop the entire folder into the desired Local Site directory.

To turn these individual images into a video, there are a few different possibilities. For this example, a Python script will be used:

import cv2
import os
import glob
from google.colab import drive

def create_video_from_images(output_file='output.avi', fps=2):
    # Get all the PNG files in the folder sorted by filename
    images = sorted(glob.glob('/content/drive/MyDrive/Combined_Experiment_Saved_ZED_Images/frame_*.png'))

    # Read the first image to get the size
    frame = cv2.imread(images[0])
    height, width, layers = frame.shape
    size = (width, height)

    # Initialize video writer
    out = cv2.VideoWriter(output_file, cv2.VideoWriter_fourcc(*'DIVX'), fps, size)

    # Loop through all images and add them to the video
    for image in images:
        frame = cv2.imread(image)
        out.write(frame)

    # Release the video writer
    out.release()
    print(f"Video saved as {output_file}")

# Example usage
create_video_from_images()

This code stacks all of the frames in the correct order, then saves the video to output.avi. Note that the framerate parameter will need to be updated to match the recording rate. In this example, each step of the while loop took approximately 0.5 seconds - thus, the framerate of the video is 2 frames per second.

Clone this wiki locally