• [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()

  • 相关阅读:
    新加坡科技巨头Sea亏损小于预期,外资“清算”阿里只为加大赌注
    查看局域网是否有外网IP之收藏必备
    SpringCloud 微服务注册中心 Eureka - Client
    第七章 贝叶斯分类器(上)
    【slam十四讲第二版】【课后习题】【第九讲~后端1】
    HyperLynx(十七)SATA的设计与仿真
    java计算机毕业设计智慧校园系统后端源码+mysql数据库+系统+lw文档+部署
    Mybatis Plus一对多联表查询及分页解决方案
    mysql(十)mysql主从复制--主库切换
    C陷阱和缺陷 第3章 语义“陷阱” 3.2 非数组的指针
  • 原文地址:https://blog.csdn.net/FL1623863129/article/details/126271549