GCLing commited on
Commit
357796f
·
verified ·
1 Parent(s): 506d677

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -0
app.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+ import os
3
+ # —— 把 DeepFace 缓存目录指向可写的 /tmp/.deepface
4
+ os.environ["DEEPFACE_HOME"] = "/tmp/.deepface"
5
+ os.makedirs(os.environ["DEEPFACE_HOME"], exist_ok=True)
6
+
7
+ import gradio as gr
8
+ import cv2
9
+ import numpy as np
10
+ from deepface import DeepFace
11
+
12
+ def face_emotion(frame: np.ndarray) -> str:
13
+ """
14
+ 接收 gr.Camera 给出的 RGB ndarray,
15
+ 转成 BGR 后交给 DeepFace 分析情绪。
16
+ """
17
+ # RGB → BGR
18
+ bgr = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
19
+ res = DeepFace.analyze(
20
+ bgr,
21
+ actions=['emotion'],
22
+ enforce_detection=False
23
+ )
24
+ # DeepFace 支持 list 或 dict 返回
25
+ if isinstance(res, list):
26
+ emo = res[0].get('dominant_emotion', 'unknown')
27
+ else:
28
+ emo = res.get('dominant_emotion', 'unknown')
29
+ return emo
30
+
31
+ # —— Gradio 前端 ——
32
+ with gr.Blocks() as demo:
33
+ gr.Markdown("## 📱 多模態即時情緒分析(示範:即時人臉情緒)")
34
+ camera = gr.Camera(label="請對準鏡頭", type="numpy")
35
+ output = gr.Textbox(label="偵測到的情緒")
36
+ # 实时流:fps 可以调低一点,减轻服务器压力
37
+ camera.stream(face_emotion, camera, output, fps=5)
38
+
39
+ if __name__ == "__main__":
40
+ demo.launch()