2024年最全人脸检测实战:使用opencv加载深度学习模型实现人脸检测_dnnir,2024年最新程序员必看

img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上C C++开发知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

如果你需要这些资料,可以戳这里获取

# prediction
confidence = detections[0, 0, i, 2]
# filter out weak detections by ensuring the `confidence` is
# greater than the minimum confidence
if confidence > args["confidence"]:
	# compute the (x, y)-coordinates of the bounding box for the
	# object
	box = detections[0, 0, i, 3:7] \* np.array([w, h, w, h])
	(startX, startY, endX, endY) = box.astype("int")

	# draw the bounding box of the face along with the associated
	# probability
	text = "{:.2f}%".format(confidence \* 100)
	y = startY - 10 if startY - 10 > 10 else startY + 10
	cv2.rectangle(image, (startX, startY), (endX, endY),
		(0, 0, 255), 2)
	cv2.putText(image, text, (startX, y),
		cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 0, 255), 2)

show the output image

cv2.imshow(“Output”, image)
cv2.waitKey(0)


遍历检测结果。


然后,我们提取置信度并将其与置信度阈值进行比较。 我们执行此检查以过滤掉弱检测。 如果置信度满足最小阈值,我们继续绘制一个矩形以及检测概率。


为此,我们首先计算边界框的 (x, y) 坐标。 然后我们构建包含检测概率的置信文本字符串。 如果我们的文本偏离图像(例如当面部检测发生在图像的最顶部时),我们将其向下移动 10 个像素。 我们的面部矩形和置信文本绘制在图像上。


然后,我们再次循环执行该过程后的其他检测。 如果没有检测到,我们准备在屏幕上显示我们的输出图像)。


打开一个终端并执行以下命令:



python detect_faces.py --image 2.jpg --prototxt deploy.proto.txt --model res10_300x300_ssd_iter_140000_fp16.caffemodel


![01](https://img-blog.csdnimg.cn/img_convert/b6278fb7d8a97ef0afc1f87523a254dd.png)


## 使用 OpenCV 和深度学习在视频和网络摄像头中进行人脸检测


既然我们已经学习了如何将 OpenCV 的人脸检测应用于单个图像,让我们还将人脸检测应用于视频、视频流和网络摄像头。 对我们来说幸运的是,我们在上一节中使用 OpenCV 在单个图像中进行人脸检测的大部分代码都可以在这里重用!


打开一个新文件,将其命名为 detect\_faces\_video.py ,并插入以下代码:



import the necessary packages

from imutils.video import VideoStream
import numpy as np
import argparse
import imutils
import time
import cv2

construct the argument parse and parse the arguments

ap = argparse.ArgumentParser()
ap.add_argument(“-p”, “–prototxt”, required=True,
help=“path to Caffe ‘deploy’ prototxt file”)
ap.add_argument(“-m”, “–model”, required=True,
help=“path to Caffe pre-trained model”)
ap.add_argument(“-c”, “–confidence”, type=float, default=0.5,
help=“minimum probability to filter weak detections”)
args = vars(ap.parse_args())


与上面相比,我们需要导入三个额外的包: VideoStream 、 imutils 和 time 。 如果您的虚拟环境中没有 imutils,您可以通过以下方式安装它:



pip install imutils


我们的命令行参数基本相同,只是这次我们没有 --image 路径参数。 我们将改用网络摄像头的视频源。 从那里我们将加载我们的模型并初始化视频流:



load our serialized model from disk

print(“[INFO] loading model…”)
net = cv2.dnn.readNetFromCaffe(args[“prototxt”], args[“model”])

initialize the video stream and allow the camera sensor to warm up

print(“[INFO] starting video stream…”)
vs = VideoStream(src=0).start()
time.sleep(2.0)


加载模型与上面相同。 我们初始化一个 VideoStream 对象,指定索引为零的相机作为源(通常这将是您笔记本电脑的内置相机或检测到的台式机的第一个相机)。 这里有一些快速说明:


如果您希望使用 Raspberry Pi 摄像头模块,Raspberry Pi + picamera 用户可以将其替换为 vs = VideoStream(usePiCamera=True).start()。 如果要解析视频文件(而不是视频流),请将 VideoStream 类换成 FileVideoStream。


然后我们让相机传感器预热 2 秒。 从那里我们循环帧并使用 OpenCV 计算人脸检测:



loop over the frames from the video stream

while True:
# grab the frame from the threaded video stream and resize it
# to have a maximum width of 400 pixels
frame = vs.read()
frame = imutils.resize(frame, width=400)

# grab the frame dimensions and convert it to a blob
(h, w) = frame.shape[:2]
blob = cv2.dnn.blobFromImage(cv2.resize(frame, (300, 300)), 1.0,
	(300, 300), (104.0, 177.0, 123.0))

# pass the blob through the network and obtain the detections and
# predictions
net.setInput(blob)
detections = net.forward()

这个块应该看起来很熟悉上一节中的静态图像版本。 在这个块中,我们从视频流中读取一个帧,创建一个 blob,并将 blob 传递给深度神经网络以获得面部检测。


我们现在可以循环检测,与置信度阈值进行比较,并在屏幕上绘制面部框 + 置信度值:



loop over the detections

for i in range(0, detections.shape[2]):
	# extract the confidence (i.e., probability) associated with the
	# prediction
	confidence = detections[0, 0, i, 2]
	# filter out weak detections by ensuring the `confidence` is
	# greater than the minimum confidence
	if confidence < args["confidence"]:
		continue
	# compute the (x, y)-coordinates of the bounding box for the
	# object
	box = detections[0, 0, i, 3:7] \* np.array([w, h, w, h])
	(startX, startY, endX, endY) = box.astype("int")

	# draw the bounding box of the face along with the associated
	# probability
	text = "{:.2f}%".format(confidence \* 100)
	y = startY - 10 if startY - 10 > 10 else startY + 10
	cv2.rectangle(frame, (startX, startY), (endX, endY),
		(0, 0, 255), 2)
	cv2.putText(frame, text, (startX, y),
		cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 0, 255), 2)

要详细查看此代码块,请查看我们对静止静态图像执行人脸检测的上一节。 这里的代码几乎相同。 现在我们已经绘制了 OpenCV 人脸检测,让我们在屏幕上显示框架并等待按键:



show the output frame

cv2.imshow("Frame", frame)
key = cv2.waitKey(1) & 0xFF

# if the `q` key was pressed, break from the loop
if key == ord("q"):
	break

do a bit of cleanup

cv2.destroyAllWindows()
vs.stop()


我们在屏幕上显示框架,直到按下“q”键,此时我们会跳出循环并执行清理。


![image-20211123135253158](https://img-blog.csdnimg.cn/img_convert/f9c68981c5fb3fd80c2513d6a1560781.png)


## 总结


在今天的博客文章中,您发现了关于 OpenCV 库的一个鲜为人知的秘密——OpenCV 提供了开箱即用的更准确的人脸检测器(与 OpenCV 的 Haar 级联相比)。


更准确的 OpenCV 人脸检测器是基于深度学习的,特别是利用单镜头检测器 (SSD) 框架和 ResNet 作为基础网络。 感谢 Aleksandr Rybnikov 和 OpenCV 的 dnn 模块的其他贡献者的辛勤工作,我们可以在自己的应用程序中享受这些更准确的 OpenCV 人脸检测器。


希望您能喜欢今天的博文。完成的权重、文件以及代码链接:


于 OpenCV 库的一个鲜为人知的秘密——OpenCV 提供了开箱即用的更准确的人脸检测器(与 OpenCV 的 Haar 级联相比)。


更准确的 OpenCV 人脸检测器是基于深度学习的,特别是利用单镜头检测器 (SSD) 框架和 ResNet 作为基础网络。 感谢 Aleksandr Rybnikov 和 OpenCV 的 dnn 模块的其他贡献者的辛勤工作,我们可以在自己的应用程序中享受这些更准确的 OpenCV 人脸检测器。




![img](https://img-blog.csdnimg.cn/img_convert/4e67ef26348d65e20f2c8c7d5dcff361.png)
![img](https://img-blog.csdnimg.cn/img_convert/f16e30ebc2a907998d24cc96b172437c.png)

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化的资料的朋友,可以添加戳这里获取](https://bbs.csdn.net/topics/618668825)**


**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化的资料的朋友,可以添加戳这里获取](https://bbs.csdn.net/topics/618668825)**


**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值