政策资讯

Policy Information


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

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

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

目录

输出结果

设计思路

核心代码


输出结果

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

设计思路


 

核心代码

  1. CV:基于keras利用cv2自带两步检测法对《跑男第六季第五期》"如花片段"(或调用摄像头)进行实时脸部表情检测——Jason Niu
  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. emotion_labels = get_labels('fer2013')
  8. frame_window = 10
  9. emotion_offsets = (20, 40)
  10. face_detection = load_detection_model(detection_model_path)
  11. emotion_classifier = load_model(emotion_model_path, compile=False)
  12. emotion_target_size = emotion_classifier.input_shape[1:3]
  13. emotion_window = []
  14. cv2.namedWindow('window_frame,by Jason Niu') 摄像头窗口名称
  15. video_capture = cv2.VideoCapture(0) 函数定义摄像头对象,其参数0表示第一个摄像头,一般就是笔记本的内建摄像头。
  16. video_capture = cv2.VideoCapture("F:\File_Python\Python_example\YOLOv3_use_TF\RunMan5.mp4")
  17. while True:
  18. bgr_image = video_capture.read()[1]
  19. gray_image = cv2.cvtColor(bgr_image, cv2.COLOR_BGR2GRAY)
  20. rgb_image = cv2.cvtColor(bgr_image, cv2.COLOR_BGR2RGB)
  21. faces = detect_faces(face_detection, gray_image)
  22. for face_coordinates in faces:
  23. x1, x2, y1, y2 = apply_offsets(face_coordinates, emotion_offsets)
  24. gray_face = gray_image[y1:y2, x1:x2] [坐标参数,尺寸参数]
  25. try:
  26. gray_face = cv2.resize(gray_face, (emotion_target_size))
  27. except:
  28. continue
  29. gray_face = preprocess_input(gray_face, True)
  30. gray_face = np.expand_dims(gray_face, 0)
  31. gray_face = np.expand_dims(gray_face, -1)
  32. emotion_prediction = emotion_classifier.predict(gray_face)
  33. emotion_probability = np.max(emotion_prediction)
  34. emotion_label_arg = np.argmax(emotion_prediction)
  35. emotion_text = emotion_labels[emotion_label_arg]
  36. emotion_window.append(emotion_text)
  37. if len(emotion_window) > frame_window:
  38. emotion_window.pop(0)
  39. try:
  40. emotion_mode = mode(emotion_window)
  41. except:
  42. continue
  43. if条件根据不同表情显示不同颜色
  44. if emotion_text == 'angry':
  45. color = emotion_probability * np.asarray((255, 0, 0)) 红色
  46. elif emotion_text == 'sad':
  47. color = emotion_probability * np.asarray((0, 0, 255)) 蓝色
  48. elif emotion_text == 'happy':
  49. color = emotion_probability * np.asarray((255, 255, 0)) 黄色
  50. elif emotion_text == 'surprise':
  51. color = emotion_probability * np.asarray((0, 255, 255)) 青色
  52. else:
  53. color = emotion_probability * np.asarray((0, 255, 0)) 绿色
  54. color = color.astype(int)
  55. color = color.tolist()
  56. draw_bounding_box(face_coordinates, rgb_image, color)
  57. draw_text(face_coordinates, rgb_image, emotion_mode,
  58. color, 0, -45, 1, 4)
  59. bgr_image = cv2.cvtColor(rgb_image, cv2.COLOR_RGB2BGR)
  60. cv2.namedWindow("window_frame,by Jason Niu",0);
  61. cv2.resizeWindow("window_frame,by Jason Niu", 640, 380);
  62. cv2.imshow('window_frame,by Jason Niu', bgr_image)
  63. if cv2.waitKey(1) & 0xFF == ord('q'):
  64. break

评论

产品推荐

更多 >

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