Skip to content

Commit

Permalink
fix the bug in 0.0.0.6
Browse files Browse the repository at this point in the history
  • Loading branch information
Skylark0924 committed Sep 9, 2022
1 parent 278c9b7 commit c923a6e
Show file tree
Hide file tree
Showing 4 changed files with 207 additions and 12 deletions.
3 changes: 2 additions & 1 deletion rofunc/devices/zed/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from __future__ import absolute_import

from .record import *
from .playback import *
from .playback import *
from .export import *
178 changes: 178 additions & 0 deletions rofunc/devices/zed/export.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
import enum
import sys
from pathlib import Path

import cv2
import numpy as np
import pyzed.sl as sl


class AppType(enum.Enum):
LEFT_AND_RIGHT = 1
LEFT_AND_DEPTH = 2
LEFT_AND_DEPTH_16 = 3


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()


def export(filepath):
if not sys.argv or len(sys.argv) != 4:
sys.stdout.write("Usage: \n\n")
sys.stdout.write(" ZED_SVO_Export A B C \n\n")
sys.stdout.write("Please use the following parameters from the command line:\n")
sys.stdout.write(" A - SVO file path (input) : \"path/to/file.svo\"\n")
sys.stdout.write(" B - AVI file path (output) or image sequence folder(output) :\n")
sys.stdout.write(" \"path/to/output/file.avi\" or \"path/to/output/folder\"\n")
sys.stdout.write(" C - Export mode: 0=Export LEFT+RIGHT AVI.\n")
sys.stdout.write(" 1=Export LEFT+DEPTH_VIEW AVI.\n")
sys.stdout.write(" 2=Export LEFT+RIGHT image sequence.\n")
sys.stdout.write(" 3=Export LEFT+DEPTH_VIEW image sequence.\n")
sys.stdout.write(" 4=Export LEFT+DEPTH_16Bit image sequence.\n")
sys.stdout.write(" A and B need to end with '/' or '\\'\n\n")
sys.stdout.write("Examples: \n")
sys.stdout.write(" (AVI LEFT+RIGHT): ZED_SVO_Export \"path/to/file.svo\" \"path/to/output/file.avi\" 0\n")
sys.stdout.write(" (AVI LEFT+DEPTH): ZED_SVO_Export \"path/to/file.svo\" \"path/to/output/file.avi\" 1\n")
sys.stdout.write(" (SEQUENCE LEFT+RIGHT): ZED_SVO_Export \"path/to/file.svo\" \"path/to/output/folder\" 2\n")
sys.stdout.write(" (SEQUENCE LEFT+DEPTH): ZED_SVO_Export \"path/to/file.svo\" \"path/to/output/folder\" 3\n")
sys.stdout.write(" (SEQUENCE LEFT+DEPTH_16Bit): ZED_SVO_Export \"path/to/file.svo\" \"path/to/output/folder\""
" 4\n")
exit()

# Get input parameters
svo_input_path = filepath
output_path = filepath.split('.')
output_as_video = True
app_type = AppType.LEFT_AND_RIGHT
if sys.argv[3] == "1" or sys.argv[3] == "3":
app_type = AppType.LEFT_AND_DEPTH
if sys.argv[3] == "4":
app_type = AppType.LEFT_AND_DEPTH_16

# Check if exporting to AVI or SEQUENCE
if sys.argv[3] != "0" and sys.argv[3] != "1":
output_as_video = False

if not output_as_video and not output_path.is_dir():
sys.stdout.write("Input directory doesn't exist. Check permissions or create it.\n",
output_path, "\n")
exit()

# 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
width_sbs = width * 2

# Prepare side by side image container equivalent to CV_8UC4
svo_image_sbs_rgba = np.zeros((height, width_sbs, 4), dtype=np.uint8)

# Prepare single image containers
left_image = sl.Mat()
right_image = sl.Mat()
depth_image = sl.Mat()

video_writer = None
if output_as_video:
# Create video writer with MPEG-4 part 2 codec
video_writer = cv2.VideoWriter(str(output_path),
cv2.VideoWriter_fourcc('M', '4', 'S', '2'),
max(zed.get_camera_information().camera_fps, 25),
(width_sbs, height))

if not video_writer.isOpened():
sys.stdout.write("OpenCV video writer cannot be opened. Please check the .avi file path and write "
"permissions.\n")
zed.close()
exit()

rt_param = sl.RuntimeParameters()
rt_param.sensing_mode = sl.SENSING_MODE.FILL

# Start SVO conversion to AVI/SEQUENCE
sys.stdout.write("Converting SVO... Use Ctrl-C to interrupt conversion.\n")

nb_frames = zed.get_svo_number_of_frames()

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)

if app_type == AppType.LEFT_AND_RIGHT:
zed.retrieve_image(right_image, sl.VIEW.RIGHT)
elif app_type == AppType.LEFT_AND_DEPTH:
zed.retrieve_image(right_image, sl.VIEW.DEPTH)
elif app_type == AppType.LEFT_AND_DEPTH_16:
zed.retrieve_measure(depth_image, sl.MEASURE.DEPTH)

if output_as_video:
# Copy the left image to the left side of SBS image
svo_image_sbs_rgba[0:height, 0:width, :] = left_image.get_data()

# Copy the right image to the right side of SBS image
svo_image_sbs_rgba[0:, width:, :] = right_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)
else:
# Generate file names
filename1 = output_path / ("left%s.png" % str(svo_position).zfill(6))
filename2 = output_path / (("right%s.png" if app_type == AppType.LEFT_AND_RIGHT
else "depth%s.png") % str(svo_position).zfill(6))

# Save Left images
cv2.imwrite(str(filename1), left_image.get_data())

if app_type != AppType.LEFT_AND_DEPTH_16:
# Save right images
cv2.imwrite(str(filename2), right_image.get_data())
else:
# Save depth images (convert to uint16)
cv2.imwrite(str(filename2), depth_image.get_data().astype(np.uint16))

# Display progress
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

if output_as_video:
# Close the video writer
video_writer.release()

zed.close()
return 0


if __name__ == "__main__":
import rofunc as rf

rf.zed.export('/home/ubuntu/Data/06_24/Video/20220624_1649/38709363.svo')
36 changes: 26 additions & 10 deletions rofunc/devices/zed/record.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
from signal import signal, SIGINT
import pyzed.sl as sl
import threading
import time
import signal
import logging
import os
from src.record import get_intrinsic_parameters
import signal
import threading
import time

import pyzed.sl as sl

zed_list = []


def get_intrinsic_parameters(cam):
calibration_params = cam.get_camera_information().camera_configuration.calibration_parameters
# Focal length of the left eye in pixels
focal_left_x = calibration_params.left_cam.fx
# First radial distortion coefficient
k1 = calibration_params.left_cam.disto[0]
# Translation between left and right eye on z-axis

def signal_handler(signal, frame, zed_list):
t = calibration_params.T
# Horizontal field of view of the left eye in degrees
h_fov = calibration_params.left_cam.h_fov
return focal_left_x, k1, t, h_fov


def signal_handler(signal, frame):
global zed_list
print('ZED', zed_list)
for cam in zed_list:
cam.disable_recording()
cam.close()
signal = True
time.sleep(0.5)
exit()

Expand All @@ -36,13 +51,14 @@ def grab_run(zed_list, recording_param_list, index):


def record(root_dir, exp_name):
global zed_list

if os.path.exists('{}/{}'.format(root_dir, exp_name)):
raise Exception('There are already some files in {}, please rename the exp_name.'.format(
'{}/{}'.format(root_dir, exp_name)))
else:
os.mkdir('{}/{}'.format(root_dir, exp_name))

zed_list = []
left_list = []
depth_list = []
timestamp_list = []
Expand Down Expand Up @@ -110,7 +126,7 @@ def record(root_dir, exp_name):

if __name__ == "__main__":
root_dir = '/home/ubuntu/Data/zed_record'
exp_name = '20220909'
exp_name = '20220909212234'

# record(root_dir, exp_name)
import rofunc as rf
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name="rofunc",
version="0.0.0.6",
version="0.0.0.7",
description='Useful functions for robot experiments',
author="skylark",
author_email="jjliu@mae.cuhk.edu.hk",
Expand Down

0 comments on commit c923a6e

Please sign in to comment.