政策资讯

Policy Information


​​​​​​​CV:利用cv2(加载人脸识别xml文件及detectMultiScale函数得到人脸列表)+keras的load_model(加载表情hdf5、性别hdf5)并标注

来源: 重庆市软件正版化服务中心    |    时间: 2022-09-20    |    浏览量: 52560    |   

CV:利用cv2+自定义load_detection_model(加载人脸识别xml文件及detectMultiScale函数得到人脸列表)+keras的load_model(加载表情hdf5、性别hdf5)实现标注脸部表情和性别label
CV:利用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

  1. CV:基于Keras利用cv2+自定义load_detection_model(加载人脸识别xml文件及detectMultiScale函数得到人脸列表)+keras的load_model(加载表情hdf5、性别hdf5)实现标注脸部表情和性别label——Jason Niu
  2. import sys
  3. import cv2
  4. from keras.models import load_model
  5. import numpy as np
  6. image_path ="F:/File_Python/Resources/hezhao05.jpg"
  7. detection_model_path = '../trained_models/detection_models/haarcascade_frontalface_default.xml'
  8. emotion_model_path = '../trained_models/emotion_models/fer2013_mini_XCEPTION.102-0.66.hdf5'
  9. gender_model_path = '../trained_models/gender_models/simple_CNN.81-0.96.hdf5'
  10. emotion_labels = get_labels('fer2013')
  11. gender_labels = get_labels('imdb')
  12. font = cv2.FONT_HERSHEY_SIMPLEX
  13. gender_offsets = (30, 60)
  14. gender_offsets = (10, 10)
  15. emotion_offsets = (20, 40)
  16. emotion_offsets = (0, 0)
  17. face_detection = load_detection_model(detection_model_path)
  18. emotion_classifier = load_model(emotion_model_path, compile=False)
  19. gender_classifier = load_model(gender_model_path, compile=False)
  20. emotion_target_size = emotion_classifier.input_shape[1:3]
  21. gender_target_size = gender_classifier.input_shape[1:3]
  22. rgb_image = load_image(image_path, grayscale=False)
  23. gray_image = load_image(image_path, grayscale=True)
  24. gray_image = np.squeeze(gray_image)
  25. gray_image = gray_image.astype('uint8')
  26. faces = detect_faces(face_detection, gray_image)
  27. for face_coordinates in faces:
  28. x1, x2, y1, y2 = apply_offsets(face_coordinates, gender_offsets)
  29. rgb_face = rgb_image[y1:y2, x1:x2]
  30. x1, x2, y1, y2 = apply_offsets(face_coordinates, emotion_offsets)
  31. gray_face = gray_image[y1:y2, x1:x2]
  32. try:
  33. rgb_face = cv2.resize(rgb_face, (gender_target_size))
  34. gray_face = cv2.resize(gray_face, (emotion_target_size))
  35. except:
  36. continue
  37. rgb_face = preprocess_input(rgb_face, False)
  38. rgb_face = np.expand_dims(rgb_face, 0)
  39. gender_prediction = gender_classifier.predict(rgb_face)
  40. gender_label_arg = np.argmax(gender_prediction)
  41. gender_text = gender_labels[gender_label_arg]
  42. gray_face = preprocess_input(gray_face, True)
  43. gray_face = np.expand_dims(gray_face, 0)
  44. gray_face = np.expand_dims(gray_face, -1)
  45. emotion_label_arg = np.argmax(emotion_classifier.predict(gray_face))
  46. emotion_text = emotion_labels[emotion_label_arg]
  47. if gender_text == gender_labels[0]:
  48. color = (255, 255, 0)
  49. else:
  50. color = (255, 0, 0)
  51. draw_bounding_box(face_coordinates, rgb_image, color)
  52. draw_text(face_coordinates, rgb_image, gender_text, color, 0, -20, 1, 2)
  53. draw_text(face_coordinates, rgb_image, emotion_text, color, 0, -50, 1, 2)
  54. bgr_image = cv2.cvtColor(rgb_image, cv2.COLOR_RGB2BGR)
  55. save_img='F:/File_Python/Resources/hezhao041.jpg'
  56. cv2.imwrite(save_img, bgr_image)
  57. cv2.imshow('Emotion and Gender test', rgb_image)
  58. cv2.waitKey(0)
  59. cv2.destroyAllWindows()

相关案例推荐

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




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

文章知识点与官方知识档案匹配,可进一步学习相关知识
Python入门技能树人工智能深度学习123871 人正在系统学习中

评论

产品推荐

更多 >

QQ咨询 扫一扫加入群聊,了解更多平台咨询
微信咨询 扫一扫加入群聊,了解更多平台咨询
意见反馈
立即提交
QQ咨询
微信咨询
意见反馈