python 实现表情识别


Posted in Python onNovember 21, 2020

表情识别

表情识别支持7种表情类型,生气、厌恶、恐惧、开心、难过、惊喜、平静等。

实现思路

使用OpenCV识别图片中的脸,在使用keras进行表情识别。

效果预览

python 实现表情识别

实现代码

与《性别识别》相似,本文表情识别也是使用keras实现的,和性别识别相同,型数据使用的是oarriaga/face_classification的,代码如下:

#coding=utf-8
#表情识别

import cv2
from keras.models import load_model
import numpy as np
import chineseText
import datetime

startTime = datetime.datetime.now()
emotion_classifier = load_model(
  'classifier/emotion_models/simple_CNN.530-0.65.hdf5')
endTime = datetime.datetime.now()
print(endTime - startTime)

emotion_labels = {
  0: '生气',
  1: '厌恶',
  2: '恐惧',
  3: '开心',
  4: '难过',
  5: '惊喜',
  6: '平静'
}

img = cv2.imread("img/emotion/emotion.png")
face_classifier = cv2.CascadeClassifier(
  "C:\Python36\Lib\site-packages\opencv-master\data\haarcascades\haarcascade_frontalface_default.xml"
)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_classifier.detectMultiScale(
  gray, scaleFactor=1.2, minNeighbors=3, minSize=(40, 40))
color = (255, 0, 0)

for (x, y, w, h) in faces:
  gray_face = gray[(y):(y + h), (x):(x + w)]
  gray_face = cv2.resize(gray_face, (48, 48))
  gray_face = gray_face / 255.0
  gray_face = np.expand_dims(gray_face, 0)
  gray_face = np.expand_dims(gray_face, -1)
  emotion_label_arg = np.argmax(emotion_classifier.predict(gray_face))
  emotion = emotion_labels[emotion_label_arg]
  cv2.rectangle(img, (x + 10, y + 10), (x + h - 10, y + w - 10),
         (255, 255, 255), 2)
  img = chineseText.cv2ImgAddText(img, emotion, x + h * 0.3, y, color, 20)

cv2.imshow("Image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

以上就是python 实现表情识别的详细内容,更多关于python 表情识别的资料请关注三水点靠木其它相关文章!

Python 相关文章推荐
CentOS 6.X系统下升级Python2.6到Python2.7 的方法
Oct 12 Python
windows下安装Python和pip终极图文教程
Mar 05 Python
Python3中使用PyMongo的方法详解
Jul 28 Python
python打包压缩、读取指定目录下的指定类型文件
Apr 12 Python
python实现关键词提取的示例讲解
Apr 28 Python
python 堆和优先队列的使用详解
Mar 05 Python
django框架CSRF防护原理与用法分析
Jul 22 Python
Python统计分析模块statistics用法示例
Sep 06 Python
使用python实现哈希表、字典、集合操作
Dec 22 Python
TensorFlow打印输出tensor的值
Apr 19 Python
PyQt5 文本输入框自动补全QLineEdit的实现示例
May 13 Python
keras模型保存为tensorflow的二进制模型方式
May 25 Python
python 实现性别识别
Nov 21 #Python
python遍历路径破解表单的示例
Nov 21 #Python
Python如何批量生成和调用变量
Nov 21 #Python
在终端启动Python时报错的解决方案
Nov 20 #Python
python 批量下载bilibili视频的gui程序
Nov 20 #Python
Python ellipsis 的用法详解
Nov 20 #Python
python 动态渲染 mysql 配置文件的示例
Nov 20 #Python
You might like
一个简单且很好用的php分页类
2013/10/26 PHP
php ci框架中加载css和js文件失败的原因及解决方法
2014/07/29 PHP
php生成rss类用法实例
2015/04/14 PHP
PHP类的封装与继承详解
2015/09/29 PHP
PHP微信开发之根据用户回复关键词\位置返回附近信息
2016/06/24 PHP
Laravel 验证码认证学习记录小结
2019/12/20 PHP
Laravel5.1 框架响应基本用法实例分析
2020/01/04 PHP
js动态生成指定行数的表格
2013/07/11 Javascript
javascript中的 object 和 function小结
2016/08/14 Javascript
基于slideout.js实现移动端侧边栏滑动特效
2016/11/28 Javascript
AngularJS使用angular.bootstrap完成模块手动加载的方法分析
2017/01/19 Javascript
js a标签点击事件
2017/03/30 Javascript
JavaScript输入分钟、秒倒计时技巧总结(附代码)
2017/08/17 Javascript
jQuery选择器之属性筛选选择器用法详解
2017/09/19 jQuery
mpvue+vant app搭建微信小程序的方法步骤
2019/02/11 Javascript
微信小程序登录session的使用
2019/03/17 Javascript
Vue中img的src是动态渲染时不显示的解决
2019/11/14 Javascript
node.js中path路径模块的使用方法实例分析
2020/02/13 Javascript
JS实现简易留言板(节点操作)
2020/03/16 Javascript
python生成九宫格图片
2018/11/19 Python
python数组循环处理方法
2019/08/26 Python
PyCharm2020.1.2社区版安装,配置及使用教程详解(Windows)
2020/08/07 Python
了解一下python内建模块collections
2020/09/07 Python
记录一下scrapy中settings的一些配置小结
2020/09/28 Python
python学习之使用Matplotlib画实时的动态折线图的示例代码
2021/02/25 Python
CSS伪类与CSS伪元素的区别及由来具体说明
2012/12/07 HTML / CSS
出门问问全球官方商城:Tichome音箱和TicWatch智能手表
2017/12/02 全球购物
Nili Lotan官网:Nili Lotan同名品牌
2018/01/07 全球购物
保安部任务及岗位职责
2014/02/25 职场文书
2014年党员干部四风问题自我剖析材料
2014/09/29 职场文书
2014年化验室工作总结
2014/11/21 职场文书
黄山导游词
2015/01/31 职场文书
职位证明模板
2015/06/23 职场文书
回门宴新娘答谢词
2015/09/29 职场文书
领导干部学习心得体会
2016/01/23 职场文书
关于的python五子棋的算法
2022/05/02 Python