图像 引言 深度学习_用树莓派4b构建深度学习应用(十二)口罩篇

本文介绍如何使用OpenCV和深度学习在树莓派4B上构建一个自动检测并模拟口罩佩戴的应用。通过检测人脸、提取关键点,结合CNN和HRNet模型,将口罩图像精准地覆盖到口鼻区域。该应用适用于当前COVID-19环境下,提醒人们正确佩戴口罩。
摘要由CSDN通过智能技术生成

前言上一篇我们把环境和网络问题都解决了,这一篇在 COVID-19 仍在全世界肆虐的当下,我们尝试用 AI 来做一个有趣的自动戴口罩应用。主要用 OpenCV + CNN 来提取面部关键点坐标,再将口罩图片作为蒙版贴合上去。记得国庆中秋双节出门在外,也别忘了戴上口罩保护自己。

整个戴口罩分为三个阶段:在图像上找到一张脸

检测脸上关键点标记

用口罩图片来覆盖到口鼻位置

人脸检测

首先,我们要在一张图像上定位脸部的位置,OpenCV 中 DNN 模块就能很轻松的做到。检测模型训练于 Caffe 框架,我们获取一个网络定义文件 face_detector.prototxt 和一个权重文件 face_detector.caffemodel。# defining prototxt and caffemodel paths

# 人脸检测模型

detector_model = args.detector_model

detector_weights = args.detector_weights

# load model

detector = cv2.dnn.readNetFromCaffe(detector_model, detector_weights)

capture = cv2.VideoCapture(0)

while True:

# capture frame-by-frame

success, frame = capture.read()

# get frame's height and width

height, width = frame.shape[:2] # 640×480

# resize and subtract BGR mean values, since Caffe uses BGR images for input

blob = cv2.dnn.blobFromImage(

frame, scalefactor=1.0, size=(300, 300), mean=(104.0, 177.0, 123.0),

)

# passing blob through the network to detect a face

detector.setInput(blob)

# detector output format:

# [image_id, class, confidence, left, bottom, right, top]

face_detections = detector.forward()

推理之后,我们可以获得人脸序号,类型(人脸),置信度,左,下,右,上坐标值。# loop over the detections

for i in range(0, face_detections.shape[2]):

# extract confidence

confidence = face_detections[0, 0, i, 2]

# filter detections by confidence greater than the minimum threshold

if confidence > 0.5:

# get coordinates of the bounding box

box &#

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值