安装配置树莓派和英特尔神经棒二代
张子豪 2019-11-14
修改者:白宸羽
文章目录
材料准备
树莓派3B+主板
Intel movidius 神经计算棒2代
顺便提一句,英特尔神经棒二代的英文名字叫做Intel® Neural Compute Stick 2
树莓派配件:购买指南见下方链接
https://www.bilibili.com/video/av47594798
https://zhuanlan.zhihu.com/p/47257546
配置树莓派系统
下载镜像、烧录镜像、配置树莓派
检查
uname -m
输出armv7l
说明正常。
换源
sudo nano /etc/apt/sources.list
用#
注释掉原文件内容,用以下内容取代:
deb http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian/ buster main contrib non-free rpi
deb-src http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian/ buster main contrib non-free rpi
保存,退出。
sudo nano /etc/apt/sources.list.d/raspi.list
用#
注释掉原文件内容,用以下内容取代:
deb http://mirror.tuna.tsinghua.edu.cn/raspberrypi/ buster main ui
deb-src http://mirror.tuna.tsinghua.edu.cn/raspberrypi/ buster main ui
保存,退出。
使用sudo apt-get update
命令更新软件源列表。
安装cmake
sudo apt install cmake
下载OpenVINO toolkit for Raspbian安装包
官网介绍:https://docs.openvinotoolkit.org/latest/_docs_install_guides_installing_openvino_raspbian.html
The OpenVINO™ toolkit quickly deploys applications and solutions that emulate human vision. Based on Convolutional Neural Networks (CNN), the toolkit extends computer vision (CV) workloads across Intel® hardware, maximizing performance. The OpenVINO toolkit includes the Intel® Deep Learning Deployment Toolkit (Intel® DLDT).
The OpenVINO™ toolkit for Raspbian* OS includes the Inference Engine and the MYRIAD plugins. You can use it with the Intel® Movidius™ Neural Compute Stick (Intel® NCS) or the Intel® Neural Compute Stick 2 plugged in one of USB ports.
用自己电脑点击下方链接下载2019年10月25日发布的OpenVINO最新版本,再用filezilla传到树莓派的Downloads文件夹下
https://download.01.org/opencv/2019/openvinotoolkit/R3/l_openvino_toolkit_runtime_raspbian_p_2019.3.334.tgz
cd ~/Downloads/
tar -xf l_openvino_toolkit_runtime_raspbian_p_2019.3.334.tgz
给解压之后的文件重命名为inference_engine_vpu_arm
source inference_engine_vpu_arm/bin/setupvars.sh
sh inference_engine_vpu_arm/install_dependencies/install_NCS_udev_rules.sh
sed -i "s|<INSTALLDIR>|$(pwd)/inference_engine_vpu_arm|" inference_engine_vpu_arm/bin/setupvars.sh
设置环境变量
sudo nano /home/pi/.bashrc
在最后一行增加以下内容
source /home/pi/Downloads/inference_engine_vpu_arm/bin/setupvars.sh
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MWrMQL8b-1617724188910)(C:\Users\40743\AppData\Roaming\Typora\typora-user-images\1573722567418.png)]
重开一个新的命令行窗口,登陆后提示[setupvars.sh] OpenVINO environment initialized
则说明配置成功。
为英特尔神经棒2代配置USB规则
sudo usermod -a -G users "$(whoami)"
sh inference_engine_vpu_arm/install_dependencies/install_NCS_udev_rules.sh
提示:
Updating udev rules...
Udev rules have been successfully installed.
则说明安装成功了。
把英特尔神经棒插在树莓派的USB口上
编译
cd inference_engine_vpu_arm/deployment_tools/inference_engine/samples
mkdir build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_FLAGS="-march=armv7-a"
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-OUGhQB6q-1617724188912)(C:\Users\40743\AppData\Roaming\Typora\typora-user-images\1573722696477.png)]
make -j2 object_detection_sample_ssd
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-DuwfGO3B-1617724188914)(C:\Users\40743\AppData\Roaming\Typora\typora-user-images\1573723423978.png)]
下载神经棒模型的模型文件和权重文件
wget --no-check-certificate https://download.01.org/openvinotoolkit/2018_R4/open_model_zoo/face-detection-adas-0001/FP16/face-detection-adas-0001.bin
wget --no-check-certificate https://download.01.org/openvinotoolkit/2018_R4/open_model_zoo/face-detection-adas-0001/FP16/face-detection-adas-0001.xml
人脸检测
<path_to_image>换成你的人脸图片的绝对路径
pi@raspberrypi:~/Downloads/inference_engine_vpu_arm/deployment_tools/inference_engine/samples/build $ ./armv7l/Release/object_detection_sample_ssd -m face-detection-adas-0001.xml -d MYRIAD -i /home/pi/Downloads/face/1.jpg
Python中调用OpenCV进行人脸检测
import cv2 as cv
# 同济子豪兄 20191114
# 参考:https://blog.csdn.net/qqqzmy/article/details/85213414
print('开始人脸检测')
# 载入模型和权重文件
net = cv.dnn.readNet('face-detection-adas-0001.xml', 'face-detection-adas-0001.bin')
# Specify target device
net.setPreferableTarget(cv.dnn.DNN_TARGET_MYRIAD)
# 读入图片,将图片路径更换为自己的人脸图片
frame = cv.imread('./6.jpg')
# 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,ymin-框的左上角点坐标
xmin = int(detection[3] * frame.shape[1])
ymin = int(detection[4] * frame.shape[0])
# xmax,ymax-框的右下角点坐标
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.imwrite('./out.png', frame)
print('人脸检测完成,已保存检测结果图像')
配置树莓派摄像头
sudo apt-get install python-opencv
sudo apt-get install fswebcam
插上树莓派摄像头
配置树莓派摄像头
sudo nano /etc/modules
在这个文件末尾添加一行
bcm2835-v4l2
重启树莓派
输入命令
vcgencmd get_camera
如果得到下面的结果,则证明摄像头连接成功
raspistill -o image.jpg
调用摄像头拍一张照片,命名为image.jpg,存储在/pi/home路径,也就是桌面左上角资源管理器一打开显示的那个路径。如果能看到摄像头上红灯亮,目录里面有照片,则进一步说明摄像头配置正确。
ls /dev
右下角的video0即为摄像头。
fswebcam 10 test.jpg
执行后会 延时10帧 拍摄 (给个准备时间)产生 一张 名称为 test 的图片,存储在/home/pi
目录中。
配置展示窗口
打开VNC Viewer
在树莓派命令行中运行这条命令
export DISPLAY=:0.0
树莓派摄像头实时人脸检测
# coding=utf-8
# face-detection-camera.py
import cv2 as cv
import numpy as np
# 同济子豪兄 20191114
# 参考:https://blog.csdn.net/c20081052/article/details/89945575
print('开始人脸摄像头实时检测')
# 载入模型文件和权重文件
net = cv.dnn.readNet('face-detection-adas-0001.xml', 'face-detection-adas-0001.bin')
# Specify target device
net.setPreferableTarget(cv.dnn.DNN_TARGET_MYRIAD)
# 从摄像头中读取图像帧
cap=cv.VideoCapture(0)
while(1):
# 获取一帧图像
ret,frame=cap.read()
# Prepare input blob and perform an inference
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'):
# 每1毫秒监听一次键盘的动作,按q键结束,并保存图片
cv.imwrite('out.png', frame)
print("save one image done!")
break
# 关闭摄像头及显示窗口
cap.release()
cv.destroyAllWindows()
print('人脸摄像头实时检测完成')
运行这个脚本文件
python3 face-detection-camera.py
pture",frame)
if cv.waitKey(1)&0xFF==ord(‘q’):
# 每1毫秒监听一次键盘的动作,按q键结束,并保存图片
cv.imwrite(‘out.png’, frame)
print(“save one image done!”)
break
关闭摄像头及显示窗口
cap.release()
cv.destroyAllWindows()
print(‘人脸摄像头实时检测完成’)
运行这个脚本文件
```shell
python3 face-detection-camera.py