image-match.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. 模板匹配:在截图中查找模板图片的位置
  5. 用法1: python image-match.py <screenshot_path> <template_path> [threshold]
  6. 用法2: python image-match.py --adb <adb_path> --device <device_id> --screenshot <out_path> --template <template_path> [--threshold 0.8] [--method template|feature]
  7. 用法2 会在 Python 内执行 adb 截图,避免 Node 处理二进制数据导致的兼容性问题
  8. --method feature: 特征点匹配(优先 RoMa,失败则 ORB + 多尺度模板),不同分辨率可复用
  9. --method template: 像素模板匹配(TM_CCOEFF_NORMED),仅适合同分辨率
  10. 输出: JSON 到 stdout
  11. """
  12. import sys
  13. import os
  14. import json
  15. import subprocess
  16. try:
  17. import cv2
  18. import numpy as np
  19. except ImportError as e:
  20. print(json.dumps({"success": False, "error": f"OpenCV 导入失败: {e}。请安装: pip install opencv-python numpy"}))
  21. sys.exit(1)
  22. try:
  23. from PIL import Image as PILImage
  24. HAS_PIL = True
  25. except ImportError:
  26. HAS_PIL = False
  27. # RoMa:若已安装(python/RoMa,pip install -e .),则优先用于 feature 匹配
  28. HAS_ROMA = False
  29. try:
  30. _roma_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', 'python', 'RoMa'))
  31. if os.path.isdir(_roma_root) and _roma_root not in sys.path:
  32. sys.path.insert(0, _roma_root)
  33. from romatch import roma_outdoor
  34. import torch as _torch_roma
  35. HAS_ROMA = True
  36. except Exception:
  37. pass
  38. def save_match_crop(screenshot, x, y, w, h, crop_output_path):
  39. """匹配成功后,从截图中裁出 (x,y,w,h) 区域保存到 crop_output_path,便于肉眼核对。"""
  40. if not crop_output_path or w <= 0 or h <= 0:
  41. return
  42. try:
  43. sh, sw = screenshot.shape[:2]
  44. x1 = max(0, min(x, sw - 1))
  45. y1 = max(0, min(y, sh - 1))
  46. x2 = max(x1 + 1, min(x + w, sw))
  47. y2 = max(y1 + 1, min(y + h, sh))
  48. crop = screenshot[y1:y2, x1:x2]
  49. if crop.size > 0:
  50. out_dir = os.path.dirname(crop_output_path)
  51. if out_dir:
  52. os.makedirs(out_dir, exist_ok=True)
  53. cv2.imwrite(crop_output_path, crop)
  54. except Exception:
  55. pass
  56. def run_adb_screencap(adb_path, device, output_path):
  57. """在 Python 内执行 adb 截图,直接处理二进制流"""
  58. # Windows 下子进程需要可执行路径,正斜杠也可用
  59. args = [adb_path.replace('/', os.sep), '-s', device, 'exec-out', 'screencap', '-p']
  60. try:
  61. result = subprocess.run(args, capture_output=True, timeout=15)
  62. if result.returncode != 0:
  63. return False, (result.stderr or result.stdout or b'').decode('utf-8', errors='replace')
  64. data = result.stdout
  65. if not data or len(data) < 100:
  66. return False, "截图数据为空"
  67. # 注意:不要对 PNG 数据做 \r\n 替换,会破坏 IDAT 压缩块导致无法解析
  68. out_dir = os.path.dirname(output_path)
  69. if out_dir:
  70. os.makedirs(out_dir, exist_ok=True)
  71. with open(output_path, 'wb') as f:
  72. f.write(data)
  73. return True, output_path
  74. except subprocess.TimeoutExpired:
  75. return False, "截图超时"
  76. except Exception as e:
  77. return False, str(e)
  78. def load_image(path):
  79. """从文件路径加载图片,兼容 OpenCV 无法直接读取的 PNG(如部分 Android 截图)"""
  80. if not os.path.exists(path):
  81. return None
  82. with open(path, 'rb') as f:
  83. data = np.frombuffer(f.read(), dtype=np.uint8)
  84. img = cv2.imdecode(data, cv2.IMREAD_COLOR)
  85. if img is not None:
  86. return img
  87. img = cv2.imread(path)
  88. if img is not None:
  89. return img
  90. if HAS_PIL:
  91. try:
  92. pil_img = PILImage.open(path).convert('RGB')
  93. img = cv2.cvtColor(np.array(pil_img), cv2.COLOR_RGB2BGR)
  94. return img
  95. except Exception:
  96. pass
  97. return None
  98. def _roma_params():
  99. """从环境变量读取 RoMa 参数,便于反复测试调参。默认针对「模板为截图中缩略图」优化。"""
  100. import os as _os
  101. coarse = int(_os.environ.get("ROMA_COARSE_RES", "560"))
  102. upsample = int(_os.environ.get("ROMA_UPSAMPLE_RES", "1152"))
  103. min_m = int(_os.environ.get("ROMA_MIN_MATCHES", "3"))
  104. sample_num = int(_os.environ.get("ROMA_SAMPLE_NUM", "20000"))
  105. ransac = float(_os.environ.get("ROMA_RANSAC_THRESH", "14.0"))
  106. return coarse, upsample, min_m, sample_num, ransac
  107. def match_by_roma(screenshot, template, min_matches=6, device=None):
  108. """
  109. 使用 RoMa 稠密特征匹配,在截图中找模板位置;精度高、跨分辨率。
  110. 返回 (x, y, w, h, center_x, center_y) 或 None。
  111. 可通过环境变量调参: ROMA_COARSE_RES, ROMA_UPSAMPLE_RES, ROMA_MIN_MATCHES, ROMA_SAMPLE_NUM, ROMA_RANSAC_THRESH
  112. """
  113. if not HAS_ROMA:
  114. return None
  115. t_h, t_w = template.shape[:2]
  116. sh_h, sh_w = screenshot.shape[:2]
  117. coarse_res, upsample_res, env_min_matches, sample_num, ransac_thresh = _roma_params()
  118. min_matches = env_min_matches # 调参时用环境变量 ROMA_MIN_MATCHES
  119. import tempfile
  120. try:
  121. if _torch_roma.get_float32_matmul_precision() != "highest":
  122. _torch_roma.set_float32_matmul_precision("highest")
  123. except Exception:
  124. pass
  125. try:
  126. if device is None:
  127. device = _torch_roma.device("cuda" if _torch_roma.cuda.is_available() else "cpu")
  128. roma_model = roma_outdoor(device=device, coarse_res=coarse_res, upsample_res=upsample_res)
  129. with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as fa:
  130. with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as fb:
  131. path_a = fa.name
  132. path_b = fb.name
  133. try:
  134. if HAS_PIL:
  135. PILImage.fromarray(cv2.cvtColor(screenshot, cv2.COLOR_BGR2RGB)).save(path_a)
  136. PILImage.fromarray(cv2.cvtColor(template, cv2.COLOR_BGR2RGB)).save(path_b)
  137. else:
  138. cv2.imwrite(path_a, cv2.cvtColor(screenshot, cv2.COLOR_BGR2RGB))
  139. cv2.imwrite(path_b, cv2.cvtColor(template, cv2.COLOR_BGR2RGB))
  140. warp, certainty = roma_model.match(path_a, path_b, device=device)
  141. matches, certainty = roma_model.sample(warp, certainty, num=sample_num)
  142. H_out, W_out = roma_model.get_output_resolution()
  143. kptsA, kptsB = roma_model.to_pixel_coordinates(matches, H_out, W_out, H_out, W_out)
  144. kptsA = kptsA.cpu().numpy().astype(np.float32)
  145. kptsB = kptsB.cpu().numpy().astype(np.float32)
  146. if kptsA.shape[0] < min_matches:
  147. return None
  148. scale_ax = sh_w / float(W_out)
  149. scale_ay = sh_h / float(H_out)
  150. scale_bx = t_w / float(W_out)
  151. scale_by = t_h / float(H_out)
  152. kptsA_orig = kptsA * np.array([scale_ax, scale_ay])
  153. kptsB_orig = kptsB * np.array([scale_bx, scale_by])
  154. # RANSAC 距离阈值略放宽,适配缩放/透视变形(可由 ROMA_RANSAC_THRESH 调节)
  155. H, mask = cv2.findHomography(kptsB_orig, kptsA_orig, cv2.RANSAC, ransac_thresh)
  156. if H is None:
  157. return None
  158. corners = np.float32([[0, 0], [t_w, 0], [t_w, t_h], [0, t_h]]).reshape(-1, 1, 2)
  159. corners_screen = cv2.perspectiveTransform(corners, H)
  160. x_coords = corners_screen[:, 0, 0]
  161. y_coords = corners_screen[:, 0, 1]
  162. x = int(round(np.min(x_coords)))
  163. y = int(round(np.min(y_coords)))
  164. w = int(round(np.max(x_coords) - np.min(x_coords)))
  165. h = int(round(np.max(y_coords) - np.min(y_coords)))
  166. center_x = int(round(np.mean(x_coords)))
  167. center_y = int(round(np.mean(y_coords)))
  168. return (x, y, w, h, center_x, center_y)
  169. finally:
  170. try:
  171. os.unlink(path_a)
  172. os.unlink(path_b)
  173. except Exception:
  174. pass
  175. except Exception:
  176. return None
  177. def match_by_features(screenshot, template, min_good_matches=6):
  178. """
  179. 基于特征点(ORB)匹配作为回退:在截图中找模板位置,返回 (x, y, w, h, center_x, center_y) 或 None。
  180. """
  181. gray_screen = cv2.cvtColor(screenshot, cv2.COLOR_BGR2GRAY)
  182. gray_tpl = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)
  183. t_h, t_w = template.shape[:2]
  184. orb = cv2.ORB_create(nfeatures=2000)
  185. kp1, desc1 = orb.detectAndCompute(gray_tpl, None)
  186. kp2, desc2 = orb.detectAndCompute(gray_screen, None)
  187. if desc1 is None or desc2 is None or len(kp1) < 4 or len(kp2) < 4:
  188. return None
  189. bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=False)
  190. matches = bf.knnMatch(desc1, desc2, k=2)
  191. good = []
  192. for m_n in matches:
  193. if len(m_n) != 2:
  194. continue
  195. m, n = m_n
  196. if m.distance < 0.82 * n.distance:
  197. good.append(m)
  198. if len(good) < min_good_matches:
  199. return None
  200. src_pts = np.float32([kp1[m.queryIdx].pt for m in good]).reshape(-1, 1, 2)
  201. dst_pts = np.float32([kp2[m.trainIdx].pt for m in good]).reshape(-1, 1, 2)
  202. H, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0)
  203. if H is None:
  204. return None
  205. # 模板四角在截图中的坐标,用质心作为中心点
  206. corners = np.float32([[0, 0], [t_w, 0], [t_w, t_h], [0, t_h]]).reshape(-1, 1, 2)
  207. corners_screen = cv2.perspectiveTransform(corners, H)
  208. x_coords = corners_screen[:, 0, 0]
  209. y_coords = corners_screen[:, 0, 1]
  210. x = int(round(np.min(x_coords)))
  211. y = int(round(np.min(y_coords)))
  212. w = int(round(np.max(x_coords) - np.min(x_coords)))
  213. h = int(round(np.max(y_coords) - np.min(y_coords)))
  214. center_x = int(round(np.mean(x_coords)))
  215. center_y = int(round(np.mean(y_coords)))
  216. return (x, y, w, h, center_x, center_y)
  217. def multi_scale_template_match(screenshot, template, threshold=0.50, scale_min=0.4, scale_max=1.65):
  218. """
  219. 多尺度模板匹配:对模板做多种缩放后在截图中匹配,适配不同分辨率(如简单图标、轮廓)。
  220. scale_min, scale_max: 缩放比范围,如 0.08~2.0 可匹配截图中小缩略图。
  221. 返回 (x, y, w, h, center_x, center_y) 或 None。
  222. """
  223. gray_screen = cv2.cvtColor(screenshot, cv2.COLOR_BGR2GRAY)
  224. gray_tpl = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)
  225. sh, sw = screenshot.shape[:2]
  226. t_h, t_w = template.shape[:2]
  227. best = None
  228. best_val = threshold
  229. step = max(0.02, (scale_max - scale_min) / 60.0)
  230. for scale in np.arange(scale_min, scale_max + step * 0.5, step):
  231. w = max(8, int(round(t_w * scale)))
  232. h = max(8, int(round(t_h * scale)))
  233. if h > sh or w > sw:
  234. continue
  235. resized = cv2.resize(gray_tpl, (w, h), interpolation=cv2.INTER_AREA)
  236. result = cv2.matchTemplate(gray_screen, resized, cv2.TM_CCOEFF_NORMED)
  237. min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
  238. if max_val > best_val:
  239. best_val = max_val
  240. x, y = int(max_loc[0]), int(max_loc[1])
  241. center_x = x + w // 2
  242. center_y = y + h // 2
  243. best = (x, y, w, h, center_x, center_y)
  244. return best
  245. def main():
  246. screenshot_path = None
  247. template_path = None
  248. threshold = 0.8
  249. method = 'feature' # feature=特征点匹配(跨分辨率), template=像素模板匹配
  250. adb_path = None
  251. device = None
  252. scale_min, scale_max = 0.4, 1.65
  253. center_ratio = 1.0 # 仅用模板中心比例 0-1,1=100% 全图;裁剪模板边缘后匹配可提高精准度
  254. crop_square_percent = None # 若设置,则用方形区域裁剪:[百分比, 以w或h为边长] 如 [1,w] [0.1,h]
  255. crop_square_base = None # 'w' 或 'h'
  256. template_output_path = None # 若有裁剪且指定,则把裁剪后的图写回该路径(覆盖原模板)
  257. crop_output_path = None # 匹配成功后,从截图中裁出匹配区域保存到该路径(与模板同级,便于肉眼核对)
  258. if len(sys.argv) >= 2 and sys.argv[1] == '--adb':
  259. # 用法2:--adb --device --screenshot --template [--scale-min 0.2] [--scale-max 1.6]
  260. i = 1
  261. while i < len(sys.argv):
  262. if sys.argv[i] == '--adb' and i + 1 < len(sys.argv):
  263. adb_path = sys.argv[i + 1]
  264. i += 2
  265. elif sys.argv[i] == '--device' and i + 1 < len(sys.argv):
  266. device = sys.argv[i + 1]
  267. i += 2
  268. elif sys.argv[i] == '--screenshot' and i + 1 < len(sys.argv):
  269. screenshot_path = sys.argv[i + 1]
  270. i += 2
  271. elif sys.argv[i] == '--template' and i + 1 < len(sys.argv):
  272. template_path = sys.argv[i + 1]
  273. i += 2
  274. elif sys.argv[i] == '--threshold' and i + 1 < len(sys.argv):
  275. threshold = float(sys.argv[i + 1])
  276. i += 2
  277. elif sys.argv[i] == '--method' and i + 1 < len(sys.argv):
  278. method = (sys.argv[i + 1] or 'feature').strip().lower()
  279. if method not in ('template', 'feature'):
  280. method = 'feature'
  281. i += 2
  282. elif sys.argv[i] == '--scale-min' and i + 1 < len(sys.argv):
  283. scale_min = float(sys.argv[i + 1])
  284. i += 2
  285. elif sys.argv[i] == '--scale-max' and i + 1 < len(sys.argv):
  286. scale_max = float(sys.argv[i + 1])
  287. i += 2
  288. elif sys.argv[i] == '--center-ratio' and i + 1 < len(sys.argv):
  289. center_ratio = float(sys.argv[i + 1])
  290. if center_ratio <= 0 or center_ratio > 1:
  291. center_ratio = 1.0
  292. i += 2
  293. elif sys.argv[i] == '--crop-square' and i + 2 < len(sys.argv):
  294. try:
  295. crop_square_percent = float(sys.argv[i + 1])
  296. crop_square_base = (sys.argv[i + 2] or '').strip().lower()
  297. if crop_square_base not in ('w', 'h') or crop_square_percent <= 0:
  298. crop_square_percent = None
  299. crop_square_base = None
  300. except (ValueError, TypeError):
  301. crop_square_percent = None
  302. crop_square_base = None
  303. i += 3
  304. elif sys.argv[i] == '--template-output' and i + 1 < len(sys.argv):
  305. template_output_path = (sys.argv[i + 1] or '').strip()
  306. if not template_output_path:
  307. template_output_path = None
  308. i += 2
  309. elif sys.argv[i] == '--crop-output' and i + 1 < len(sys.argv):
  310. crop_output_path = (sys.argv[i + 1] or '').strip()
  311. if not crop_output_path:
  312. crop_output_path = None
  313. i += 2
  314. else:
  315. i += 1
  316. if adb_path and device and screenshot_path and template_path:
  317. ok, msg = run_adb_screencap(adb_path, device, screenshot_path)
  318. if not ok:
  319. print(json.dumps({"success": False, "error": f"截图失败: {msg}"}))
  320. sys.exit(1)
  321. else:
  322. print(json.dumps({"success": False, "error": "缺少 --adb/--device/--screenshot/--template 参数"}))
  323. sys.exit(1)
  324. else:
  325. # 用法1:位置参数
  326. if len(sys.argv) < 3:
  327. print(json.dumps({"success": False, "error": "用法: image-match.py <screenshot_path> <template_path> [threshold] [method=feature|template]"}))
  328. sys.exit(1)
  329. screenshot_path = sys.argv[1]
  330. template_path = sys.argv[2]
  331. threshold = float(sys.argv[3]) if len(sys.argv) > 3 else 0.8
  332. if len(sys.argv) > 4 and sys.argv[4].lower() in ('template', 'feature'):
  333. method = sys.argv[4].lower()
  334. if not os.path.exists(screenshot_path):
  335. print(json.dumps({"success": False, "error": f"截图文件不存在: {screenshot_path}"}))
  336. sys.exit(1)
  337. if not os.path.exists(template_path):
  338. print(json.dumps({"success": False, "error": f"模板文件不存在: {template_path}"}))
  339. sys.exit(1)
  340. screenshot = load_image(screenshot_path)
  341. template = load_image(template_path)
  342. if screenshot is None:
  343. print(json.dumps({"success": False, "error": "无法读取截图(文件损坏或格式不支持)"}))
  344. sys.exit(1)
  345. if template is None:
  346. print(json.dumps({"success": False, "error": f"无法读取模板: {template_path}"}))
  347. sys.exit(1)
  348. t_h, t_w = template.shape[:2]
  349. did_crop = False
  350. # 方形区域裁剪:以 template 的宽或高的百分比作为正方形边长,取中心正方形再匹配
  351. if crop_square_percent is not None and crop_square_base in ('w', 'h'):
  352. side_raw = (t_w if crop_square_base == 'w' else t_h) * crop_square_percent
  353. side = min(max(1, int(round(side_raw))), t_w, t_h)
  354. x0 = (t_w - side) // 2
  355. y0 = (t_h - side) // 2
  356. template = template[y0:y0 + side, x0:x0 + side].copy()
  357. did_crop = True
  358. # 兼容旧参数:只裁剪模板边缘(取中心比例)
  359. elif center_ratio < 1.0:
  360. nw = max(1, int(t_w * center_ratio))
  361. nh = max(1, int(t_h * center_ratio))
  362. x0 = (t_w - nw) // 2
  363. y0 = (t_h - nh) // 2
  364. template = template[y0:y0 + nh, x0:x0 + nw].copy()
  365. did_crop = True
  366. if did_crop:
  367. out_path = template_output_path if template_output_path else template_path
  368. try:
  369. cv2.imwrite(out_path, template)
  370. except Exception:
  371. pass
  372. t_h, t_w = template.shape[:2]
  373. if method == 'template' and (t_h > screenshot.shape[0] or t_w > screenshot.shape[1]):
  374. print(json.dumps({"success": False, "error": "模板尺寸大于截图"}))
  375. sys.exit(1)
  376. if method == 'feature':
  377. sh, sw = screenshot.shape[:2]
  378. # 仅对相册缩略图(路径含 pic):小模板时优先多尺度匹配;scale_min 不低于 0.18,避免极小尺度误匹配到右上角草稿箱等区域
  379. GALLERY_SCALE_MIN = 0.18
  380. is_gallery_thumb = template_path and 'pic' in os.path.basename(template_path)
  381. scale_min_use = max(scale_min, GALLERY_SCALE_MIN) if is_gallery_thumb else scale_min
  382. if is_gallery_thumb and t_w < sw * 0.5 and t_h < sh * 0.5:
  383. for th in (0.52, 0.48, 0.44, 0.40):
  384. scale_result = multi_scale_template_match(screenshot, template, threshold=th, scale_min=scale_min_use, scale_max=scale_max)
  385. if scale_result is not None:
  386. x, y, w, h, center_x, center_y = scale_result
  387. save_match_crop(screenshot, x, y, w, h, crop_output_path)
  388. output = {"success": True, "x": x, "y": y, "width": w, "height": h, "center_x": center_x, "center_y": center_y}
  389. print(json.dumps(output))
  390. sys.exit(0)
  391. # 1) RoMa 稠密特征匹配(若已安装);失败时用备用参数再试一次
  392. if HAS_ROMA:
  393. roma_result = match_by_roma(screenshot, template, min_matches=4)
  394. if roma_result is None:
  395. _save = (os.environ.get('ROMA_COARSE_RES'), os.environ.get('ROMA_UPSAMPLE_RES'), os.environ.get('ROMA_MIN_MATCHES'))
  396. for co, up, mn in [(672, 1120, 4), (448, 864, 2)]:
  397. try:
  398. os.environ['ROMA_COARSE_RES'] = str(co)
  399. os.environ['ROMA_UPSAMPLE_RES'] = str(up)
  400. os.environ['ROMA_MIN_MATCHES'] = str(mn)
  401. roma_result = match_by_roma(screenshot, template, min_matches=mn)
  402. if roma_result is not None:
  403. break
  404. finally:
  405. pass
  406. try:
  407. if _save[0] is None and 'ROMA_COARSE_RES' in os.environ:
  408. del os.environ['ROMA_COARSE_RES']
  409. elif _save[0] is not None:
  410. os.environ['ROMA_COARSE_RES'] = _save[0]
  411. if _save[1] is None and 'ROMA_UPSAMPLE_RES' in os.environ:
  412. del os.environ['ROMA_UPSAMPLE_RES']
  413. elif _save[1] is not None:
  414. os.environ['ROMA_UPSAMPLE_RES'] = _save[1]
  415. if _save[2] is None and 'ROMA_MIN_MATCHES' in os.environ:
  416. del os.environ['ROMA_MIN_MATCHES']
  417. elif _save[2] is not None:
  418. os.environ['ROMA_MIN_MATCHES'] = _save[2]
  419. except Exception:
  420. pass
  421. if roma_result is not None:
  422. x, y, w, h, center_x, center_y = roma_result
  423. save_match_crop(screenshot, x, y, w, h, crop_output_path)
  424. output = {
  425. "success": True,
  426. "x": x,
  427. "y": y,
  428. "width": w,
  429. "height": h,
  430. "center_x": center_x,
  431. "center_y": center_y
  432. }
  433. print(json.dumps(output))
  434. sys.exit(0)
  435. # 2) 回退:ORB 特征点匹配
  436. feat_result = match_by_features(screenshot, template)
  437. if feat_result is not None:
  438. x, y, w, h, center_x, center_y = feat_result
  439. save_match_crop(screenshot, x, y, w, h, crop_output_path)
  440. output = {
  441. "success": True,
  442. "x": x,
  443. "y": y,
  444. "width": w,
  445. "height": h,
  446. "center_x": center_x,
  447. "center_y": center_y
  448. }
  449. print(json.dumps(output))
  450. sys.exit(0)
  451. # 3) 回退:多尺度模板匹配,阈值逐级放宽至 0.40;相册缩略图 scale_min 不低于 GALLERY_SCALE_MIN,避免误匹配草稿箱
  452. scale_min_use = max(scale_min, GALLERY_SCALE_MIN) if is_gallery_thumb else scale_min
  453. scale_result = None
  454. for fallback_threshold in (0.52, 0.48, 0.44, 0.40):
  455. scale_result = multi_scale_template_match(screenshot, template, threshold=min(threshold, fallback_threshold), scale_min=scale_min_use, scale_max=scale_max)
  456. if scale_result is not None:
  457. break
  458. if scale_result is None and (t_w > 1.3 * t_h or t_h > 1.3 * t_w):
  459. t_s = min(t_w, t_h)
  460. cx, cy = t_w // 2, t_h // 2
  461. y0, y1 = max(0, cy - t_s // 2), min(t_h, cy + t_s // 2)
  462. x0, x1 = max(0, cx - t_s // 2), min(t_w, cx + t_s // 2)
  463. if y1 > y0 and x1 > x0:
  464. crop = template[y0:y1, x0:x1]
  465. for fallback_threshold in (0.52, 0.48, 0.44, 0.40):
  466. scale_result = multi_scale_template_match(screenshot, crop, threshold=min(threshold, fallback_threshold), scale_min=scale_min_use, scale_max=scale_max)
  467. if scale_result is not None:
  468. break
  469. if scale_result is not None:
  470. x, y, w, h, center_x, center_y = scale_result
  471. save_match_crop(screenshot, x, y, w, h, crop_output_path)
  472. output = {
  473. "success": True,
  474. "x": x,
  475. "y": y,
  476. "width": w,
  477. "height": h,
  478. "center_x": center_x,
  479. "center_y": center_y
  480. }
  481. print(json.dumps(output))
  482. sys.exit(0)
  483. print(json.dumps({"success": False, "error": "RoMa/特征点与多尺度模板均未匹配(可检查模板是否在画面中或使用 --method template)"}))
  484. sys.exit(1)
  485. # 使用 TM_CCOEFF_NORMED 进行模板匹配(仅同分辨率推荐)
  486. result = cv2.matchTemplate(screenshot, template, cv2.TM_CCOEFF_NORMED)
  487. min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
  488. if max_val < threshold:
  489. print(json.dumps({"success": False, "error": f"未找到匹配 (相似度 {max_val:.3f} < {threshold})"}))
  490. sys.exit(1)
  491. x, y = int(max_loc[0]), int(max_loc[1])
  492. center_x = x + t_w // 2
  493. center_y = y + t_h // 2
  494. save_match_crop(screenshot, x, y, t_w, t_h, crop_output_path)
  495. output = {
  496. "success": True,
  497. "x": x,
  498. "y": y,
  499. "width": t_w,
  500. "height": t_h,
  501. "center_x": center_x,
  502. "center_y": center_y
  503. }
  504. print(json.dumps(output))
  505. sys.exit(0)
  506. if __name__ == "__main__":
  507. main()