使用jetpack安装的jetson,自带了opencv,但是没有cuda加速的,输入opencv_version
使用jtop查看,可以确认自带的opencv是没用cuda的
卸载opencv,先查看有哪些包
pip3 list | grep opencv
opencv-python
然后卸载掉python包以及lib库
pip3 uninstall opencv-python
sudo apt purge libopencv*
sudo apt autoremove
sudo apt update
安装后面编译需要用到的依赖库
sudo apt install -y build-essential checkinstall cmake pkg-config yasm git gfortran
sudo apt update
sudo apt install -y libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev
sudo apt install -y libjpeg8-dev libjasper-dev libpng12-dev libtiff5-dev libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev libxine2-dev libv4l-dev
sudo apt install -y libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev libgtk2.0-dev libtbb-dev libatlas-base-dev libfaac-dev libmp3lame-dev libtheora-dev libvorbis-dev libxvidcore-dev libopencore-amrnb-dev libopencore-amrwb-dev x264 v4l-utils
sudo apt install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
下载如下两个仓库
https://github.com/opencv/opencv/releases
https://github.com/opencv/opencv_contrib/releases
解压在同一个文件夹下面
cd opencv-4.5.5
mkdir build
cmake \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX=/usr/local \
-DOPENCV_ENABLE_NONFREE=1 \
-DBUILD_opencv_python2=1 \
-DBUILD_opencv_python3=1 \
-DWITH_FFMPEG=1 \
-DCUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda \
-DCUDA_ARCH_BIN=7.2 \
-DCUDA_ARCH_PTX=7.2 \
-DWITH_CUDA=1 \
-DENABLE_FAST_MATH=1 \
-DCUDA_FAST_MATH=1 \
-DWITH_CUBLAS=1 \
-DOPENCV_GENERATE_PKGCONFIG=1 \
-DOPENCV_EXTRA_MODULES_PATH=/home/jetson/Downloads/opencv-4.5.5/opencv_contrib-4.5.5/modules \
..
make -j6
sudo make install
可以看到cmake的时候,显示的一些配置信息
其中cv2的包将安装到/lib/python3.8/site-packages/cv2/python-3.8,如下为make的过程
如下为make install的过程
使用jtop可以看到opencv已经支持cuda加速了
但import cv2时,还是会报no module "cv2"的报错,到build目录下,找到python_load文件夹,这个文件夹就是cv2编译的python package
cd opencv-4.5.5/build/python_loader
sudo pip3 install .
再测试一下,发现和jtop看到的是一致的
jetson@ubuntu:~/Downloads/opencv-4.5.5/build/python_loader$ python3
Python 3.8.10 (default, Nov 22 2023, 10:22:35)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'4.5.5'
>>> quit()
Jetson Orin Nano安装OpenCV带cuda加速版本的全过程