assets_test.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import cv2
  2. import numpy as np
  3. from numpy import float32, int32, uint8, fromfile
  4. from pathlib import Path
  5. from module.logger import logger
  6. from module.atom.image import RuleImage
  7. from module.atom.ocr import RuleOcr
  8. def load_image(file: str):
  9. file = Path(file)
  10. img = cv2.imdecode(fromfile(file, dtype=uint8), -1)
  11. img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
  12. height, width, channels = img.shape
  13. if height != 720 or width != 1280:
  14. logger.error(f'Image size is {height}x{width}, not 720x1280')
  15. return None
  16. return img
  17. def detect_image(file: str, targe: RuleImage) -> bool:
  18. img = load_image(file)
  19. result = targe.test_match(img)
  20. logger.info(f'[{targe.name}]: {result}')
  21. return result
  22. def detect_ocr(file: str, target: RuleOcr):
  23. img = load_image(file)
  24. return target.ocr(img)
  25. # 图片文件路径 可以是相对路径
  26. IMAGE_FILE = r"C:\Users\萌萌哒\Desktop\349772746-36423909-af3d-4d9c-bae4-9d2597e2ecda.png"
  27. if __name__ == '__main__':
  28. from tasks.RyouToppa.assets import RyouToppaAssets
  29. from tasks.GameUi.assets import GameUiAssets
  30. targe = GameUiAssets.I_MAIN_GOTO_GUILD
  31. print(detect_image(IMAGE_FILE, targe))
  32. # ocr demo
  33. # from tasks.KekkaiActivation.assets import KekkaiActivationAssets
  34. # target = KekkaiActivationAssets.O_CARD_ALL_TIME
  35. # print(detect_ocr(IMAGE_FILE, target))