官方网址https://github.com/IntelRealSense/librealsense/tree/master/wrappers/python
两个RealSenseD435连接到电脑,对图像进行同时读取和显示:
# 相机1
self.pipeline1 = rs.pipeline() # ***刚开始出错,明明自我感觉良好却开始怀疑人生,是因为刚开始这里写的是self.pipeline1 = rs.pipeline,用类初始化时忘掉了括号()***
self.config1 = rs.config() # rs.config()函数用于创建摄像头配置对象,用于在pipeline对象启动配置对象之前对摄像头rgb流或depth流进行配置
self.config1.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 60)
self.config1.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 60) # Configure depth and color streams
self.config1.enable_device(str(938422072062))
self.profile1 = self.pipeline1.start(self.config1) # Start streaming
self.depth_sensor1 = self.profile1.get_device().first_depth_sensor()
self.depth_scale1 = self.depth_sensor1.get_depth_scale()
self.frames1 = self.pipeline1.wait_for_frames()
self.color_frame1 = self.frames1.get_color_frame()
self.color_and_depth_images["color_and_depth_images1"]["color"] = np.asanyarray(self.color_frame1.get_data())
# 相机2
self.pipeline2 = rs.pipeline()
self.config2 = rs.config() # rs.config()函数用于创建摄像头配置对象,用于在pipeline对象启动配置对象之前对摄像头rgb流或depth流进行配置
self.config2.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 60)
self.config2.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 60) # Configure depth and color streams
self.config1.enable_device(str(935322072420))
self.profile2 = self.pipeline2.start(self.config2) # Start streaming
self.depth_sensor2 = self.profile2.get_device().first_depth_sensor()
self.depth_scale2 = self.depth_sensor2.get_depth_scale()
self.frames2 = self.pipeline2.wait_for_frames()
self.color_frame2 = self.frames2.get_color_frame()
self.color_and_depth_images["color_and_depth_images2"]["color"] = np.asanyarray(self.color_frame2.get_data()) # [:, 80:]
验证程序
import pyrealsense2 as rs
import numpy as np
import cv2
pipeline1 = rs.pipeline()
config1 = rs.config()
config1.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 60)
config1.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 60)
config1.enable_device(str(938422072062))
profile1 = pipeline1.start(config1)
depth_sensor1 = profile1.get_device().first_depth_sensor()
frames1 = pipeline1.wait_for_frames()
color_frame1 = frames1.get_color_frame()
img1 = np.asanyarray(color_frame1.get_data())
# name1 = "img1"
# cv2.namedWindow(name1)
# cv2.imshow(name1, img1)
# cv2.waitKey(0)
# cv2.destroyWindow(name1)
pipeline2 = rs.pipeline()
config2 = rs.config()
config2.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 60)
config2.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 60)
config2.enable_device(str(935322072420))
profile2 = pipeline2.start(config2)
depth_sensor2 = profile2.get_device().first_depth_sensor()
frames2 = pipeline2.wait_for_frames()
color_frame2 = frames2.get_color_frame()
img2 = np.asanyarray(color_frame2.get_data())
img = np.concatenate([img1, img2], axis=1)
name2 = "img"
cv2.namedWindow(name2)
cv2.imshow(name2, img)
cv2.waitKey(0)
cv2.destroyWindow(name2)
# print(depth_sensor1)
# print(depth_sensor2)