Digambar29 commited on
Commit
05fbb64
·
1 Parent(s): c7cce82

Don't really know the changes

Browse files
model/emotion_state.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import time
2
+ from collections import deque
3
+
4
+ class EmotionState:
5
+ def __init__(
6
+ self,
7
+ confidence_threshold=0.6,
8
+ window_size=5,
9
+ decay_seconds=2.0
10
+ ):
11
+ self.confidence_threshold = confidence_threshold
12
+ self.window_size = window_size
13
+ self.decay_seconds = decay_seconds
14
+
15
+ self.recent_emotions = deque(maxlen=window_size)
16
+ self.current_emotion = None
17
+ self.current_confidence = 0.0
18
+ self.last_confident_ts = None
19
+
20
+ def update(self, prediction: dict):
21
+ """
22
+ prediction = {
23
+ 'emotion': str,
24
+ 'confidence': float
25
+ }
26
+ """
27
+
28
+ now = time.time()
29
+ confidence = prediction["confidence"]
30
+ emotion = prediction["emotion"]
31
+
32
+ # Case 1: confident prediction
33
+ if confidence >= self.confidence_threshold:
34
+ self.recent_emotions.append(emotion)
35
+
36
+ dominant_emotion = max(
37
+ set(self.recent_emotions),
38
+ key=self.recent_emotions.count
39
+ )
40
+
41
+ self.current_emotion = dominant_emotion
42
+ self.current_confidence = confidence
43
+ self.last_confident_ts = now
44
+
45
+ return self._stable_response()
46
+
47
+ # Case 2: uncertain prediction, but within decay window
48
+ if self.last_confident_ts and (now - self.last_confident_ts <= self.decay_seconds):
49
+ return self._uncertain_response(confidence)
50
+
51
+ # Case 3: decay expired
52
+ self._reset_state()
53
+ return self._unknown_response()
54
+
55
+ def _stable_response(self):
56
+ return {
57
+ "state": "stable",
58
+ "emotion": self.current_emotion,
59
+ "confidence": self.current_confidence,
60
+ "is_confident": True
61
+ }
62
+
63
+ def _uncertain_response(self, confidence):
64
+ return {
65
+ "state": "uncertain",
66
+ "emotion": self.current_emotion,
67
+ "confidence": confidence,
68
+ "is_confident": False
69
+ }
70
+
71
+ def _unknown_response(self):
72
+ return {
73
+ "state": "unknown",
74
+ "emotion": None,
75
+ "confidence": 0.0,
76
+ "is_confident": False
77
+ }
78
+
79
+ def _reset_state(self):
80
+ self.recent_emotions.clear()
81
+ self.current_emotion = None
82
+ self.current_confidence = 0.0
83
+ self.last_confident_ts = None
tools/{webcam.py → webcam_model_test.py} RENAMED
File without changes