【问题标题】:How to get intel realsense D435i camera serial numbers from frames for multiple cameras?如何从多个相机的帧中获取英特尔 realsense D435i 相机序列号?
【发布时间】:2021-06-14 20:10:07
【问题描述】:

我已经为两个相机初始化了一个管道,并且我正在从相同的相机中获取颜色和深度图像。

问题是我找不到对应帧的相机序列号来确定哪个相机捕获了这些帧。

下面是我的代码:

import pyrealsense2 as rs
import numpy as np
import cv2
import logging
import time

# Configure depth and color streams...
pipeline_1 = rs.pipeline()
config_1 = rs.config()
config_1.enable_device('938422072752')
config_1.enable_device('902512070386')
config_1.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
config_1.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)



# Start streaming from both cameras
pipeline_1.start(config_1)

try:
    while True:

        # Camera 1
        # Wait for a coherent pair of frames: depth and color
        frames_1 = pipeline_1.wait_for_frames()
        depth_frame_1 = frames_1.get_depth_frame()
        color_frame_1 = frames_1.get_color_frame()
        if not depth_frame_1 or not color_frame_1:
            continue
        # Convert images to numpy arrays
        depth_image_1 = np.asanyarray(depth_frame_1.get_data())
        color_image_1 = np.asanyarray(color_frame_1.get_data())
        # Apply colormap on depth image (image must be converted to 8-bit per pixel first)
        depth_colormap_1 = cv2.applyColorMap(cv2.convertScaleAbs(depth_image_1, alpha=0.5), cv2.COLORMAP_JET)

        # Camera 2
        # Wait for a coherent pair of frames: depth and color
        frames_2 = pipeline_1.wait_for_frames()
        depth_frame_2 = frames_2.get_depth_frame()
        color_frame_2 = frames_2.get_color_frame()
    
        if not depth_frame_2 or not color_frame_2:
            continue
        # Convert images to numpy arrays

        depth_image_2 = np.asanyarray(depth_frame_2.get_data())
        color_image_2 = np.asanyarray(color_frame_2.get_data())
        # Apply colormap on depth image (image must be converted to 8-bit per pixel first)
        depth_colormap_2 = cv2.applyColorMap(cv2.convertScaleAbs(depth_image_2, alpha=0.5), cv2.COLORMAP_JET)

        # Stack all images horizontally
        images = np.hstack((color_image_1, depth_colormap_1,color_image_2, depth_colormap_2))

        # Show images from both cameras
        cv2.namedWindow('RealSense', cv2.WINDOW_NORMAL)
        cv2.imshow('RealSense', images)
        
        cv2.waitKey(20)

       
finally:
    pipeline_1.stop()

如何在 wait_for_frames() 之后找到相机序列号以确定哪个相机捕获了深度和彩色图像。

【问题讨论】:

标签: realsense


【解决方案1】:

这将查找连接的设备并找到序列号。

它们保存在一个列表中,您可以使用它们来启动可用的相机。

# Configure depth and color streams...
realsense_ctx = rs.context()
connected_devices = []
for i in range(len(realsense_ctx.devices)):
    detected_camera = ealsense_ctx.devices[i].get_info(rs.camera_info.serial_number)
    connected_devices.append(detected_camera)

【讨论】:

    【解决方案2】:

    我采用了您的代码,将它与 nayab 发布的 C++ example 结合起来,编写了以下代码,该代码抓取了多个 RealSense 摄像头的彩色图像(仅)并将它们水平堆叠:

    import pyrealsense2 as rs
    import numpy as np
    import cv2
    import logging
    import time
    
    realsense_ctx = rs.context()  # The context encapsulates all of the devices and sensors, and provides some additional functionalities.
    connected_devices = []
    
    # get serial numbers of connected devices:
    for i in range(len(realsense_ctx.devices)):
        detected_camera = realsense_ctx.devices[i].get_info(
            rs.camera_info.serial_number)
        connected_devices.append(detected_camera)
    
    pipelines = []
    configs = []
    for i in range(len(realsense_ctx.devices)):
        pipelines.append(rs.pipeline())  # one pipeline for each device
        configs.append(rs.config())      # one config for each device
        configs[i].enable_device(connected_devices[i])
        configs[i].enable_stream(rs.stream.color, 1920, 1080, rs.format.bgr8, 30)
        pipelines[i].start(configs[i])
    
    try:
        while True:
            images = []
            for i in range(len(pipelines)):
                print("waiting for frame at cam", i)
                frames = pipelines[i].wait_for_frames()
                color_frame = frames.get_color_frame()
                images.append(np.asanyarray(color_frame.get_data()))
    
            # Stack all images horizontally
            image_composite = images[0]
            for i in range(1, len(images)):
                images_composite = np.hstack((image_composite, images[i]))
    
            # Show images from both cameras
            cv2.namedWindow('RealSense', cv2.WINDOW_NORMAL)
            cv2.imshow('RealSense', images_composite)
    
            cv2.waitKey(20)
    
    finally:
        for i in range(len(pipelines)):
            pipelines[i].stop()
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-15
      • 2016-10-31
      • 2019-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-16
      • 2014-06-02
      相关资源
      最近更新 更多