LoginSignup
0
0

pythonでFERを使用して感情を数値化する_part2

Last updated at Posted at 2024-06-16

概要

今回はpythonでFERを使用して感情を数値化する方法について記載していきます。
複数の画像データから、顔の表情を読み取り、
感情を数値化することができました。
勉強用に簡易的に記載していますので、ご了承ください。

実行環境

・windows:Windows11Pro 23H2
・Python 3.12.3

参考サイト

前回の投稿と同様

実行ファイル

emotion.py
from fer import FER
import matplotlib.pyplot as plt
import sys
import json
import os
import numpy as np
import pandas as pd
# 感情のリスト
EMOTIONS_LIST = ["angry","disgust","fear","happy","sad","surprise","neutral"]

def emotion_cap(pic):
    # 画像の取得
    test_image_one = plt.imread(pic)
    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]

    return EMOTIONS

def emotion_show(emoton_dict):
    # 全ての画像の感情値の出力
    print("########################################")
    print(emoton_dict)
    print("########################################")

    # DataFrameの作成
    df = pd.DataFrame(emoton_dict)
    print(df)

    # 円グラフの作成(1枚ずつ表示)
    for i in df.columns:
        x = df[i]
        fig, ax = plt.subplots()
        ax.pie(x, labels=EMOTIONS_LIST, autopct="%1.1f %%")

        plt.show()
if __name__ == '__main__':

    # 第一引数は設定ファイル
    json_file = sys.argv[1]
    json_file = open(json_file,'r')
    json_data = json.load(json_file)
    
    # 感情の値を格納する辞書型
    emoton_dict = {}

    # 画像を一枚ずつ読み込み感情の値を取得
    for i in json_data["picture"].values():
        pic = i
        emotion_result = emotion_cap(pic)
        filename = os.path.basename(pic)
        emoton_dict[filename] = emotion_result
    
    emotion_show(emoton_dict)


設定ファイル

{
# pictureの値に画像ファイルのパスを記載する
    "picture":{
                "pic00":"./data/test00.jpeg",
                "pic01":"./data/test01.jpeg"}
}

実行結果

1)数値
スクリーンショット 2024-06-16 184605.png

2)円グラフ
output.png

output2.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0