GCLing commited on
Commit
6c948c7
·
verified ·
1 Parent(s): ed1de40

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. 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
- # Welcome to Streamlit!
8
-
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
-
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
15
-
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
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)