Hi! I use zed2 with Jetson AGX Xavier to save svo files, and I want to watch the svo result in my windows laptop. However, when I use ZEDfu or Zed Explorer to open the saved svo file, the program always crash suddenly. What’s more, I can’t use the following code to open the camera:
filepath = 'total.svo'
input_type = sl.InputType()
input_type.set_from_svo_file(filepath)
init = sl.InitParameters(input_t=input_type, svo_real_time_mode=False)
cam = sl.Camera()
status = cam.open(init)
What should I do to read svo file normally?
My code that saves svo file in Jetson is as follow:
import sys
import pyzed.sl as sl
from signal import signal, SIGINT
import numpy as np
import cv2
from pathlib import Path
import enum
import os
import time
import subprocess
def handler(signal_received, frame):
cam.disable_recording()
cam.close()
sys.exit(0)
signal(SIGINT, handler)
def save_imu_data(zed_pose,zed_sensors,txt_path_output):
now = zed_pose.timestamp.get_milliseconds()
with open(txt_path_output,"a") as file:
file.write("Timestamp;{t}\n".format(t = now))
# Display the translation and timestamp
py_translation = sl.Translation()
tx = round(zed_pose.get_translation(py_translation).get()[0], 3)
ty = round(zed_pose.get_translation(py_translation).get()[1], 3)
tz = round(zed_pose.get_translation(py_translation).get()[2], 3)
file.write("Translation;Tx-{0};Ty-{1};Tz-{2};\n".format(tx, ty, tz))
# Display the orientation quaternion
py_orientation = sl.Orientation()
ox = round(zed_pose.get_orientation(py_orientation).get()[0], 3)
oy = round(zed_pose.get_orientation(py_orientation).get()[1], 3)
oz = round(zed_pose.get_orientation(py_orientation).get()[2], 3)
ow = round(zed_pose.get_orientation(py_orientation).get()[3], 3)
file.write("Orientation;Ox-{0};Oy-{1};Oz-{2};Ow-{3};\n".format(ox, oy, oz, ow))
zed_imu = zed_sensors.get_imu_data()
#Display the IMU acceleratoin
acceleration = [0,0,0]
zed_imu.get_linear_acceleration(acceleration)
ax = round(acceleration[0], 3)
ay = round(acceleration[1], 3)
az = round(acceleration[2], 3)
file.write("IMUAcceleration;Ax-{0};Ay-{1};Az-{2};\n".format(ax, ay, az))
#Display the IMU angular velocity
a_velocity = [0,0,0]
zed_imu.get_angular_velocity(a_velocity)
vx = round(a_velocity[0], 3)
vy = round(a_velocity[1], 3)
vz = round(a_velocity[2], 3)
file.write("IMUAngularVelocity;Vx-{0};Vy-{1};Vz-{2};\n".format(vx, vy, vz))
# Display the IMU orientation quaternion
zed_imu_pose = sl.Transform()
ox = round(zed_imu.get_pose(zed_imu_pose).get_orientation().get()[0], 3)
oy = round(zed_imu.get_pose(zed_imu_pose).get_orientation().get()[1], 3)
oz = round(zed_imu.get_pose(zed_imu_pose).get_orientation().get()[2], 3)
ow = round(zed_imu.get_pose(zed_imu_pose).get_orientation().get()[3], 3)
file.write("IMUOrientation;Ox-{0};Oy-{1};Oz-{2};Ow-{3};\n".format(ox, oy, oz, ow))
def record(num,cam,size,zed_pose,zed_sensors,svo_path_output,avi_path_output,h264_path_output,txt_path_output,frames_expected=500):
print("total.svo and {num}.avi is Recording, expected frames are {fc}.".format(num = num,fc = frames_expected))
# video_writer = cv2.VideoWriter(str(avi_path_output),
# cv2.VideoWriter_fourcc('M', '4', 'S', '2'),
# max(cam.get_camera_information().camera_fps, 25),
# size)
video_writer = cv2.VideoWriter(str(h264_path_output),cv2.VideoWriter_fourcc('H', '2', '6', '4'),
max(cam.get_camera_information().camera_fps, 25),size,True)
if not video_writer.isOpened():
sys.stdout.write("OpenCV video writer cannot be opened. Please check the .avi file path and write "
"permissions.\n")
cam.close()
exit()
frames_recorded = 0
left_image = sl.Mat()
svo_image_sbs_rgba = np.zeros((height, width, 4), dtype=np.uint8)
while frames_recorded <= frames_expected -1:
if cam.grab(runtime) == sl.ERROR_CODE.SUCCESS :
cam.get_position(zed_pose, sl.REFERENCE_FRAME.WORLD)
cam.get_sensors_data(zed_sensors, sl.TIME_REFERENCE.IMAGE)
save_imu_data(zed_pose,zed_sensors,txt_path_output)
cam.retrieve_image(left_image, sl.VIEW.LEFT)
svo_image_sbs_rgba[0:height, 0:width, :] = left_image.get_data()
# Convert SVO image from RGBA to RGB
ocv_image_sbs_rgb = cv2.cvtColor(svo_image_sbs_rgba, cv2.COLOR_RGBA2RGB)
# Write the RGB image in the video
video_writer.write(ocv_image_sbs_rgb)
frames_recorded += 1
print("Frame count: " + str(frames_recorded), end="\r")
if __name__ == "__main__":
file_path = sys.argv[1]
sleep_time = sys.argv[2]
try:
os.mkdir(file_path)
except:
pass
print("Sleeping...")
time.sleep(int(sleep_time))
print("Start!")
num=0
svo_path_output = os.path.join(file_path,'total.svo')
txt_path_output = os.path.join(file_path,'imu.txt')
cam = sl.Camera()
init = sl.InitParameters()
init.camera_resolution = sl.RESOLUTION.HD720
init.camera_fps = 15
init.depth_mode = sl.DEPTH_MODE.QUALITY
init.coordinate_system = sl.COORDINATE_SYSTEM.RIGHT_HANDED_Y_UP
init.coordinate_units = sl.UNIT.METER # Set units in meters
status = cam.open(init)
if status != sl.ERROR_CODE.SUCCESS:
print(repr(status))
exit(1)
runtime = sl.RuntimeParameters()
recording_param = sl.RecordingParameters(svo_path_output, sl.SVO_COMPRESSION_MODE.H264,bitrate = 2000)
err = cam.enable_recording(recording_param)
if err != sl.ERROR_CODE.SUCCESS:
print(repr(status))
exit(1)
image_size = cam.get_camera_information().camera_resolution
width = image_size.width
height = image_size.height
size = (width,height)
# Enable positional tracking with default parameters
py_transform = sl.Transform() # First create a Transform object for TrackingParameters object
tracking_parameters = sl.PositionalTrackingParameters(_init_pos=py_transform)
err = cam.enable_positional_tracking(tracking_parameters)
if err != sl.ERROR_CODE.SUCCESS:
exit(1)
zed_pose = sl.Pose()
zed_sensors = sl.SensorsData()
while True:
avi_path_output = os.path.join(file_path,'{num}.avi'.format(num = num))
h264_path_output = os.path.join(file_path,'{num}.h264'.format(num = num))
record(num,cam,size,zed_pose,zed_sensors,svo_path_output,avi_path_output,h264_path_output,txt_path_output,frames_expected=50000)
num = num+1
if num == 501:
num = 0