python -- realsense系列 两个相机同时接入并显示

import pyrealsense2 as rs
import cv2
import numpy as np
import time
import os
import sys

if __name__ == '__main__':


    # 确定图像的输入分辨率与帧率
    resolution_width = 640  # pixels
    resolution_height = 480  # pixels
    frame_rate = 15  # fps

    # 注册数据流,并对其图像
    align = rs.align(rs.stream.color)
    rs_config = rs.config()
    rs_config.enable_stream(rs.stream.depth, resolution_width, resolution_height, rs.format.z16, frame_rate)
    rs_config.enable_stream(rs.stream.color, resolution_width, resolution_height, rs.format.bgr8, frame_rate)
    ### d435i
    #
    rs_config.enable_stream(rs.stream.infrared, 1, 640, 480, rs.format.y8, frame_rate)
    rs_config.enable_stream(rs.stream.infrared, 2, 640, 480, rs.format.y8, frame_rate)
    # check相机是不是进来了
    connect_device = []
    for d in rs.context().devices:
        print('Found device: ',
              d.get_info(rs.camera_info.name), ' ',
              d.get_info(rs.camera_info.serial_number))
        if d.get_info(rs.camera_info.name).lower() != 'platform camera':
            connect_device.append(d.get_info(rs.camera_info.serial_number))


    if len(connect_device) < 2:
        print('Registrition needs two camera connected.But got one.')
        exit()

    # 确认相机并获取相机的内部参数
    pipeline1 = rs.pipeline()
    rs_config.enable_device(connect_device[0])
    # pipeline_profile1 = pipeline1.start(rs_config)
    pipeline1.start(rs_config)

    pipeline2 = rs.pipeline()
    rs_config.enable_device(connect_device[1])
    # pipeline_profile2 = pipeline2.start(rs_config)
    pipeline2.start(rs_config)

    try:

        while True:

            # 等待数据进来
            frames1 = pipeline1.wait_for_frames()
            frames2 = pipeline2.wait_for_frames()

            # 将进来的RGBD数据对齐
            aligned_frames1 = align.process(frames1)
            aligned_frames2 = align.process(frames2)

            # 将对其的RGB—D图取出来
            color_frame1 = aligned_frames1.get_color_frame()
            depth_frame1 = aligned_frames1.get_depth_frame()
            color_frame2 = aligned_frames2.get_color_frame()
            depth_frame2 = aligned_frames2.get_depth_frame()
            # --------------------------------------
            depth_frame1 = frames1.get_depth_frame()
            color_frame1 = frames1.get_color_frame()
            depth_frame2 = frames2.get_depth_frame()
            color_frame2 = frames2.get_color_frame()

            # 数组化数据便于处理

            ir_frame_left1 = frames1.get_infrared_frame(1)
            ir_frame_right1 = frames1.get_infrared_frame(2)
            if not depth_frame1 or not color_frame1:
                continue
            ir_frame_left2 = frames2.get_infrared_frame(1)
            ir_frame_right2 = frames2.get_infrared_frame(2)
            if not depth_frame2 or not color_frame2:
                continue

            color_image1 = np.asanyarray(color_frame1.get_data())
            depth_image1 = np.asanyarray(depth_frame1.get_data())
            ir_left_image1 = np.asanyarray(ir_frame_left1.get_data())
            ir_right_image1 = np.asanyarray(ir_frame_right1.get_data())

            color_image2 = np.asanyarray(color_frame2.get_data())
            depth_image2 = np.asanyarray(depth_frame2.get_data())
            ir_left_image2 = np.asanyarray(ir_frame_left2.get_data())
            ir_right_image2 = np.asanyarray(ir_frame_right2.get_data())

            depth_colormap1 = cv2.applyColorMap(cv2.convertScaleAbs(depth_image1, alpha=0.03), cv2.COLORMAP_JET)
            images1 = np.hstack((color_image1, depth_colormap1))

            depth_colormap2 = cv2.applyColorMap(cv2.convertScaleAbs(depth_image2, alpha=0.03), cv2.COLORMAP_JET)
            images2 = np.hstack((color_image2, depth_colormap2))
            cv2.imshow('RealSense1', images1)
            cv2.imshow('RealSense2', images2)

            key = cv2.waitKey(1)

            if key & 0xFF == ord('q') or key == 27:
                cv2.destroyAllWindows()
                break
    finally:
        pipeline1.stop()
        pipeline2.stop()
  • 5
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值