CV:基于Keras利用cv2+自定义(加载人脸识别xml文件)+keras的load_model(加载表情hdf5、性别hdf5)实现标注脸部表情和性别label

简介: CV:基于Keras利用cv2+自定义(加载人脸识别xml文件)+keras的load_model(加载表情hdf5、性别hdf5)实现标注脸部表情和性别label

输出结果

image.png


设计思路

image.png


核心代码

#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()  


相关文章
|
19天前
|
XML 数据格式
小米备份descript.xml文件
小米备份descript.xml文件
21 0
|
1月前
|
XML Java 数据库连接
mybatis中在xml文件中通用查询结果列如何使用
mybatis中在xml文件中通用查询结果列如何使用
33 0
|
1月前
|
XML JavaScript 前端开发
xml文件使用及解析
xml文件使用及解析
|
2月前
|
SQL
Mybatis.xml文件中大于小于等于
Mybatis.xml文件中大于小于等于
9 0
|
2月前
|
XML 关系型数据库 MySQL
【Mysql】有关数据库中一对多/一对一,多对一xml中文件映射问题
【Mysql】有关数据库中一对多/一对一,多对一xml中文件映射问题
19 0
|
2月前
|
XML C# 数据格式
使用C#操作XML文件
使用C#操作XML文件
12 0
|
25天前
|
XML 数据格式 Windows
如何从xml文件创建R语言数据框dataframe
如何从xml文件创建R语言数据框dataframe
|
21小时前
|
XML 数据格式
加载 XML 字符串
这段代码展示如何在不同浏览器中加载和解析XML字符串。对于非IE浏览器,它使用DOMParser创建一个新的解析器实例,然后调用parseFromString方法。在IE中,它创建一个ActiveXObject,使用loadXML方法进行解析。注意,IE设置async为false以同步加载。
|
1天前
|
Android开发
android string.xml文件中的整型和string型代替
android string.xml文件中的整型和string型代替
|
1天前
|
XML 前端开发 数据格式
BeautifulSoup 是一个 Python 库,用于从 HTML 和 XML 文件中提取数据
BeautifulSoup 是 Python 的一个库,用于解析 HTML 和 XML 文件,即使在格式不规范的情况下也能有效工作。通过创建 BeautifulSoup 对象并使用方法如 find_all 和 get,可以方便地提取和查找文档中的信息。以下是一段示例代码,展示如何安装库、解析 HTML 数据以及打印段落、链接和特定类名的元素。BeautifulSoup 还支持更复杂的查询和文档修改功能。
8 1

相关课程

更多