It is a memorandum when connecting realsense D435 to a PC with ubuntu 16.04 installed and saving a depth video with python.
PC with ubuntu 16.04 installed realsenseD435
pyrealsense2 installation
pip install pyrealsense2
動画を保存する:record.py 保存した動画を再生する:play.py 保存した動画を連番画像にする:save.py
record.py
import pyrealsense2 as rs
import numpy as np
import cv2
import time
# Configure depth and color streams
config = rs.config()
config.enable_stream(rs.stream.infrared, 1, 640, 480, rs.format.y8, 30)
config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)
config.enable_record_to_file('d435data.bag')
# Start streaming
pipeline = rs.pipeline()
pipeline.start(config)
start = time.time()
frame_no = 1
try:
while True:
# Wait for a coherent pair of frames: depth and color
frames = pipeline.wait_for_frames()
color_frame = frames.get_color_frame()
depth_frame = frames.get_depth_frame()
ir_frame = frames.get_infrared_frame()
fps = frame_no / (time.time() - start)
print(fps)
frame_no = frame_no+1
if not ir_frame or not color_frame :
ir_image = np.asanyarray(ir_frame .get_data())
depth_image = np.asanyarray(depth_color_frame.get_data())
color_image = np.asanyarray(color_frame.get_data())
finally:
pipeline.stop()
play.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pyrealsense2 as rs
import numpy as np
import cv2
#stream(Color/Depth/Infrared)settings of
config = rs.config()
#↓ Set the file name here
config.enable_device_from_file('d435data.bag')
#config.enable_device_from_file('./20191229.bag')
config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)
config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
config.enable_stream(rs.stream.infrared, 1, 640, 480, rs.format.y8, 30)
#Start streaming
pipeline = rs.pipeline()
profile = pipeline.start(config)
try:
while True:
#Waiting for a frame(Color & Depth)
frames = pipeline.wait_for_frames()
ir_frame = frames.get_infrared_frame()
depth_frame = frames.get_depth_frame()
color_frame = frames.get_color_frame()
if not depth_frame or not color_frame or not ir_frame :
continue
# Convert images to numpy arrays
ir_image = np.asanyarray(ir_frame .get_data())
depth_color_frame = rs.colorizer().colorize(depth_frame)
depth_image = np.asanyarray(depth_color_frame.get_data())
color_image = np.asanyarray(color_frame.get_data())
# Show images
cv2.namedWindow('ir_image', cv2.WINDOW_AUTOSIZE)
cv2.imshow('ir_image', ir_image)
cv2.namedWindow('color_image', cv2.WINDOW_AUTOSIZE)
cv2.imshow('color_image', color_image)
cv2.namedWindow('depth_image', cv2.WINDOW_AUTOSIZE)
cv2.imshow('depth_image', depth_image)
cv2.waitKey(1)
finally:
# Stop streaming
pipeline.stop()
save.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pyrealsense2 as rs
import numpy as np
import cv2
#stream(Color/Depth/Infrared)settings of
config = rs.config()
#↓ Set the file name here
config.enable_device_from_file('d435data.bag')
#config.enable_device_from_file('./20191229.bag')
config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)
config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
config.enable_stream(rs.stream.infrared, 1, 640, 480, rs.format.y8, 30)
#Start streaming
pipeline = rs.pipeline()
profile = pipeline.start(config)
try:
count = 0
while True:
count_padded = '%05d' % count
count += 1
#Waiting for a frame(Color & Depth)
frames = pipeline.wait_for_frames()
ir_frame = frames.get_infrared_frame()
depth_frame = frames.get_depth_frame()
color_frame = frames.get_color_frame()
if not depth_frame or not color_frame or not ir_frame :
continue
# Convert images to numpy arrays
ir_image = np.asanyarray(ir_frame .get_data())
depth_color_frame = rs.colorizer().colorize(depth_frame)
depth_image = np.asanyarray(depth_color_frame.get_data())
color_image = np.asanyarray(color_frame.get_data())
write_file_name_color = "color_" + count_padded + ".jpg "
cv2.imwrite(write_file_name_color, color_image)
write_file_name_depth = "depth_" + count_padded + ".jpg "
cv2.imwrite(write_file_name_depth, depth_image)
# Show images
cv2.namedWindow('ir_image', cv2.WINDOW_AUTOSIZE)
cv2.imshow('ir_image', ir_image)
cv2.namedWindow('color_image', cv2.WINDOW_AUTOSIZE)
cv2.imshow('color_image', color_image)
cv2.namedWindow('depth_image', cv2.WINDOW_AUTOSIZE)
cv2.imshow('depth_image', depth_image)
cv2.waitKey(1)
finally:
# Stop streaming
pipeline.stop()
To convert the saved image to a video Generate a video from a serial number image with ffmpeg / Generate a serial number image from a video ~ To prevent dropped frames ~ Seems to be good.
I am running with python2.7 series, but if it is more than that, an error may occur. ..
Running Intel's Realsense D435 Record and output with Intel Realsense D435 3D sensing starting with Python !! Generate video from serial number image with ffmpeg / Generate serial number image from video ~ How to prevent dropped frames ~
Recommended Posts