树莓派+神经计算棒2实时人脸检测

树莓派配置摄像头

sudo apt-get install python-opencv
sudo apt-get install fswebcam

配置摄像头

sudo nano /etc/modules

查看树莓派CPU型号

 cat /proc/cpuinfo

在这里插入图片描述
cpu型号为bcm2711
添加

bcm2711-v4l2
vcgencmd get_camera

在这里插入图片描述
配置展示窗口

export DISPLAY=:0.0

实时人脸检测代码

#coding=utf-8
#face-detection-camera.py
import cv2 as cv
import numpy as np

print('开始人脸摄像头实时监测')
print('press "q" to exit ')
#载入模型文件和权重文件
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'):
        #每一毫秒监听一次键盘的动作,按q键结束,并保存图片
        cv.imwrite('out_cam.png',frame)
        print("save one image done!")
        break
#关闭摄像头及显示窗口
cap.release()
cv.destoryAllWindows()


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值