• [python][deepface][原创]使用deepface进行表情识别


    1. # -*- coding: utf-8 -*-
    2. # Copyright (C) 2022 FIRC. All Rights Reserved
    3. # @Time : 2022/8/10 下午5:41
    4. # @Author : FIRC
    5. # @File : emotion_detect.py
    6. # @Software: PyCharm
    7. # @ Function Description:
    8. '''
    9. function as follows:
    10. '''
    11. import numpy as np
    12. from deepface.basemodels import VGGFace
    13. import tensorflow as tf
    14. import cv2
    15. import os
    16. from deepface.detectors import FaceDetector
    17. from deepface.commons import functions
    18. import tensorflow as tf
    19. tf_version = int(tf.__version__.split(".")[0])
    20. if tf_version == 1:
    21. import keras
    22. from keras.models import Model, Sequential
    23. from keras.layers import Conv2D, MaxPooling2D, AveragePooling2D, Flatten, Dense, Dropout
    24. elif tf_version == 2:
    25. from tensorflow import keras
    26. from tensorflow.keras.models import Model, Sequential
    27. from tensorflow.keras.layers import Conv2D, MaxPooling2D, AveragePooling2D, Flatten, Dense, Dropout
    28. def loadModel(weights='./weights/facial_expression_model_weights.h5'):
    29. num_classes = 7
    30. model = Sequential()
    31. # 1st convolution layer
    32. model.add(Conv2D(64, (5, 5), activation='relu', input_shape=(48, 48, 1)))
    33. model.add(MaxPooling2D(pool_size=(5, 5), strides=(2, 2)))
    34. # 2nd convolution layer
    35. model.add(Conv2D(64, (3, 3), activation='relu'))
    36. model.add(Conv2D(64, (3, 3), activation='relu'))
    37. model.add(AveragePooling2D(pool_size=(3, 3), strides=(2, 2)))
    38. # 3rd convolution layer
    39. model.add(Conv2D(128, (3, 3), activation='relu'))
    40. model.add(Conv2D(128, (3, 3), activation='relu'))
    41. model.add(AveragePooling2D(pool_size=(3, 3), strides=(2, 2)))
    42. model.add(Flatten())
    43. # fully connected neural networks
    44. model.add(Dense(1024, activation='relu'))
    45. model.add(Dropout(0.2))
    46. model.add(Dense(1024, activation='relu'))
    47. model.add(Dropout(0.2))
    48. model.add(Dense(num_classes, activation='softmax'))
    49. # ----------------------------
    50. home = functions.get_deepface_home()
    51. model.load_weights(weights)
    52. return model
    53. detector_backend = 'opencv'
    54. face_detector = FaceDetector.build_model(detector_backend)
    55. emotion_model = loadModel()
    56. emotion_labels = ['Angry', 'Disgust', 'Fear', 'Happy', 'Sad', 'Surprise', 'Neutral']
    57. cap = cv2.VideoCapture(0) # webcam
    58. while True:
    59. ret, img = cap.read()
    60. if img is None:
    61. break
    62. try:
    63. # faces store list of detected_face and region pair
    64. faces = FaceDetector.detect_faces(face_detector, detector_backend, img, align=False)
    65. except: # to avoid exception if no face detected
    66. faces = []
    67. for face, (x, y, w, h) in faces:
    68. if w > 130: # discard small detected faces
    69. roi_img = img[y:y + h, x:x + w]
    70. gray_img = functions.preprocess_face(img=roi_img, target_size=(48, 48), grayscale=True,
    71. enforce_detection=False, detector_backend='opencv')
    72. emotion_predictions = emotion_model.predict(gray_img)[0, :]
    73. print(emotion_predictions)
    74. # print(apparent_age)
    75. cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 3) # draw rectangle to main image
    76. cv2.putText(img, 'emotion={}'.format(emotion_labels[np.argmax(emotion_predictions)]), (x - 10, y), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 255)
    77. , 3)
    78. cv2.imshow('result', img)
    79. if cv2.waitKey(1) & 0xFF == ord('q'): # press q to quit
    80. break
    81. # kill open cv things
    82. cap.release()
    83. cv2.destroyAllWindows()

  • 相关阅读:
    46届世界技能大赛湖北省选拔赛wp 3.0
    力扣1005-K 次取反后最大化的数组和——贪心算法
    【无标题】
    Spring: Feign原理解析
    MySQL 1055报错 -this is incompatible with sql_mode=only_full_group_by
    体验昇腾Ascend C 编程语言极简易用的算子开发
    「MySQL高级篇」MySQL存储引擎
    MFC-TCP网络编程客户端-Socket
    npm install常见错误的完整指南
    【算法1-6】二分查找与二分答案——查找
  • 原文地址:https://blog.csdn.net/FL1623863129/article/details/126271549