面部表情展示人类内心的情感。它们帮助我们识别一个人是愤怒、悲伤、快乐还是正常。医学研究人员也使用面部情绪来检测和了解一个人的心理健康。
人工智能在识别一个人的情绪方面可以发挥很大的作用。在卷积神经网络的帮助下,我们可以根据一个人的图像或实时视频来识别他的情绪。
Facial Expression Recognition 是一个 Python 库,可用于以更少的努力和更少的代码行检测一个人的情绪。它是使用 Python 中实现的 Tensorflow 和 Keras 库通过深度神经网络开发的。其中使用的数据集来自表示学习中的 Kaggle 竞赛挑战:面部表情识别挑战。
☞☞☞AI 智能聊天, 问答助手, AI 智能搜索, 免费无限量使用 DeepSeek R1 模型☜☜☜
我们可以使用 pip 在本地系统中安装库。只需运行下面的命令,就会看到您的库正在安装。
pip install per
依赖项:
from fer import FERimport 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 FERimport matplotlib.pyplot as pltimport streamlit as stfrom PIL import Image, ImageOpsst.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 并粘贴到你的浏览器中,就可以看到网页应用程序的运行情况。
以上就是用10行代码如何轻松检测脸部情绪?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号