【亲测有效】在ubuntu20.04LTS上使用python代码调用realsense深度相机(aarch64架构)

查看主机ip
 hostname -I
查看ssh服务是否启动
 sudo ps -e | grep ssh
查看系统版本,使用代码
 root@ubuntu:~# lsb_release -a

输出示例

 Distributor ID: Ubuntu
 Description:    Ubuntu 22.04.2 LTS
 Release:    22.04
 Codename:   jammy
查看系统架构,使用代码

(x86 架构与 ARM 架构的 CPU 架构不同,如果回显为 aarch64表示为 ARM 架构,如果回显为 x86_64 表示为x86架构)

 root@ubuntu:~# uname -a

输出示例

 Linux k8s-master 5.15.0-1027-raspi #29-Ubuntu SMP PREEMPT Mon Apr 3 10:12:21 UTC 2023 aarch64 aarch64 aarch64 GNU/Linux

或则使用如下代码查看系统架构

 root@ubuntu:~# uname -m

输出示例

 aarch64
Ubuntu 拷贝文件后文件带锁

在 Ubuntu 中,如果你使用 sudo 命令拷贝文件到一个需要 root 权限的目录,这个文件就会被标记为锁定状态(locked),只有root 用户或者具有相应权限的用户才能修改或删除它。如果你想要修改这个文件,可以使用 chown 命令将文件的所有权转移给自己,然后再进行修改。具体步骤如下: 1.打开终端,使用以下命令将文件的所有权转移给自己:

 sudo chown your_username:your_username file path

其中,"your username"为你的用户名,"file_path"为文件的路径。

参考:Ubuntu 拷贝文件后文件带锁 - CSDN文库

查看python所在的路径
 which python

/usr/bin/目录下通过ls -l查看软链接的情况

系统换源

参考:Ubuntu更换软件源(20.04LTS版本)【ARM架构以及X86架构的Ubuntu】_ubunutu(arm)20.04更换软件源 csdn-CSDN博客

1.1备份原来的源

 sudo cp /etc/apt/sources.list /etc/apt/sources_init.list

1.2更换源

清华大学的镜像源地址:清华大学开源软件镜像站 | Tsinghua Open Source Mirror

选择对应的Ubuntu版本复制里面的内容。复制到刚刚的 /etc/apt/sources.list文件中。 注意:Ubuntu必须要对应版本!!!!否则镜像源无效。然后执行(这里以ARM架构为例)

 # 先更新
 sudo apt-get update 
 sudo apt-get upgrade
 # 安装nano编辑器
 sudo apt-get install nano 

打开镜像源文件

 sudo nano /etc/apt/sources.list

将下边的清华源复制进去,然后ctrl+o保存,回车,ctrl+X退出编辑。

# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
 deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy main restricted universe multiverse
 # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy main restricted universe multiverse
 deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy-updates main restricted universe multiverse
 # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy-updates main restricted universe multiverse
 deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy-backports main restricted universe multiverse
 # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy-backports main restricted universe multiverse
 ​
 deb http://ports.ubuntu.com/ubuntu-ports/ jammy-security main restricted universe multiverse
 # deb-src http://ports.ubuntu.com/ubuntu-ports/ jammy-security main restricted universe multiverse

1.3更新

 sudo apt-get update 
 sudo apt-get upgrade

配置D415驱动

2.1下载RealSense SDK

 sudo apt-get install git
 git clone https://github.com/IntelRealSense/librealsense

2.2编译

cd librealsense/
 ​
mkdir build && cd build
 ​
sudo apt-get install libxrandr-dev
sudo apt-get install libxinerama-dev
sudo apt-get install libsdl2-dev
sudo apt-get install python3-dev
sudo apt install -y build-essential
sudo apt-get install libssl-dev
 ​
 cmake ../ -DFORCE_RSUSB_BACKEND=true -DBUILD_PYTHON_BINDINGS=true -DCMAKE_BUILD_TYPE=release -DBUILD_EXAMPLES=true -DBUILD_GRAPHICAL_EXAMPLES=true
 ​
 sudo apt-get install libusb-1.0-0-dev
 ​
 sudo make uninstall && make clean && make && sudo make install   #此处编译大约花费时间1.5h

2.3设置udev规则

 cd librealsense
 sudo ./scripts/setup_udev_rules.sh

2.4测试

在命令行中输入如下指令能看到界面就证明成功了

 realsense-viewer

安装pyrealsense2

pyrealsense2库不适用于arm架构树莓派,所以pip install pyrealsense2无法安装。目前还没有arm架构下的pyralsense2的PyPi包,因此在jetson使用pip安装的话,无法找到满足的版本。在python3下import pyrealsense2显示ImportError: No module named pyrealsense2

参考:【亲测有效】树莓派4B安装realsense(Intel深度摄像头)_树莓派realsense-CSDN博客

在Jetson Nano上编译 pyrealsense2库包,并在Intel的tof相机上进行测试_no internet connection, disabling import_depth_cam-CSDN博客

 cd ~/librealsense/build
 ​
 # for python3
 cmake .. -DBUILD_PYTHON_BINDINGS=bool:true -DPYTHON_EXECUTABLE=$(which python3)
 ​
 make -j1
 sudo make install

添加python路径

 nano ~/.zshrc 

复制下面语句到~/.zshrc中

 export PYTHONPATH=$PYTHONPATH:/usr/local/lib

然后ctrl+o保存,回车,ctrl+X退出编辑,输入如下指令

 source ~/.zshrc

到librealsense/build/release目录下看发现有了一些so、so.2.54 和 so.2.54.2 结尾的文件,我们将这些文件全部拷贝至 /usr/lib/python3/dist-packages/ 文件夹下,不是 /usr/lib/python3.7 文件夹下。

librealsense2-gl.so                                  
pyrsutils.cpython-310-aarch64-linux-gnu.so
librealsense2-gl.so.2.54                              
pyrsutils.cpython-310-aarch64-linux-gnu.so.2.54
librealsense2-gl.so.2.54.2                           
pyrsutils.cpython-310-aarch64-linux-gnu.so.2.54.2
librealsense2.so
librealsense2.so.2.54
librealsense2.so.2.54.2
pybackend2.cpython-310-aarch64-linux-gnu.so
pybackend2.cpython-310-aarch64-linux-gnu.so.2
pybackend2.cpython-310-aarch64-linux-gnu.so.2.54.2
pyrealsense2.cpython-310-aarch64-linux-gnu.so
pyrealsense2.cpython-310-aarch64-linux-gnu.so.2.54
pyrealsense2.cpython-310-aarch64-linux-gnu.so.2.54.2

python调用realsense深度相机测试代码

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()

当能够出现画面即说明安装成功

报错1

Pyrealsense2-D435i:frames = pipeline.wait_for_frames() RuntimeError: Frame didn't arrival within 5000 #12462

pyrealsense2库不适用于arm架构树莓派,所以pip install pyrealsense2无法安装。

参考:Pyrealsense2-D435i:frames = pipeline.wait_for_frames() RuntimeError: Frame didn't arrive within 5000 · Issue #12462 · IntelRealSense/librealsense (github.com)

报错2

 Could NOT find OpenSSL, try to set the path to OpenSSL root folder in the

解决方案

 sudo apt-get install libssl-dev

报错3

GnuTLS recv error (-110): The TLS connection was non-properly terminated.

参考:解决 git clone 时遇到的一些问题_gnutls recv error (-110): the tls connection was n-CSDN博客

报错4

CMake Error at CMakeLists.txt:5 (project):
   No CMAKE_CXX_COMPILER could be found.
 ​
   Tell CMake where to find the compiler by setting either the environment
   variable "CXX" or the CMake cache entry CMAKE_CXX_COMPILER to the full path
   to the compiler, or to the compiler name if it is in the PATH.

解决方案

 sudo apt install -y build-essential

参考:解决 No CMAKE_CXX_COMPILER could be found问题_cmake error at cmakelists.txt:2 (project): no cmak-CSDN博客

报错5

 /home/ubuntu/librealsense/build/third-party/pybind11/include/pybind11/detail/common.h:266:10: fatal error: Python.h: No such file or directory
   266 | #include <Python.h>

解决方案

sudo apt-get install python3-dev

  • 21
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Realsense是由英特尔开发的一套深度摄像头和软件开发包(SDK),用于实时获取三维图像和深度信息。在Python使用Realsense,你需要安装pyrealsense库。 首先,确保你已经安装了Realsense深度摄像头的驱动程序和SDK。然后,你可以使用pip安装pyrealsense库: ``` pip install pyrealsense2``` 安装完成后,你可以通过以下代码示例来使用Realsense摄像头获取深度图像和彩色图像: ```pythonimport pyrealsense2 as rsimport numpy as npimport cv2# 初始化摄像头pipeline = rs.pipeline() config = rs.config() config.enable_stream(rs.stream.color,640,480, rs.format.bgr8,30) config.enable_stream(rs.stream.depth,640,480, rs.format.z16,30) pipeline.start(config) try: while True: # 等待下一帧图像 frames = pipeline.wait_for_frames() color_frame = frames.get_color_frame() depth_frame = frames.get_depth_frame() if not color_frame or not depth_frame: continue # 将深度图像和彩色图像转换为数组 color_image = np.asanyarray(color_frame.get_data()) depth_image = np.asanyarray(depth_frame.get_data()) # 显示彩色图像和深度图像 cv2.imshow("Color Image", color_image) cv2.imshow("Depth Image", depth_image) # 按下ESC键退出循环 if cv2.waitKey(1) ==27: breakfinally: # 关闭摄像头 pipeline.stop() cv2.destroyAllWindows() ``` 这是一个简单的示例,用于获取并显示Realsense深度摄像头的彩色图像和深度图像。你可以根据自己的需求进一步扩展功能。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值