​​​​​​​CV之FR:基于cv2(加载人脸识别xml文件及detectMultiScale得到人脸特征)、keras的load_model(加载表情hdf5、性别hdf5)实现表情识别和性别识别

28 篇文章 10 订阅

CV:利用cv2+自定义load_detection_model(加载人脸识别xml文件及detectMultiScale函数得到人脸列表)+keras的load_model(加载表情hdf5、性别hdf5)实现标注脸部表情和性别labelCV之FR:基于cv2(加载人脸识别xml文件及detectMultiScale得到人脸特征)、keras的load_model(加载表情hdf5、性别hdf5)实现表情识别和性别识别

目录

基于cv2(加载人脸识别xml文件及detectMultiScale得到人脸特征)、keras的load_model(加载表情hdf5、性别hdf5)实现表情识别和性别识别

输出结果

设计思路

核心代码

相关案例推荐


基于cv2(加载人脸识别xml文件及detectMultiScale得到人脸特征)、keras的load_model(加载表情hdf5、性别hdf5)实现表情识别和性别识别

输出结果

设计思路

核心代码

gitee链接

https://gitee.com/yunyaniu/Python_GUI/tree/master/SR%20and%20TD%20with%20BCT%20to%20Automatic%20Alarm%20GDCup/CV_face_classification_emotion_genderhttps://gitee.com/yunyaniu/Python_GUI/tree/master/SR%20and%20TD%20with%20BCT%20to%20Automatic%20Alarm%20GDCup/CV_face_classification_emotion_gender

#CV:基于Keras利用cv2+自定义load_detection_model(加载人脸识别xml文件及detectMultiScale函数得到人脸列表)+keras的load_model(加载表情hdf5、性别hdf5)实现标注脸部表情和性别label——Jason Niu
import sys

import cv2
from keras.models import load_model
import numpy as np

image_path ="F:/File_Python/Resources/hezhao05.jpg"
detection_model_path = '../trained_models/detection_models/haarcascade_frontalface_default.xml'
emotion_model_path = '../trained_models/emotion_models/fer2013_mini_XCEPTION.102-0.66.hdf5'
gender_model_path = '../trained_models/gender_models/simple_CNN.81-0.96.hdf5'
emotion_labels = get_labels('fer2013') 
gender_labels = get_labels('imdb')      
font = cv2.FONT_HERSHEY_SIMPLEX   

gender_offsets = (30, 60) 
gender_offsets = (10, 10)  
emotion_offsets = (20, 40)
emotion_offsets = (0, 0)

face_detection = load_detection_model(detection_model_path)
emotion_classifier = load_model(emotion_model_path, compile=False)
gender_classifier = load_model(gender_model_path, compile=False)


emotion_target_size = emotion_classifier.input_shape[1:3] 
gender_target_size = gender_classifier.input_shape[1:3]

rgb_image = load_image(image_path, grayscale=False)  
gray_image = load_image(image_path, grayscale=True) 
gray_image = np.squeeze(gray_image) 
gray_image = gray_image.astype('uint8') 

faces = detect_faces(face_detection, gray_image)

for face_coordinates in faces: 
    x1, x2, y1, y2 = apply_offsets(face_coordinates, gender_offsets)
    rgb_face = rgb_image[y1:y2, x1:x2]    

    x1, x2, y1, y2 = apply_offsets(face_coordinates, emotion_offsets)
    gray_face = gray_image[y1:y2, x1:x2]  

    try:
        rgb_face = cv2.resize(rgb_face, (gender_target_size))
        gray_face = cv2.resize(gray_face, (emotion_target_size))
    except:
        continue
    rgb_face = preprocess_input(rgb_face, False)
    rgb_face = np.expand_dims(rgb_face, 0)   
    gender_prediction = gender_classifier.predict(rgb_face)  
    gender_label_arg = np.argmax(gender_prediction)
    gender_text = gender_labels[gender_label_arg]   

    gray_face = preprocess_input(gray_face, True)
    gray_face = np.expand_dims(gray_face, 0)
    gray_face = np.expand_dims(gray_face, -1)
    emotion_label_arg = np.argmax(emotion_classifier.predict(gray_face))
    emotion_text = emotion_labels[emotion_label_arg]

    if gender_text == gender_labels[0]: 
        color = (255, 255, 0)
    else:
        color = (255, 0, 0)

    draw_bounding_box(face_coordinates, rgb_image, color)  
    draw_text(face_coordinates, rgb_image, gender_text, color, 0, -20, 1, 2)
    draw_text(face_coordinates, rgb_image, emotion_text, color, 0, -50, 1, 2)
 
bgr_image = cv2.cvtColor(rgb_image, cv2.COLOR_RGB2BGR) 
save_img='F:/File_Python/Resources/hezhao041.jpg'
cv2.imwrite(save_img, bgr_image)

cv2.imshow('Emotion and Gender test', rgb_image)  

cv2.waitKey(0)
cv2.destroyAllWindows()  

相关案例推荐

类似案例:https://blog.csdn.net/qq_41185868/article/details/90488469



相关文章
CV:利用cv2+自定义load_detection_model(加载人脸识别xml文件及detectMultiScale函数得到人脸列表)+keras的load_model(加载表情hdf5、性别hdf5)实现标注脸部表情和性别label

评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

一个处女座的程序猿

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

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

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

打赏作者

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

抵扣说明:

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

余额充值