Spaces:
Sleeping
Sleeping
Kota Takahashi
commited on
Commit
·
e3476c2
1
Parent(s):
89899f3
emotion_analyser.pyの`detect_best_image`をなくしてapp.pyの方に処理を移行
Browse files- README.md +1 -4
- app.py +7 -2
- emotion_analyzer.py +11 -23
README.md
CHANGED
@@ -8,7 +8,4 @@ sdk: streamlit
|
|
8 |
sdk_version: 1.35.0
|
9 |
app_file: app.py
|
10 |
pinned: false
|
11 |
-
---
|
12 |
-
|
13 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
14 |
-
[README.md](README.md)[README.md](README.md)
|
|
|
8 |
sdk_version: 1.35.0
|
9 |
app_file: app.py
|
10 |
pinned: false
|
11 |
+
---
|
|
|
|
|
|
app.py
CHANGED
@@ -2,6 +2,7 @@ import sys
|
|
2 |
import os
|
3 |
import glob
|
4 |
|
|
|
5 |
import streamlit as st
|
6 |
from PIL import Image
|
7 |
import zipfile
|
@@ -67,10 +68,14 @@ if uploaded_file:
|
|
67 |
|
68 |
if st.button('表情分析開始'):
|
69 |
# 画像を指定して表情認識を実行
|
70 |
-
with st.spinner('表情分析中...'):
|
71 |
ema = EmotionAnalyzer(img_file_path_list)
|
72 |
df_emotions = ema.create_emotion_dataflame()
|
73 |
-
|
|
|
|
|
|
|
|
|
74 |
|
75 |
# ベストショットのファイル名
|
76 |
best_image_record_filename = best_image_record.name
|
|
|
2 |
import os
|
3 |
import glob
|
4 |
|
5 |
+
import numpy as np
|
6 |
import streamlit as st
|
7 |
from PIL import Image
|
8 |
import zipfile
|
|
|
68 |
|
69 |
if st.button('表情分析開始'):
|
70 |
# 画像を指定して表情認識を実行
|
71 |
+
with (st.spinner('表情分析中...')):
|
72 |
ema = EmotionAnalyzer(img_file_path_list)
|
73 |
df_emotions = ema.create_emotion_dataflame()
|
74 |
+
|
75 |
+
# 各表情の値と設定値との二乗和を取り、最も値が小さい(設定値に近い)写真をベストショットをする
|
76 |
+
df_emotions['distance'] = \
|
77 |
+
np.sqrt(sum((df_emotions[emotion] - ratio) ** 2 for emotion, ratio in emotion_ratios.items()))
|
78 |
+
best_image_record = df_emotions.loc[df_emotions['distance'].idxmin()]
|
79 |
|
80 |
# ベストショットのファイル名
|
81 |
best_image_record_filename = best_image_record.name
|
emotion_analyzer.py
CHANGED
@@ -1,6 +1,5 @@
|
|
1 |
import pandas as pd
|
2 |
from feat import Detector
|
3 |
-
import numpy as np
|
4 |
|
5 |
# 検出器の定義
|
6 |
detector = Detector()
|
@@ -10,32 +9,10 @@ class EmotionAnalyzer:
|
|
10 |
def __init__(self, img_file_path_list):
|
11 |
self.img_file_path_list = img_file_path_list
|
12 |
|
13 |
-
def detect_best_image(self, df_emotions, emotion_ratios):
|
14 |
-
"""
|
15 |
-
各表情の値と設定値との二乗和を取り、最も値が小さい(設定値に近い)写真をベストショットをする
|
16 |
-
"""
|
17 |
-
df_emotions['distance'] = np.sqrt(
|
18 |
-
sum((df_emotions[emotion] - ratio) ** 2 for emotion, ratio in emotion_ratios.items())
|
19 |
-
)
|
20 |
-
best_image_record = df_emotions.loc[df_emotions['distance'].idxmin()]
|
21 |
-
return best_image_record
|
22 |
-
|
23 |
-
@staticmethod
|
24 |
-
def analyze_emotion(img_file_path):
|
25 |
-
"""
|
26 |
-
イメージファイルから感情を分析
|
27 |
-
"""
|
28 |
-
# 検出器の定義
|
29 |
-
detector = Detector()
|
30 |
-
result = detector.detect_image(img_file_path)
|
31 |
-
result_emotions_mean = result.emotions.mean()
|
32 |
-
return result_emotions_mean, result
|
33 |
-
|
34 |
def create_emotion_dataflame(self):
|
35 |
"""
|
36 |
イメージファイルの分析結果をデータフレーム化
|
37 |
"""
|
38 |
-
|
39 |
emotions_deg_list = []
|
40 |
result_detections_list = []
|
41 |
for img_file_path in self.img_file_path_list:
|
@@ -47,3 +24,14 @@ class EmotionAnalyzer:
|
|
47 |
df_emotion_result['detections'] = result_detections_list
|
48 |
|
49 |
return df_emotion_result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import pandas as pd
|
2 |
from feat import Detector
|
|
|
3 |
|
4 |
# 検出器の定義
|
5 |
detector = Detector()
|
|
|
9 |
def __init__(self, img_file_path_list):
|
10 |
self.img_file_path_list = img_file_path_list
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
def create_emotion_dataflame(self):
|
13 |
"""
|
14 |
イメージファイルの分析結果をデータフレーム化
|
15 |
"""
|
|
|
16 |
emotions_deg_list = []
|
17 |
result_detections_list = []
|
18 |
for img_file_path in self.img_file_path_list:
|
|
|
24 |
df_emotion_result['detections'] = result_detections_list
|
25 |
|
26 |
return df_emotion_result
|
27 |
+
|
28 |
+
@staticmethod
|
29 |
+
def analyze_emotion(img_file_path):
|
30 |
+
"""
|
31 |
+
イメージファイルから感情を分析
|
32 |
+
"""
|
33 |
+
# 検出器の定義
|
34 |
+
detector = Detector()
|
35 |
+
result = detector.detect_image(img_file_path)
|
36 |
+
result_emotions_mean = result.emotions.mean()
|
37 |
+
return result_emotions_mean, result
|