Browse Source

fix(Hyakkiyakou): Ignore an off-screen track when the heatmap has no match

An empty score map fell back to the last box. Indexing that box past the 1280-wide frame raised IndexError and stopped the script.

Co-authored-by: Cursor <cursoragent@cursor.com>
Tyrion 2 ngày trước cách đây
mục cha
commit
4785e4bfb2

+ 8 - 1
tasks/Hyakkiyakou/agent/agent.py

@@ -114,7 +114,7 @@ class Agent:
         return z
 
     @classmethod
-    def argmax_gamma(cls, z: np.ndarray, tracks: list[tuple]) -> Focus:
+    def argmax_gamma(cls, z: np.ndarray, tracks: list[tuple]) -> Focus | None:
         max_y, max_x = np.unravel_index(np.argmax(z), z.shape)
         max_variance = 2000
         max_index = -1
@@ -124,6 +124,8 @@ class Agent:
             if variance < max_variance:
                 max_index = index
                 max_variance = variance
+        if max_index < 0:
+            return None
         return Focus(inputs=tracks[max_index])
 
     def decision(self, tracks: list[tuple], state: list, freeze: bool = False) -> list:
@@ -147,6 +149,11 @@ class Agent:
     def check_observe(self, tracks: list[tuple]):
         z = Agent.gamma(tracks=tracks, weights=self.weights, priorities=self.priorities)
         focus = Agent.argmax_gamma(z=z, tracks=tracks)
+        if focus is None:
+            if self.focus is not None:
+                logger.info('Focus disappear')
+            self.focus = None
+            return
         omega = focus.omega(z)
         if omega < self.OBSERVE_THRESHOLD:
             if self.focus is not None:

+ 4 - 0
tasks/Hyakkiyakou/agent/focus.py

@@ -41,6 +41,10 @@ xywh: ({self._cx}, {self._cy}, {self._w}, {self._h})
 velocity: {self._v}"""
 
     def omega(self, z):
+        height, width = z.shape[:2]
+        if not (0 <= self._cy < height and 0 <= self._cx < width):
+            self._omega = 0.
+            return self._omega
         __omega = z[self._cy, self._cx]
         self._omega = __omega
         return __omega

+ 28 - 0
tests/tasks/Hyakkiyakou/test_agent_focus.py

@@ -0,0 +1,28 @@
+import numpy as np
+
+from tasks.Hyakkiyakou.agent.agent import Agent
+from tasks.Hyakkiyakou.agent.focus import Focus
+
+# 2026-11-21 14:36:42 崩溃帧:两个 class=0 的框都不会画进热力图,
+# 最后一个框中心 x=1320,画面宽 1280。
+OFFSCREEN_FRAME = [
+    (41, 0, 0.9723028540611267, 847, 293, 41, 98, 0.041499774903059006),
+    (45, 0, 0.6597861051559448, 1320, 404, 64, 99, 0.3642634451389313),
+]
+
+
+def test_empty_heatmap_does_not_focus_the_last_offscreen_track():
+    agent = Agent()
+    z = Agent.gamma(OFFSCREEN_FRAME, weights=agent.weights, priorities=agent.priorities)
+
+    assert Agent.argmax_gamma(z, OFFSCREEN_FRAME) is None
+
+    agent.check_observe(OFFSCREEN_FRAME)
+    assert agent.focus is None
+
+
+def test_focus_outside_the_frame_reads_as_no_observation():
+    focus = Focus(inputs=OFFSCREEN_FRAME[-1])
+    z = np.zeros((720, 1280), dtype=np.float32)
+
+    assert focus.omega(z) == 0.0