From 3a5c27e01fe68418bf7bf48ef71bc2be6e60f08e Mon Sep 17 00:00:00 2001 From: Kasper Date: Fri, 30 Jan 2026 20:33:16 +0100 Subject: [PATCH] fix: update face detected at same time as emotions ref: N25B-395 --- .../visual_emotion_recognition_agent.py | 27 ++++++++++++++----- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/control_backend/agents/perception/visual_emotion_recognition_agent/visual_emotion_recognition_agent.py b/src/control_backend/agents/perception/visual_emotion_recognition_agent/visual_emotion_recognition_agent.py index 9bfbb5c..9cc2c32 100644 --- a/src/control_backend/agents/perception/visual_emotion_recognition_agent/visual_emotion_recognition_agent.py +++ b/src/control_backend/agents/perception/visual_emotion_recognition_agent/visual_emotion_recognition_agent.py @@ -90,6 +90,9 @@ class VisualEmotionRecognitionAgent(BaseAgent): # Tracks counts of detected emotions per face index face_stats = defaultdict(Counter) + # How many times a face has been detected + face_detection_yes_no = [0, 0] + prev_dominant_emotions = set() while self._running: @@ -109,13 +112,11 @@ class VisualEmotionRecognitionAgent(BaseAgent): # Get the dominant emotion from each face current_emotions = self.emotion_recognizer.sorted_dominant_emotions(frame) - # Form (or unform) face_detected belief - if len(current_emotions) == 0 and self._face_detected: - self._face_detected = False - await self._inform_face_detected() - elif len(current_emotions) > 0 and not self._face_detected: - self._face_detected = True - await self._inform_face_detected() + # Update face face_detection_yes_no + if len(current_emotions) > 0: + face_detection_yes_no[0] += 1 + else: + face_detection_yes_no[1] += 1 # Update emotion counts for each detected face for i, emotion in enumerate(current_emotions): @@ -132,6 +133,18 @@ class VisualEmotionRecognitionAgent(BaseAgent): dominant_emotion = counter.most_common(1)[0][0] window_dominant_emotions.add(dominant_emotion) + if ( + face_detection_yes_no[0] > face_detection_yes_no[1] + and not self._face_detected + ): + self._face_detected = True + await self._inform_face_detected() + elif ( + face_detection_yes_no[0] <= face_detection_yes_no[1] and self._face_detected + ): + self._face_detected = False + await self._inform_face_detected() + await self.update_emotions(prev_dominant_emotions, window_dominant_emotions) prev_dominant_emotions = window_dominant_emotions face_stats.clear()