CV:基于keras利用cv2自带两步检测法对《跑男第六季第五期》之如花片段(或调用摄像头)进行实时性别&脸部表情检测


中科曙光招聘
中科曙光招聘 2022-09-20 09:29:35 51918
分类专栏: 资讯

CV:基于keras利用cv2自带两步检测法对《跑男第六季第五期》之如花片段(或调用摄像头)进行实时性别&脸部表情检测

目录

输出结果

设计思路

核心代码


输出结果

设计思路

核心代码

  1. from statistics import mode
  2. import cv2
  3. from keras.models import load_model
  4. import numpy as np
  5. detection_model_path = '../trained_models/detection_models/haarcascade_frontalface_default.xml'
  6. emotion_model_path = '../trained_models/emotion_models/fer2013_mini_XCEPTION.102-0.66.hdf5'
  7. gender_model_path = '../trained_models/gender_models/simple_CNN.81-0.96.hdf5'
  8. emotion_labels = get_labels('fer2013')
  9. gender_labels = get_labels('imdb')
  10. font = cv2.FONT_HERSHEY_SIMPLEX
  11. frame_window = 10
  12. gender_offsets = (30, 60)
  13. emotion_offsets = (20, 40)
  14. face_detection = load_detection_model(detection_model_path)
  15. emotion_classifier = load_model(emotion_model_path, compile=False)
  16. gender_classifier = load_model(gender_model_path, compile=False)
  17. emotion_target_size = emotion_classifier.input_shape[1:3]
  18. gender_target_size = gender_classifier.input_shape[1:3]
  19. gender_window = []
  20. emotion_window = []
  21. cv2.namedWindow('window_frame_by_Jason_Niu')
  22. video_capture = cv2.VideoCapture(0)
  23. video_capture = cv2.VideoCapture("F:\File_Python\Python_example\YOLOv3_use_TF\RunMan5.mp4")
  24. while True:
  25. bgr_image = video_capture.read()[1]
  26. gray_image = cv2.cvtColor(bgr_image, cv2.COLOR_BGR2GRAY) 分别将读取的图像进行灰化、RGB化处理
  27. rgb_image = cv2.cvtColor(bgr_image, cv2.COLOR_BGR2RGB)
  28. faces = detect_faces(face_detection, gray_image)
  29. for face_coordinates in faces:
  30. x1, x2, y1, y2 = apply_offsets(face_coordinates, gender_offsets)
  31. rgb_face = rgb_image[y1:y2, x1:x2]
  32. x1, x2, y1, y2 = apply_offsets(face_coordinates, emotion_offsets)
  33. gray_face = gray_image[y1:y2, x1:x2]
  34. try:
  35. rgb_face = cv2.resize(rgb_face, (gender_target_size))
  36. gray_face = cv2.resize(gray_face, (emotion_target_size))
  37. except:
  38. continue
  39. gray_face = preprocess_input(gray_face, False)
  40. gray_face = np.expand_dims(gray_face, 0)
  41. gray_face = np.expand_dims(gray_face, -1)
  42. emotion_label_arg = np.argmax(emotion_classifier.predict(gray_face))
  43. emotion_text = emotion_labels[emotion_label_arg]
  44. emotion_window.append(emotion_text)
  45. rgb_face = np.expand_dims(rgb_face, 0)
  46. rgb_face = preprocess_input(rgb_face, False)
  47. gender_prediction = gender_classifier.predict(rgb_face)
  48. gender_label_arg = np.argmax(gender_prediction)
  49. gender_text = gender_labels[gender_label_arg]
  50. gender_window.append(gender_text)
  51. if len(gender_window) > frame_window:
  52. emotion_window.pop(0)
  53. gender_window.pop(0)
  54. try:
  55. emotion_mode = mode(emotion_window)
  56. gender_mode = mode(gender_window)
  57. except:
  58. continue
  59. if gender_text == gender_labels[0]:
  60. color = (0, 0, 255)
  61. else:
  62. color = (255, 0, 0)
  63. draw_bounding_box(face_coordinates, rgb_image, color)
  64. draw_text(face_coordinates, rgb_image, gender_mode,
  65. color, 0, -20, 1, 4)
  66. draw_text(face_coordinates, rgb_image, emotion_mode,
  67. color, 0, -45, 1, 4)
  68. bgr_image = cv2.cvtColor(rgb_image, cv2.COLOR_RGB2BGR)
  69. cv2.namedWindow("window_frame_by_Jason_Niu",0);
  70. cv2.resizeWindow("window_frame_by_Jason_Niu", 640, 380);
  71. cv2.imshow('window_frame_by_Jason_Niu', bgr_image)
  72. if cv2.waitKey(1) & 0xFF == ord('q'):
  73. break

网站声明:如果转载,请联系本站管理员。否则一切后果自行承担。

本文链接:https://www.xckfsq.com/news/show.html?id=3970
赞同 0
评论 0 条
中科曙光招聘L0
粉丝 0 发表 10 + 关注 私信
上周热门
如何使用 StarRocks 管理和优化数据湖中的数据?  2672
【软件正版化】软件正版化工作要点  2637
统信UOS试玩黑神话:悟空  2532
信刻光盘安全隔离与信息交换系统  2216
镜舟科技与中启乘数科技达成战略合作,共筑数据服务新生态  1092
grub引导程序无法找到指定设备和分区  743
WPS City Talk · 校招西安站来了!  15
金山办公2024算法挑战赛 | 报名截止日期更新  15
看到某国的寻呼机炸了,就问你用某水果手机发抖不?  14
有在找工作的IT人吗?  13
本周热议
我的信创开放社区兼职赚钱历程 40
今天你签到了吗? 27
信创开放社区邀请他人注册的具体步骤如下 15
如何玩转信创开放社区—从小白进阶到专家 15
方德桌面操作系统 14
我有15积分有什么用? 13
用抖音玩法闯信创开放社区——用平台宣传企业产品服务 13
如何让你先人一步获得悬赏问题信息?(创作者必看) 12
2024中国信创产业发展大会暨中国信息科技创新与应用博览会 9
中央国家机关政府采购中心:应当将CPU、操作系统符合安全可靠测评要求纳入采购需求 8

加入交流群

请使用微信扫一扫!