一、Windows下环境配置
python环境:python3.6
Realsense环境配置:anaconda prompt终端中进入自己的python3.6环境中,pip安装。
(Realsense官网:Intel RealSense for Developers - Build your own computer vision apps)
#第一步进入自己的环境(我的是pytorch_gpu)
conda activate pytorch_gpu
#第二步安装pyrealsense2
pip install pyrealsense2
测试:参考自Windows下使用Python配置环境、调用Intel realsense D435/D435i-python黑洞网
import pyrealsense2 as rs
import numpy as np
import cv2
if __name__ == "__main__":
# Configure depth and color streams
pipeline = rs.pipeline()
config = rs.config()
config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)
# Start streaming
pipeline.start(config)
try:
while True:
# Wait for a coherent pair of frames: depth and color
frames = pipeline.wait_for_frames()
depth_frame = frames.get_depth_frame()
color_frame = frames.get_color_frame()
if not depth_frame or not color_frame:
continue
# Convert images to numpy arrays
depth_image = np.asanyarray(depth_frame.get_data())
color_image = np.asanyarray(color_frame.get_data())
# Apply colormap on depth image (image must be converted to 8-bit per pixel first)
depth_colormap = cv2.applyColorMap(cv2.convertScaleAbs(depth_image, alpha=0.03), cv2.COLORMAP_JET)
# Stack both images horizontally
images = np.hstack((color_image, depth_colormap))
# Show images
cv2.namedWindow('RealSense', cv2.WINDOW_AUTOSIZE)
cv2.imshow('RealSense', images)
key = cv2.waitKey(1)
# Press esc or 'q' to close the image window
if key & 0xFF == ord('q') or key == 27:
cv2.destroyAllWindows()
break
finally:
# Stop streaming
pipeline.stop()
二、视频录制
用通过调用OpenCV 的 VideoWriter 这个类来保存Realsense帧数据:
VideoWriter(filename, fourcc, fps, frameSize[, isColor]) -> <VideoWriter object>
"""
1.filename:视频保存路径
2.fourcc:指定编码器
3.fps:视频帧率
4.frameSize 视频图像尺寸
5.isColor False黑白画面、True彩色的画面
"""
下面示例为保存Realsense的RGB视频流,深度图类似。
import pyrealsense2 as rs
import numpy as np
import cv2
# # VIDEO SETTINGS
video_name = 'testwrite.avi'
video_fps = 30
video_w = 640
video_h = 480
# # REALSENSE SET
pipeline = rs.pipeline()
config = rs.config()
config.enable_stream(rs.stream.color, video_w, video_h, rs.format.bgr8, 30)
# Start streaming
pipeline.start(config)
# # SAVE
# fourcc 指定编码器
fourcc = cv2.VideoWriter_fourcc(*'XVID')
# Out settings(OpenCV)
out = cv2.VideoWriter(video_name, fourcc, video_fps, (video_w, video_h), True)
while True:
frames = pipeline.wait_for_frames()
color_frame = frames.get_color_frame()
color_image = np.asanyarray(color_frame.get_data()) # 转为数组
cv2.imshow('frame', color_image)
out.write(color_image)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
pipeline.stop()
out.release()
cv2.destroyAllWindows()
其中cv2.waitKey(1) & 0xFF == ord(‘q’)的意义:
cv2.waitKey(1)
:在1ms内根据键盘输入返回一个值0xFF
:一个十六进制数ord('q')
:返回q的ascii码
代码段中其作用为显示图片。而如果使用cv2.waitKey(0)则会导致进程卡在等待键盘输入-->出错。