Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +43 -39
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,44 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
""
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
"
|
| 28 |
-
"
|
| 29 |
-
"
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
st.
|
| 34 |
-
|
| 35 |
-
.
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import cv2, numpy as np, base64, io, os
|
| 3 |
+
import librosa, joblib
|
| 4 |
+
from deepface import DeepFace
|
| 5 |
+
|
| 6 |
+
# 1) 加载所有模型
|
| 7 |
+
@st.cache_resource
|
| 8 |
+
def load_models():
|
| 9 |
+
DeepFace.analyze(img_path=np.zeros((224,224,3),dtype=np.uint8),
|
| 10 |
+
actions=['emotion'], enforce_detection=False)
|
| 11 |
+
voice_clf = joblib.load("voice_model.joblib")
|
| 12 |
+
return voice_clf
|
| 13 |
+
|
| 14 |
+
voice_clf = load_models()
|
| 15 |
+
|
| 16 |
+
st.title("📱 即時多模態情緒分析")
|
| 17 |
+
|
| 18 |
+
# 2) 即时人脸
|
| 19 |
+
st.header("🖼 實時人臉情緒")
|
| 20 |
+
img_data = st.camera_input("對準鏡頭")
|
| 21 |
+
if img_data is not None:
|
| 22 |
+
arr = np.frombuffer(img_data.read(), np.uint8)
|
| 23 |
+
img = cv2.imdecode(arr, cv2.IMREAD_COLOR)
|
| 24 |
+
res = DeepFace.analyze(img, actions=["emotion"], enforce_detection=False)
|
| 25 |
+
emo = (res[0] if isinstance(res,list) else res).get("dominant_emotion","unknown")
|
| 26 |
+
st.write("情緒:", emo)
|
| 27 |
+
|
| 28 |
+
# 3) 語音上傳
|
| 29 |
+
st.header("🎤 上傳語音情緒")
|
| 30 |
+
audio = st.file_uploader("請上傳 WAV 音檔", type=["wav"])
|
| 31 |
+
if audio is not None:
|
| 32 |
+
with open("tmp.wav","wb") as f: f.write(audio.getbuffer())
|
| 33 |
+
y, sr = librosa.load("tmp.wav", sr=None)
|
| 34 |
+
mf = np.mean(librosa.feature.mfcc(y=y, sr=sr, n_mfcc=13).T,axis=0)
|
| 35 |
+
emo = voice_clf.predict([mf])[0]
|
| 36 |
+
st.write("情緒:", emo)
|
| 37 |
+
|
| 38 |
+
# 4) 文字輸入
|
| 39 |
+
st.header("📝 輸入文字情緒")
|
| 40 |
+
txt = st.text_input("打些文字…")
|
| 41 |
+
if txt:
|
| 42 |
+
# copy 你的 analyze_text_fn
|
| 43 |
+
emo = analyze_text_fn(txt)
|
| 44 |
+
st.write("情緒:", emo)
|