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


    导入必要的库

    cv2OpenCV库,用于图像处理。
    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 img
        img = 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 img
        if 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 img
        img = 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 img
        if 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

  • 相关阅读:
    第二章 MyBatis入门
    第七章《Java的异常处理》第3节:异常的嵌套处理
    人大女王金融硕士项目——无论在任何时候,都不必怀疑努力的意义
    深度学习-支持向量机(SVM)
    前端框架海洋:如何破浪前行,寻找你的“黄金舟”
    【GlobalMapper精品教程】004:生成标准经纬网图幅(1:100万)案例教程
    SnapHelper解析
    趣味证书制作生成微信小程序源码下载-安装简单方便
    对话以太坊Vitalik及其父,至暗时刻为什么加密比金钱更重要?
    2022年东湖科学城建设扶持政策申报奖励补贴标准以及认定条件汇总
  • 原文地址:https://blog.csdn.net/2301_76794217/article/details/139359647