Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import cv2, numpy as np
|
| 3 |
+
import mediapipe as mp
|
| 4 |
+
|
| 5 |
+
# --- your existing imports / functions ---
|
| 6 |
+
# from your_module import au_measures, stress_score_from_features # if you split them out
|
| 7 |
+
|
| 8 |
+
mp_face_mesh = mp.solutions.face_mesh
|
| 9 |
+
face_mesh = mp_face_mesh.FaceMesh(
|
| 10 |
+
min_detection_confidence=0.5,
|
| 11 |
+
min_tracking_confidence=0.5,
|
| 12 |
+
refine_landmarks=True
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
def compute_stress_score_bgr(frame_bgr: np.ndarray) -> dict:
|
| 16 |
+
"""Minimal example: run FaceMesh + your AU→stress logic and return a dict."""
|
| 17 |
+
h, w = frame_bgr.shape[:2]
|
| 18 |
+
rgb = cv2.cvtColor(frame_bgr, cv2.COLOR_BGR2RGB)
|
| 19 |
+
res = face_mesh.process(rgb)
|
| 20 |
+
|
| 21 |
+
if not res.multi_face_landmarks:
|
| 22 |
+
return {"status": "no_face", "stress_score": None, "label": "NO FACE"}
|
| 23 |
+
|
| 24 |
+
lms = res.multi_face_landmarks[0].landmark
|
| 25 |
+
|
| 26 |
+
# ---- plug in your real AU code here ----
|
| 27 |
+
# F = au_measures(lms, w, h)
|
| 28 |
+
# score = stress_score_from_features(F)
|
| 29 |
+
score = 42.0 # <--- placeholder; replace with your real computation
|
| 30 |
+
label = "STRESSED" if score >= 55 else ("POSSIBLY STRESSED" if score >= 25 else "NOT STRESSED")
|
| 31 |
+
|
| 32 |
+
return {"status": "ok", "stress_score": round(score, 2), "label": label}
|
| 33 |
+
|
| 34 |
+
def predict(frame: np.ndarray):
|
| 35 |
+
"""
|
| 36 |
+
Gradio streaming callback receives a single frame (BGR numpy array).
|
| 37 |
+
Return any JSON-serializable object. You can also overlay results and return a video frame.
|
| 38 |
+
"""
|
| 39 |
+
out = compute_stress_score_bgr(frame)
|
| 40 |
+
return out
|
| 41 |
+
|
| 42 |
+
demo = gr.Interface(
|
| 43 |
+
fn=predict,
|
| 44 |
+
inputs=gr.Video(source="webcam", streaming=True), # webcam in the browser → streamed to Python
|
| 45 |
+
outputs=gr.JSON(label="Result"),
|
| 46 |
+
title="StressDetection V1 (MediaPipe)"
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
if __name__ == "__main__":
|
| 50 |
+
demo.launch()
|