专栏首页贾志刚-OpenCV学堂OpenCV实现年龄与性别预测

OpenCV实现年龄与性别预测

关注:OpenCV干货与教程第一时间送达! 欢迎星标或者置顶【OpenCV学堂】

概述

前面我写了很多篇关于OpenCV DNN应用相关的文章,这里再来一篇文章,用OpenCV DNN实现一个很有趣好玩的例子,基于Caffe的预训练模型实现年龄与性别预测,这个在很多展会上都有展示,OpenCV DNN实现这里非常简洁明了,总共不到100行的代码。下面就来说一下怎么实现的,首先下载两个Caffe的预训练模型:

Gender Net and Age Net

https://www.dropbox.com/s/iyv483wz7ztr9gh/gender_net.caffemodel?dl=0"

https://www.dropbox.com/s/xfb20y596869vbb/age_net.caffemodel?dl=0"

上述两个模型一个是预测性别的,一个是预测年龄的,性别预测返回的是一个二分类结果

Male
Female

年龄预测返回的是8个年龄的阶段!

'(0-2)', 
'(4-6)', 
'(8-12)', 
'(15-20)', 
'(25-32)', 
'(38-43)', 
'(48-53)', 
'(60-100)'

人脸检测是基于OPenCV DNN模块自带的残差网络的人脸检测算法模型!非常的强大与好用!

实现步骤

完整的实现步骤需要如下几步:

  1. 预先加载三个网络模型
  2. 打开摄像头视频流/加载图像
  3. 对每一帧进行人脸检测 - 对检测到的人脸进行性别与年龄预测 - 解析预测结果 - 显示结果

代码实现详解

加载模型

MODEL_MEAN_VALUES = (78.4263377603, 87.7689143744, 114.895847746)
ageList = ['(0-2)', '(4-6)', '(8-12)', '(15-20)', '(25-32)', '(38-43)', '(48-53)', '(60-100)']
genderList = ['Male', 'Female']

# Load network
ageNet = cv.dnn.readNet(ageModel, ageProto)
genderNet = cv.dnn.readNet(genderModel, genderProto)
faceNet = cv.dnn.readNet(faceModel, faceProto)

人脸检测

frameOpencvDnn = frame.copy()
    frameHeight = frameOpencvDnn.shape[0]
    frameWidth = frameOpencvDnn.shape[1]
    blob = cv.dnn.blobFromImage(frameOpencvDnn, 1.0, (300, 300), [104, 117, 123], True, False)

    net.setInput(blob)
    detections = net.forward()
    bboxes = []
    for i in range(detections.shape[2]):
        confidence = detections[0, 0, i, 2]
        if confidence > conf_threshold:
            x1 = int(detections[0, 0, i, 3] * frameWidth)
            y1 = int(detections[0, 0, i, 4] * frameHeight)
            x2 = int(detections[0, 0, i, 5] * frameWidth)
            y2 = int(detections[0, 0, i, 6] * frameHeight)
            bboxes.append([x1, y1, x2, y2])
            cv.rectangle(frameOpencvDnn, (x1, y1), (x2, y2), (0, 255, 0), int(round(frameHeight/150)), 8)

性别与年龄预测

    for bbox in bboxes:
        # print(bbox)
        face = frame[max(0,bbox[1]-padding):min(bbox[3]+padding,frame.shape[0]-1),max(0,bbox[0]-padding):min(bbox[2]+padding, frame.shape[1]-1)]

        blob = cv.dnn.blobFromImage(face, 1.0, (227, 227), MODEL_MEAN_VALUES, swapRB=False)
        genderNet.setInput(blob)
        genderPreds = genderNet.forward()
        gender = genderList[genderPreds[0].argmax()]
        # print("Gender Output : {}".format(genderPreds))
        print("Gender : {}, conf = {:.3f}".format(gender, genderPreds[0].max()))

        ageNet.setInput(blob)
        agePreds = ageNet.forward()
        age = ageList[agePreds[0].argmax()]
        print("Age Output : {}".format(agePreds))
        print("Age : {}, conf = {:.3f}".format(age, agePreds[0].max()))

        label = "{},{}".format(gender, age)
        cv.putText(frameFace, label, (bbox[0], bbox[1]-10), cv.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 255), 2, cv.LINE_AA)
        cv.imshow("Age Gender Demo", frameFace)
    print("time : {:.3f} ms".format(time.time() - t))

运行效果(看到这个预测,我又相信技术了!@_@):

完整源代码:

import cv2 as cv
import time


def getFaceBox(net, frame, conf_threshold=0.7):
    frameOpencvDnn = frame.copy()
    frameHeight = frameOpencvDnn.shape[0]
    frameWidth = frameOpencvDnn.shape[1]
    blob = cv.dnn.blobFromImage(frameOpencvDnn, 1.0, (300, 300), [104, 117, 123], True, False)

    net.setInput(blob)
    detections = net.forward()
    bboxes = []
    for i in range(detections.shape[2]):
        confidence = detections[0, 0, i, 2]
        if confidence > conf_threshold:
            x1 = int(detections[0, 0, i, 3] * frameWidth)
            y1 = int(detections[0, 0, i, 4] * frameHeight)
            x2 = int(detections[0, 0, i, 5] * frameWidth)
            y2 = int(detections[0, 0, i, 6] * frameHeight)
            bboxes.append([x1, y1, x2, y2])
            cv.rectangle(frameOpencvDnn, (x1, y1), (x2, y2), (0, 255, 0), int(round(frameHeight/150)), 8)
    return frameOpencvDnn, bboxes


faceProto = "D:/projects/opencv_tutorial/data/models/face_detector/opencv_face_detector.pbtxt"
faceModel = "D:/projects/opencv_tutorial/data/models/face_detector/opencv_face_detector_uint8.pb"

ageProto = "D:/projects/opencv_tutorial/data/models/cnn_age_gender_models/age_deploy.prototxt"
ageModel = "D:/projects/opencv_tutorial/data/models/cnn_age_gender_models/age_net.caffemodel"

genderProto = "D:/projects/opencv_tutorial/data/models/cnn_age_gender_models/gender_deploy.prototxt"
genderModel = "D:/projects/opencv_tutorial/data/models/cnn_age_gender_models/gender_net.caffemodel"

MODEL_MEAN_VALUES = (78.4263377603, 87.7689143744, 114.895847746)
ageList = ['(0-2)', '(4-6)', '(8-12)', '(15-20)', '(25-32)', '(38-43)', '(48-53)', '(60-100)']
genderList = ['Male', 'Female']

# Load network
ageNet = cv.dnn.readNet(ageModel, ageProto)
genderNet = cv.dnn.readNet(genderModel, genderProto)
faceNet = cv.dnn.readNet(faceModel, faceProto)

# Open a video file or an image file or a camera stream
cap = cv.VideoCapture(0)
padding = 20
while cv.waitKey(1) < 0:
    # Read frame
    t = time.time()
    hasFrame, frame = cap.read()
    frame = cv.flip(frame, 1)
    if not hasFrame:
        cv.waitKey()
        break

    frameFace, bboxes = getFaceBox(faceNet, frame)
    if not bboxes:
        print("No face Detected, Checking next frame")
        continue

    for bbox in bboxes:
        # print(bbox)
        face = frame[max(0,bbox[1]-padding):min(bbox[3]+padding,frame.shape[0]-1),max(0,bbox[0]-padding):min(bbox[2]+padding, frame.shape[1]-1)]

        blob = cv.dnn.blobFromImage(face, 1.0, (227, 227), MODEL_MEAN_VALUES, swapRB=False)
        genderNet.setInput(blob)
        genderPreds = genderNet.forward()
        gender = genderList[genderPreds[0].argmax()]
        # print("Gender Output : {}".format(genderPreds))
        print("Gender : {}, conf = {:.3f}".format(gender, genderPreds[0].max()))

        ageNet.setInput(blob)
        agePreds = ageNet.forward()
        age = ageList[agePreds[0].argmax()]
        print("Age Output : {}".format(agePreds))
        print("Age : {}, conf = {:.3f}".format(age, agePreds[0].max()))

        label = "{},{}".format(gender, age)
        cv.putText(frameFace, label, (bbox[0], bbox[1]-10), cv.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 255), 2, cv.LINE_AA)
        cv.imshow("Age Gender Demo", frameFace)
    print("time : {:.3f} ms".format(time.time() - t))

源码与模型下载地址

https://github.com/gloomyfish1998/opencv_tutorial

本文分享自微信公众号 - OpenCV学堂(CVSCHOOL),作者:gloomyfish

原文出处及转载信息见文内详细说明,如有侵权,请联系 yunjia_community@tencent.com 删除。

原始发表时间:2019-03-11

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • OpenCV中神经网络介绍与使用

    OpenCV中神经网络介绍与使用 一:神经网络介绍 人工神经网络(ANN) 简称神经网络(NN),最早它的产生跟并行计算有关系,主要是学习生物神经元互联触发实现...

    OpenCV学堂
  • OpenVINO开发教程之八 – 道路分割

    基于OpenVINO预训练模块中的道路分割模型,实现像素级别的图像分割,把像素划分为如下四个类别

    OpenCV学堂
  • OpenCV图像处理之基于积分图实现NCC快速相似度匹配

    基于Normalized cross correlation(NCC)用来比较两幅图像的相似程度已经是一个常见的图像处理手段。在工业生产环节检测、监控领域对对象...

    OpenCV学堂
  • OpenCV实现年龄与性别预测

    前面我写了很多篇关于OpenCV DNN应用相关的文章,这里再来一篇文章,用OpenCV DNN实现一个很有趣好玩的例子,基于Caffe的预训练模型实现年龄与性...

    小小詹同学
  • 走近流行强化学习算法:最优Q-Learning

    Q-Learning 是最著名的强化学习算法之一。我们将在本文中讨论该算法的一个重要部分:探索策略。但是在开始具体讨论之前,让我们从一些入门概念开始吧。

    啦啦啦
  • Source Insight3.x注册码

    官方下载地址: http://www.sourceinsight.com/distribute/Si3563Setup.exe 官方网站: http://www...

    py3study
  • GPS网络校时服务器如何选择?

    随着科学技术的不断发展,各行各业对授时设备的功能和授时精度也在不断提升,所以需要GPS网络校时服务器来满足授时的需求。GPS网络校时服务器是指能够接收GPS卫星...

    时频专家
  • 正能量猫,到哪哪亮——Scratch发光猫实验

    偶然想起一句话“请保持你心里的光,因为你不知道谁会借此走出黑暗。”心里小感动,决定用Scratch表达一下,于是有了本案例中走哪哪亮的正能量猫。 效果如下:

    一石匠人
  • kubernetes下jenkins实战maven项目编译构建

    在kubernetes环境部署的jenkins集群,执行任务时会新建pod,任务完成后pod被销毁,架构如下:

    程序员欣宸
  • 特写 | CVPR十年轶事:走出象牙塔

    这不是石建萍第一次来 CVPR 了。过去这八年,她几乎没落下过一届,倒也习惯了每年办一次美国签证。只不过,这么多年以来,参加 CVPR 的身份却在不断变化:从一...

    机器之心

扫码关注云+社区

领取腾讯云代金券