Ubuntu20.04配置以及openvino的安装

ROBOMASTER设备配置指南(LC-视觉)

1.NUC配置

1.ubuntu系统(建议20.04)

一些常用的ubuntu命令:

sudo apt install 软件包名安装软件

cd /home/nuc/下载进入下载界面

用Tab可以自动补全目录

ls可以查找该目录下的文件(zzh为个人目录,建议nuclinux命名全为nuc)

zzh@zzh:~$ cd /home/zzh/下载/
zzh@zzh:~/下载$ ls
bin
cmake-3.22.1.tar.gz
cuda_11.1.0_455.23.05_linux.run
cuda_11.3.0_465.19.01_linux.run
l_openvino_toolkit_p_2021.4.752.tgz
Qv2ray-v2.7.0-linux-x64.AppImage
sunloginclient-11.0.0.36662-amd64.deb
torch-1.10.1+cu113-cp38-cp38-linux_x86_64.whl
torch-1.8.0+cu111-cp38-cp38-linux_x86_64.whl
torch-1.9.0+cu111-cp38-cp38-win_amd64.whl
Typora.tar.gz
v2ray-linux-64
v2ray-linux-64.zip
zzh@zzh:~/下载$ 

1.换源

ubuntu20.04换源:

1.备份原来的源,将以前的源备份一下,以防以后可以用的。

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

2.打开/etc/apt/sources.list文件,在前面添加如下条目,并保存。

sudo vim /etc/apt/sources.list(可将vim更换为自己熟悉的编辑器)

也可用sudo gedit /etc/apt/sources.listsudo apt install vim后再使用vim

#添加阿里源
deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse下载
deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
#添加清华源
deb https://mirrors.tuna下载.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse multiverse

建议将源文件内容删除后再添加

更新源搜索

sudo apt-get update

如出现依赖问题,解决方式如下:

sudo apt-get -f install

更新软件:

下载sudo apt-get upgrade

换源完成!

2.下载cmake

sudo apt install cmake版本估计为3.16,需要更高请搜索自行下载

2.opencv -openvino 版以及opencv-contrib版安装(先看bug解决)

​ 用openvino的原因,对opencv以及深度学习和神经网络提供加速,对于英特尔的cpu有着良好的支持,提高帧率和速度。contrib包包含了一些目标追踪的函数(目前用到的)

​ 现在以我的安装为列:opencv4.5.1+contrib

​ 获取opencv-vino

git clone --branch 4.5.1 https://github.com/opencv/opencv_contrib.git
git clone --branch 4.5.1-openvino https://github.com/opencv/opencv.git

​ 将opencv_contrib到opencv目录下

安装依赖项:

	sudo apt-get install cmake #如果已经安装过cmake,则该步骤省略
	sudo apt-get install build-essential libgtk2.0-dev libgtk-3-dev libavcodec-dev libavformat-dev 	libjpeg-dev libswscale-dev libtiff5-dev

安装python支持

sudo apt install python3-dev python3-numpy
sudo apt install libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev
sudo apt install libpng-dev libopenexr-dev libtiff-dev libwebp-dev

​ 进入opencv目录下

mkdir build & cd build
cmake ..
sudo make
sudo make install

没有科学上网有可能会很麻烦,建议科学上网

配置环境

将opencv库添加到系统路径

sudo gedit /etc/ld.so.conf.d/opencv.conf 

其中opencv.conf有可能是空白
在其中添加

/usr/local/lib

保存后执行以下命令使配置路径生效

sudo ldconfig 

之后配置bash

sudo gedit /etc/bash.bashrc 

在文件最后添加

PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig  
export PKG_CONFIG_PATH  

保存,执行如下命令使得配置生效

source /etc/bash.bashrc 

然后更新配置

sudo updatedb

如果

zzh@zzh:~/opencv/build$ sudo updatedb 
sudo: updatedb:找不到命令

sudo su进入root

apt-get install mlocate

eixt退出root

再执行sudo updatedb

3.安装openvino

1.从官网注册并下载OpenVINO开发包的Linux版本

https://software.intel.com/content/www/us/en/develop/tools/openvino-toolkit/choose-download/linux.html

保存文件,在下载中可以看到openvino文件,后缀名为tar的压缩包

在下载中执行

tar -xvzf l_openvino_toolkit_p_2021.4.752.tgz

可以重命名文件夹为openvino并将其放置在主文件夹下面

cd openvino

sudo ./install_GUI.sh

一路next安装即可,安装文件将位于/opt/intel/openvino_2021.4.752/,同时会生成一个符号链接/opt/intel/openvino_2021 指向最新的安装目录。

zzh@zzh:~$ cd /opt/intel/openvino_2021.4.752/
zzh@zzh:/opt/intel/openvino_2021.4.752$ ls
bin               documentation         licensing                     python
data_processing   inference_engine      opencv
deployment_tools  install_dependencies  openvino_toolkit_uninstaller
2.安装依赖包
  1. 使用 OpenVINO 写一个完整的视觉类应用,除了 OpenVINO 本身之外,还需要安装一些依赖包,包括但不限于 FFMpeg视频框架、CMake 编译工具、libusb(Movidius 神经计算棒 插件需要用到)等。
    安装步骤如下:
    cd /opt/intel/openvino_2021/install_dependencies

    运行以下命令安装必要的依赖包:
    sudo -E ./install_openvino_dependencies.sh

    设置环境变量:
    source /opt/intel/openvino/bin/setupvars.sh
    建议将以上环境变量设置命令加入到用户的环境脚本当中,方法如下:
    vi <用户目录>/.bashrc,在末尾加入source /opt/intel/openvino/bin/setupvars.sh
    按 Esc 键,然后输入“:wq”保存并退出。

    感觉麻烦可以再开一个窗口,cd到地址用Tab补全,如:

    zzh@zzh:~$ cd /opt/intel/openvino_2021/bin/
    zzh@zzh:/opt/intel/openvino_2021/bin$ ls
    setupvars.sh
    

    接下来配置模型优化器,依次运行以下命令:

    cd /opt/intel/openvino_2021/deployment_tools/model_optimizer/install_prerequisites/

    sudo ./install_prerequisites

    上面这条命令会安装所有的深度学习框架的支持,如果只希望安装某一个框架的支持,以安装Caffe 框架支持为例,可以这么做:$ sudo ./install_prerequisites_caffe.sh

至此,安装工作结束,下面验证安装好的 OpenVINO 环境是否可以工作。

第二步:验证 OpenVINO 环境

进入推理引擎示例程序目录:
cd /opt/intel/openvino_2021.4.752/deployment_tools/demo/
如果要用GPU推理在命令后面加 -d GPU

运行图片分类示例程序的验证脚本:
./demo_squeezenet_download_convert_run.sh

./demo_security_barrier_camera.sh

如果一切顺利,输出结果将如图所示。

###################################################

Demo completed successfully.

因为openvino自带的opencv不包含contrib库,因此很多都不能用,所以我们要换成我们安装的opencv

cd /opt/intel/openvino_2021.4.752/bin

sudo chmod 777 setupvars.sh

vim setupvars.sh

if [ -e "$INSTALLDIR/opencv" ]; then
    if [ -f "$INSTALLDIR/opencv/setupvars.sh" ]; then
        source "$INSTALLDIR/opencv/setupvars.sh"
    else
        export OpenCV_DIR="$INSTALLDIR/home/zzh/opencv/share/OpenCV"
        export LD_LIBRARY_PATH="$INSTALLDIR/home/zzh/opencv/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}"
        export zzh@zzh:~/opencv/build$ opencv_version
4.5.1-openvino:LD_LIBRARY_PATH="$INSTALLDIR/home/zzh/opencv/share/OpenCV/3rdparty/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}"
    fi
fi

将其中的export OpenCV_DIR="$INSTALLDIR后面改为自己的opencv-openvino路径,凡是我上面加了/home/zzh/opencv的地方都变为自己的opencv-openvino路径

再次推理,运行demo查看是否可行。

如果显示缺失什么东西,可自行复制bug网上搜索如何debug

至此openvino下的opencv配置完成。

4.遇到的bug解决

1.在查找opencv的路径的时候

使用pkg-config --cflags opencv查找路径的时候会出现

Package opencv was not found in the pkg-config search path.
Perhaps you should add the directory containing `opencv.pc'
to the PKG_CONFIG_PATH environment variable
No package 'opencv' found

原因是在我们安装的opencv4之后在make的时候4以上的版本不会生成opencv.pc这个文件

1.解决方法一:手写opencv.pc(可行但没有成功)

首先创建opencv.pc文件,这里要注意它的路径信息:

cd /usr/local/lib
sudo mkdir pkgconfig
cd pkgconfig
sudo touch opencv.pc

然后在opencv.pc中添加以下信息,注意这些信息需要与自己安装opencv时的库路径对应:

prefix=/home/zzh/opencv#自己的路径
exec_prefix=${prefix}
includedir=${prefix}/build/include
libdir=${exec_prefix}/build/lib#build后的lib路径

Name: opencv
Description: The opencv library
Version:4.5.1#你的opencv版本
Cflags: -I${includedir}/opencv4
Libs: -L${libdir} -lopencv_shape -lopencv_stitching -lopencv_objdetect -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_ml -lopencv_imgproc -lopencv_flann  -lopencv_core                              

保存退出,然后将文件导入到环境变量:

export  PKG_CONFIG_PATH=/usr/local/lib/pkgconfig

至此就配置好opencv.pc啦~
再执行 pkg-config --cflags --libs opencv时输出结果如下:

-I/usr/local/include/opencv4 -L/usr/local/lib \
-lopencv_shape -lopencv_stitching -lopencv_objdetect \
-lopencv_superres -lopencv_videostab -lopencv_calib3d \
 -lopencv_features2d -lopencv_highgui -lopencv_videoio \
 -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_ml \
 -lopencv_imgproc -lopencv_flann -lopencv_core

我们再次查找版本的时候会出现如下情况:

pkg-config --cflags opencvzzh@zzh:~$ pkg-config --cflags opencv
-I/home/zzh/opencv/include/opencv4

opencv4.5.1-openvino版官方demo个人路径如下

zzh@zzh:~/opencv/samples/cpp/example_cmake$ vim example.cpp

#include "opencv2/core.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/videoio.hpp"
#include 

using namespace cv;
using namespace std;

void drawText(Mat & image);

int main()
{
    cout << "Built with OpenCV " << CV_VERSION << endl;
    Mat image;
    VideoCapture capture;
    capture.open(0);
    if(capture.isOpened())
    {
        cout << "Capture is opened" << endl;
        for(;ud;)
        {
            capture >> image;
            if(image.empty())
                break;
            drawText(image);
            imshow("Sample", image);
            if(waitKey(10) >= 0)
                break;
        }
    }
    else
    {
        cout << "No capture" << endl;
        image = Mat::zeros(480, 640, CV_8UC1);
        drawText(image);
        imshow("Sample", image);
        waitKey(0);
    }
    return 0;
}

void drawText(Mat & image)
{
    putText(image, "Hello OpenCV",
            Point(20, 50),
            FONT_HERSHEY_COMPLEX, 1, // font face and scale
            Scalar(255, 255, 255), // white
            1, LINE_AA); // line thickness and type
}

进行编译

g++ example.cpp -o example.o -c -Wall -I/home/zzh/opencv/include/opencv4
g++ example.o -o opencv_example -L/home/zzh/opencv/build/lib -lopencv_shape -lopencv_stitching -lopencv_objdetect -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_ml -lopencv_imgproc -lopencv_flann -lopencv_core

我编译出现问题,这里不知道什么情况,报错,我们换一个方法:

zzh@zzh:~/opencv/samples/cpp/example_cmake$ g++ example.cpp -o example.o -c -Wall -I/home/zzh/opencv/build/include/opencv4
example.cpp:1:10: fatal error: opencv2/core.hpp: 没有那个文件或目录
    1 | #include "opencv2/core.hpp"
      |          ^~~~~~~~~~~~~~~~~~
compilation terminated.
2.解决方法二:cmake进行编译

重新配置cmake:

cd opencv/build
cmake -D CMAKE_BUILD_TYPE=Release -D BUILD_TIFF=ON -D WITH_IPP=ON -D OPENCV_GENERATE_PKGCONFIG=ON -D CMAKE_INSTALL_PREFIX=/usr/local ..

在build下用camke进行配置:

-D CMAKE_INSTALL_PREFIX=/usr/local为指定安装路径

-D OPENCV_GENERATE_PKGCONFIG=ON为自动生成opencv.pc

cmake -D CMAKE_BUILD_TYPE=Release -D BUILD_TIFF=ON -D WITH_IPP=ON -D OPENCV_GENERATE_PKGCONFIG=ON -D CMAKE_INSTALL_PREFIX=/usr/local ..
sudo make -j16
make install

这条命令会在build目录里生成对应配置的Makefile文件,可以看到配置信息之间是通过空格和-D来分割和标示的,配置了很多信息,我觉得比较重要的一个是 -D OPENCV_GENERATE_PKGCONFIG=ON,生成opencv.pc文件的配置(注意,opencv4生成的文件叫做opencv4.pc),另外就是 CMAKE_INSTALL_PREFIX=/usr/local这个关于安装路径的配置,这里的安装路径是在/usr/local下,这也是opencv的默认配置。

生成这里没有一个参数是多余的,少一个都会出错,一般错误为:

有可能因为anconda里面的python库链接出问题,可以在bash里将conda的环境变量注释

在这里我不是这个bug

/usr/bin/ld: ../../lib/libopencv_imgcodecs.so.4.5.1: undefined reference to `TIFFReadRGBAStrip@LIBTIFF_4.0'

make的时候建议添加sudo权限

查看opencv版本

opencv_version
zzh@zzh:~/opencv/build$ opencv_version
4.5.1-openvino

配置环境变量

cd /usr/local/lib/pkgconfig
pkg-config --cflags opencv4
zzh@zzh:/usr/local/lib/pkgconfig$ pkg-config --cflags opencv4
-I/usr/local/include/opencv4
zzh@zzh:/usr/local/lib/pkgconfig$ ls
opencv4.pc

环境变量配置完成

我们可以看一下这里的opencv4.pc,其中/usr/local为默认路径,建议安装的时候指定默认安装路径

prefix=/usr/local
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${prefix}/include/opencv4

Name: OpenCV
Description: Open Source Computer Vision Library
Version: 4.5.1
Libs: -L${exec_prefix}/lib -lopencv_gapi -lopencv_stitching -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dnn_objdetect -lopencv_dnn_superres -lopencv_dpm -lopencv_face -lopencv_freetype -lopencv_fuzzy -lopencv_hfs -lopencv_img_hash -lopencv_intensity_transform -lopencv_line_descriptor -lopencv_mcc -lopencv_quality -lopencv_rapid -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_superres -lopencv_optflow -lopencv_surface_matching -lopencv_tracking -lopencv_highgui -lopencv_datasets -lopencv_text -lopencv_plot -lopencv_videostab -lopencv_videoio -lopencv_xfeatures2d -lopencv_shape -lopencv_ml -lopencv_ximgproc -lopencv_video -lopencv_dnn -lopencv_xobjdetect -lopencv_objdetect -lopencv_calib3d -lopencv_imgcodecs -lopencv_features2d -lopencv_flann -lopencv_xphoto -lopencv_photo -lopencv_imgproc -lopencv_core
Libs.private: -ldl -lm -lpthread -lrt

pkg-config --cflags代表我们安装的头文件路径,pkg-config --libs opencv4`代表我们安装的库文件路径

编译一下sample里面的文件:

zzh@zzh:~/opencv/samples/cpp$ g++ -o edge edge.cpp `pkg-config --cflags --libs opencv4`

注意这里的两个``是键盘左上方的符号,不是单引号,这代表shell的运行语句

之后会出现可执行文件,比如会出现两张图片,则为编译完成

zzh@zzh:~/opencv/samples/cpp$ ./edge 

This sample demonstrates Canny edge detection
Call:
    ./edge [image_name -- Default is fruits.jpg]

[ WARN:0] global /home/zzh/opencv/modules/core/src/utils/samples.cpp (59) findFile cv::samples::findFile('fruits.jpg') =>

之后查找opencv

zzh@zzh:~$ pkg-config --modversion opencv4
4.5.1

openvion的配置还是按照上面的配置就可以了,亲测可用。

迈德威视工业相机配置(更新中)

1.驱动安装:

在官网上可以找到linux系统下的和winows系统下的sdk,里面有readme文档直接对应这来就可以,配置好之后,打开SDK所在文件夹

cd linuxSDK_V2.1.0.30/demo/OpenCv/

查看Makefile文件:

CXX ?= g++
CFLAGS =`pkg-config --cflags opencv4`#安装的版本为opencv4
LIBS = `pkg-config --libs opencv4`
INCLUDE= -I../../include

all: main
main:  
        $(CXX) $(CFLAGS)  -o main  main.cpp $(LIBS)   $(INCLUDE)   -lMVSDK
clean:
        rm -f *.o 
        rm -f main 

opencv4的版本修改mian.cpp内容:(头文件添加)

#include "CameraApi.h" //相机SDK的API头文件
#include
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include 

因为opencv版本为4,所以有部分bug是因为版本过高而导致

之后sudo makesudo ./make就可以用了

你可能感兴趣的:(linux,ubuntu,opencv,openvino)