#coding: utf-8
# ////////////////////////////////////////////////////////////////////////////
# ///【DeepFace試験】
# ///
# /// 【改訂履歴】
# /// V0.00 2024/03/13 DeepFaceシンプルテスト
# /// https://cppx.hatenablog.com/entry/2017/12/25/231121
# ///
# ////////////////////////////////////////////////////////////////////////////
# ///【使用環境】
# /// ・ Python 3.9.18
# /// ・ opencv-python 4.8.1.78
# /// ・ deepface 0.0.86
# ///
# ///【事前インストール】
# /// ・ pip install opencv-python
# /// ・ pip install deepface
# ///
# ////////////////////////////////////////////////////////////////////////////
if "__file__" in globals():
import os, sys
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
sys.path.append(os.path.join(os.path.dirname(__file__), "..") + "//..//")
os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0'
from DeZero.common.nlp_util import *
import cv2
from deepface import DeepFace
tensorflow_warning_cancel()
if __name__ == "__main__":
img = cv2.imread("c:\\Photo\\aragaki.jpg")
# /// STEP01 : 感情分析
# ////////////////////////////////////////////////////////////////////////////
result = DeepFace.analyze(img)
print(result)
print(type(result[0]))
# [result]
# (py39) d:\VisualStudio2017\Python3.5_GPU\Sample_TEST\顔認識2024>python DeepFace_test01.py
# WARNING:tensorflow:From C:\Users\yamin\anaconda3\envs\py39\lib\site-packages\keras\src\losses.py:2976: The name tf.losses.sparse_softmax_cross_entropy is deprecated. Please use tf.compat.v1.losses.sparse_softmax_cross_entropy instead.
#
# Action: race: 100%|██████████████████████████████████████████████████████████████████████| 4/4 [00:06<00:00, 1.59s/it]
# [{
# 'emotion': {'angry': 3.791340158487699e-09, 'disgust': 2.002449489743453e-18, 'fear': 1.5015461747244774e-10, 'happy': 99.4605004787445, 'sad': 5.323184670835701e-08, 'surprise': 3.609682108773882e-07, 'neutral': 0.5395032931119204},
# 'dominant_emotion': 'happy',
# 'region': {'x': 102, 'y': 75, 'w': 276, 'h': 276, 'left_eye': (86, 100), 'right_eye': (187, 101)},
# 'face_confidence': 0.95,
# 'age': 31,
# 'gender': {'Woman': 99.9056875705719, 'Man': 0.09431541548110545},
# 'dominant_gender': 'Woman',
# 'race': {'asian': 99.99996423721313, 'indian': 1.0933387528666572e-07, 'black': 1.9666831097692183e-11, 'white': 4.716102708357539e-06, 'middle eastern': 3.519574388877178e-10, 'latino hispanic': 3.081885324718314e-05},
# 'dominant_race': 'asian'}]
# <class 'dict'>
# py39) d:\VisualStudio2017\Python3.5_GPU\Sample_TEST\顔認識2024>python DeepFace_test02.py
# Traceback (most recent call last):
# File "d:\VisualStudio2017\Python3.5_GPU\Sample_TEST\顔認識2024\DeepFace_test02.py", line 40, in
# emotion_dict = result["emotion"]
# TypeError: list indices must be integers or slices, not str
#coding: utf-8
if "__file__" in globals():
import os, sys
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
sys.path.append(os.path.join(os.path.dirname(__file__), "..") + "//..//")
os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0'
from DeZero.common.nlp_util import *
import cv2
from deepface import DeepFace
tensorflow_warning_cancel()
if __name__ == "__main__":
img = cv2.imread("c:\\Photo\\aragaki.jpg")
# /// STEP01 : 感情認証
# ////////////////////////////////////////////////////////////////////////////
result = DeepFace.analyze(img,actions=['emotion'])
# /// STEP02 : 感情抽出
# ////////////////////////////////////////////////////////////////////////////
# emotion_dict = result["emotion"] ← 024/03/13修正
emotion_dict = result[0]["emotion"]
print("emotion_dict : " , emotion_dict)
# /// STEP03 : 最大値感情抽出
# ////////////////////////////////////////////////////////////////////////////
max_emt = max(emotion_dict, key=emotion_dict.get)
print("max_emt ; " , max_emt,"\n")
# /// STEP04 : 感情辞書変数分析
# ////////////////////////////////////////////////////////////////////////////
values = np.array(list(emotion_dict.values()))
labels = list(emotion_dict.keys())
print(values)
print(labels)
# (py39) d:\VisualStudio2017\Python3.5_GPU\Sample_TEST\顔認識2024>python DeepFace_test02.py
# emotion_dict : {'angry': 3.791355424054288e-09, 'disgust': 2.0024499744195933e-18, 'fear': 1.501551704155557e-10, 'happy': 99.4605004787445, 'sad': 5.323184670835701e-08, 'surprise': 3.6096823308184867e-07, 'neutral': 0.5395035725086927}
# max_emt ; happy
#
# [3.79135542e-09 2.00244997e-18 1.50155170e-10 9.94605005e+01
# 5.32318467e-08 3.60968233e-07 5.39503573e-01]
# ['angry', 'disgust', 'fear', 'happy', 'sad', 'surprise', 'neutral']