想要在无人机平台部署CV,但是无人机的机载电脑需要安装ROS,而ROS需要在Ubuntu的平台才能方便使用,所以树莓派3B+上安装的是Ubuntu Mate18.04。
Intel Ncs2(英特尔神经计算棒)官方的文档写的是部署在树莓派官方系统上,而对于其他平台甚至树莓派平台其他版本的系统都不提供支持。
我在安装Intel Nc2到树莓派上时也遇到了很多坑,初始化setupvar.sh的时候,报错

64 bitness python is required

然后执行demo的时候,自动退出,没法使用。

后来想要在树莓派上直接编译官方的github,也是国外的一篇文章上推荐的。在编译的时候因为被墙,一直疯狂报错,后来连接手机热点,成功编译,但是这时根本找不到如何使用的方法,官方也没有相关文档。最后通过修改setupvar.sh,成功安装。

1.Intel Ncs2安装

下载固件:https://download.01.org/opencv/2020/openvinotoolkit/2020.4/ 并上传到树莓派

1.1下载解压

cd ~
mkdir intel 
tar -zxvf l_openvino_toolkit_runtime_raspbian_p_2020.4.287.tgz #解压缩
mv  l_openvino_toolkit_runtime_raspbian_p_2020.4.287.tgz openvino  # 重命名

1.2修改setupvar.sh

1.
INSTALLDIR=/home/pi/intel/openvino  # 修改开头的安装目录 为你自己的安装目录

2.
if [ "$python_bitness" != "" ] && [ "$python_bitness" != "64" ] && [ "$OS_NAME" != "Raspian" ]; then
    echo "[setupvars.sh] 64 bitness for Python" $python_version "is requred"

修改为
if [ "$python_bitness" != "" ] && [ "$python_bitness" != "64" ] && [ "$OS_NAME" != "Ubuntu" ]; then
    echo "[setupvars.sh] 64 bitness for Python" $python_version "is requred"

修改之后,添加到bashrc

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

仅仅出现下面的提示,就成功了
[setupvars.sh] OpenVINO environment initialized

1.3 更新USB规则

sudo usermod -a -G users "$(whoami)"
sh ~/intel/openvino/install_dependencies/install_NCS_udev_rules.sh

2.运行样例

 mkdir build && cd build
 cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_FLAGS="-march=armv7-a" \ 
  ~/intel/openvino/deployment_tools/inference_engine/samples/c
  make -j2 object_detection_sample_ssd_c
# 切换到生成的目录
cd ~/intel/openvino/build/armv7l/Release
# 下载训练好的权重
 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

 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

# 执行测试
./object_detection_sample_ssd_c -m face-detection-adas-0001.xml -d MYRIAD -i /home/pi/face.jpg

3.运行视频的人脸检测

效果:https://www.bilibili.com/video/BV17p4y1v7bx/

import cv2 as cv
import numpy as np
# 载入模型
net = cv.dnn.readNet('face-detection-adas-0001.xml','face-detection-adas-0001.bin')

# 选择目标设备
net.setPreferableTarget(cv.dnn.DNN_TARGET_MYRIAD)

# 读取图片
print('读取视频')
frame = cv.VideoCapture('/home/pi/face.mp4')
fps = frame.get(cv.CAP_PROP_FPS)
size = (int(frame.get(cv.CAP_PROP_FRAME_WIDTH)), int(frame.get(cv.CAP_PROP_FRAME_HEIGHT)))
output = cv.VideoWriter('/home/pi/face_out.avi',cv.VideoWriter_fourcc(*'XVID'), fps, size)

while(frame.isOpened()):
    ret, fra = frame.read()
    if ret==True:
        blob = cv.dnn.blobFromImage(fra, size=(672,384), ddepth=cv.CV_8U) 
        net.setInput(blob)         
        out = net.forward()
        print('开始处理第一帧')
        # Draw detected faces on the frame 
        for detection in out.reshape(-1, 7): 
            confidence = float(detection[2]) 
            xmin = int(detection[3] * fra.shape[1]) 
            ymin = int(detection[4] * fra.shape[0]) 
            xmax = int(detection[5] * fra.shape[1]) 
            ymax = int(detection[6] * fra.shape[0])
            if confidence > 0.5:
                cv.rectangle(fra, (xmin, ymin), (xmax, ymax), color=(0, 255, 0))
        output.write(fra)
    else:
        break


frame.release()
output.release()
print("ALL DONE SUCCESSFULLY")