test_exploration_marker.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. """Exploration marker recognition under observed map-layout shifts."""
  2. import json
  3. from pathlib import Path
  4. import unittest
  5. import numpy as np
  6. from module.atom.image import RuleImage
  7. from tasks.GameUi.assets import GameUiAssets
  8. ROOT = Path(__file__).resolve().parents[3]
  9. class ExplorationMarkerTests(unittest.TestCase):
  10. def rule(self):
  11. item = next(item for item in json.loads(
  12. (ROOT / 'tasks/GameUi/page/image_exploration.json').read_text(encoding='utf-8'))
  13. if item['itemName'] == 'check_exploration')
  14. return RuleImage(tuple(map(int, item['roiFront'].split(','))),
  15. tuple(map(int, item['roiBack'].split(','))),
  16. item['method'], item['threshold'],
  17. './tasks/GameUi/page/' + item['imageName'])
  18. def frame(self, rule, x=1152, y=148):
  19. frame = np.full((720, 1280, 3), 100, dtype=np.uint8)
  20. height, width = rule.image.shape[:2]
  21. frame[y:y + height, x:x + width] = rule.image
  22. return frame
  23. def test_detects_both_observed_horizontal_positions(self):
  24. for x in (1152, 1166):
  25. with self.subTest(x=x):
  26. rule = self.rule()
  27. self.assertTrue(rule.match(self.frame(rule, x=x)))
  28. self.assertEqual(rule.roi_front[:2], [x, 148])
  29. def test_old_search_box_reproduces_shifted_map_failure(self):
  30. rule = self.rule()
  31. rule.roi_back = (1150, 146, 25, 26) # Original ROI plus global 2px padding.
  32. self.assertFalse(rule.match(self.frame(rule, x=1166)))
  33. def test_marker_elsewhere_does_not_classify_a_map(self):
  34. rule = self.rule()
  35. self.assertFalse(rule.match(self.frame(rule, x=900)))
  36. def test_blank_frame_does_not_classify_a_map(self):
  37. self.assertFalse(self.rule().match(np.full((720, 1280, 3), 100, dtype=np.uint8)))
  38. def test_generated_asset_keeps_the_source_search_box(self):
  39. source = self.rule()
  40. self.assertEqual(GameUiAssets.I_CHECK_EXPLORATION.roi_back, source.roi_back)
  41. self.assertEqual(GameUiAssets.I_CHECK_EXPLORATION.threshold, source.threshold)
  42. if __name__ == '__main__':
  43. unittest.main()