我刷机的系统是 Jetpack 4.5.1,默认已经安装了 opencv4.4.1,为了重新安装 opencv 我先卸载掉默认安装的 opencv。
nvidia@nvidia-desktop:~$ sudo apt purge libopencv*
再来看一下,opencv 已经被完全卸载了。
接下来就简单了,直接用热心人士直接写好的脚本进行安装,哈哈。
https://github.com/jetsonhacks/buildOpenCVXavier.git
nvidia@nvidia-desktop:~/zuosi/repository/buildOpenCVXavier-master$ ./buildOpenCV.sh
.....漫长的等待......
-- -----------------------------------------------------------------
-- General configuration for OpenCV 3.4.3 =====================================
-- Version control: 3.4.3-dirty
--
-- Platform:
-- Timestamp: 2021-04-13T11:35:27Z
-- Host: Linux 4.9.201-tegra aarch64
-- CMake: 3.10.2
-- CMake generator: Unix Makefiles
-- CMake build tool: /usr/bin/make
-- Configuration: RELEASE
--
-- CPU/HW features:
-- Baseline: NEON FP16
-- required: NEON
-- disabled: VFPV3
--
-- C/C++:
-- Built as dynamic libs?: YES
-- C++11: YES
-- C++ Compiler: /usr/bin/c++ (ver 7.5.0)
-- C++ flags (Release): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wsuggest-override -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG -DNDEBUG
-- C++ flags (Debug): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wsuggest-override -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -fvisibility=hidden -fvisibility-inlines-hidden -g -O0 -DDEBUG -D_DEBUG
-- C Compiler: /usr/bin/cc
-- C flags (Release): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-narrowing -Wno-comment -Wimplicit-fallthrough=3 -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -fvisibility=hidden -O3 -DNDEBUG -DNDEBUG
-- C flags (Debug): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-narrowing -Wno-comment -Wimplicit-fallthrough=3 -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -fvisibility=hidden -g -O0 -DDEBUG -D_DEBUG
-- Linker flags (Release):
-- Linker flags (Debug):
-- ccache: YES
-- Precompiled headers: NO
-- Extra dependencies: m pthread /usr/lib/aarch64-linux-gnu/libGL.so /usr/lib/aarch64-linux-gnu/libGLU.so cudart_static -lpthread dl rt nppc nppial nppicc nppicom nppidei nppif nppig nppim nppist nppisu nppitc npps cublas cufft -L/usr/local/cuda/lib64 -L/usr/lib/aarch64-linux-gnu
-- 3rdparty dependencies:
--
-- OpenCV modules:
-- To be built: calib3d core cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev dnn features2d flann highgui imgcodecs imgproc java_bindings_generator ml objdetect photo python2 python3 python_bindings_generator shape stitching superres ts video videoio videostab viz
-- Disabled: js world
-- Disabled by dependency: -
-- Unavailable: java
-- Applications: tests perf_tests apps
-- Documentation: NO
-- Non-free algorithms: NO
--
-- GUI:
-- QT: YES (ver 5.9.5)
-- QT OpenGL support: YES (Qt5::OpenGL 5.9.5)
-- GTK+: NO
-- OpenGL support: YES (/usr/lib/aarch64-linux-gnu/libGL.so /usr/lib/aarch64-linux-gnu/libGLU.so)
-- VTK support: YES (ver 6.3.0)
--
-- Media I/O:
-- ZLib: /usr/lib/aarch64-linux-gnu/libz.so (ver 1.2.11)
-- JPEG: /usr/lib/aarch64-linux-gnu/libjpeg.so (ver 80)
-- WEBP: /usr/lib/aarch64-linux-gnu/libwebp.so (ver encoder: 0x020e)
-- PNG: /usr/lib/aarch64-linux-gnu/libpng.so (ver 1.6.34)
-- TIFF: /usr/lib/aarch64-linux-gnu/libtiff.so (ver 42 / 4.0.9)
-- JPEG 2000: build (ver 1.900.1)
-- OpenEXR: build (ver 1.7.1)
-- HDR: YES
-- SUNRASTER: YES
-- PXM: YES
--
-- Video I/O:
-- DC1394: YES (ver 2.2.5)
-- FFMPEG: YES
-- avcodec: YES (ver 58.135.100)
-- avformat: YES (ver 58.77.100)
-- avutil: YES (ver 56.72.100)
-- swscale: YES (ver 5.10.100)
-- avresample: YES (ver 3.7.0)
-- GStreamer:
-- base: YES (ver 1.14.5)
-- video: YES (ver 1.14.5)
-- app: YES (ver 1.14.5)
-- riff: YES (ver 1.14.5)
-- pbutils: YES (ver 1.14.5)
-- libv4l/libv4l2: 1.14.2 / 1.14.2
-- v4l/v4l2: linux/videodev2.h
--
-- Parallel framework: TBB (ver 2017.0 interface 9107)
--
-- Trace: YES (built-in)
--
-- Other third-party libraries:
-- Lapack: YES (/usr/lib/aarch64-linux-gnu/liblapack.so /usr/lib/aarch64-linux-gnu/libcblas.so /usr/lib/aarch64-linux-gnu/libatlas.so)
-- Eigen: YES (ver 3.3.4)
-- Custom HAL: YES (carotene (ver 0.0.1))
-- Protobuf: build (3.5.1)
--
-- NVIDIA CUDA: YES (ver 10.2, CUFFT CUBLAS NVCUVID FAST_MATH)
-- NVIDIA GPU arch: 72
-- NVIDIA PTX archs:
--
-- OpenCL: YES (no extra features)
-- Include path: /home/nvidia/opencv/3rdparty/include/opencl/1.2
-- Link libraries: Dynamic load
--
-- Python 2:
-- Interpreter: /usr/bin/python2.7 (ver 2.7.17)
-- Libraries: /usr/lib/aarch64-linux-gnu/libpython2.7.so (ver 2.7.17)
-- numpy: /usr/lib/python2.7/dist-packages/numpy/core/include (ver 1.13.3)
-- packages path: lib/python2.7/dist-packages
--
-- Python 3:
-- Interpreter: /usr/bin/python3 (ver 3.6.9)
-- Libraries: /usr/lib/aarch64-linux-gnu/libpython3.6m.so (ver 3.6.9)
-- numpy: /usr/lib/python3/dist-packages/numpy/core/include (ver 1.13.3)
-- packages path: lib/python3.6/dist-packages
--
-- Python (for build): /usr/bin/python2.7
--
-- Java:
-- ant: NO
-- JNI: NO
-- Java wrappers: NO
-- Java tests: NO
--
-- Matlab: NO
--
-- Install to: /usr/local
-- -----------------------------------------------------------------
这是安装完后给出的支持选项,你看 ffmpeg 也有了,cuda 也有了,gstreamer 也有了!
nvidia@nvidia-desktop:~$ sudo ldconfig
[sudo] password for nvidia:
nvidia@nvidia-desktop:~$ opencv_version
3.4.3
nvidia@nvidia-desktop:~/usb/zuosi$ pkg-config --cflags --libs opencv
-I/usr/local/include/opencv -I/usr/local/include -L/usr/local/lib -lopencv_cudabgsegm -lopencv_cudaobjdetect -lopencv_cudastereo -lopencv_dnn -lopencv_ml -lopencv_shape -lopencv_stitching -lopencv_cudafeatures2d -lopencv_superres -lopencv_cudacodec -lopencv_videostab -lopencv_cudaoptflow -lopencv_cudalegacy -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_photo -lopencv_imgcodecs -lopencv_cudawarping -lopencv_cudaimgproc -lopencv_cudafilters -lopencv_video -lopencv_objdetect -lopencv_imgproc -lopencv_flann -lopencv_cudaarithm -lopencv_viz -lopencv_core -lopencv_cudev
【测试】
测试就直接用 opencv 提供的样例程序就可以了,上面的安装脚本将 opencv 克隆在了/home/nvidia/opencv 目录下,其中包含 samples 目录即 opencv 提供的样例代码。
我把 samples 下面的样例程序编译一下。
nvidia@nvidia-desktop:~/opencv/samples$ mkdir build
nvidia@nvidia-desktop:~/opencv/samples$ cd build
nvidia@nvidia-desktop:~/opencv/samples$ cmake ..
nvidia@nvidia-desktop:~/opencv/samples$ make -j8
nvidia@nvidia-desktop:~/opencv/samples/build$ ll
total 184
drwxrwxr-x 7 nvidia nvidia 4096 4月 14 11:33 ./
drwxrwxr-x 20 nvidia nvidia 4096 4月 14 11:33 ../
-rw-rw-r-- 1 nvidia nvidia 23416 4月 14 11:33 CMakeCache.txt
drwxrwxr-x 4 nvidia nvidia 4096 4月 14 11:36 CMakeFiles/
-rw-rw-r-- 1 nvidia nvidia 1891 4月 14 11:33 cmake_install.cmake
drwxrwxr-x 3 nvidia nvidia 12288 4月 14 11:36 cpp/
drwxrwxr-x 3 nvidia nvidia 4096 4月 14 11:36 dnn/
-rw-rw-r-- 1 nvidia nvidia 121707 4月 14 11:33 Makefile
drwxrwxr-x 3 nvidia nvidia 4096 4月 14 11:33 opencl/
drwxrwxr-x 3 nvidia nvidia 4096 4月 14 11:36 tapi/
咦,samples 下面不是有很多目录么,怎么编译完后只有 cpp,dnn,tapi 这少数几个目录呢?
那是因为 CMakeLists.txt 中默认把一些目录给注释了,你可以去掉注释,例如我去掉了 gpu,因为等会想测试一下。
然后重新编译就可以看到编译好的 gpu 样例了。
1. 先来测试一下基本的图片显示功能
nvidia@nvidia-desktop:~/opencv/samples/build$ cd cpp
nvidia@nvidia-desktop:~/opencv/samples/build/cpp$ ./example_cpp_image ../../data/home.jpg
可以显示!
2. 再来测试一下 gpu 的解码功能
nvidia@nvidia-desktop:~/opencv/samples/build$ cd gpu
nvidia@nvidia-desktop:~/opencv/samples/build/gpu$ ./example_gpu_video_reader /home/nvidia/usb/zuosi/test-videos/desert-1.mp4
Segmentation fault (core dumped)
gpu 解码失败,这个平台不支持!
【附录】
安装过程中遇到的问题
参考这篇博客,修改下列文件
modules/cudacodec/src/precomp.hpp
modules/cudacodec/src/frame_queue.hpp
modules/cudacodec/src/cuvid_video_source.hpp
modules/cudacodec/src/video_decoder.hpp
modules/cudacodec/src/video_parser.hpp
将代码中的:
#if CUDA_VERSION >= 9000
#include
#else
#include
#endif
修改为:
#if CUDA_VERSION >= 9000 && CUDA_VERSION < 10000
#include
#else
#include
#endif
因为我装的是 cuda 10.2,其中提供的是 nvcuvid.h 文件,而非 dynlink_nvcuvid.h。