面部表情展示人類內(nèi)心的情感。它們幫助我們識別一個人是憤怒、悲傷、快樂還是正常。醫(yī)學(xué)研究人員也使用面部情緒來檢測和了解一個人的心理健康。
人工智能在識別一個人的情緒方面可以發(fā)揮很大的作用。在卷積神經(jīng)網(wǎng)絡(luò)的幫助下,我們可以根據(jù)一個人的圖像或?qū)崟r視頻來識別他的情緒。
Facial Expression Recognition 是一個 Python 庫,可用于以更少的努力和更少的代碼行檢測一個人的情緒。它是使用 Python 中實現(xiàn)的 Tensorflow 和 Keras 庫通過深度神經(jīng)網(wǎng)絡(luò)開發(fā)的。其中使用的數(shù)據(jù)集來自表示學(xué)習(xí)中的 Kaggle 競賽挑戰(zhàn):面部表情識別挑戰(zhàn)。
我們可以使用 pip 在本地系統(tǒng)中安裝庫。只需運行下面的命令,就會看到您的庫正在安裝。
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’, {‘a(chǎn)ngry’: 0.0, ‘disgust’: 0.0, ‘fear’: 0.0, ‘happy’: 1.0, ‘sad’: 0.0, ‘surprise’: 0.0, ‘neutral’: 0.0})])]
實時預(yù)測的 Web 應(yīng)用程序代碼
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
復(fù)制 URL 并粘貼到你的瀏覽器中,就可以看到網(wǎng)頁應(yīng)用程序的運行情況。
本文鏈接:http://www.www897cc.com/showinfo-26-57883-0.html檢測臉部情緒有多難?10行代碼就可以搞定!
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。郵件:2376512515@qq.com
上一篇: 在 Rust 編程中使用多線程