Files
douyi_guaji_main/douyin/detection/detection_pkg/finder_template_match.py
T

284 lines
9.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""douyin/detection/detection_pkg/finder_template_match - OpenCV 模板匹配兜底
像素检测(numpy + 逐像素)都失败时的最后兜底:用 OpenCV 模板匹配
(TM_CCOEFF_NORMED)在搜索区域里找 fudai.png 模板。
入口:match_template(analyser, pic_new, rx, ry, y_pianyi_val, loop) -> (detected, x)
"""
from __future__ import annotations
import os
import time
from typing import Any, Optional, Tuple
def _resolve_template_path() -> str:
"""获取 fudai.png 模板的绝对路径。优先 path_config,回退到本文件相邻 assets/。"""
try:
from douyin.core import path_config
return path_config.get_assets_dir() + "/fudai.png"
except Exception:
# 兜底:导入失败时显式拼到项目根的 assets/,
# 不要用 dirname(__file__) 拼 "assets"(那样会推到 detection_pkg/assets)
return os.path.normpath(
os.path.join(
os.path.dirname(__file__),
"..", "..", "..",
"assets", "fudai.png",
)
)
def _save_search_roi(analyser, search_roi, last_match_path: str, loop: int, x1: int, x2: int, y_top: int, y_bottom: int, rw: int, rh: int) -> None:
"""保存搜索区域到 last_match.png(仅作日志/调试用)。"""
try:
import cv2
save_success = cv2.imwrite(last_match_path, search_roi)
if save_success:
try:
analyser._log_kv(
"[找福袋图标] 已保存搜索区域截图",
loop=loop,
文件路径=last_match_path,
搜索区域尺寸=f"{rw}x{rh}",
搜索区域坐标=f"({x1},{y_top})-({x2},{y_bottom})",
)
except Exception:
pass
else:
try:
analyser._log_kv(
"[找福袋图标] 保存搜索区域截图失败",
loop=loop,
文件路径=last_match_path,
)
except Exception:
pass
except Exception as save_err:
try:
analyser._log_kv(
"[找福袋图标] 保存搜索区域截图异常",
loop=loop,
错误类型=type(save_err).__name__,
错误信息=str(save_err),
)
except Exception:
pass
def match_template(
analyser: Any,
pic_new,
rx: int,
ry: int,
y_pianyi_val: int,
loop: int,
) -> Tuple[bool, Optional[int]]:
"""像素检测全部失败后的模板匹配兜底。
Args:
analyser: 提供 fudai_template_match_threshold / _log_kv / last_find_fudai_time
pic_new: PIL.Image,已加载的当前截图
rx, ry: 目标分辨率宽/高(默认 1080x2400 基准)
y_pianyi_val: Y 偏移
loop: 当前循环次数(仅用于日志)
Returns:
(detected, detected_x) — 命中时 detected_x 是 1080 基准坐标。
"""
detected = False
detected_x: Optional[int] = None
try:
import cv2
import numpy as np
template_path = _resolve_template_path()
if not os.path.exists(template_path):
return False, None
# 获取匹配阈值
try:
template_threshold = float(
getattr(analyser, "fudai_template_match_threshold", 0.7) or 0.7
)
except Exception:
template_threshold = 0.7
# 读取模板
template = cv2.imread(template_path, cv2.IMREAD_COLOR)
if template is None:
return False, None
# 将 PIL 图片转换为 OpenCV 格式
pic_rgb = pic_new.convert("RGB")
screenshot_bgr = cv2.cvtColor(np.array(pic_rgb), cv2.COLOR_RGB2BGR)
height, width = screenshot_bgr.shape[:2]
# 定义搜索区域:X=156-269(1080 基准),Y=351-451(2400 基准)
x1 = max(0, 156 * rx // 1080)
x2 = min(width, 269 * rx // 1080)
y_top = max(0, 351 * ry // 2400 + y_pianyi_val)
y_bottom = min(height, 451 * ry // 2400 + y_pianyi_val)
if not (x1 < x2 and y_top < y_bottom):
try:
analyser._log_kv(
"[找福袋图标] 模板匹配失败-坐标无效",
loop=loop,
x1=x1,
x2=x2,
y_top=y_top,
y_bottom=y_bottom,
截图尺寸=f"{width}x{height}",
分辨率=f"{rx}x{ry}",
)
except Exception:
pass
return False, None
search_roi = screenshot_bgr[y_top:y_bottom, x1:x2] # type: ignore[index]
if search_roi.size == 0:
try:
analyser._log_kv(
"[找福袋图标] 模板匹配失败-搜索区域为空",
loop=loop,
搜索区域=f"({x1},{y_top})-({x2},{y_bottom})",
截图尺寸=f"{width}x{height}",
)
except Exception:
pass
return False, None
th, tw = template.shape[:2]
rh, rw = search_roi.shape[:2]
# 保存搜索区域截图到 last_match.png
try:
from douyin.core import path_config as _pc
match_dir = os.path.join(_pc.get_pic_dir(), "template_match")
except Exception:
match_dir = os.path.normpath(
os.path.join(
os.path.dirname(__file__),
"..", "..", "..",
"pic", "template_match",
)
)
try:
os.makedirs(match_dir, exist_ok=True)
last_match_path = os.path.join(match_dir, "last_match.png")
_save_search_roi(
analyser, search_roi, last_match_path, loop,
x1, x2, y_top, y_bottom, rw, rh,
)
except Exception:
pass
# 模板太大就缩小
if th > rh or tw > rw:
scale = min(rh / max(th, 1), rw / max(tw, 1))
if scale > 0:
template = cv2.resize(
template,
(max(1, int(tw * scale)), max(1, int(th * scale))),
)
th, tw = template.shape[:2]
# 执行模板匹配
if not (th <= rh and tw <= rw):
try:
analyser._log_kv(
"[找福袋图标] 模板匹配失败-尺寸不匹配",
loop=loop,
模板尺寸=f"{tw}x{th}",
搜索区域尺寸=f"{rw}x{rh}",
条件=f"th({th})<=rh({rh}) and tw({tw})<=rw({rw})",
)
except Exception:
pass
return False, None
res = cv2.matchTemplate(search_roi, template, cv2.TM_CCOEFF_NORMED)
_min_val, max_val, _min_loc, max_loc = cv2.minMaxLoc(res)
# 双截图记录:保存实时操作截图(搜索区域)
try:
from douyin.handlers import douyin_screenshot_manager
from PIL import Image as PILImage
search_roi_pil = PILImage.fromarray(
cv2.cvtColor(search_roi, cv2.COLOR_BGR2RGB)
)
douyin_screenshot_manager.save_latest_attempt(
search_roi_pil, operation_type="template_match"
)
except Exception:
pass
try:
analyser._log_kv(
"[找福袋图标] 模板匹配兜底",
loop=loop,
匹配分数=round(max_val, 4),
阈值=template_threshold,
是否通过=bool(max_val >= template_threshold),
搜索区域=f"({x1},{y_top})-({x2},{y_bottom})",
搜索区域尺寸=f"{rw}x{rh}",
模板尺寸=f"{tw}x{th}",
截图尺寸=f"{width}x{height}",
分辨率=f"{rx}x{ry}",
)
except Exception:
pass
if max_val < template_threshold:
return False, None
# 命中:保存成功识别存档
try:
from douyin.handlers import douyin_screenshot_manager
from PIL import Image as PILImage
search_roi_pil = PILImage.fromarray(
cv2.cvtColor(search_roi, cv2.COLOR_BGR2RGB)
)
douyin_screenshot_manager.save_last_success(
search_roi_pil, operation_type="template_match"
)
except Exception:
pass
# 1080 基准坐标
detected_x_in_roi = max_loc[0]
detected_x = int(
(x1 + detected_x_in_roi + tw // 2) * 1080.0 / max(1, rx)
)
detected = True
try:
analyser.last_fudai_x = detected_x
except Exception:
pass
try:
analyser.last_find_fudai_time = time.time()
except Exception:
pass
return detected, detected_x
except (ImportError, Exception) as e:
# OpenCV 不可用或模板匹配失败,让 caller 继续走未命中分支
try:
analyser._log_kv(
"[找福袋图标] 模板匹配异常",
loop=loop,
错误类型=type(e).__name__,
错误信息=str(e),
)
except Exception:
pass
return False, None