assets_test.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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, strict_size: bool = True):
  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 strict_size and (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. def _roi_to_text(roi: list | tuple) -> str:
  26. x, y, w, h = roi
  27. return f"{int(round(x))},{int(round(y))},{int(round(w))},{int(round(h))}"
  28. def detect_image_detail(file: str, target: RuleImage) -> dict:
  29. img = load_image(file, strict_size=False)
  30. if img is None:
  31. return {
  32. "matched": False,
  33. "similarity": 0.0,
  34. "roiFront": _roi_to_text(target.roi_front),
  35. "roiBack": _roi_to_text(target.roi_back),
  36. "message": "image_load_failed",
  37. }
  38. similarity = 0.0
  39. matched = False
  40. message = "not_match"
  41. if target.is_template_match:
  42. source = target.corp(img)
  43. mat = target.image
  44. if source.shape[0] < mat.shape[0] or source.shape[1] < mat.shape[1]:
  45. return {
  46. "matched": False,
  47. "similarity": 0.0,
  48. "roiFront": _roi_to_text(target.roi_front),
  49. "roiBack": _roi_to_text(target.roi_back),
  50. "message": "roi_back_too_small",
  51. }
  52. res = cv2.matchTemplate(source, mat, cv2.TM_CCOEFF_NORMED)
  53. _, max_val, _, max_loc = cv2.minMaxLoc(res)
  54. similarity = float(max_val)
  55. matched = similarity >= target.threshold
  56. target.roi_front[0] = int(max_loc[0] + target.roi_back[0])
  57. target.roi_front[1] = int(max_loc[1] + target.roi_back[1])
  58. target.roi_front[2] = int(mat.shape[1])
  59. target.roi_front[3] = int(mat.shape[0])
  60. message = "match" if matched else "not_match"
  61. else:
  62. try:
  63. matched = bool(target.test_match(img))
  64. similarity = 1.0 if matched else 0.0
  65. message = "match" if matched else "not_match"
  66. except Exception as e:
  67. message = f"sift_error:{e}"
  68. return {
  69. "matched": matched,
  70. "similarity": round(similarity, 4),
  71. "roiFront": _roi_to_text(target.roi_front),
  72. "roiBack": _roi_to_text(target.roi_back),
  73. "message": message,
  74. }
  75. def detect_ocr_detail(file: str, target: RuleOcr) -> dict:
  76. img = load_image(file, strict_size=False)
  77. if img is None:
  78. return {
  79. "matched": False,
  80. "similarity": 0.0,
  81. "text": "",
  82. "roiFront": _roi_to_text(target.roi),
  83. "roiBack": _roi_to_text(target.area),
  84. "message": "image_load_failed",
  85. }
  86. boxed = target.detect_and_ocr(img, logDisplay=False)
  87. selected = None
  88. if boxed:
  89. if target.keyword:
  90. for item in boxed:
  91. text = str(item.ocr_text or "")
  92. if target.keyword in text or text == target.keyword:
  93. selected = item
  94. break
  95. if selected is None:
  96. selected = max(boxed, key=lambda x: float(x.score))
  97. if selected is None:
  98. return {
  99. "matched": False,
  100. "similarity": 0.0,
  101. "text": "",
  102. "roiFront": _roi_to_text(target.roi),
  103. "roiBack": _roi_to_text(target.area),
  104. "message": "not_match",
  105. }
  106. box = selected.box
  107. x0 = int(np.min(box[:, 0])) + int(target.roi[0])
  108. y0 = int(np.min(box[:, 1])) + int(target.roi[1])
  109. x1 = int(np.max(box[:, 0])) + int(target.roi[0])
  110. y1 = int(np.max(box[:, 1])) + int(target.roi[1])
  111. w = max(1, x1 - x0)
  112. h = max(1, y1 - y0)
  113. target.roi = [x0, y0, w, h]
  114. target.area = [x0, y0, w, h]
  115. text = str(selected.ocr_text or "")
  116. matched = target.match(text, included=True) if target.keyword else bool(text)
  117. return {
  118. "matched": matched,
  119. "similarity": round(float(selected.score), 4),
  120. "text": text,
  121. "roiFront": _roi_to_text(target.roi),
  122. "roiBack": _roi_to_text(target.area),
  123. "message": "match" if matched else "not_match",
  124. }
  125. # 图片文件路径 可以是相对路径
  126. IMAGE_FILE = r"C:\Users\Ryland\Desktop\ScreenShot_2026-05-30_141821_235.png"
  127. if __name__ == '__main__':
  128. from tasks.Exploration.script_task import ScriptTask
  129. targe = ScriptTask.I_TREASURE_BOX_CLICK
  130. print(detect_image(IMAGE_FILE, targe))
  131. # ocr demo
  132. # from tasks.KekkaiActivation.assets import KekkaiActivationAssets
  133. # target = KekkaiActivationAssets.O_CARD_ALL_TIME
  134. # print(detect_ocr(IMAGE_FILE, target))