python-opencv:基于SSD的人脸检测

SSD:

SSD是一种基于深度学习的目标检测算法,opencv在3.3版本以后将其引入作为基于深度学习的人脸检测器;

  • 模型

opencv实现的SSD人脸检测器的骨干网络是REsNet-10,当前它提供了两个训练好的模型:基于深度学习框架caffe训练的模型和基于TensorFlow训练的模型

下载地址:https://github.com/opencv/opencv/blob/master/samples/dnn/face_detector/weights.meta4

  • 配置文件

下载地址:

https://github.com/opencv/opencv/tree/master/samples/dnn/face_detector

face_detector文件分析:
deploy.prototxt:调用.caffemodel时的测试网络文件
how_to_train_face_detector.txt:如何使用自定义数据集来训练网络的说明
solver.prototxt:超参数文件
test.prototxt:测试网络文件
train.prototxt:训练网络文件

  • 模型导入方式

(1)基于深度学习框架caffe训练的模型导入

  1. config_file = "face_detector/deploy.prototxt";

  2. model_file = "face_detector/res10_300x300_ssd_iter_140000_fp16.caffemodel";

  3. net = cv2.dnn.readNetFromCaffe(config_file, model_file)

(2)基于TensorFlow训练的模型导入

  1. config_file = "face_detector/opencv_face_detect.pbtxt";

  2. model_file = "face_detector/opencv_face_detect_uint8.pb";

  3. net = cv2.dnn.readNetFromTensorflow(model_file, config_file)

完整测试示例: 

# -*- coding: utf-8 -*-
"""
Created on Sun Jun 16 09:27:01 2019

@author: zfjua
"""

import cv2 as cv
from cv2 import dnn

net = dnn.readNetFromCaffe('./data/face_detector/deploy.prototxt', './data/face_detector/res10_300x300_ssd_iter_140000_fp16.caffemodel')


inWidth = 300
inHeight = 300
confThreshold = 0.5

frame = cv.imread('.\\image\\test.png')
cols = frame.shape[1]
rows = frame.shape[0]

net.setInput(dnn.blobFromImage(frame, 1.0, (inWidth, inHeight), (104.0, 177.0, 123.0), False, False))
detections = net.forward()
# perf_stats = net.getPerfProfile()
# print('Inference time, ms: %.2f' % (perf_stats[0] / cv.getTickFrequency() * 1000))
for i in range(detections.shape[2]):
    confidence = detections[0, 0, i, 2]
    if confidence > confThreshold:
        xLeftBottom = int(detections[0, 0, i, 3] * cols)
        yLeftBottom = int(detections[0, 0, i, 4] * rows)
        xRightTop = int(detections[0, 0, i, 5] * cols)
        yRightTop = int(detections[0, 0, i, 6] * rows)

        cv.rectangle(frame, (xLeftBottom, yLeftBottom), (xRightTop, yRightTop), (0, 0, 255))

cv.imshow("detections", frame)
cv.waitKey(0)

cv.destroyAllWindows()

  • 2
    点赞
  • 49
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JoannaJuanCV

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值