基于图像的面部表情识别
fer的Python项目详细描述
FER
面部表情识别。
安装
目前,fer只支持python 3.6以后的版本。可以安装 通过PIP:
$ pip install fer
此实现要求opencv>;=3.2和tensorflow>;=1.7.0 安装在系统中,具有python3的绑定。
它们可以通过PIP安装(如果PIP版本>;=9.0.1):
$ pip install tensorflow>=1.7 opencv-contrib-python==3.3.0.9
或直接从源代码编译 (OpenCV3, Tensorflow)。
注意,如果GPU设备 在系统上可用,这将加速结果。可能是 与PIP一起安装:
$ pip install tensorflow-gpu\>=1.7.0
用法
下面的示例演示了此软件包的易用性:
fromferimportFERimportcv2img=cv2.imread("justin.jpg")detector=FER()detector.detect_emotions(img)
样本输出:
OrderedDict([{'box': [277, 90, 48, 63], 'emotions': {'angry': 0.02, 'disgust': 0.0, 'fear': 0.05, 'happy': 0.16, 'neutral': 0.09, 'sad': 0.27, 'surprise': 0.41}])
只想要最强烈的情感?尝试:
emotion,score=detector.top_emotion(img)# 'happy', 0.99
mtcnn面部识别
默认情况下,使用opencv的haar级联分类器检测面。为了使用更精确的MTCNN网络, 添加参数:
detector=FER(mtcnn=True)
视频
为了识别视频中的面部表情,Video
类将视频分割成帧。它可以在后端使用本地keras模型(默认)或peltarion api:
fromferimportVideofromferimportFERvideo_filename="tests/woman2.mp4"video=Video(video_filename)# Analyze video, displaying the outputdetector=FER(mtcnn=True)raw_data=video.analyze(detector,display=True)df=video.to_pandas(raw_data)
检测器返回一个json对象列表。每个json对象都包含 两个键:“box”和“emotions”:
- 边框的格式为键下的[X,Y,Width,Height] “盒子”。
- 这些情绪被格式化为一个json对象,并带有“anger”键, “厌恶”、“恐惧”、“快乐”、“悲伤”、“惊讶”和“中立”。
其他好的用法示例可以在文件中找到 example.py和video-example.py 位于此存储库的根目录中。
Peltarion API
将api url作为emotion_model
参数添加到FER()
:
detector = FER(emotion_model=MY_API_URL)
然后,fer将从环境中获取url和标记EMOTION_API_URL
和EMOTION_API_TOKEN
。
型号
fer捆绑了keras模型,以及对Peltarionapi的支持。
该模型是一个卷积神经网络,权值保存到hdf5。
与模块路径相关的data
文件夹中的文件。可能是
在
使用emotion_model
参数实例化。
许可证
学分
此代码包括复制或派生自 伊万·德帕斯·森特诺的implementation MTCNN和Octavia Arriaga的facial expression recognition repo。
参考
FER 2013数据集由Pierre Luc Carrier和Aaron Courville管理,如所述:
《表示学习的挑战:三机器学习竞赛的报告》,由Ian J. Goodfellow、Dumitru Erhan、Pierre Luc Carrier、Aaron Courville、Mehdi Mirza、Ben Hamner、Will Cukierski、So、Y、Y、Y、Y、Y、Y、Y、Y、Y、Y、John Shawe Taylor,Maxim Milakov,John Park,Radu Ionescu,Marius Popescu,Cristian Grozea,James Bergstra,Pig,Y,Y,γ,和,^ {A12}。