-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemotions.py
More file actions
59 lines (43 loc) · 1.43 KB
/
Copy pathemotions.py
File metadata and controls
59 lines (43 loc) · 1.43 KB
1
2
3
4
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import mediapipe as mp
# Face + Hand models
mp_face = mp.solutions.face_mesh
mp_hands = mp.solutions.hands
face_mesh = mp_face.FaceMesh(refine_landmarks=True)
hands = mp_hands.Hands(max_num_hands=1)
def detect_emotion(frame):
h, w, _ = frame.shape
rgb = frame[:, :, ::-1]
# ---------- ANGRY via hand gesture ----------
hand_result = hands.process(rgb)
if hand_result.multi_hand_landmarks:
return "angry"
# ---------- Face emotions ----------
result = face_mesh.process(rgb)
if not result.multi_face_landmarks:
return "neutral"
lm = result.multi_face_landmarks[0].landmark
# Key landmarks
top_lip = lm[13].y
bottom_lip = lm[14].y
left_mouth = lm[61].x
right_mouth = lm[291].x
left_eye_top = lm[159].y
left_eye_bottom = lm[145].y
right_eye_top = lm[386].y
right_eye_bottom = lm[374].y
left_brow = lm[105].y
mouth_open = bottom_lip - top_lip
smile_width = right_mouth - left_mouth
left_eye_open = left_eye_bottom - left_eye_top
right_eye_open = right_eye_bottom - right_eye_top
eye_open = (left_eye_open + right_eye_open) / 2
brow_raise = left_eye_top - left_brow
if mouth_open > 0.065:
return "playful"
if eye_open > 0.035 and mouth_open > 0.03:
return "surprised"
if smile_width > 0.09:
return "happy"
if eye_open < 0.018 and mouth_open < 0.02:
return "thinking"
return "neutral"