test_roi_padding.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import numpy as np
  2. import pytest
  3. from module.atom import image as image_module
  4. from module.atom.image import RuleImage
  5. from module.atom.animate import RuleAnimate
  6. @pytest.mark.parametrize('method', ['match', 'match_multi_scale', 'match_all', 'match_all_any'])
  7. @pytest.mark.parametrize('position', [(0, 0), (16, 17)])
  8. @pytest.mark.parametrize('pad', [0, 2])
  9. def test_matching_padding_preserves_absolute_coordinates(monkeypatch, method, position, pad):
  10. monkeypatch.setattr(image_module, 'ROI_MATCH_PAD', pad)
  11. template = np.random.default_rng(19).integers(0, 256, (10, 10, 3), dtype=np.uint8)
  12. screen = np.zeros((40, 40, 3), dtype=np.uint8)
  13. x, y = position
  14. screen[y:y + 10, x:x + 10] = template
  15. # Desktop padding compensates for a one-pixel displacement; emulator uses exact ROI.
  16. roi = (x + bool(pad), y + bool(pad), 10, 10)
  17. rule = RuleImage(roi, roi, 'Template matching', 0.99, 'unused.png')
  18. rule._image = template
  19. if method == 'match_multi_scale':
  20. assert rule.match_multi_scale(screen, scales=[1.0])
  21. assert tuple(rule.roi_front[:2]) == position
  22. elif method == 'match':
  23. assert rule.match(screen)
  24. assert tuple(rule.roi_front[:2]) == position
  25. else:
  26. results = getattr(rule, method)(screen)
  27. assert any(tuple(result[1:3]) == position for result in results)
  28. def test_emulator_without_padding_does_not_accept_shifted_template(monkeypatch):
  29. monkeypatch.setattr(image_module, 'ROI_MATCH_PAD', 0)
  30. pixels = np.random.default_rng(19).integers(0, 256, (10, 10, 3), dtype=np.uint8)
  31. screen = np.zeros((30, 30, 3), dtype=np.uint8)
  32. screen[10:20, 10:20] = pixels
  33. rule = RuleImage((11, 11, 10, 10), (11, 11, 10, 10), 'Template matching', 0.99, 'unused.png')
  34. rule._image = pixels
  35. assert not rule.match(screen)
  36. def test_animation_samples_fixed_region_after_a_desktop_match_moves(monkeypatch):
  37. monkeypatch.setattr(image_module, 'ROI_MATCH_PAD', 2)
  38. roi = (10, 10, 8, 8)
  39. rule = RuleAnimate(RuleImage(roi, roi, 'Template matching', 0.99, 'unused.png'))
  40. pixels = np.random.default_rng(23).integers(0, 256, (8, 8, 3), dtype=np.uint8)
  41. first = np.zeros((32, 32, 3), dtype=np.uint8)
  42. first[10:18, 10:18] = pixels
  43. shifted = np.roll(first, 2, axis=1)
  44. assert not rule.stable(first)
  45. assert rule.stable(shifted)
  46. assert rule.roi_front[:2] == [12, 10]
  47. np.testing.assert_array_equal(rule._last_image, shifted[10:18, 10:18])