ubuntu22.04@Jetson Orin Nano之OpenCV安装
- 1. 源由
- 2. 分析
- 3. 证实
- 3.1 jtop安装
- 3.2 jtop指令
- 3.3 GPU支持情况
- 4. 安装OpenCV
- 4.1 修改内容
- 4.2 Python2环境【不需要】
- 4.3 ubuntu22.04环境
- 4.4 国内/本地环境问题
- 4.5 cudnn版本问题
- 5. 总结
- 6. 参考资料
1. 源由
昨天用Jetson跑demo程序发现帧率很慢(只有10FPS左右),按照视频文件怎么说应该有30FPS。但是为什么jetson orin nano跑不起来呢???
dnn_object_detection_embedded_device
想着,估计是GPU没有跑起来,正好凑着调试了下板子,记录、整理下资料。
2. 分析
从现象上看,OpenCV运行的时候没有跑GPU,从而导致高价买的Jetson Orin Nano连这么简单的Demo都跑不起来。
接下来,就先看下板子运行的环境:
- ubuntu22.04/jammy/aarch64
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 22.04.4 LTS
Release: 22.04
Codename: jammy$ uname -a
Linux daniel-nvidia 5.15.122-tegra #1 SMP PREEMPT Mon Dec 18 21:24:25 PST 2023 aarch64 aarch64 aarch64 GNU/Linux
- OpenCV版本:4.8.0
$ opencv_version
4.8.0$ python3
Python 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> print(cv2.__version__)
4.8.0
>>>
- 安装最新OpenCV4.9.0版本
- 与之前《ubuntu22.04@laptop OpenCV安装》版本一致
- 安装或者升级Jetson Orin Nano上OpenCV4.8.0版本
- 官方论坛讨论:NVIDIA: install OpenCV for python3 in Jetson Nano
- Google的网上资料:Install OpenCV on Jetson Nano
- Google的教学视频
OpenCV with CUDA in Python on Jetson
3. 证实
要正式是否真的是OpenCV库对GPU的支持问题导致前面验证FPS帧率低,那么就需要通过jtop
指令来查看。
有朋友问:你怎么知道的这些?
Ans: 其实我并不知道,是我搜出来的。其实就这么简单,并不是我知道的多,是网上这些问题很多人都已经早就解决了。即使真的有没有人解决的,那么也是有办法的,只不过思路和这个不太一样。
- NVIDIA: Jtop install fail
- NVIDIA: Where can I find these numbers in CUDA_ARCH_BIN?
- NVIDIA: Your GPU Compute Capability
- NVIDIA: OpenCV build script
3.1 jtop安装
$ pip3 --version
$ sudo -H pip3 install --no-cache-dir jetson-stats
$ sudo systemctl restart jtop.service
$ sudo reboot
注:一定要重启板子,否则无效。
3.2 jtop指令
$ jtop -h
usage: jtop [-h] [--health] [--error-log] [--no-warnings] [--restore] [--loop] [--color-filter] [-r REFRESH] [-p PAGE] [-v]jtop is system monitoring utility and runs on terminaloptions:-h, --help show this help message and exit--health Status jtop and fix (default: False)--error-log Generate a log for GitHub (default: False)--no-warnings Do not show warnings (default: False)--restore Reset Jetson configuration (default: False)--loop Automatically switch page every 5s (default: False)--color-filter Change jtop base colors, you can use also JTOP_COLOR_FILTER=True (default: False)-r REFRESH, --refresh REFRESHrefresh interval (default: 1000)-p PAGE, --page PAGE Open fix page (default: 1)-v, --version show program's version number and exit
3.3 GPU支持情况
OpenCV: 4.8.0 with CUDA: NO,所以该版本不支持GPU。
4. 安装OpenCV
两个链接脚本供参考安装:
- JetsonHacksNano/buildOpenCV
- mdegans/nano_build_opencv
为了更好的配合我们的例子,就整合到SnapLearnOpenCV/scripts中供大家参考,上述脚本支持:
- 笔记本电脑(64位普通CPU)安装:
install_opencv_for_laptop.sh
– ubuntu22.04@laptop OpenCV安装 - AI最火的(Jetson Orin Nano)安装:
install_opencv_for_jetson.sh
4.1 修改内容
$ git diff
diff --git a/build_opencv.sh b/build_opencv.sh
index c12bb40..891eea3 100755
--- a/build_opencv.sh
+++ b/build_opencv.sh
@@ -45,8 +45,8 @@ setup () {git_source () {echo "Getting version '$1' of OpenCV"
- git clone --depth 1 --branch "$1" https://github.com/opencv/opencv.git
- git clone --depth 1 --branch "$1" https://github.com/opencv/opencv_contrib.git
+ git clone --depth 1 --branch "$1" git@github.com:opencv/opencv.git
+ git clone --depth 1 --branch "$1" git@github.com:opencv/opencv_contrib.git}install_dependencies () {
@@ -63,9 +63,8 @@ install_dependencies () {libatlas-base-dev \libavcodec-dev \libavformat-dev \
- libavresample-dev \libcanberra-gtk3-module \
- libdc1394-22-dev \
+ libdc1394-dev \libeigen3-dev \libglew-dev \libgstreamer-plugins-base1.0-dev \
@@ -90,8 +89,6 @@ install_dependencies () {libxvidcore-dev \libx264-dev \pkg-config \
- python-dev \
- python-numpy \python3-dev \python3-numpy \python3-matplotlib \
@@ -103,14 +100,13 @@ install_dependencies () {configure () {local CMAKEFLAGS="-D BUILD_EXAMPLES=OFF
- -D BUILD_opencv_python2=ON-D BUILD_opencv_python3=ON-D CMAKE_BUILD_TYPE=RELEASE-D CMAKE_INSTALL_PREFIX=${PREFIX}-D CUDA_ARCH_BIN=5.3,6.2,7.2,8.7-D CUDA_ARCH_PTX=-D CUDA_FAST_MATH=ON
- -D CUDNN_VERSION='8.0'
+ -D CUDNN_VERSION='8.9'-D EIGEN_INCLUDE_PATH=/usr/include/eigen3-D ENABLE_NEON=ON-D OPENCV_DNN_CUDA=ON
4.2 Python2环境【不需要】
- python2环境:
python-dev
python-numpy
- OpenCV编译选项:
-D BUILD_opencv_python2=ON
4.3 ubuntu22.04环境
- 无
libavresample-dev
库 libdc1394-22-dev
变更为libdc1394-dev
4.4 国内/本地环境问题
- 将https链接调整为git库链接
- git clone --depth 1 --branch "$1" https://github.com/opencv/opencv.git
- git clone --depth 1 --branch "$1" https://github.com/opencv/opencv_contrib.git
+ git clone --depth 1 --branch "$1" git@github.com:opencv/opencv.git
+ git clone --depth 1 --branch "$1" git@github.com:opencv/opencv_contrib.git
- git库采用git协议才能下载
$ git config --global "url.ssh://git@ssh.github.com:443/.insteadOf" git@github.com:
4.5 cudnn版本问题
《Linux 36.2@Jetson Orin Nano基础环境构建》安装的是8.9.4.25.
- -D CUDNN_VERSION='8.0'
+ -D CUDNN_VERSION='8.9'
5. 总结
修改完上述脚本后,可以直接执行脚本指令:
$ git clone git@github.com:mdegans/nano_build_opencv.git
$ cd nano_build_opencv
$ nano build_opencv.sh
$ ./build_opencv.sh或者
$ git clone git@github.com:SnapDragonfly/SnapLearnOpenCV.git
$ cd SnapLearnOpenCV/scripts
$ ./install_opencv_for_jetson.sh
大致耗时约1个小时???没有计时,反正也挺快的。执行opencv_version
python
jtop
指令:
$ echo $PATH
/home/daniel/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin$ opencv_version
4.9.0$ python
Python 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> print(cv2.__version__)
4.9.0
>>> $ jtop
最后,关于使用GPU和使用CPU对Demo的影响:
dnn_object_detection_jetson_orin_nano
注:本次环境安装为遇到默认路径导致的优先级问题。
6. 参考资料
【1】ubuntu22.04@laptop OpenCV安装
【2】Linux 36.2@Jetson Orin Nano之基础环境构建
【3】ubuntu22.04@laptop OpenCV定制化安装
【4】Github操作网络异常笔记