首页 互联网资讯 检测脸部情绪有多难?10行代码就可以搞定!

检测脸部情绪有多难?10行代码就可以搞定!

互联网资讯 315
2024-01-07,

面部表情展示人类内心的情感。它们帮助我们识别一个人是愤怒、悲伤、快乐还是正常。医学研究人员也使用面部情绪来检测和了解一个人的心理健康。

人工智能在识别一个人的情绪方面可以发挥很大的作用。在卷积神经网络的帮助下,我们可以根据一个人的图像或实时视频来识别他的情绪。

Facial Expression Recognition 是一个 Python 库,可用于以更少的努力和更少的代码行检测一个人的情绪。它是使用 Python 中实现的 Tensorflow 和 Keras 库通过深度神经网络开发的。其中使用的数据集来自表示学习中的 Kaggle 竞赛挑战:面部表情识别挑战。

安装

我们可以使用 pip 在本地系统中安装库。只需运行下面的命令,就会看到您的库正在安装。

pip install per

依赖项:

OpenCV 3.2+ Tensorflow 1.7+ Python 3.6+

预测图像上的情绪 from fer import FER import matplotlib.pyplot as plt img = plt.imread("img.jpg") detector = FER(mtcnn=True) print(detector.detect_emotions(img)) plt.imshow(img)

使用 emotion.py 保存并简单地使用 Python emotion.py 运行它。

输出:

[OrderedDict([(‘box’, (160, 36, 99, 89)), (’emotions’, {‘angry’: 0.0, ‘disgust’: 0.0, ‘fear’: 0.0, ‘happy’: 1.0, ‘sad’: 0.0, ‘surprise’: 0.0, ‘neutral’: 0.0})])]

实时预测的 Web 应用程序代码

from fer import FER import matplotlib.pyplot as plt import streamlit as st from PIL import Image, ImageOps st.write(''' # Emotion Detector ''') st.write("A Image Classification Web App That Detects the Emotions Based On An Image") file = st.file_uploader("Please Upload an image of Person With Face", type=['jpg','png']) if file is None: st.text("Please upload an image file") else: image = Image.open(file) detector = FER(mtcnn=True) result = detector.detect_emotions(image) st.write(result) st.image(image, use_column_width=True)

用 Emotion _ web.py 保存 Python 文件。

运行 streamlit run FILENAME.py

复制 URL 并粘贴到你的浏览器中,就可以看到网页应用程序的运行情况。

PS:本文来源:检测脸部情绪有多难?10行代码就可以搞定!,Python,人工智能,神经网络,开发,作者:小P