test_realm_multiscale.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. """Offline regression tests for shared realm assets and distant carp banners."""
  2. from copy import deepcopy
  3. import json
  4. from pathlib import Path
  5. import unittest
  6. from unittest.mock import patch
  7. import cv2
  8. import numpy as np
  9. from module.atom.image import RuleImage, set_roi_match_padding
  10. from tasks.Component.Costume.config import CarpBannerType
  11. from tasks.Component.Costume.costume_base import CostumeBase
  12. from tasks.Component.CostumeCarpBanner.assets import CostumeCarpBannerAssets
  13. from tasks.KekkaiUtilize.assets import KekkaiUtilizeAssets
  14. ROOT = Path(__file__).resolve().parents[3]
  15. TARGETS = {
  16. 'I_SHI_GROWN': (599, 293, 45, 65),
  17. 'I_SHI_CARD': (777, 288, 50, 72),
  18. 'I_SHI_DEFENSE': (410, 288, 45, 80),
  19. 'I_REALM_SHIN': (308, 300, 85, 85),
  20. 'I_CARD_EXP': (770, 158, 70, 68),
  21. 'I_BOX_AP': (880, 312, 65, 65),
  22. 'I_BOX_EXP': (937, 312, 52, 70),
  23. }
  24. ORIGINAL_REGIONS = {
  25. 'I_SHI_GROWN': (519, 212, 220, 281),
  26. 'I_SHI_CARD': (823, 210, 252, 297),
  27. 'I_SHI_DEFENSE': (178, 209, 261, 274),
  28. 'I_REALM_SHIN': (22, 359, 393, 328),
  29. 'I_CARD_EXP': (829, 33, 231, 257),
  30. 'I_BOX_AP': (744, 336, 395, 216),
  31. 'I_BOX_EXP': (792, 334, 359, 232),
  32. }
  33. class Probe(CostumeBase, KekkaiUtilizeAssets):
  34. """Only rule replacement is exercised; no Config or Device is created."""
  35. pass
  36. def screenshot():
  37. fixture = Path(__file__).parent / 'fixtures/distant_realm_elements.png'
  38. crop = cv2.cvtColor(cv2.imdecode(np.fromfile(fixture, dtype=np.uint8), cv2.IMREAD_COLOR),
  39. cv2.COLOR_BGR2RGB)
  40. image = np.zeros((720, 1280, 3), dtype=np.uint8)
  41. image[150:460, 300:1000] = crop
  42. return image
  43. def configured_rules():
  44. probe = Probe()
  45. for name in TARGETS:
  46. setattr(probe, name, deepcopy(getattr(KekkaiUtilizeAssets, name)))
  47. with patch('tasks.Component.Costume.costume_base.I18n.trans_zh_cn', return_value='carp'):
  48. probe.check_costume_carpbanner(CarpBannerType.COSTUME_CARPBANNER_3)
  49. return {name: getattr(probe, name) for name in TARGETS}
  50. class RealmMultiScaleTests(unittest.TestCase):
  51. def tearDown(self):
  52. set_roi_match_padding(0)
  53. def test_all_seven_visible_elements_match_at_correct_locations(self):
  54. for padding in (0, 2):
  55. set_roi_match_padding(padding)
  56. for name, rule in configured_rules().items():
  57. with self.subTest(padding=padding, name=name):
  58. self.assertTrue(rule.match(screenshot(), threshold=0.6 if name == 'I_BOX_EXP' else None))
  59. x, y, w, h = rule.roi_front
  60. ex, ey, ew, eh = TARGETS[name]
  61. self.assertTrue(ex <= x + w / 2 < ex + ew)
  62. self.assertTrue(ey <= y + h / 2 < ey + eh)
  63. self.assertEqual(rule.last_scale, None if name == 'I_BOX_EXP' else
  64. (0.6 if name.startswith('I_SHI_') else 1.0))
  65. def test_missing_target_is_not_accepted(self):
  66. for padding in (0, 2):
  67. set_roi_match_padding(padding)
  68. for name, rule in configured_rules().items():
  69. with self.subTest(padding=padding, name=name):
  70. image = screenshot()
  71. x, y, w, h = TARGETS[name]
  72. image[y-5:y+h+5, x-5:x+w+5] = 0
  73. self.assertFalse(rule.match(image, threshold=0.6 if name == 'I_BOX_EXP' else None), (name, rule.last_score, rule.roi_front))
  74. def test_empty_screen_has_no_matches(self):
  75. for name, rule in configured_rules().items():
  76. with self.subTest(name=name):
  77. self.assertFalse(rule.match(np.zeros((720, 1280, 3), dtype=np.uint8)))
  78. def test_cross_skin_search_regions(self):
  79. expected = {
  80. 'I_SHI_DEFENSE': (178, 209, 292, 274),
  81. 'I_SHI_GROWN': (560, 250, 130, 220),
  82. 'I_SHI_CARD': (760, 250, 220, 220),
  83. 'I_REALM_SHIN': (140, 270, 270, 285),
  84. 'I_CARD_EXP': (750, 90, 250, 230),
  85. 'I_BOX_AP': (790, 285, 295, 240),
  86. 'I_BOX_EXP': (875, 285, 255, 240),
  87. 'I_BOX_EXP_MAX': (875, 285, 255, 240),
  88. 'I_UTILIZE_EXP': (500, 65, 220, 280),
  89. }
  90. for name, roi in expected.items():
  91. self.assertEqual(tuple(getattr(KekkaiUtilizeAssets, name).roi_back), roi)
  92. if name.startswith('I_SHI_'):
  93. for skin in range(1, 4):
  94. self.assertEqual(tuple(getattr(CostumeCarpBannerAssets,
  95. name + '_' + str(skin)).roi_back), roi)
  96. def test_collected_bottle_does_not_match_white_animals_at_task_threshold(self):
  97. fixture = Path(__file__).parent / 'fixtures/collected_bottle_animals.png'
  98. crop = cv2.cvtColor(cv2.imdecode(np.fromfile(fixture, dtype=np.uint8),
  99. cv2.IMREAD_COLOR), cv2.COLOR_BGR2RGB)
  100. image = np.zeros((720, 1280, 3), dtype=np.uint8)
  101. image[280:640, 790:1150] = crop
  102. for padding in (0, 2):
  103. set_roi_match_padding(padding)
  104. for name in ('I_BOX_EXP', 'I_BOX_EXP_MAX'):
  105. for roi in ((875, 285, 255, 240), (790, 480, 350, 150)):
  106. rule = deepcopy(getattr(KekkaiUtilizeAssets, name))
  107. rule.roi_back = roi
  108. self.assertFalse(rule.match(image, threshold=0.6),
  109. (name, roi, rule.last_score))
  110. def test_bottle_click_dimensions_match_replacement_template(self):
  111. rule = deepcopy(KekkaiUtilizeAssets.I_BOX_EXP)
  112. height, width = rule.image.shape[:2]
  113. self.assertEqual(tuple(rule.roi_front[2:]), (width, height))
  114. self.assertTrue(rule.match(screenshot(), threshold=0.6))
  115. def test_runtime_rules_keep_original_templates_and_thresholds(self):
  116. defaults = {'I_REALM_SHIN': 0.9, 'I_CARD_EXP': 0.8,
  117. 'I_BOX_AP': 0.7, 'I_BOX_EXP': 0.7}
  118. for name, rule in configured_rules().items():
  119. with self.subTest(name=name):
  120. if name.startswith('I_SHI_'):
  121. source = getattr(CostumeCarpBannerAssets, name + '_3')
  122. self.assertEqual(rule.file, source.file)
  123. self.assertEqual(rule.threshold, 0.5)
  124. else:
  125. self.assertEqual(rule.file, getattr(KekkaiUtilizeAssets, name).file)
  126. self.assertEqual(rule.threshold, defaults[name])
  127. self.assertEqual(rule.method, 'Template matching' if name == 'I_BOX_EXP'
  128. else 'Multi-scale template matching')
  129. def test_original_size_templates_remain_recognizable(self):
  130. for name, rule in configured_rules().items():
  131. with self.subTest(name=name):
  132. image = np.zeros((720, 1280, 3), dtype=np.uint8)
  133. x, y, _, _ = rule.roi_back
  134. template = rule.image
  135. h, w = template.shape[:2]
  136. image[y+10:y+10+h, x+10:x+10+w] = template
  137. self.assertTrue(rule.match(image))
  138. self.assertEqual(rule.last_scale, None if name == 'I_BOX_EXP' else 1.0)
  139. self.assertEqual(rule.roi_front, [x+10, y+10, w, h])
  140. def test_banner_defense_matches_at_each_skins_original_position(self):
  141. for skin in range(1, 4):
  142. for padding in (0, 2):
  143. with self.subTest(skin=skin, padding=padding):
  144. set_roi_match_padding(padding)
  145. rule = deepcopy(getattr(CostumeCarpBannerAssets, f'I_SHI_DEFENSE_{skin}'))
  146. image = np.zeros((720, 1280, 3), dtype=np.uint8)
  147. self.assertFalse(rule.match(image))
  148. x, y, w, h = rule.roi_front
  149. image[y:y+h, x:x+w] = rule.image
  150. self.assertTrue(rule.match(image))
  151. self.assertEqual(rule.roi_front, [x, y, w, h])
  152. def test_replacement_copies_method_and_clears_cached_dispatch(self):
  153. probe = Probe()
  154. probe.I_SHI_GROWN = deepcopy(KekkaiUtilizeAssets.I_SHI_GROWN)
  155. target = probe.I_SHI_GROWN
  156. self.assertTrue(target.is_template_match)
  157. self.assertFalse(target.is_multi_scale_template_match)
  158. _ = target.image
  159. source = CostumeCarpBannerAssets.I_SHI_GROWN_3
  160. probe.replace_img('I_SHI_GROWN', source)
  161. self.assertIs(probe.I_SHI_GROWN, target)
  162. self.assertIsNone(target._image)
  163. self.assertFalse(target.is_template_match)
  164. self.assertTrue(target.is_multi_scale_template_match)
  165. self.assertTrue(target.match(screenshot()))
  166. self.assertEqual(source.roi_front, [600, 277, 19, 34])
  167. self.assertIsNot(target.roi_front, source.roi_front)
  168. def test_replacement_can_return_to_original_matching_method(self):
  169. probe = Probe()
  170. probe.I_SHI_GROWN = deepcopy(KekkaiUtilizeAssets.I_SHI_GROWN)
  171. probe.replace_img('I_SHI_GROWN', CostumeCarpBannerAssets.I_SHI_GROWN_3)
  172. self.assertTrue(probe.I_SHI_GROWN.is_multi_scale_template_match)
  173. probe.replace_img('I_SHI_GROWN', KekkaiUtilizeAssets.I_SHI_GROWN)
  174. self.assertTrue(probe.I_SHI_GROWN.is_template_match)
  175. self.assertFalse(probe.I_SHI_GROWN.is_multi_scale_template_match)
  176. def test_replacement_can_preserve_fixed_search_region(self):
  177. probe = Probe()
  178. probe.I_SHI_GROWN = deepcopy(KekkaiUtilizeAssets.I_SHI_GROWN)
  179. original = probe.I_SHI_GROWN.roi_back
  180. probe.replace_img('I_SHI_GROWN', CostumeCarpBannerAssets.I_SHI_GROWN_3,
  181. rp_roi_back=False)
  182. self.assertEqual(probe.I_SHI_GROWN.roi_back, original)
  183. def test_manual_realm_tool_references_existing_enum_members(self):
  184. import ast
  185. from tasks.Component.Costume.config import RealmType
  186. tool = ROOT / 'tasks/Component/Costume/costume_test_realm.py'
  187. tree = ast.parse(tool.read_text(encoding='utf-8'))
  188. members = [node.attr for node in ast.walk(tree)
  189. if isinstance(node, ast.Attribute)
  190. and isinstance(node.value, ast.Name)
  191. and node.value.id == 'RealmType']
  192. self.assertTrue(members)
  193. for member in members:
  194. self.assertIn(member, RealmType.__members__)
  195. def test_generated_classes_match_json(self):
  196. for rel, assets in [('tasks/KekkaiUtilize/realm/image.json', KekkaiUtilizeAssets),
  197. ('tasks/Component/CostumeCarpBanner/cb3/image.json',
  198. CostumeCarpBannerAssets)]:
  199. for row in json.loads((ROOT / rel).read_text(encoding='utf-8')):
  200. with self.subTest(file=rel, name=row['itemName']):
  201. rule = getattr(assets, 'I_' + row['itemName'].upper())
  202. self.assertEqual(rule.method, row['method'])
  203. self.assertEqual(tuple(rule.roi_back), tuple(map(int, row['roiBack'].split(','))))
  204. self.assertEqual(rule.threshold, row['threshold'])
  205. if __name__ == '__main__':
  206. unittest.main()