人脸检测实战:使用opencv加载深度学习模型实现人脸检测(1)

本文介绍如何利用OpenCV的dnn模块结合深度学习模型进行人脸检测。通过命令行参数解析输入图像、prototxt文件和预训练模型,然后对图像进行预处理,创建blob,并通过网络获取检测结果。对于每个检测到的人脸,根据置信度筛选并绘制边界框及概率。代码还展示了如何将该方法扩展到视频流和网络摄像头的实时人脸检测。
摘要由CSDN通过智能技术生成

import argparse

import cv2

construct the argument parse and parse the arguments

ap = argparse.ArgumentParser()

ap.add_argument(“-i”, “–image”, required=True,

help=“path to input image”)

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())

导入所需的包并解析命令行参数。 我们有三个必需的参数:

–image :输入图像的路径。

–prototxt :Caffe prototxt 文件的路径。

–model :预训练 Caffe 模型的路径。

可选参数 --confidence 可以覆盖默认阈值 0.5。 从那里让我们加载我们的模型并从我们的图像创建一个 blob:

load our serialized model from disk

print(“[INFO] loading model…”)

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

load the input image and construct an input blob for the image

by resizing to a fixed 300x300 pixels and then normalizing it

image = cv2.imread(args[“image”])

(h, w) = image.shape[:2]

blob = cv2.dnn.blobFromImage(cv2.resize(image, (300, 300)), 1.0,

(300, 300), (104.0, 177.0, 123.0))

首先,我们使用 --prototxt 和 --model 文件路径加载我们的模型。 我们将模型存储为 net(第 20 行)。

然后我们加载图像,提取尺寸,并创建一个 blob。 dnn.blobFromImage 负责预处理,包括设置 blob 尺寸和规范化。 如果您有兴趣了解有关 dnn.blobFromImage 函数的更多信息,我会在这篇博文中详细介绍。 接下来,我们将应用人脸检测:

pass the blob through the network and obtain the detections and

predictions

print(“[INFO] computing object detections…”)

net.setInput(blob)

detections = net.forward()

为了检测人脸,将 blob 通过网络。 然后我们将循环检测并在检测到的人脸周围绘

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值