assets_test.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. # 掩码 / 多尺度 / SIFT 走本地 test_match
  63. try:
  64. matched = bool(target.test_match(img))
  65. # 优先取匹配过程记录的真实分数; SIFT 等未记录分数的方法退回 0/1
  66. similarity = float(getattr(target, "last_score", -1.0))
  67. if similarity < 0:
  68. similarity = 1.0 if matched else 0.0
  69. message = "match" if matched else "not_match"
  70. # 多尺度命中时带回最高分档的缩放倍数
  71. scale = getattr(target, "last_scale", None)
  72. if matched and scale is not None:
  73. message += f" scale={float(scale):.2f}"
  74. except Exception as e:
  75. message = f"match_error:{e}"
  76. return {
  77. "matched": matched,
  78. "similarity": round(similarity, 4),
  79. "roiFront": _roi_to_text(target.roi_front),
  80. "roiBack": _roi_to_text(target.roi_back),
  81. "message": message,
  82. }
  83. def detect_ocr_detail(file: str, target: RuleOcr) -> dict:
  84. img = load_image(file, strict_size=False)
  85. if img is None:
  86. return {
  87. "matched": False,
  88. "similarity": 0.0,
  89. "text": "",
  90. "roiFront": _roi_to_text(target.roi),
  91. "roiBack": _roi_to_text(target.area),
  92. "message": "image_load_failed",
  93. }
  94. boxed = target.detect_and_ocr(img, logDisplay=False)
  95. selected = None
  96. if boxed:
  97. if target.keyword:
  98. for item in boxed:
  99. text = str(item.ocr_text or "")
  100. if target.keyword in text or text == target.keyword:
  101. selected = item
  102. break
  103. if selected is None:
  104. selected = max(boxed, key=lambda x: float(x.score))
  105. if selected is None:
  106. return {
  107. "matched": False,
  108. "similarity": 0.0,
  109. "text": "",
  110. "roiFront": _roi_to_text(target.roi),
  111. "roiBack": _roi_to_text(target.area),
  112. "message": "not_match",
  113. }
  114. box = selected.box
  115. x0 = int(np.min(box[:, 0])) + int(target.roi[0])
  116. y0 = int(np.min(box[:, 1])) + int(target.roi[1])
  117. x1 = int(np.max(box[:, 0])) + int(target.roi[0])
  118. y1 = int(np.max(box[:, 1])) + int(target.roi[1])
  119. w = max(1, x1 - x0)
  120. h = max(1, y1 - y0)
  121. target.roi = [x0, y0, w, h]
  122. target.area = [x0, y0, w, h]
  123. text = str(selected.ocr_text or "")
  124. matched = target.match(text, included=True) if target.keyword else bool(text)
  125. return {
  126. "matched": matched,
  127. "similarity": round(float(selected.score), 4),
  128. "text": text,
  129. "roiFront": _roi_to_text(target.roi),
  130. "roiBack": _roi_to_text(target.area),
  131. "message": "match" if matched else "not_match",
  132. }
  133. # 图片文件路径 可以是相对路径
  134. IMAGE_FILE = r"C:\Users\Ryland\Desktop\ScreenShot_2026-05-30_141821_235.png"
  135. if __name__ == '__main__':
  136. from tasks.Exploration.script_task import ScriptTask
  137. targe = ScriptTask.I_TREASURE_BOX_CLICK
  138. print(detect_image(IMAGE_FILE, targe))
  139. # ocr demo
  140. # from tasks.KekkaiActivation.assets import KekkaiActivationAssets
  141. # target = KekkaiActivationAssets.O_CARD_ALL_TIME
  142. # print(detect_ocr(IMAGE_FILE, target))