Nvidia Jetson nano 研究系列之三-安装python3的opencv4.1

0.576字数 1275阅读 1779

1、opencv4.1新特性

opencv4.0重大版本更新

重回英特尔的OpenCV终于迎来一次大版本更新,增加了诸多新特性。

因为OpenCV最开始开发时的语言是C,导致许多模块无法利用C++ 11的良好特性,OpenCV 4.0版本的一个重要使命就是去除C语言风格的接口,使其完全支持C++ 11!

1.大量从OpenCV 1.x遗留的C语言分割的API接口被删掉,主要影响的模块是objdetect, photo, video, videoio, imgcodecs, calib3d。

2.在core模块中的持久化(在XML,YAML或JSON中存储和加载结构化数据)已用完全用 C ++ 重新实现,并去除了相应的 C 风格的接口。 目前,base64支持尚未完成(仅支持加载base64编码的XML和YAML,还未支持编码)。 现在,存储在FileNode中的序列的随机访问是O(N)操作; 使用cv :: FileNodeIterator能够更快地进行顺序访问。 速度上,FileStorage的加载比以前的实现少了3-6倍的内存!

3.编译新版OpenCV现在需要支持 C++ 11 的编译器。

DNN(深度神经网络)模块是目前OpenCV更新最重要的模块!

1.增加Mask-RCNN模型支持。

其使用指南:

https://github.com/opencv/opencv/wiki/TensorFlow-Object-Detection-API

Python例子:https://github.com/opencv/opencv/blob/master/samples/dnn/mask_rcnn.py

52CV曾经报道过的例子:

OpenCV4.0 Mask RCNN 实例分割示例 C++/Python实现

2.集成ONNX解析器。 支持多个流行的分类网络。 部分支持YOLO对象检测网络(YOLO的ONNX版本缺少一些提供矩形列表的最终图层)。

3.通过引入Intel DLDT 改进DNN模块速度。

Intel DLDT近期已经开源,相见:

https://software.intel.com/openvino-toolkit

4.API更改:默认情况下,blobFromImage方法不会交换R和B通道,也不会对输入图像进行裁剪。 而且,此API更改也已添加到OpenCV 3.4分支。

5.为不支持OpenCL而支持Vulkan的硬件平台添加了实验性质的Vulkan后端(还在开发中!不建议大家使用——52CV君)。

6.为OpenCV支持的最流行的深度学习网络添加了快捷方式。 可以通过指定模型的别名,跳过预处理参数甚至模型的路径!

比如:

python object_detection.py --model opencv_face_detector.caffemodel --config opencv_face_detector.prototxt --mean 104 177 123 --width 300 --height 300

可以直接被替代为:

python object_detection.py opencv_fd

7.修复了AMD和NVIDIA GPU上的OpenCL加速。 现在,可以为模型启用DNN_TARGET_OPENCL,开启OpenCL支持。 请注意,DNN_TARGET_OPENCL_FP16仅在英特尔GPU上进行测试,因此仍需要额外的flags。(这个善意的提醒也就意味着你较好不要用那两家的显卡玩OpenCV

G-API为算法的硬件优化加速提供计算图支持!

opencv_gapi是全新添加的模块, 它是非常有效的图像处理引擎。开发者可以在程序中定义多个图像处理步骤组成的图,执行的时候进行优化,使得fast and portable。

非常看好这一模块的应用!

“加速”是OpenCV开发永恒不变的主题!

每次发布这块的说明都差不多,总结起来就是一句话:

我们很努力地给你们在CPU的指令级上进行了加速,只要是支持该指令的CPU尽量让你们用上,而且我们还支持NEON。

opencv4.1常规更新

DNN 模块改进

  1. 降低部分模型的峰值内存使用,最多降低30%;

  2. 推断引擎:

2.1 目前最小支持版本是Inference Engine 2018R3;

2.2 支持新发布的Myriad X (Intel® Neural Compute Stick 2) ;

2.3 Automatic IR network reshaping for different inputs;

2.4 改进了 使用 OpenVINO Open Model Zoo 中模型的例子;

  1. 支持TensorFlow 目标检测 API的新网络: Faster-RCNNs, SSDs and Mask-RCNN with dilated convolutions, FPN SSD;
    性能改进

  2. 支持AVX2指令集的更多优化;

  3. 从core 到 imgproc的大量函数现在支持运行时调度(Automatic runtime dispatching);

Android 支持改进

VideoCapture C++ 接口支持 Android Media NDK (21+) video I/O file capture back-end(https://github.com/opencv/opencv/pull/14005

2、jetson nano安装脚本

先把之前教程安装的python3-opencv 3.2.0卸载掉;

root@jetson-desktop:~# sudo apt-get remove python3-opencv
正在读取软件包列表... 完成
正在分析软件包的依赖关系树       
正在读取状态信息... 完成       
下列软件包是自动安装的并且现在不需要了:
  apt-clone archdetect-deb busybox-static cryptsetup-bin dpkg-repack gdal-data gir1.2-timezonemap-1.0 gir1.2-xkl-1.0 grub-common kde-window-manager kinit kio kpackagetool5 kwayland-data kwin-common kwin-data kwin-x11 libaec0 libarmadillo8
  libarpack2 libcharls1 libdap25 libdapclient6v5 libdebian-installer4 libepsilon1 libfreexl1 libfyba0 libgdal20 libgdcm2.8 libgeos-3.6.2 libgeos-c1v5 libgeotiff2 libgif7 libgl2ps1.4 libhdf4-0-alt libhdf5-100 libhdf5-openmpi-100 libjsoncpp1
  libkdecorations2-5v5 libkdecorations2private5v5 libkf5activities5 libkf5attica5 libkf5completion-data libkf5completion5 libkf5declarative-data libkf5declarative5 libkf5doctools5 libkf5globalaccel-data libkf5globalaccel5
  libkf5globalaccelprivate5 libkf5idletime5 libkf5jobwidgets-data libkf5jobwidgets5 libkf5kcmutils-data libkf5kcmutils5 libkf5kiocore5 libkf5kiontlm5 libkf5kiowidgets5 libkf5newstuff-data libkf5newstuff5 libkf5newstuffcore5
  libkf5package-data libkf5package5 libkf5plasma5 libkf5quickaddons5 libkf5solid5 libkf5solid5-data libkf5sonnet5-data libkf5sonnetcore5 libkf5sonnetui5 libkf5textwidgets-data libkf5textwidgets5 libkf5waylandclient5 libkf5waylandserver5
  libkf5xmlgui-bin libkf5xmlgui-data libkf5xmlgui5 libkmlbase1 libkmldom1 libkmlengine1 libkscreenlocker5 libkwin4-effect-builtins1 libkwineffects11 libkwinglutils11 libkwinxrenderutils11 liblept5 libminizip1 libnetcdf-c++4 libnetcdf13
  libodbc1 libogdi3.2 libopencv-calib3d3.2 libopencv-contrib3.2 libopencv-core3.2 libopencv-features2d3.2 libopencv-flann3.2 libopencv-highgui3.2 libopencv-imgcodecs3.2 libopencv-imgproc3.2 libopencv-ml3.2 libopencv-objdetect3.2
  libopencv-photo3.2 libopencv-shape3.2 libopencv-stitching3.2 libopencv-superres3.2 libopencv-video3.2 libopencv-videoio3.2 libopencv-videostab3.2 libopencv-viz3.2 libpq5 libproj12 libqgsttools-p1 libqhull7 libqt5designer5 libqt5help5
  libqt5multimedia5 libqt5multimedia5-plugins libqt5multimediaquick-p5 libqt5multimediawidgets5 libqt5opengl5 libqt5positioning5 libqt5printsupport5 libqt5qml5 libqt5quick5 libqt5quickwidgets5 libqt5sensors5 libqt5sql5 libqt5test5
  libqt5webchannel5 libqt5webkit5 libsocket++1 libspatialite7 libsuperlu5 libsz2 libtcl8.6 libtesseract4 libtk8.6 liburiparser1 libvtk6.3 libxcb-composite0 libxcb-cursor0 libxcb-damage0 libxerces-c3.2 odbcinst odbcinst1debian2 os-prober
  proj-bin proj-data python3-dbus.mainloop.pyqt5 python3-icu python3-numpy python3-pam python3-pyqt5 python3-pyqt5.qtsvg python3-pyqt5.qtwebkit python3-sip qml-module-org-kde-kquickcontrolsaddons qml-module-qtmultimedia qml-module-qtquick2
  rdate
使用'sudo apt autoremove'来卸载它(它们)。
下列软件包将被【卸载】:
  python3-opencv
升级了 0 个软件包,新安装了 0 个软件包,要卸载 1 个软件包,有 10 个软件包未被升级。
解压缩后将会空出 2,573 kB 的空间。
您希望继续执行吗? [Y/n] y
(正在读取数据库 ... 系统当前共安装有 135006 个文件和目录。)
正在卸载 python3-opencv (3.2.0+dfsg-4ubuntu0.1) ...

opencv4.1安装脚本

#!/bin/bash
#
# Copyright (c) 2018, NVIDIA CORPORATION.  All rights reserved.
#
# NVIDIA Corporation and its licensors retain all intellectual property
# and proprietary rights in and to this software, related documentation
# and any modifications thereto.  Any use, reproduction, disclosure or
# distribution of this software and related documentation without an express
# license agreement from NVIDIA Corporation is strictly prohibited.
#

if [ "$#" -ne 1 ]; then
    echo "Usage: $0 <Install Folder>"
    exit
fi
folder="$1"

echo "** Install requirement"
sudo apt-get update
sudo apt-get install -y build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install -y libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev
sudo apt-get install -y python2.7-dev python3.6-dev python-dev python-numpy python3-numpy
sudo apt-get install -y libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
sudo apt-get install -y libv4l-dev v4l-utils qv4l2 v4l2ucp
sudo apt-get install -y curl
sudo apt-get update

echo "** Download opencv-4.1.0"
cd $folder
curl -L https://github.com/opencv/opencv/archive/4.1.0.zip -o opencv-4.1.0.zip
curl -L https://github.com/opencv/opencv_contrib/archive/4.1.0.zip -o opencv_contrib-4.1.0.zip
unzip opencv-4.1.0.zip 
unzip opencv_contrib-4.1.0.zip 
cd opencv-4.1.0/

echo "** Building..."
mkdir release
cd release/
cmake \
  -D CMAKE_BUILD_TYPE=RELEASE \
  -D CMAKE_INSTALL_PREFIX=/usr/local \
  -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-4.1.0/modules \
  -D CUDA_ARCH_BIN=5.3 \
  -D CUDA_ARCH_PTX="" \
  -D WITH_CUDA=ON \
  -D WITH_TBB=ON \
  -D BUILD_opencv_python3=ON \
  -D BUILD_TESTS=OFF \
  -D BUILD_PERF_TESTS=OFF \
  -D WITH_V4L=ON \
  -D BUILD_EXAMPLES=OFF \
  -D WITH_OPENGL=ON \
  -D ENABLE_FAST_MATH=1 \
  -D CUDA_FAST_MATH=1 \
  -D WITH_CUBLAS=1 \
  -D WITH_NVCUVID=ON \
  -D WITH_GSTREAMER=ON \
  -D WITH_OPENCL=YES \
  -D BUILD_opencv_cudacodec=OFF ..
make -j3
sudo make install
echo "** Install opencv-4.1.0 successfully"
echo "** Bye :)"

在home目录下新建opencv4.1.0_install.sh,授权0777让其可执行;
然后执行即可;参数为解压文件的目录;

root@jetson-desktop:/home# ./opencv4.1.0_install.sh  /home/software

获取:7 file:/var/visionworks-sfm-repo  Release [2,003 B]                               
获取:8 file:/var/visionworks-tracking-repo  Release [2,008 B]                                                   
命中:9 http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports bionic InRelease                                        
命中:10 http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports bionic-security InRelease   
命中:12 http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports bionic-updates InRelease
命中:13 http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports bionic-backports InRelease
正在读取软件包列表... 完成    
** Download opencv-4.1.0
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   117    0   117    0     0    123      0 --:--:-- --:--:-- --:--:--   123

下载文件时间较长,耐心等待

编译过程摘录;

-- Performing Test HAVE_CXX_WNO_UNUSED_BUT_SET_VARIABLE
-- Performing Test HAVE_CXX_WNO_UNUSED_BUT_SET_VARIABLE - Success
-- Excluding from source files list: modules/imgproc/src/corner.avx.cpp
-- Excluding from source files list: modules/imgproc/src/imgwarp.avx2.cpp
-- Excluding from source files list: modules/imgproc/src/imgwarp.sse4_1.cpp
-- Excluding from source files list: modules/imgproc/src/resize.avx2.cpp
-- Excluding from source files list: modules/imgproc/src/resize.sse4_1.cpp
-- Excluding from source files list: modules/imgproc/src/sumpixels.avx512_skx.cpp
-- Excluding from source files list: <BUILD>/modules/dnn/layers/layers_common.avx.cpp
-- Excluding from source files list: <BUILD>/modules/dnn/layers/layers_common.avx2.cpp
-- Excluding from source files list: <BUILD>/modules/dnn/layers/layers_common.avx512_skx.cpp
-- freetype2:   YES
-- harfbuzz:    YES
-- Excluding from source files list: modules/features2d/src/fast.avx2.cpp
-- Performing Test HAVE_CXX_WNO_OVERLOADED_VIRTUAL
-- Performing Test HAVE_CXX_WNO_OVERLOADED_VIRTUAL - Success
-- Checking for modules 'tesseract;lept'
--   No package 'tesseract' found
--   No package 'lept' found
-- Tesseract:   NO
-- Excluding from source files list: modules/calib3d/src/undistort.avx2.cpp
-- xfeatures2d/boostdesc: Download: boostdesc_bgm.i
-- xfeatures2d/boostdesc: Download: boostdesc_bgm_bi.i
-- xfeatures2d/boostdesc: Download: boostdesc_bgm_hd.i
-- xfeatures2d/boostdesc: Download: boostdesc_binboost_064.i
-- xfeatures2d/boostdesc: Download: boostdesc_binboost_128.i
-- xfeatures2d/boostdesc: Download: boostdesc_binboost_256.i
-- xfeatures2d/boostdesc: Download: boostdesc_lbgm

如果从github上下载太慢,请用迅雷什么的下载后上传到板子;
请屏蔽sh文件中下载的语句;

# curl -L https://github.com/opencv/opencv/archive/4.1.0.zip -o opencv-4.1.0.zip
# curl -L https://github.com/opencv/opencv_contrib/archive/4.1.0.zip -o opencv_contrib-4.1.0.zip

如果编译不成功,把脚本中make -j3 改成make -j1

[100%] Built target gen_opencv_python_source
[100%] Building CXX object modules/python2/CMakeFiles/opencv_python2.dir/__/src2/cv2.cpp.o
[100%] Linking CXX shared module ../../lib/cv2.so
[100%] Built target opencv_python2
[100%] Building CXX object modules/python3/CMakeFiles/opencv_python3.dir/__/src2/cv2.cpp.o
[100%] Linking CXX shared module ../../lib/python3/cv2.cpython-36m-aarch64-linux-gnu.so
[100%] Built target opencv_python3
[100%] Built target opencv_annotation
[100%] Built target opencv_visualisation
[100%] Built target opencv_interactive-calibration
[100%] Built target opencv_version


查看安装结果,已然是opencv4.1。

root@jetson-desktop:/home/software/opencv-4.1.0/release# python3
Python 3.6.7 (default, Oct 22 2018, 11:32:17) 
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> print(cv2.__version__)
4.1.0
>>> 


推荐阅读更多精彩内容