01 准备工作

在这之前已经安装了librealsense和realsense-ros,并进行了测试
安装过程可参考:https://blog.csdn.net/gyxx1998/article/details/121204091

02 安装pyrealsense2

pyrealsense(上一版本)只支持SR300,F200和R200系列,因为需要使用D435i,所以安装pyrealsense2。

pip安装pyrealsense2,这一步也可以手动安装https://pypi.org/project/pyrealsense2/

  • S1:因为是新电脑,所以先安装pip

sudo apt install python-pip

  • S2:然后pip安装pyrealsense2

pip install --user pyrealsense2

guyue@guyue:~$ pip install --user pyrealsense2
Collecting pyrealsense2
  Downloading https://files.pythonhosted.org/packages/7e/6c/4ef325cc7261c8c83dc86f09058553b030ef2a9760b952520db0e9e11e02/pyrealsense2-2.50.0.3812-cp27-cp27mu-manylinux1_x86_64.whl (17.1MB)
    100% |████████████████████████████████| 17.1MB 86kB/s 
Installing collected packages: pyrealsense2
Successfully installed pyrealsense2-2.50.0.3812

pip show pyrealsense2查询pyrealsense2包安装的位置:Location: /home/guyue/.local/lib/python2.7/site-packages

测试1

  • S1:在某目录下新建py文件:touch test.py
  • S2:设为可执行文件:sudo chmod +x test.py
  • S3:打开py文件,写入下述内容,并保存

import pyrealsense2 as rs
pipe = rs.pipeline()
profile = pipe.start()
try:
  for i in range(0, 100):
    frames = pipe.wait_for_frames()
    for f in frames:
      print(f.profile)
finally:
    pipe.stop()

S4:然后在该目录下打开终端,在终端输入:python test.py

测试2

之前以为还需要安装opencv,但是运行pkg-config --modversion opencv,发现已经有OpenCV 3.2.0了,可能是安装ros过程中已经装了吧

使用以下代码测试,发现可以正常显示画面,说明所有驱动和包都已经安装了

## License: Apache 2.0. See LICENSE file in root directory.
## Copyright(c) 2015-2017 Intel Corporation. All Rights Reserved.

###############################################
##      Open CV and Numpy integration        ##
###############################################

import pyrealsense2 as rs
import numpy as np
import cv2

# Configure depth and color streams
pipeline = rs.pipeline()
config = rs.config()

# Get device product line for setting a supporting resolution
pipeline_wrapper = rs.pipeline_wrapper(pipeline)
pipeline_profile = config.resolve(pipeline_wrapper)
device = pipeline_profile.get_device()
device_product_line = str(device.get_info(rs.camera_info.product_line))

found_rgb = False
for s in device.sensors:
    if s.get_info(rs.camera_info.name) == 'RGB Camera':
        found_rgb = True
        break
if not found_rgb:
    print("The demo requires Depth camera with Color sensor")
    exit(0)

config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)

if device_product_line == 'L500':
    config.enable_stream(rs.stream.color, 960, 540, rs.format.bgr8, 30)
else:
    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)

        depth_colormap_dim = depth_colormap.shape
        color_colormap_dim = color_image.shape

        # If depth and color resolutions are different, resize color image to match depth image for display
        if depth_colormap_dim != color_colormap_dim:
            resized_color_image = cv2.resize(color_image, dsize=(depth_colormap_dim[1], depth_colormap_dim[0]), interpolation=cv2.INTER_AREA)
            images = np.hstack((resized_color_image, depth_colormap))
        else:
            images = np.hstack((color_image, depth_colormap))

        # Show images
        cv2.namedWindow('RealSense', cv2.WINDOW_AUTOSIZE)
        cv2.imshow('RealSense', images)
        cv2.waitKey(1)

finally:

    # Stop streaming
    pipeline.stop()