树莓派4B安装realsense的Python开发环境
in 树莓派 with 0 comment

树莓派4B安装realsense的Python开发环境

in 树莓派 with 0 comment

github官方文档: https://github.com/IntelRealSense/librealsense/blob/master/doc/installation_raspbian.md

设置虚拟内存

sudo mousepad /etc/dphys-swapfile
sudo /etc/init.d/dphys-swapfile restart

改为固定的2048或者干脆使用动态分配的大小,重启

安装依赖包

sudo apt-get update && sudo apt-get upgrade && sudo apt-get dist-upgrade
sudo apt-get install git cmake libssl-dev libusb-1.0-0-dev pkg-config libgtk-3-dev
sudo apt-get install libglfw3-dev libgl1-mesa-dev libglu1-mesa-dev

第二行的是核心依赖,必装。第三行是3D相关的依赖,如果不打算使用realsense-viewer,可以不装。

下载Realsense SDK

git clone https://github.com/IntelRealSense/librealsense.git

或者直接去上述地址下载zip包,解压

编译准备

mkdir build && cd build
cmake ../ -DCMAKE_BUILD_TYPE=Release -DBUILD_EXAMPLES=true -DFORCE_RSUSB_BACKEND=ON -DBUILD_WITH_TM2=false -DIMPORT_DEPTH_CAM_FW=false

编译

sudo make uninstall && make clean && make && sudo make install

这步大概要1~2小时,反正很慢。。

设置udev规则

sudo ./scripts/setup_udev_rules.sh

会提示你拔掉摄像头,然后按任意键,主要是为了让realsense-viewer更方便的识别设备

测试

realsense-viewer

安装pyrealsense2

$ cd ~/librealsense/build

for python2
$ cmake .. -DBUILD_PYTHON_BINDINGS=bool:true -DPYTHON_EXECUTABLE=$(which python)

for python3
$ cmake .. -DBUILD_PYTHON_BINDINGS=bool:true -DPYTHON_EXECUTABLE=$(which python3)

$ make -j1
$ sudo make install

add python path
$ vim ~/.zshrc
export PYTHONPATH=$PYTHONPATH:/usr/local/lib

$ source ~/.zshrc

注: 树莓派zero相同步骤,但是编译python3的时候提示缺少GL/hl.h,说明系统里面缺少OpenGl库,需要安装以下内容

sudo apt-get install mesa-common-dev
sudo apt-get install libgl1-mesa-dev libglu1-mesa-dev freeglut3-dev
sudo apt-get install libgl1-mesa-dev libglu1-mesa-dev

这里做完,在xxx/librealsense/build/wrappers/python目录下会生成一些so文件,拷贝到python的库文件夹下

sudo cp pyxxxxx /usr/lib/python3/dist-packages/

测试

新建test.py文件,写入以下内容,并且运行

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导入报错

ImportError: libhdf5_serial.so.100: cannot open shared object file: No such file or directory

原因:缺少了若干模块
解决方案:直接安装

pip3 install opencv-contrib-python==3.4.3.18 -i https://www.piwheels.org/simple
sudo apt-get update #安装依赖库
sudo apt-get install libhdf5-dev
sudo apt-get install libatlas-base-dev
sudo apt-get install libjasper-dev
sudo apt-get install libqt4-test
sudo apt-get install libqtgui4
sudo apt-get update
 
python3
import cv2 # 检查导入成功
Responses