深度学习之人物年龄预测

在这里插入图片描述

1.年龄检测

在这里插入图片描述
论文地址:《Age and Gender Classification using Convolutional Neural Networks》
论文作者提出了一个简单的类似AlexNet的网络结构,该网络总共学习了8个年龄段:

  1. 0-2
  2. 4-6
  3. 8-12
  4. 15-20
  5. 25-32
  6. 38-43
  7. 48-53
  8. 60-100

注意:这些年龄段不是连续的
首先,要搞懂检测年龄适合用回归还是用分类来做
举个栗子:

1. 回归
在这里插入图片描述

2. 分类
在这里插入图片描述
年龄预测是基于面部外观,有的人保养的好,显得年轻,实际年龄与测得会有差别。在不结合其它有关信息作推断情况下,网络模型很难预测到实际的真实年龄。若看作是回归问题,模型很难预测到图像中年龄的一个准确值,而看作是分类问题,预测一个年龄段相对来说模型更容易训练,比回归产生更高的准确性。

2.思路方法

自动识别年龄步骤:

1. 检测出输入图像或视频中的人脸
2. 提取面部感兴趣区域(ROI)
3. 用年龄检测器预测人物的年龄
4. 返回结果

对于检测人脸的分类器:

分类器 优缺点
Haar级联 速度快,嵌入式设备上运行,但准确性低
HOG +线性SVM 相比Haar级联精确,但速度慢,对遮挡,面部角度变化时检测效果不好
深度学习检测器 相比以上两者效果最佳,但需消耗更多计算资源

3.代码实现

环境:

  • win10
  • pycharm
  • anaconda3
  • python3.7
  • opencv4.2.0

对于OpenCV尽量用最新版本,可参考这篇仅一个命令行简单快速安装:https://blog.csdn.net/y459541195/article/details/104851892

文件结构:

在这里插入图片描述

3.1 单张图像检测代码

import numpy as np
import cv2

"""
#图片年龄预测
执行:
python test_age.py 

"""
# 检测年龄段
AGE_LIST = ["(0-2)","(4-6)","(8-12)","(15-20)","(25-32)","(38-43)","(48-53)","(60-100)"]

# 人脸检测模型路径
prototxtPathF ="./models/face_detector/face_deploy.prototxt"
weightsPathF = "./models/face_detector/res10_300x300_ssd_iter_140000.caffemodel"
# 加载人脸模型
faceNet = cv2.dnn.readNet(prototxtPathF,weightsPathF)

# 年龄检测模型
prototxtPathA ="./models/age_detector/age_deploy.prototxt"
weightsPathA = "./models/age_detector/age_net.caffemodel"
#加载模型
ageNet = cv2.dnn.readNet(prototxtPathA,weightsPathA)

#获取图像
image = cv2.imread("./input/test01.jpg")
src = image.copy()
(h,w)= image.shape[:2]

# 构造blob
blob = cv2.dnn.blobFromImage(image,1.0,(300,300),
                             (104,177,123))
# 送入网络计算
faceNet.setInput(blob)
detect = faceNet.forward()
# 检测
for i in range(0,detect.shape[2]):
    confidence = detect[0,0,i,2]
    # 过滤掉小的置信度,计算坐标,提取面部roi,构造面部blob特征
    if confidence > 0.5:
        box = detect[0,0,i,3:7]*np.array([w,h,w,h])
        (startX,startY,endX,endY) = box.astype("int")
        face = image[startY:endY,startX:endX]
        faceBlob = cv2.dnn.blobFromImage(face, 1.0, (227, 227),
                                         (78.4263377603, 87.7689143744, 114.895847746),
                                         swapRB=False)
        # 预测年龄
        ageNet.setInput(faceBlob)
        predictions = ageNet.forward()
        i = predictions[0].argmax()
        age = AGE_LIST[i]
        ageConfidence = predictions[0][i]

        #显示打印
        text = "age{}:{:.2f}%".format(age,ageConfidence*100)
        print(text)

        #绘制显示框
        y = startY - 10 if startY - 10 > 10 else startY + 10
        cv2.rectangle(image, (startX, startY), (endX, endY),
                      (0, 0, 255), 2)
        cv2.putText(image, text, (startX, y),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)

cv2.imshow("Result",image)
cv2.waitKey(0)

3.2 视频流检测代码

import numpy as np
import cv2
import imutils

"""
#视频流年龄预测
执行:
python test_video_age.py 
"""
def detect_age(frame,faceNet,ageNet,minConfidence=0.5):
    # 检测年龄段
    AGE_LIST = ["(0-2)","(4-6)","(8-12)","(15-20)","(25-32)","(38-43)","(48-53)","(60-100)"]
    #定义空列表存放结果
    results = []
    (h,w)= frame.shape[:2]
    # 构造blob
    blob = cv2.dnn.blobFromImage(frame,1.0,(300,300),
                                 (104,177,123))
    # 送入网络计算
    faceNet.setInput(blob)
    detect = faceNet.forward()
    # 检测
    for i in range(0,detect.shape[2]):
        confidence = detect[0,0,i,2]
        # 过滤掉小的置信度,计算坐标,提取面部roi,
        if confidence > minConfidence:
            box = detect[0,0,i,3:7]*np.array([w,h,w,h])
            (startX,startY,endX,endY) = box.astype("int")
            face = frame[startY:endY,startX:endX]
            # 过滤干扰
            if face.shape[0]<20 or face.shape[1]<20:
                continue
            # 构造面部blob特
            faceBlob = cv2.dnn.blobFromImage(face, 1.0, (227, 227),
                                             (78.4263377603, 87.7689143744, 114.895847746),
                                             swapRB=False)
            # 预测年龄
            ageNet.setInput(faceBlob)
            predictions = ageNet.forward()
            i = predictions[0].argmax()
            age = AGE_LIST[i]
            ageConfidence = predictions[0][i]
            # 构造字典存放结果
            dicts = {
    
    
               "location":(startX,startY,endX,endY),
               "age":(age,ageConfidence)
           }
        results.append(dicts)
    return results



# 人脸检测模型路径
prototxtPathF ="./models/face_detector/face_deploy.prototxt"
weightsPathF = "./models/face_detector/res10_300x300_ssd_iter_140000.caffemodel"
# 加载人脸模型
faceNet = cv2.dnn.readNet(prototxtPathF,weightsPathF)

# 年龄检测模型
prototxtPathA ="./models/age_detector/age_deploy.prototxt"
weightsPathA = "./models/age_detector/age_net.caffemodel"
#加载模型
ageNet = cv2.dnn.readNet(prototxtPathA,weightsPathA)

#获取视频图像
videoPath = "./input/test1.mp4"
vs = cv2.VideoCapture(videoPath)

#处理视频流
while True:
    (grabbed,frame) = vs.read()

    # 判断是否结束
    if not grabbed:
        print("无视频读取...")
        break
    frame = imutils.resize(frame,width=720)

    #调用上面函数计算
    results = detect_age(frame,faceNet,ageNet,minConfidence=0.5)
    for  i in results:
        #显示信息
        text = "age{}:{:.2f}%".format(i["age"][0], i["age"][1] * 100)
        (startX,startY,endX,endY) = i["location"]
        # 绘制显示框
        y = startY - 10 if startY - 10 > 10 else startY + 10
        cv2.rectangle(frame, (startX, startY), (endX, endY),
                      (0, 0, 255), 2)
        cv2.putText(frame, text, (startX, y),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)
    # 显示
    cv2.imshow("Result", frame)
    key = cv2.waitKey(1) & 0xFF
    # 按q键退出循环
    if key == ord("q"):
        break
cv2.destroyAllWindows()
vs.release()

4.测试结果

4.1 单张图像测试

虚拟环境下命令行输入:

python test_age.py 

效果一:
在这里插入图片描述

效果二:

在这里插入图片描述

效果三:
在这里插入图片描述

4.2 视频流检测

深度学习之年龄检测


视频地址: https://www.bilibili.com/video/BV16g4y187iQ/

Reference:
1.https://talhassner.github.io/home/publication/2015_CVPR
2.https://github.com/dpressel/rude-carnie
3.https://github.com/GilLevi/AgeGenderDeepLearning

猜你喜欢

转载自blog.csdn.net/y459541195/article/details/105611391