【openvino+树莓派】实现实时摄像头人脸检测

目录

参考步骤 

人脸检测py代码 

图片形式

摄像头实时

树莓派+openvino 人脸检测实时效果

openvino 树莓派 人脸实时检测测试效果_哔哩哔哩_bilibili 


参考步骤 

  • Openvino:安装、部署。(可参考以下文档,验证不建议参考)

树莓派+神经计算棒2部署Openvino的human_pose_estimation_demo实例_hwxhxyz1998403的博客-CSDN博客_openpose 树莓派

树莓派4B使用OpenVINO部署人体关键点检测模型Demo (视频中使用的文件在介绍里下载)_哔哩哔哩_bilibili

安装包链接:https://pan.baidu.com/s/1g-pCfJY0wkwTCclqOCKOng 
提取码:48z7

参考到此部分

  

人脸检测py代码 

图片形式

#coding=utf-8
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('/home/pi/Downloads/inference_engine_vpu_arm/deployment_tools/inference_engine/samples/cpp/build/1.jpeg')
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('out111.png', frame)

摄像头实时

#coding=utf-8
import cv2 as cv
import numpy as np

print("start!")
# 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('/home/pi/Downloads/inference_engine_vpu_arm/deployment_tools/inference_engine/samples/cpp/build/1.jpeg')

#
cap = cv.VideoCapture(0)
while(1):
    ret, frame = cap.read()
    frame = cv.resize(frame,(480,320),interpolation=cv.INTER_CUBIC)
    # 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))
    cv.imshow('capture',frame)
    if cv.waitKey(1) &0xFF==ord('q'):
        # Save the frame to an image file.
        cv.imwrite('out111.png', frame)
        print('save done')
cap.release()
cv.destroyAllWindows()

树莓派+openvino 人脸检测实时效果

openvino 树莓派 人脸实时检测测试效果_哔哩哔哩_bilibili

问题小记录(其中涉及到一些我的机器人的具体问题,仅作为自己复习用)

  1.  手机要开启定位才能进入局域网连接,误操作长按KEY1进入直连模式再次尝试。
  2. 问题:cmake编译问题,重新换了一个终端可以了。
  • 4
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
要在树莓4B上运行人脸识别,可以使用OpenCV和Dlib这两个库来实现。这里提供一个基于Python的简单示例代码: 1. 安装OpenCV和Dlib 在终端中输入以下命令来安装OpenCV和Dlib: ``` sudo apt-get install python3-opencv sudo apt-get install libopencv-dev python3-opencv sudo pip3 install dlib ``` 2. 摄像头捕捉人脸 使用OpenCV的VideoCapture类来捕捉摄像头视频,并使用OpenCV的CascadeClassifier类来检测摄像头视频中的人脸。示例代码如下: ```python import cv2 face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') # 加载人脸识别模型 cap = cv2.VideoCapture(0) # 打开摄像头 while True: ret, frame = cap.read() # 读取摄像头视频帧 gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # 将视频帧转换为灰度图像 faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5) # 检测人脸 for (x, y, w, h) in faces: cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2) # 绘制人脸框 cv2.imshow('frame', frame) # 显示视频帧 if cv2.waitKey(1) & 0xFF == ord('q'): # 按‘q’键退出 break cap.release() # 释放摄像头 cv2.destroyAllWindows() # 关闭窗口 ``` 3. 人脸识别 在检测人脸后,可以使用Dlib库中的人脸识别模型来识别人脸。示例代码如下: ```python import cv2 import dlib detector = dlib.get_frontal_face_detector() # 加载人脸检测器 predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat") # 加载人脸关键点检测模型 cap = cv2.VideoCapture(0) # 打开摄像头 while True: ret, frame = cap.read() # 读取摄像头视频帧 gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # 将视频帧转换为灰度图像 faces = detector(gray) # 检测人脸 for face in faces: landmarks = predictor(gray, face) # 检测人脸关键点 for n in range(0, 68): x = landmarks.part(n).x y = landmarks.part(n).y cv2.circle(frame, (x, y), 2, (0, 255, 0), -1) # 绘制人脸关键点 cv2.imshow('frame', frame) # 显示视频帧 if cv2.waitKey(1) & 0xFF == ord('q'): # 按‘q’键退出 break cap.release() # 释放摄像头 cv2.destroyAllWindows() # 关闭窗口 ``` 以上代码只是一个简单的例子,如果要实现更复杂的人脸识别功能,需要对算法进行优化和改进。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Clark-dj

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值