Raspberrypi连接神经计算棒人脸识别
树莓派3B上部署英特尔神经网络计算棒Intel NCS2进行最普通的人脸识别。
参考
- 硬件: 树莓派3B
- 树莓派3B安装系统 :2020-02-13-raspbian-buster.zip
- 神经计算棒: IntelNCS2
1. 安装 openvino 工具包
到网站下载工具: openvino 工具包,也可以找其它的版本下载到 cd ~/Downloads/ ,这时使用以下版本:
l_openvino_toolkit_runtime_raspbian_p_2020.3.341.tgz
打开终端
cd ~/Downloads/
sudo tar -xf l_openvino_toolkit_runtime_raspbian_p_2020.3.341.tgz//解压文件
mv l_openvino_toolkit_runtime_raspbian_p_2020.3.341.tgz inference_engine_vpu_arm//修改文件夹名称
2. 安装外部软件支持
sudo apt install cmake
3. 设置环境变量
执行以下命令,会自动对setupvars.sh文件做修改
sed -i "s|<INSTALLDIR>|$(pwd)/inference_engine_vpu_arm|" inference_engine_vpu_arm/bin/setupvars.sh
source /home/pi/Downloads/inference_engine_vpu_arm/bin/setupvars.sh
永久设置环境变量
echo "source /home/pi/Downloads/inference_engine_vpu_arm/bin/setupvars.sh" >> ~/.bashrc
显示 以下,就正常了
[setupvars.sh] OpenVINO environment initialized
4. 添加USB规则
sudo usermod -a -G users "$(whoami)" //添加规则
source /home/pi/Downloads/inference_engine_vpu_arm/bin/setupvars.sh //加载修改后的设置,使之生效
sh inference_engine_vpu_arm/install_dependencies/install_NCS_udev_rules.sh //退出 重新登录
显示以下正常:
Updating udev rules...
Udev rules have been successfully installed.
插上神经计算棒
5. 运行对象检测示例验证安装的推理机
进入此目录
cd inference_engine_vpu_arm/deployment_tools/inference_engine/samples/cpp
sudo mkdir build && cd build
构建对象检测样本:
cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_FLAGS="-march=armv7-a"
编译
make -j2 object_detection_sample_ssd
下载预培训的人脸检测模型
下载带有权重的.bin文件
wget --no-check-certificate https://download.01.org/opencv/2019/open_model_zoo/R1/models_bin/face-detection-adas-0001/FP16/face-detection-adas-0001.bin
下载带有网络拓扑结构的.xml文件:
wget --no-check-certificate https://download.01.org/opencv/2019/open_model_zoo/R1/models_bin/face-detection-adas-0001/FP16/face-detection-adas-0001.xml
运行示例时指定模型和输入图像的路径:
./armv7l/Release/object_detection_sample_ssd -m face-detection-adas-0001.xml -d MYRIAD -i yangmi.jpg
应用程序输出一个图像(out_0.bmp),检测到的面用矩形括起来。
6. 验证OpenCV的安装
6.1 下载带有权重的.bin文件:
wget --no-check-certificate https://download.01.org/opencv/2019/open_model_zoo/R1/models_bin/face-detection-adas-0001/FP16/face-detection-adas-0001.bin
6.2 下载带有网络拓扑结构的.xml文件:
wget --no-check-certificate https://download.01.org/opencv/2019/open_model_zoo/R1/models_bin/face-detection-adas-0001/FP16/face-detection-adas-0001.xml
7. 了解树莓派的工作流
创建 Python 文件 openvino_fd_myriad.py 复制下面的脚本
import cv2 as cv
# Load the model.
net = cv.dnn.readNet('face-detection-adas-0001.xml',
'face-detection-adas-0001.bin')
# Specify target device.
net.setPreferableTarget(cv.dnn.DNN_TARGET_MYRIAD)
# Read an image.
frame = cv.imread('/path/to/image')
if frame is None:
raise Exception('Image not found!')
# Prepare input blob and perform an inference.
blob = cv.dnn.blobFromImage(frame, size=(672, 384), ddepth=cv.CV_8U)
net.setInput(blob)
out = net.forward()
# Draw detected faces on the frame.
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))
# Save the frame to an image file.
cv.imwrite('out.png', frame)
将以下文件放到一个文件夹中:
运行脚本
python3 openvino_fd_myriad.py