本博文是基于tensorflow的mtcnn算法来检测人脸,效果超好,然后用fer2013数据集训练一个表情识别的网络,最后将检测的人脸进行情绪识别。一共可以识别以下7种表情:
angry
disgust
fear
happy
sad
surprise
neutral
项目目录:只需运行下面的main.py文件即可。
可以支持图片视频和摄像头,多人脸的情绪识别。
下面是部分代码,如需要全部代码可自行下载:
import os
import cv2
import numpy as np
from keras.applications.imagenet_utils import preprocess_input
from model_bq import mini_XCEPTION
import utils.utils as utils
from net.mobilenet import MobileNet
from net.mtcnn import mtcnn
def preprocess_input(x, v2=True):
x = x.astype('float32')
x = x / 255.0
if v2:
x = x - 0.5
x = x * 2.0
return x
class face_rec():
def __init__(self):
#-------------------------#
# 创建mtcnn的模型
# 用于检测人脸
#-------------------------#
self.mtcnn_model = mtcnn()
self.threshold = [0.5,0.6,0.8]
#-------------------------#
# 创建mobilenet的模型
# 用于判断是否佩戴口罩
#-------------------------#
self.classes_path = "model_data/classes.txt"
self.class_names = self._get_class()
self.Crop_HEIGHT = 224
self.Crop_WIDTH = 224
self.NUM_CLASSES = len(self.class_names)
self.mask_model = mini_XCEPTION(input_shape=(48,48,1),num_classes=7)
self.mask_model.load_weights("./model_data/ep122-loss0.845-val_loss0.971.h5")
def _get_class(self):
classes_path = os.path.expanduser(self.classes_path)
with open(classes_path) as f:
class_names = f.readlines()
class_names = [c.strip() for c in class_names]
return class_names
def recognize(self,draw):
height,width,_ = np.