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

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 通过网络。 然后我们将循环检测并在检测到的人脸周围绘制框:

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”]:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值