概要
今回はpythonでFERを使用して感情を数値化する方法について記載していきます。
勉強用に簡易的に記載していますので、ご了承ください。
実行環境
・windows:Windows11Pro 23H2
・Python 3.12.3
参考サイト
スクリプト
emotion.py
from fer import FER
import matplotlib.pyplot as plt
# 画像の取得
test_image_one = plt.imread("顔が映っている画像のパス")
emo_detector = FER(mtcnn=True)
captured_emotion = emo_detector.detect_emotions(test_image_one)
# 全ての感情の取得
INITDATA = 0
EMOTIONS = {"angry":INITDATA,"disgust":INITDATA,"fear":INITDATA, "happy":INITDATA, "sad":INITDATA, "surprise":INITDATA,"neutral":INITDATA}
for key in EMOTIONS.keys():
if captured_emotion[0]['emotions'][key]:
EMOTIONS[key] = captured_emotion[0]['emotions'][key]
# 全ての感情を抽出
print(EMOTIONS)
# 最も高い感情とその値を抽出
dominant_emotion = max(EMOTIONS.items(), key=lambda x: x[1])
print(dominant_emotion)
# 感情をグラフ化する
figure_diagram = EMOTIONS.items()
figure_diagram = sorted(figure_diagram)
x, y = zip(*figure_diagram)
plt.plot(x, y)
plt.show()