【OpenCV】127 OpenCV DNN 基于残差网络的视频人脸检测

127 OpenCV DNN 基于残差网络的视频人脸检测

代码

import cv2 as cv

model_bin = "../models/face_detector/res10_300x300_ssd_iter_140000_fp16.caffemodel";
config_text = "../models/face_detector/deploy.prototxt";

# load caffe model
net = cv.dnn.readNetFromCaffe(config_text, model_bin)

# set back-end
net.setPreferableBackend(cv.dnn.DNN_BACKEND_OPENCV)
net.setPreferableTarget(cv.dnn.DNN_TARGET_CPU)

cap = cv.VideoCapture("../images/sample.mp4")
while True:
    ret, image = cap.read()
    image = cv.flip(image, 1)
    if ret is False:
        break
    # 人脸检测
    h, w = image.shape[:2]
    blobImage = cv.dnn.blobFromImage(image, 1.0, (300, 300), (104.0, 177.0, 123.0), False, False);
    net.setInput(blobImage)
    cvOut = net.forward()

    # Put efficiency information.
    t, _ = net.getPerfProfile()
    fps = 1000 / (t * 1000.0 / cv.getTickFrequency())
    label = 'FPS: %.2f' % fps
    cv.putText(image, label, (0, 15), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0))

    # 绘制检测矩形
    for detection in cvOut[0,0,:,:]:
        score = float(detection[2])
        objIndex = int(detection[1])
        if score > 0.5:
            left = detection[3]*w
            top = detection[4]*h
            right = detection[5]*w
            bottom = detection[6]*h

            # 绘制
            cv.rectangle(image, (int(left), int(top)), (int(right), int(bottom)), (255, 0, 0), thickness=2)
            cv.putText(image, "score:%.2f"%score, (int(left), int(top)), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1)
    cv.imshow('face-detection-demo', image)
    c = cv.waitKey(2)
    if c == 27:
        break
cv.waitKey(0)
cv.destroyAllWindows()

实验结果

在这里插入图片描述

解释

OpenCV在DNN模块中提供了基于残差SSD网络训练的人脸检测模型,还支持单精度的fp16的检测准确度更好的Caffe模型加载与使用,这里实现了一个基于Caffe Model的视频实时人脸监测模型,基于Python代码的CPU运行,帧率均可以到达15以上。非常好用。


所有内容均来源于贾志刚老师的知识星球——OpenCV研习社,本文为个人整理学习,已获得贾老师授权,有兴趣、有能力的可以加入贾老师的知识星球进行深入学习。
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值