目录

树莓派4B安装openvino环境指南(参考官网撰写)

openvino环境安装步骤

下载发行工具包

openvino官方下载发行版工具包,一般建议下载最新版本。

解压工具包,配置依赖等

下载好后,放到树莓派的home/pi/Downloads目录下准备安装。(不建议修改位置)

  • 创建安装目录

    1
    
    sudo mkdir -p /opt/intel/openvino
    
  • 解压安装

    1
    
    sudo tar -xf  l_openvino_toolkit_runtime_raspbian_p_<version>.tgz --strip 1 -C /opt/intel/openvino
    
  • 安装外部依赖CMake

    1
    
    sudo apt install cmake
    
  • 设置环境变量

    临时设置环境变量

    1
    
    source /opt/intel/openvino/bin/setupvars.sh
    

    永久设置环境变量

    1
    
    echo "source /opt/intel/openvino/bin/setupvars.sh" >> ~/.bashrc
    

    永久环境变量成功的测试,新开一个终端(terminal),在第一行会看到如下字样:

    1
    
    [setupvars.sh] OpenVINO environment initialized
    
  • 添加USB规则

    1、将当前的Linux用户添加到该users组:

    1
    
    sudo usermod -a -G users "$(whoami)"
    

    若没有永久设置环境变量,请保证在setupvars.sh运行后执行。

    2、安装运行install_NCS_udev_rules.sh脚本的USB规则:

    1
    
    sh /opt/intel/openvino/install_dependencies/install_NCS_udev_rules.sh
    

    3、插入神经计算棒2。

测试用例(人脸检测)

框选输入图像中的人脸

  • 创建build目录

    1
    
    mkdir build && cd build
    
  • 构建对象检测样本

    1
    2
    
    cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_FLAGS="-march=armv7-a" /opt/intel/openvino/deployment_tools/inference_engine/samples/cpp
    make -j2 object_detection_sample_ssd
    
  • 下载预训练的人脸检测模型

    .bin文件

    1
    
    wget --no-check-certificate https://download.01.org/opencv/2020/openvinotoolkit/2020.1/open_model_zoo/models_bin/1/face-detection-adas-0001/FP16/face-detection-adas-0001.bin
    

    .xml文件

    1
    
    wget --no-check-certificate https://download.01.org/opencv/2020/openvinotoolkit/2020.1/open_model_zoo/models_bin/1/face-detection-adas-0001/FP16/face-detection-adas-0001.xml
    
  • 运行用例

    1
    
    ./armv7l/Release/object_detection_sample_ssd -m face-detection-adas-0001.xml -d MYRIAD -i <path_to_image>
    

    该应用程序输入图像的路径,输出一个图像(out_0.bmp),其中检测到的面孔用矩形括起来。

通过树莓派摄像头实时检测人脸(参考b站同济子豪兄)

  • 配置摄像头

    1
    2
    3
    
    sudo nano /etc/modules
    在该文件末尾添加一行
    bcm2835-v412
    

    输入命令

    1
    
    vcgencmd get_camera
    

    得到supported = 1 detected = 1,证明摄像头连接成功。

    1
    
    ls /dev
    

    看到video0即为摄像头。

  • 配置展示窗口

    1
    
    export DISPLAY=:0.0
    
  • 例程

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    
    import cv2 as cv
    import numpy as np
      
    print("------------------start detection!------------------")
    # 载入bin文件和xml文件
    net = cv.dnn.readNet("face-detection-adas-0001.xml",
                         "face-detection-adas-0001.bin")
    net.setPreferableTarget(cv.dnn.DNN_TARGET_MYRIAD)
      
    cap = cv.VideoCapture(0)
    while(1):
        ret, frame = cap.read()
        frame = cv.resize(frame, (480, 320), interpolation=cv.INTER_CUBIC)
        blob = cv.dnn.blobFromImage(frame, size=(672, 384), ddepth=cv.CV_8U)
        net.setInput(blob)
        out = net.forward()
      
        # 画人脸框
        for detection in out.reshape(-1, 7):
            confidence = float(detection[2])
            # 获取左上角坐标
            xmin = int(detection[3] * frame.shape[1])
            ymin = int(detection[4] * frame.shape[0])
            # 获取右下角坐标
            xmax = int(detection[5] * frame.shape[1])
            ymax = int(detection[6] * frame.shape[0])
            if confidence > 0.5:
                cv.rectangle(frame, (xmin, ymin), (xmax, ymax), color=(0, 255, 0))
        # 展示图像
        cv.imshow("capture", frame)
        if cv.waitKey(1) & 0XFF == ord("q"):
            cv.imwrite("out.png", frame)
            print("------------------save image!------------------")
            break
    # 关闭摄像头和窗口
    cap.release()
    cv.destoryAllWindows()
    print("------------------end detection!------------------")
    

树莓派运行推理模型的开发流程

  1. 选择预训练模型;

  2. 使用模型优化器,来转换模型;

  3. 最后在树莓派上推理模型。

    open model zoo预训练模型:

    https://download.01.org/opencv/2020/openvinotoolkit/2020.4/open_model_zoo/models_bin/

常规的开发方式,需要在 open model zoo 中寻找适合的模型,对于大多数业务来说,都能满足基本需要。若需要跑一些比较前沿的模型或者是自己设计的神经网络时,那各类模型转换的方法则是必备技能,难度相应也会大一些。

附上pytorch转ONNX格式后应用在openvino的方法:

https://zhuanlan.zhihu.com/p/116065374

参考链接