keras系列︱人脸表情分类与识别:opencv人脸检测+Keras情绪分类(四)

文章转自:https://blog.csdn.net/sinat_26917383/article/details/72885715




人脸识别热门,表情识别更加。但是表情识别很难,因为人脸的微表情很多,本节介绍一种比较粗线条的表情分类与识别的办法。


Keras系列:

1、keras系列︱Sequential与Model模型、keras基本结构功能(一)
2、keras系列︱Application中五款已训练模型、VGG16框架(Sequential式、Model式)解读(二)
3、keras系列︱图像多分类训练与利用bottleneck features进行微调(三)
4、keras系列︱人脸表情分类与识别:opencv人脸检测+Keras情绪分类(四)
5、keras系列︱迁移学习:利用InceptionV3进行fine-tuning及预测、完整案例(五)


本次讲述的表情分类是识别的分析流程分为:

  • 1、加载pre-model网络与权重;
  • 2、利用opencv的函数进行简单的人脸检测;
  • 3、抠出人脸的图并灰化;
  • 4、表情分类器检测

.


一、表情数据集

主要来源于kaggle比赛,下载地址
有七种表情类别: (0=Angry, 1=Disgust, 2=Fear, 3=Happy, 4=Sad, 5=Surprise, 6=Neutral).
数据是48x48 灰度图,格式比较奇葩。
第一列是情绪分类,第二列是图像的numpy,第三列是train or test。
这里写图片描述
.


二、opencv的人脸识别

参考《opencv+Recorder︱OpenCV 中使用 Haar 分类器进行面部检测》
理论略过,直接来看重点:
(1)加载人脸检测器,haarcascade_frontalface_default.xml;
(2)图片加载并灰化,cvtColor,可参考: opencv︱图像的色彩空間cvtColor(HSV、HSL、HSB 、BGR)
(2)人脸探测,detectMultiScale.

# (1)加载人脸检测器
cascPath = '/.../haarcascade_frontalface_default.xml'
faceCascade = cv2.CascadeClassifier(cascPath)

# (2)图片加载并灰化
jpg_file = '/home/ubuntu/keras/image/8c80abb4gw1f3b5hxd3aaj20jg0cx411.jpg'
img_gray = cv2.imread(jpg_file)
img_gray = cv2.cvtColor(img_gray, cv2.COLOR_BGR2GRAY)

# 人脸探测
faces = faceCascade.detectMultiScale(
        img_gray,
        scaleFactor=1.1,
        minNeighbors=1,# minNeighbors=5比较难检测
        minSize=(30, 30),
        flags=cv2.cv.CV_HAAR_SCALE_IMAGE
    )
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

其中minNeighbors设置小一些,容易检测出来。这个检测器还是有点粗糙。
.


三、表情分类与识别

本节源自github的mememoji
网络结构:
这里写图片描述

这里写图片描述

opencv中的人脸检测的pre-model文件(haarcascade_frontalface_default.xml)和表情识别pre-model文件(model.h5)都在作者的github下载。

是利用Keras实现的。直接来看完整的代码:

import cv2
import sys
import json
import time
import numpy as np
from keras.models import model_from_json


emotion_labels = ['angry', 'fear', 'happy', 'sad', 'surprise', 'neutral']

# load json and create model arch
json_file = open('/.../model.json','r')
loaded_model_json = json_file.read()
json_file.close()
model = model_from_json(loaded_model_json)

# load weights into new model
model.load_weights('/.../model.h5')

def predict_emotion(face_image_gray): # a single cropped face
    resized_img = cv2.resize(face_image_gray, (48,48), interpolation = cv2.INTER_AREA)
    # cv2.imwrite(str(index)+'.png', resized_img)
    image = resized_img.reshape(1, 1, 48, 48)
    list_of_list = model.predict(image, batch_size=1, verbose=1)
    angry, fear, happy, sad, surprise, neutral = [prob for lst in list_of_list for prob in lst]
    return [angry, fear, happy, sad, surprise, neutral]


# -------------------直接预测-----------------------
img_gray = cv2.imread('/.../real-time_emotion_analyzer-master/meme_faces/angry-angry.png')
img_gray = cv2.cvtColor(img_gray, cv2.COLOR_BGR2GRAY)
angry, fear, happy, sad, surprise, neutral = predict_emotion(img_gray)


# -------------------人脸预测-----------------------
# 加载检测器
cascPath = '/.../real-time_emotion_analyzer-master/haarcascade_frontalface_default.xml'
faceCascade = cv2.CascadeClassifier(cascPath)

# 图像灰化
jpg_file = '/.../001.jpg'
img_gray = cv2.imread(jpg_file)
img_gray = cv2.cvtColor(img_gray, cv2.COLOR_BGR2GRAY)

# 人脸检测
faces = faceCascade.detectMultiScale(
        img_gray,
        scaleFactor=1.1,
        minNeighbors=1,# minNeighbors=5比较难检测
        minSize=(30, 30),
        flags=cv2.cv.CV_HAAR_SCALE_IMAGE
    )

# 表情画框
for (x, y, w, h) in faces:
    face_image_gray = img_gray[y:y+h, x:x+w]
    cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
    angry, fear, happy, sad, surprise, neutral = predict_emotion(face_image_gray)
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58



人脸识别热门,表情识别更加。但是表情识别很难,因为人脸的微表情很多,本节介绍一种比较粗线条的表情分类与识别的办法。


猜你喜欢

转载自blog.csdn.net/junchengberry/article/details/81089743