zed_svo只导出左视图的视频
zed官方提供的代码无法只导出左视图的视频,以下代码更改路径后可直接食用
import sys
import pyzed.sl as sl
import numpy as np
import cv2
from pathlib import Path
import traceback
def progress_bar(percent_done, bar_length=50):
done_length = int(bar_length * percent_done / 100)
bar = '=' * done_length + '-' * (bar_length - done_length)
sys.stdout.write('[%s] %f%s\r' % (bar, percent_done, '%'))
sys.stdout.flush()
try:
# Get input parameters
svo_input_path = Path(r'K:\11-15-11.svo') #svo文件路径
output_path = Path(r'K:\1.avi')#输出的avi文件路径
output_as_video = True
# Specify SVO path parameter
init_params = sl.InitParameters()
init_params.set_from_svo_file(str(svo_input_path))
init_params.svo_real_time_mode = False # Don't convert in realtime
init_params.coordinate_units = sl.UNIT.MILLIMETER # Use milliliter units (for depth measurements)
# Create ZED objects
zed = sl.Camera()
# Open the SVO file specified as a parameter
err = zed.open(init_params)
if err != sl.ERROR_CODE.SUCCESS:
sys.stdout.write(repr(err))
zed.close()
exit()
# Get image size
image_size = zed.get_camera_information().camera_resolution
width = image_size.width
height = image_size.height
# create video object
video_writer = cv2.VideoWriter(
str(output_path),
cv2.VideoWriter_fourcc('M', '4', 'S', '2'),
max(zed.get_camera_information().camera_fps, 25),
(width, height)
)
# parameters
rt_param = sl.RuntimeParameters()
rt_param.sensing_mode = sl.SENSING_MODE.FILL
nb_frames = zed.get_svo_number_of_frames()
# Prepare single image containers
left_image = sl.Mat()
#right_image = sl.Mat()
#depth_image = sl.Mat()
# empty RGBA image
img_rgba = np.zeros((height, width, 4), dtype=np.uint8)
while True:
if zed.grab(rt_param) == sl.ERROR_CODE.SUCCESS:
svo_position = zed.get_svo_position()
# Retrieve SVO images
zed.retrieve_image(left_image, sl.VIEW.LEFT)
#zed.retrieve_image(right_image, sl.VIEW.RIGHT)
#zed.retrieve_image(depth_image, sl.VIEW.DEPTH)
if output_as_video:
#img_right = right_image.get_data()
#img_depth = depth_image.get_data()
img_rgba = left_image.get_data()
img_out = cv2.cvtColor(img_rgba, cv2.COLOR_RGBA2RGB)
video_writer.write(img_out)
progress_bar((svo_position + 1) / nb_frames * 100, 30)
# Check if we have reached the end of the video
if svo_position >= (nb_frames - 1): # End of SVO
sys.stdout.write("\nSVO end has been reached. Exiting now.\n")
break
except Exception as ex:
traceback.print_exc()
finally:
zed.close()