您的位置:首页 > 教育 > 培训 > 对人脸图像进行性别和年龄的判断

对人脸图像进行性别和年龄的判断

2025/5/18 22:10:38 来源:https://blog.csdn.net/2301_76794217/article/details/139359647  浏览:    关键词:对人脸图像进行性别和年龄的判断

判断性别和年龄

    • 导入必要的库
    • 加载预训练的人脸检测模型
    • 加载预训练的性别和年龄识别模型
    • 定义性别和年龄的标签列表
    • 创建Tkinter窗口:
    • 定义选择图片的函数:
    • 创建一个按钮,用于打开文件选择对话框
    • 定义显示图片的函数
    • 创建预测性别和年龄的函数
    • 创建预测性别和年龄的按钮
    • 运行Tkinter事件循环
    • 完整代码

导入必要的库

cv2:OpenCV库,用于图像处理。
numpy:用于数值计算。
tkinter:Python的标准GUI库,用于创建图形用户界面。
filedialog:Tkinter的文件对话框,用于打开文件。
Image:PIL(Pillow)库,用于图像处理。
ImageTk:PIL(Pillow)库的Tkinter封装,用于将PIL图像转换为Tkinter支持的格式。

import cv2
import numpy as np
import tkinter as tk
from tkinter import filedialog
from PIL import Image, ImageTk

加载预训练的人脸检测模型

使用cv2.CascadeClassifier加载Haar级联分类器,用于检测图像中的人脸。

face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

加载预训练的性别和年龄识别模型

使用cv2.dnn.readNetFromCaffe加载Caffe模型,用于预测人脸的性别和年龄。

gender_net = cv2.dnn.readNetFromCaffe('deploy_gender.prototxt', 'gender_net.caffemodel')
age_net = cv2.dnn.readNetFromCaffe('deploy_age.prototxt', 'age_net.caffemodel')

定义性别和年龄的标签列表

创建两个列表,分别用于存储性别和年龄的标签。

gender_list = ['man', 'woman']
age_list = ['(0-10)', '(10-15)', '(15-20)', '(20-30)', '(45-55)', '(55-65)', '(65-80)', '(80-100)']

创建Tkinter窗口:

创建一个名为"选择图片"的窗口,并设置其大小。

root = tk.Tk()
root.title("选择图片")
root.geometry("1000x620")

定义选择图片的函数:

使用filedialog.askopenfilename打开一个文件选择对话框,让用户选择要处理的图片。
如果用户选择了图片,则调用display_image函数来显示图片。

def select_image():file_path = filedialog.askopenfilename()if file_path:img = cv2.imread(file_path)if img is not None:display_image(file_path)

创建一个按钮,用于打开文件选择对话框

创建一个按钮,当用户点击时,会调用select_image函数。

open_image_btn = tk.Button(root, text="选择图片",font=('微软雅黑', 12), command=select_image)
open_image_btn.place(x=250,y=10)

定义显示图片的函数

读取用户选择的图片,并将其转换为PIL图像。
将PIL图像转换为Tkinter支持的格式,并在窗口中显示。

def display_image(file_path):# 确保 img 变量已经定义global imgimg = cv2.imread(file_path)if img is not None:# 调整图像大小到相等的大小img = cv2.resize(img, (300, 350))# 将 OpenCV 图像转换为 PIL 图像pil_image = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))# 将 PIL 图像转换为 tkinter 支持的格式image_tk = ImageTk.PhotoImage(pil_image)# 在 root 窗口中创建一个标签来显示图像label = tk.Label(root, image=image_tk)label.image = image_tk  # 保持引用,否则图像在重新绘制时会丢失label.place(x=60, y=50)

创建预测性别和年龄的函数

检测图片中的人脸。
对于每个检测到的人脸,裁剪出人脸区域。
将裁剪出的人脸区域转换为Caffe模型所需的格式,并输入模型进行预测。
预测出性别和年龄后,在原图上画出人脸框,并在框内显示性别和年龄。
将处理后的图片转换为PIL图像,并转换为Tkinter支持的格式,以便在窗口中显示。

def predict_gender_and_age():# 确保 img 变量已经定义global imgif img is not None:# 复制原始图像,以免影响其他预测result_img = img.copy()# 转换为灰度图像gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)# 检测人脸faces = face_cascade.detectMultiScale(gray, 1.1, 4)# 遍历检测到的人脸for (x, y, w, h) in faces:# 从原始图像中裁剪人脸区域face_img = img[y:y + h, x:x + w].copy()# 预处理人脸图像以适应神经网络输入blob = cv2.dnn.blobFromImage(face_img, 1, (227, 227), (78.4263377603, 87.7689143744, 114.895847746), swapRB=False)# 预测性别gender_net.setInput(blob)gender_preds = gender_net.forward()gender = gender_list[gender_preds[0].argmax()]# 预测年龄age_net.setInput(blob)age_preds = age_net.forward()age = age_list[age_preds[0].argmax()]# 在人脸周围画框并显示性别和年龄cv2.rectangle(result_img, (x, y), (x + w, y + h), (255, 255, 0), 2)cv2.putText(result_img, f'{gender}, {age}', (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 2, cv2.LINE_AA)# 将 OpenCV 图像转换为 PIL 图像pil_image = Image.fromarray(cv2.cvtColor(result_img, cv2.COLOR_BGR2RGB))# 将 PIL 图像转换为 tkinter 支持的格式image_tk = ImageTk.PhotoImage(pil_image)# 在 root 窗口中创建一个标签来显示图像label = tk.Label(root, image=image_tk)label.image = image_tk  # 保持引用,否则图像在重新绘制时会丢失label.place(x=550, y=50)

创建预测性别和年龄的按钮

创建一个按钮,当用户点击时,会调用predict_gender_and_age函数。

predict_gender_age_btn = tk.Button(root, text="预测性别和年龄",font=('微软雅黑', 12), command=predict_gender_and_age)
predict_gender_age_btn.place(x=650,y=10)

运行Tkinter事件循环

启动Tkinter的事件循环,使窗口保持打开状态。

root.mainloop()

完整代码

import cv2
import numpy as np
import tkinter as tk
from tkinter import filedialog
from PIL import Image, ImageTk# 加载预训练的人脸检测模型
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')# 加载预训练的性别和年龄识别模型
gender_net = cv2.dnn.readNetFromCaffe('deploy_gender.prototxt', 'gender_net.caffemodel')
age_net = cv2.dnn.readNetFromCaffe('deploy_age.prototxt', 'age_net.caffemodel')# 定义性别和年龄的标签列表
gender_list = ['man', 'woman']
age_list = ['(0-10)', '(10-15)', '(15-20)', '(20-30)', '(45-55)', '(55-65)', '(65-80)', '(80-100)']# 创建 tkinter 窗口
root = tk.Tk()
root.title("选择图片")
root.geometry("1000x620")
# 定义选择图片的函数
def select_image():file_path = filedialog.askopenfilename()if file_path:img = cv2.imread(file_path)if img is not None:display_image(file_path)# 创建一个按钮,用于打开文件选择对话框
open_image_btn = tk.Button(root, text="选择图片",font=('微软雅黑', 12), command=select_image)
open_image_btn.place(x=250,y=10)# 定义显示图片的函数
def display_image(file_path):# 确保 img 变量已经定义global imgimg = cv2.imread(file_path)if img is not None:# 调整图像大小到相等的大小img = cv2.resize(img, (300, 350))# 将 OpenCV 图像转换为 PIL 图像pil_image = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))# 将 PIL 图像转换为 tkinter 支持的格式image_tk = ImageTk.PhotoImage(pil_image)# 在 root 窗口中创建一个标签来显示图像label = tk.Label(root, image=image_tk)label.image = image_tk  # 保持引用,否则图像在重新绘制时会丢失label.place(x=60, y=50)# label.pack()# 创建预测性别和年龄的函数
def predict_gender_and_age():# 确保 img 变量已经定义global imgif img is not None:# 复制原始图像,以免影响其他预测result_img = img.copy()# 转换为灰度图像gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)# 检测人脸faces = face_cascade.detectMultiScale(gray, 1.1, 4)# 遍历检测到的人脸for (x, y, w, h) in faces:# 从原始图像中裁剪人脸区域face_img = img[y:y + h, x:x + w].copy()# 预处理人脸图像以适应神经网络输入blob = cv2.dnn.blobFromImage(face_img, 1, (227, 227), (78.4263377603, 87.7689143744, 114.895847746), swapRB=False)# 预测性别gender_net.setInput(blob)gender_preds = gender_net.forward()gender = gender_list[gender_preds[0].argmax()]# 预测年龄age_net.setInput(blob)age_preds = age_net.forward()age = age_list[age_preds[0].argmax()]# 在人脸周围画框并显示性别和年龄cv2.rectangle(result_img, (x, y), (x + w, y + h), (255, 255, 0), 2)cv2.putText(result_img, f'{gender}, {age}', (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 2, cv2.LINE_AA)# 将 OpenCV 图像转换为 PIL 图像pil_image = Image.fromarray(cv2.cvtColor(result_img, cv2.COLOR_BGR2RGB))# 将 PIL 图像转换为 tkinter 支持的格式image_tk = ImageTk.PhotoImage(pil_image)# 在 root 窗口中创建一个标签来显示图像label = tk.Label(root, image=image_tk)label.image = image_tk  # 保持引用,否则图像在重新绘制时会丢失label.place(x=550, y=50)# 创建预测性别和年龄的按钮
predict_gender_age_btn = tk.Button(root, text="预测性别和年龄",font=('微软雅黑', 12), command=predict_gender_and_age)
predict_gender_age_btn.place(x=650,y=10)# 运行 tkinter 事件循环
root.mainloop()

![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/4c3da06144d14c74a6952d75d6dddcd3.png

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com