Python利用opencv库实现识别静态图像中的人数、年龄、性别

1.明确环境

以下代码运行使用的环境为:Python3.6+opencv4.3.0.38

2.所需文件

“opencv_face_detector.pbtxt”
“opencv_face_detector_uint8.pb”
“age_deploy.prototxt”
“age_net.caffemodel”
“gender_deploy.prototxt”
“gender_net.caffemodel”
我将源代码、训练好的模型以及测试图片进行了整理,下载地址为:
https://download.csdn.net/download/sunshine_boy1/87653224

3.代码
import cv2 as cv
import time

# 加载网络
faceProto = "opencv_face_detector.pbtxt"
faceModel = "opencv_face_detector_uint8.pb"
ageProto = "age_deploy.prototxt"
ageModel = "age_net.caffemodel"
genderProto = "gender_deploy.prototxt"
genderModel = "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']
faceNet = cv.dnn.readNet(faceModel, faceProto)
ageNet = cv.dnn.readNet(ageModel, ageProto)
genderNet = cv.dnn.readNet(genderModel, genderProto)

# 检测人脸并绘制人脸bounding box
def getFaceBox(net, frame, conf_threshold=0.7):
    frameOpencvDnn = frame.copy()
    frameHeight, frameWidth = frameOpencvDnn.shape[:2]
    blob = cv.dnn.blobFromImage(frameOpencvDnn, 1.0, (300, 300), MODEL_MEAN_VALUES, True, False)
    net.setInput(blob)
    detections = net.forward()
    bboxes = []
    face_count = 0
    for i in range(detections.shape[2]):
        confidence = detections[0, 0, i, 2]
        if confidence > conf_threshold:
            x1, y1, x2, y2 = (detections[0, 0, i, 3:7] * [frameWidth, frameHeight, frameWidth, frameHeight]).astype(int)
            bboxes.append([x1, y1, x2, y2])
            cv.rectangle(frameOpencvDnn, (x1, y1), (x2, y2), (0, 255, 0), int(round(frameHeight / 150)), 8)
            face_count += 1
    cv.putText(frameOpencvDnn, "Faces detected: " + str(face_count), (50, 50), cv.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2, cv.LINE_AA)
    return frameOpencvDnn, bboxes

# 打开一个图片文件
img_file = "hihi.jpg"
frame = cv.imread(img_file)

padding = 20
frameFace, bboxes = getFaceBox(faceNet, frame)

if not bboxes:
    print("未检测到人脸")
else:
    for bbox in bboxes:
        x1, y1, x2, y2 = bbox
        face = frame[max(0, y1 - padding):min(y2 + padding, frame.shape[0] - 1),
               max(0, x1 - padding):min(x2 + 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()]

        ageNet.setInput(blob)
        agePreds = ageNet.forward()
        age = ageList[agePreds[0].argmax()]

        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("Detecting age and gender", frameFace)

    cv.waitKey(0)
    cv.destroyAllWindows()

注意:将模型以及图片的地址改为自己的

4.效果

在这里插入图片描述

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

奋进的小hang

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

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

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

打赏作者

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

抵扣说明:

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

余额充值