瀏覽代碼

Merge pull request #4 from LLLLLUOTJ/fix-exploration-marker-roi

fix(GameUi): tolerate exploration marker horizontal shifts
blylana 6 天之前
父節點
當前提交
8fb6f834eb
共有 3 個文件被更改,包括 59 次插入 和 2 次删除
  1. 1 1
      tasks/GameUi/assets.py
  2. 1 1
      tasks/GameUi/page/image_exploration.json
  3. 57 0
      tests/tasks/GameUi/test_exploration_marker.py

+ 1 - 1
tasks/GameUi/assets.py

@@ -31,7 +31,7 @@ class GameUiAssets:
 
 	# Image Rule Assets
 	# 探索界面标识 
-	I_CHECK_EXPLORATION = RuleImage(roi_front=(1152,148,21,22), roi_back=(1152,148,21,22), threshold=0.8, method="Template matching", file="./tasks/GameUi/page/page_check_exploration.png")
+	I_CHECK_EXPLORATION = RuleImage(roi_front=(1152,148,21,22), roi_back=(1138,134,56,50), threshold=0.8, method="Template matching", file="./tasks/GameUi/page/page_check_exploration.png")
 	# 探索前往觉醒 
 	I_EXPLORATION_GOTO_AWAKE_ZONE = RuleImage(roi_front=(161,633,47,44), roi_back=(33,619,1150,99), threshold=0.8, method="Template matching", file="./tasks/GameUi/page/page_exploration_goto_awake_zone.png")
 	# 探索前往御魂 

+ 1 - 1
tasks/GameUi/page/image_exploration.json

@@ -3,7 +3,7 @@
     "itemName": "check_exploration",
     "imageName": "page_check_exploration.png",
     "roiFront": "1152,148,21,22",
-    "roiBack": "1152,148,21,22",
+    "roiBack": "1138,134,56,50",
     "method": "Template matching",
     "threshold": 0.8,
     "description": "探索界面标识"

+ 57 - 0
tests/tasks/GameUi/test_exploration_marker.py

@@ -0,0 +1,57 @@
+"""Exploration marker recognition under observed map-layout shifts."""
+import json
+from pathlib import Path
+import unittest
+
+import numpy as np
+
+from module.atom.image import RuleImage
+from tasks.GameUi.assets import GameUiAssets
+
+
+ROOT = Path(__file__).resolve().parents[3]
+
+
+class ExplorationMarkerTests(unittest.TestCase):
+    def rule(self):
+        item = next(item for item in json.loads(
+            (ROOT / 'tasks/GameUi/page/image_exploration.json').read_text(encoding='utf-8'))
+                    if item['itemName'] == 'check_exploration')
+        return RuleImage(tuple(map(int, item['roiFront'].split(','))),
+                         tuple(map(int, item['roiBack'].split(','))),
+                         item['method'], item['threshold'],
+                         './tasks/GameUi/page/' + item['imageName'])
+
+    def frame(self, rule, x=1152, y=148):
+        frame = np.full((720, 1280, 3), 100, dtype=np.uint8)
+        height, width = rule.image.shape[:2]
+        frame[y:y + height, x:x + width] = rule.image
+        return frame
+
+    def test_detects_both_observed_horizontal_positions(self):
+        for x in (1152, 1166):
+            with self.subTest(x=x):
+                rule = self.rule()
+                self.assertTrue(rule.match(self.frame(rule, x=x)))
+                self.assertEqual(rule.roi_front[:2], [x, 148])
+
+    def test_old_search_box_reproduces_shifted_map_failure(self):
+        rule = self.rule()
+        rule.roi_back = (1150, 146, 25, 26)  # Original ROI plus global 2px padding.
+        self.assertFalse(rule.match(self.frame(rule, x=1166)))
+
+    def test_marker_elsewhere_does_not_classify_a_map(self):
+        rule = self.rule()
+        self.assertFalse(rule.match(self.frame(rule, x=900)))
+
+    def test_blank_frame_does_not_classify_a_map(self):
+        self.assertFalse(self.rule().match(np.full((720, 1280, 3), 100, dtype=np.uint8)))
+
+    def test_generated_asset_keeps_the_source_search_box(self):
+        source = self.rule()
+        self.assertEqual(GameUiAssets.I_CHECK_EXPLORATION.roi_back, source.roi_back)
+        self.assertEqual(GameUiAssets.I_CHECK_EXPLORATION.threshold, source.threshold)
+
+
+if __name__ == '__main__':
+    unittest.main()