From 787c4e8eecbfc7a6a6b3a41ff747923622ba99e2 Mon Sep 17 00:00:00 2001 From: Codex Date: Thu, 21 May 2026 16:55:24 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8B=86=E5=88=86=20douyin=5Fdetection=20(1756?= =?UTF-8?q?=20=E2=86=92=203=20=E4=B8=AA=E5=AD=90=E6=A8=A1=E5=9D=97=20+=202?= =?UTF-8?q?2=20=E8=A1=8C=20shim)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 把 douyin/detection/douyin_detection.py 按职责拆成 douyin/detection/detection_pkg/ 子包: - _common (14) _ensure_detection_cache 共享缓存初始化 - detail (323) check_detail_height + check_in_fudai_detail(详情页相关) - finder (1438) check_have_fudai_with_ready_screenshot(找福袋图标主入口,最大头) 原文件改为 22 行 re-export shim,4 个公共符号全部保留。 - check_in_fudai_detail 5 处外部引用 - check_have_fudai_with_ready_screenshot 3 处外部引用 - check_detail_height 2 处外部引用 全部不需修改。 子包名 detection_pkg 是为了避免与同目录下的 shim 文件 douyin_detection.py 潜在的命名混淆。 回归: - 4 个新文件 + shim compileall 通过 - pytest 353 passed / 1 skipped - 旧 import 路径与新子包路径返回同一函数对象 Co-Authored-By: Claude Opus 4.7 --- douyin/detection/detection_pkg/__init__.py | 23 + douyin/detection/detection_pkg/_common.py | 14 + douyin/detection/detection_pkg/detail.py | 323 ++++ douyin/detection/detection_pkg/finder.py | 1438 ++++++++++++++++ douyin/detection/douyin_detection.py | 1772 +------------------- 5 files changed, 1817 insertions(+), 1753 deletions(-) create mode 100644 douyin/detection/detection_pkg/__init__.py create mode 100644 douyin/detection/detection_pkg/_common.py create mode 100644 douyin/detection/detection_pkg/detail.py create mode 100644 douyin/detection/detection_pkg/finder.py diff --git a/douyin/detection/detection_pkg/__init__.py b/douyin/detection/detection_pkg/__init__.py new file mode 100644 index 0000000..9b747d5 --- /dev/null +++ b/douyin/detection/detection_pkg/__init__.py @@ -0,0 +1,23 @@ +"""detection_pkg - 福袋检测子包。 + +原 douyin/detection/douyin_detection.py 拆成 3 个子模块: + +- _common _ensure_detection_cache +- detail check_detail_height / check_in_fudai_detail +- finder check_have_fudai_with_ready_screenshot(最大头) + +外部 import 路径不变: +``from douyin.detection.douyin_detection import check_in_fudai_detail`` 仍可用 +(原文件改成 re-export shim)。 +""" + +from ._common import _ensure_detection_cache +from .detail import check_detail_height, check_in_fudai_detail +from .finder import check_have_fudai_with_ready_screenshot + +__all__ = [ + "_ensure_detection_cache", + "check_detail_height", + "check_have_fudai_with_ready_screenshot", + "check_in_fudai_detail", +] diff --git a/douyin/detection/detection_pkg/_common.py b/douyin/detection/detection_pkg/_common.py new file mode 100644 index 0000000..06301b3 --- /dev/null +++ b/douyin/detection/detection_pkg/_common.py @@ -0,0 +1,14 @@ +"""detection_pkg._common - 共享:分析器检测缓存初始化。""" +from __future__ import annotations + +from typing import Any + + +def _ensure_detection_cache(analyser: Any) -> None: + """Ensure analyser has a simple detection cache structure.""" + if not hasattr(analyser, "_detection_cache"): + analyser._detection_cache = {} + if not hasattr(analyser, "_detection_cache_ttl"): + analyser._detection_cache_ttl = 2.0 + + diff --git a/douyin/detection/detection_pkg/detail.py b/douyin/detection/detection_pkg/detail.py new file mode 100644 index 0000000..188d12e --- /dev/null +++ b/douyin/detection/detection_pkg/detail.py @@ -0,0 +1,323 @@ +"""detection_pkg.detail - 福袋详情页相关检测。 + +- check_detail_height 识别详情页弹窗中"任务"数量(0/1/2/3) +- check_in_fudai_detail 判断当前是否处于福袋详情页(综合多种特征) +""" +from __future__ import annotations + +# pylint: disable=no-member + +import os +import time +from typing import Any + +from PIL import Image + +from ._common import _ensure_detection_cache + + +def check_detail_height(analyser: Any) -> int: + """Detect how many tasks are required in the fudai detail popup. + + Returns: + 0 / 1 / 2 / 3 (number of tasks). + """ + # 使用统一的截图路径配置 + try: + from douyin.core import path_config + + pic1_path = path_config.get_screenshot_path() + except Exception: + # 回退到旧路径(向后兼容) + from douyin.core import path_config + + pic1_path = path_config.get_screenshot_path() + + # 记录截图文件的时间戳(如果存在) + screenshot_timestamp = None + try: + if os.path.exists(pic1_path): + screenshot_timestamp = os.path.getmtime(pic1_path) + except Exception: + pass + + pic = None + for attempt in range(3): + try: + pic = Image.open(pic1_path) + pic.load() # force load to catch truncated files early + break + except Exception: + if attempt < 2: + try: + analyser.operation.get_screenshot( + analyser.device_id, use_service=False + ) + analyser.operation.delay(0.2) + # 重新获取截图后,更新时间戳 + try: + if os.path.exists(pic1_path): + screenshot_timestamp = os.path.getmtime(pic1_path) + except Exception: + pass + except Exception: + pass + pic = None + if pic is None: + try: + analyser._log_kv( + "检测", + "读取截图失败,默认不需要任务", + level="WARN", + screenshot_timestamp=screenshot_timestamp, + ) + except Exception: + pass + return 0 + + pic_new = pic.convert("RGBA") + pix = pic_new.load() + width, height = pic_new.size + + # X of “参与” button + try: + rx = int(getattr(analyser, "resolution_ratio_x", 1080) or 1080) + except Exception: + rx = 1080 + try: + ry = int(getattr(analyser, "resolution_ratio_y", 2400) or 2400) + except Exception: + ry = 2400 + + x = 536 * rx // 1080 + if not (0 <= x < width): + return 0 + + # (base_y, y_pianyi_offset, task_count) + try: + y_pianyi_val = int(getattr(analyser, "y_pianyi", 0) or 0) + except Exception: + y_pianyi_val = 0 + + check_points = [ + (983, 0, 2), + (983, y_pianyi_val, 2), + (983, -y_pianyi_val, 2), + (883, 0, 3), + (883, y_pianyi_val, 3), + (883, -y_pianyi_val, 3), + (1058, 0, 1), + (1058, y_pianyi_val, 1), + (1058, -y_pianyi_val, 1), + ] + + def _check_pixel_color(y_coord: int) -> bool: + if not (0 <= y_coord < height): + return False + try: + p = pix[x, y_coord] + if not (isinstance(p, tuple) and len(p) >= 3): + return False + r, g, b = int(p[0]), int(p[1]), int(p[2]) + return 28 <= r <= 40 and 32 <= g <= 42 and 76 <= b <= 86 + except Exception: + return False + + detected_task_count = None + detected_y = None + try: + for base_y, dy, task_count in check_points: + y = base_y * ry // 2400 + dy + if _check_pixel_color(y): + detected_task_count = task_count + detected_y = y + try: + import time + + current_time = time.time() + analyser._log_kv( + "检测", + f"识别到福袋任务数量:{task_count}个", + task_count=task_count, + detected_y=detected_y, + screenshot_timestamp=screenshot_timestamp, + current_time=current_time, + time_diff=( + round(current_time - screenshot_timestamp, 3) + if screenshot_timestamp + else None + ), + ) + except Exception: + pass + break + except Exception: + pass + + # 保存调试图片:标注检测区域和检测点 + try: + from douyin.handlers import douyin_screenshot_manager + + # 计算检测区域(参与按钮附近的区域) + check_region_x1 = max(0, x - 50) + check_region_x2 = min(width, x + 50) + check_region_y1 = max(0, (883 * ry // 2400) - 100) # 3个任务的Y坐标附近 + check_region_y2 = min(height, (1058 * ry // 2400) + 100) # 1个任务的Y坐标附近 + + # 标注检测点 + coords_list = [] + for base_y, dy, task_count_val in check_points: + check_y = base_y * ry // 2400 + dy + if 0 <= check_y < height: + # 标注检测点(小矩形区域) + coords_list.append( + ( + max(0, x - 10), + max(0, check_y - 5), + min(width, x + 10), + min(height, check_y + 5), + ) + ) + + # 保存调试图片 + pic_rgb = pic.convert("RGB") + debug_path = douyin_screenshot_manager.save_debug_image( + pic_rgb, + operation_type="detection", + picname="福袋任务数量", + step="task_count_detection", + coords=(check_region_x1, check_region_y1, check_region_x2, check_region_y2), + extra_info={ + "task_count": ( + detected_task_count if detected_task_count is not None else 0 + ), + "detected_y": detected_y, + "check_x": x, + "check_points_count": len(check_points), + "region": f"({check_region_x1},{check_region_y1})-({check_region_x2},{check_region_y2})", + }, + ) + if debug_path: + try: + analyser._log_kv( + "[调试图像]", + "福袋任务数量检测", + path=debug_path, + task_count=( + detected_task_count if detected_task_count is not None else 0 + ), + ) + except Exception: + pass + except Exception: + pass + + if detected_task_count is not None: + return detected_task_count + + # Optional robot-check path from original implementation + try: + if ( + hasattr(analyser, "check_have_robot_analyse") + and analyser.check_have_robot_analyse() + ): + analyser.deal_robot_analyse() + except Exception: + pass + try: + import time + + current_time = time.time() + analyser._log_kv( + "检测", + "识别福袋任务数量:0个(不需要任务)", + level="INFO", + task_count=0, + screenshot_timestamp=screenshot_timestamp, + current_time=current_time, + time_diff=( + current_time - screenshot_timestamp if screenshot_timestamp else None + ), + ) + except Exception: + pass + return 0 + + + + +def check_in_fudai_detail(analyser: Any) -> bool: + """Detect whether we are currently on the fudai detail page. + + 使用模板匹配检测,与 detect_fudai_detail_page 保持一致。 + """ + _ensure_detection_cache(analyser) + cache_key = "check_in_fudai_detail" + now = time.time() + cached = analyser._detection_cache.get(cache_key) + if cached: + cached_ts, cached_result = cached + ttl = float(getattr(analyser, "_detection_cache_ttl", 2.0) or 2.0) + if (now - cached_ts) < ttl: + return bool(cached_result) + + # 使用模板匹配检测,与 detect_fudai_detail_page 保持一致 + try: + from douyin.detection import douyin_pixel + from douyin.core import path_config + + # 获取截图 + pic_path = path_config.get_screenshot_path() + if not os.path.exists(pic_path): + try: + analyser.operation.get_screenshot(analyser.device_id) + except Exception: + analyser._detection_cache[cache_key] = (now, False) + return False + pic_path = path_config.get_screenshot_path() + if not os.path.exists(pic_path): + analyser._detection_cache[cache_key] = (now, False) + return False + + pic = Image.open(pic_path) + pic.load() + pic_rgb = pic.convert("RGB") + + # 获取分辨率比例和Y偏移 + rx = int(getattr(analyser, "resolution_ratio_x", 1080) or 1080) + ry = int(getattr(analyser, "resolution_ratio_y", 2400) or 2400) + y_pianyi = int(getattr(analyser, "y_pianyi", 0) or 0) + + # 获取模板匹配阈值(优先使用福袋详情页专用阈值) + template_threshold = float( + getattr(analyser, "fudai_detail_template_threshold", None) + or getattr(analyser, "fudai_template_match_threshold", 0.7) + or 0.7 + ) + if template_threshold < 0.0: + template_threshold = 0.0 + if template_threshold > 1.0: + template_threshold = 1.0 + + # 使用任务数量检测 + result = douyin_pixel.detect_fudai_detail_page( + pic_rgb, + rx, + ry, + y_pianyi, + template_threshold, + return_info=False, # 不需要详细信息 + analyser=analyser, + ) + + is_in_detail = bool(result) + analyser._detection_cache[cache_key] = (now, is_in_detail) + return is_in_detail + except Exception: + analyser._detection_cache[cache_key] = (now, False) + return False + + +# ========== 以下为旧的像素检测代码,已暂时禁用 ========== +# 像素检测代码已删除,使用模板匹配替代 +# 如需恢复像素检测,请参考 git 历史记录 diff --git a/douyin/detection/detection_pkg/finder.py b/douyin/detection/detection_pkg/finder.py new file mode 100644 index 0000000..48558ca --- /dev/null +++ b/douyin/detection/detection_pkg/finder.py @@ -0,0 +1,1438 @@ +"""detection_pkg.finder - 找福袋图标主入口(1420 行)。 + +包含 check_have_fudai_with_ready_screenshot:在直播间内通过模板匹配 +定位福袋图标,并把找到的位置写到 analyser 上供后续点击使用。 +""" +from __future__ import annotations + +# pylint: disable=no-member + +import os +import time +from typing import Any + +from PIL import Image + +from ._common import _ensure_detection_cache + + +def check_have_fudai_with_ready_screenshot( + analyser: Any, screenshot_ready: bool = False +): + """判定直播页面福袋的小图标是否存在(优化版,优先使用模块化查找器)。 + + 如果启用了优化查找器(fudai_finder_optimized_enabled=True),则使用新的模块化查找器。 + 否则使用原版像素检测实现。 + """ + # 检查是否启用优化查找器 + try: + use_optimized = bool(getattr(analyser, "fudai_finder_optimized_enabled", False)) + except Exception: + use_optimized = False + + if use_optimized: + try: + from douyin.fudai.douyin_fudai_finder import ( + check_have_fudai_with_ready_screenshot_optimized, + ) + + return check_have_fudai_with_ready_screenshot_optimized( + analyser, screenshot_ready + ) + except ImportError: + # 优化版不可用,回退到原版 + try: + analyser._log_kv( + "[找福袋图标]", "优化查找器不可用,使用原版", error="ImportError" + ) + except Exception: + pass + except Exception as e: + # 优化版异常,回退到原版 + try: + analyser._log_kv( + "[找福袋图标]", + "优化查找器异常,使用原版", + error=str(e), + error_type=type(e).__name__, + ) + except Exception: + pass + + # 原版实现(像素检测版) + func_start = time.time() + # 使用统一的截图路径配置 + try: + from douyin.core import path_config + + pic1_path = path_config.get_screenshot_path() + except Exception: + # 回退到旧路径(向后兼容) + from douyin.core import path_config + + pic1_path = path_config.get_screenshot_path() + attempt_count = 0 # 仅用于日志记录,不影响循环控制 + + # ===== 像素检测版(优先使用),不依赖 OpenCV ===== + # 改为按时间控制循环,而不是按次数限制 + try: + retry_delay_s = float( + getattr(analyser, "fudai_icon_retry_delay_seconds", 0.6) or 0.6 + ) + except Exception: + retry_delay_s = 0.6 + try: + max_wait_s = float(getattr(analyser, "fudai_icon_max_wait_seconds", 5.0) or 5.0) + except Exception: + max_wait_s = 5.0 + if retry_delay_s < 0.1: + retry_delay_s = 0.1 # 最小间隔改为0.1秒 + if max_wait_s < 0: + max_wait_s = 0.0 + # 防止无限等待:最大等待时间限制为30秒 + if max_wait_s > 30.0: + max_wait_s = 30.0 + + loop = 0 + max_loops = int(max_wait_s / retry_delay_s) + 10 # 额外的循环次数保护 + consecutive_failures = 0 # 连续失败计数 + max_consecutive_failures = 5 # 最大连续失败次数 + + # 额外的时间限制保护,防止死循环 + absolute_timeout = func_start + max( + max_wait_s * 1.5, 15 + ) # 最长15秒,但不超过 max_wait_s * 1.5 + + while (time.time() - func_start) < max_wait_s and time.time() < absolute_timeout: + # 额外的循环次数保护:防止无限循环 + if loop >= max_loops: + try: + analyser._log_kv( + "[找福袋图标] 警告", + "达到最大循环次数限制,提前退出", + max_loops=max_loops, + elapsed_s=round(time.time() - func_start, 2), + ) + except Exception: + pass + break + + # 每次循环都重新截图(除了第一次如果已经提供了截图) + if not (screenshot_ready and loop == 0): + try: + analyser._shot_and_diagnose( + "找福袋图标", "判定是否存在福袋", analyser.device_id, diagnose=False + ) + except Exception: + # 如果截图失败,尝试直接调用 get_screenshot 并验证文件可读性(避免半写入/截断图片导致后续 PIL 打开失败) + try: + analyser.operation.get_screenshot( + analyser.device_id, use_service=False + ) + # 简单验证:文件存在、大小足够且能由 PIL 打开/加载 + valid = False + try: + if ( + os.path.exists(pic1_path) + and os.path.getsize(pic1_path) >= 1024 + ): + from PIL import Image as PILImage + + with PILImage.open(pic1_path) as _im: + _im.verify() + valid = True + except Exception: + valid = False + if not valid: + # 再尝试一次直接截图作为最后回退 + try: + analyser.operation.get_screenshot( + analyser.device_id, use_service=False + ) + except Exception: + pass + except Exception: + pass + + # 读取截图 + pic = None + try: + pic = Image.open(pic1_path) + pic.load() + except Exception: + # 如果读取失败,尝试重新截图 + try: + analyser.operation.get_screenshot(analyser.device_id, use_service=False) + pic = Image.open(pic1_path) + pic.load() + except Exception: + pass + + # 如果截图读取失败,等待后继续下一次循环 + if pic is None: + loop += 1 + consecutive_failures += 1 + # 如果连续失败太多次,提前退出 + if consecutive_failures >= max_consecutive_failures: + try: + analyser._log_kv( + "[找福袋图标] 警告", + "连续截图失败次数过多,提前退出", + consecutive_failures=consecutive_failures, + elapsed_s=round(time.time() - func_start, 2), + ) + except Exception: + pass + break + # 检查是否还有剩余时间 + elapsed = time.time() - func_start + if elapsed < max_wait_s: + remaining_time = max_wait_s - elapsed + sleep_time = min(retry_delay_s, remaining_time) + if sleep_time > 0: + try: + analyser.operation.delay(sleep_time) + except Exception: + time.sleep(sleep_time) + continue + + pic_new = pic.convert("RGBA") + width, height = pic_new.size + try: + rx = int(getattr(analyser, "resolution_ratio_x", 1080) or 1080) + except Exception: + rx = 1080 + try: + ry = int(getattr(analyser, "resolution_ratio_y", 2400) or 2400) + except Exception: + ry = 2400 + try: + y_pianyi_val = int(getattr(analyser, "y_pianyi", 0) or 0) + except Exception: + y_pianyi_val = 0 + + detected = False + detected_x = None + # 初始化扫描范围变量(用于日志记录) + scan_x_start = 41 * rx // 1080 + scan_x_end = min(410 * rx // 1080, width) + scan_y = 403 * ry // 2400 + y_pianyi_val + + # 获取最优搜索起始位置(使用历史记录或默认值) + try: + # 尝试获取上次找到的福袋位置(1080基准) + last_fudai_x = int(getattr(analyser, "last_fudai_x", 0) or 0) + if last_fudai_x > 0 and scan_x_start <= last_fudai_x <= scan_x_end: + optimal_x_scaled = last_fudai_x * rx // 1080 + else: + # 使用默认最优位置:最热门区域中心(98-240的中心,约169) + optimal_x_scaled = 169 * rx // 1080 + except Exception: + # 使用默认最优位置 + optimal_x_scaled = 169 * rx // 1080 + + # 确保最优位置在搜索范围内 + optimal_x_scaled = max(scan_x_start, min(optimal_x_scaled, scan_x_end - 1)) + + # 保存完整截图用于调试(检测前) + try: + from douyin.handlers import douyin_screenshot_manager + + pic_rgb_full = pic_new.convert("RGB") + + # 保存完整截图 + debug_path = douyin_screenshot_manager.save_debug_image( + pic_rgb_full, + operation_type="pixel_detect", + picname="fudai_icon", + step="before_detect", + coords=( + (scan_x_start, scan_y - 3, scan_x_end, scan_y + 4) + if scan_y >= 3 + else None + ), + extra_info={ + "scan_range": f"x:{scan_x_start}-{scan_x_end}, y:{scan_y}", + "resolution": f"{rx}x{ry}", + "y_offset": y_pianyi_val, + "loop": loop, + }, + ) + if debug_path: + try: + analyser._log_kv( + "[调试图像]", "福袋图标检测前保存完整截图", path=debug_path + ) + except Exception: + pass + except Exception: + pass + + # 先尝试 numpy 加速扫描水平带(性能优化:批量处理) + try: + import numpy as _np + + # 计算检测区域,考虑分辨率缩放和偏移 + # 将像素检测的扫描带左上角改为图片坐标 x=157, y=354(右下不变) + y0 = 354 * ry // 2400 + y_pianyi_val + x1 = max(0, 157 * rx // 1080) + x2 = min(width, 410 * rx // 1080) + y_top = max(0, y0 - 3) + y_bottom = min(height, y0 + 4) + + # 边界检查 + if x1 < x2 and y_top < y_bottom: + roi = pic_new.crop((x1, y_top, x2, y_bottom)).convert("RGB") + + # 双截图记录:保存实时操作截图(像素检测区域) + try: + from douyin.handlers import douyin_screenshot_manager + + douyin_screenshot_manager.save_latest_attempt( + roi, operation_type="pixel_detect" + ) + except Exception: + pass + + arr = _np.array(roi) + if arr.size > 0: + r = arr[:, :, 0] # type: ignore[index] + g = arr[:, :, 1] # type: ignore[index] + b = arr[:, :, 2] # type: ignore[index] + # 精度优化:扩大颜色范围,提高容错性(考虑不同屏幕色温和亮度) + # 原来的范围:R[193-203], G[176-193], B[237-247] + # 优化后:适度放宽范围,但保持中心值权重 + mask_center = (r >= 190) & (r <= 206) & (g >= 173) & (g <= 196) & (b >= 234) & (b <= 250) # type: ignore[operator] + # 速度优化:使用更宽松的预筛选,减少计算量 + mask = mask_center + # 检查每一列是否有足够多的匹配像素(提高准确性,避免单个噪点误判) + cols = _np.sum(mask, axis=0) + # 至少需要2个像素匹配(避免单个噪点误判) + valid_cols = cols >= 2 + if valid_cols.any(): + # 优化:从最优位置开始向左右两侧搜索 + roi_width = x2 - x1 + optimal_col = optimal_x_scaled - x1 # 转换为ROI内的列索引 + optimal_col = max(0, min(optimal_col, roi_width - 1)) + + # 从最优位置开始,向左右两侧交替搜索 + found_col = None + search_range = max(optimal_col, roi_width - optimal_col) + + # 先检查最优位置本身 + if valid_cols[optimal_col]: + found_col = optimal_col + else: + # 向左右两侧交替搜索 + for offset in range(1, search_range + 1): + # 先检查右侧 + right_col = optimal_col + offset + if right_col < roi_width and valid_cols[right_col]: + found_col = right_col + break + # 再检查左侧 + left_col = optimal_col - offset + if left_col >= 0 and valid_cols[left_col]: + found_col = left_col + break + + # 如果没找到,回退到原来的方法(从左到右) + if found_col is None: + found_col = int(_np.argmax(valid_cols)) + + col_idx = found_col + # 计算检测到的x坐标:转换为1080基准坐标,确保精度 + detected_x = int((x1 + col_idx) * 1080.0 / max(1, rx)) + detected = True + + # 保存找到的位置作为下次的最优位置 + try: + analyser.last_fudai_x = detected_x + except Exception: + pass + + # 双截图记录:保存成功识别存档截图 + try: + from douyin.handlers import douyin_screenshot_manager + + douyin_screenshot_manager.save_last_success( + roi, operation_type="pixel_detect" + ) + + # 保存完整截图(检测成功) + pic_rgb_full = pic_new.convert("RGB") + debug_path = douyin_screenshot_manager.save_debug_image( + pic_rgb_full, + operation_type="pixel_detect", + picname="fudai_icon", + step="detected", + coords=(x1, y_top, x2, y_bottom), + extra_info={ + "detected_x": detected_x, + "detected_x_scaled": x1 + col_idx, + "scan_range": f"x:{x1}-{x2}, y:{y_top}-{y_bottom}", + "resolution": f"{rx}x{ry}", + "result": "success", + }, + ) + if debug_path: + try: + analyser._log_kv( + "[调试图像]", + "福袋图标检测成功保存完整截图", + path=debug_path, + detected_x=detected_x, + ) + except Exception: + pass + except Exception: + pass + + try: + analyser.last_find_fudai_time = time.time() + except Exception: + pass + except (ImportError, ValueError, IndexError, AttributeError): + # numpy不可用或计算错误,回退到逐像素检测 + detected = False + + # numpy 未命中/不可用时回退到原始逐像素检查 + if not detected: + try: + pix = pic_new.load() + pic_width, pic_height = pic_new.size + # 计算实际扫描范围,防止越界 + scan_x_start = 41 * rx // 1080 + scan_x_end = min(410 * rx // 1080, pic_width) + scan_y = 403 * ry // 2400 + y_pianyi_val + + # 获取最优搜索起始位置(使用历史记录或默认值) + try: + # 尝试获取上次找到的福袋位置(1080基准) + last_fudai_x = int(getattr(analyser, "last_fudai_x", 0) or 0) + if ( + last_fudai_x > 0 + and scan_x_start <= last_fudai_x * rx // 1080 <= scan_x_end + ): + optimal_x_scaled = last_fudai_x * rx // 1080 + else: + # 使用默认最优位置:最热门区域中心(98-240的中心,约169) + optimal_x_scaled = 169 * rx // 1080 + except Exception: + # 使用默认最优位置 + optimal_x_scaled = 169 * rx // 1080 + + # 确保最优位置在搜索范围内 + optimal_x_scaled = max( + scan_x_start, min(optimal_x_scaled, scan_x_end - 1) + ) + + # 确保y坐标在有效范围内 + if 0 <= scan_y < pic_height: + # 精度优化:同时检查上下几行像素,提高检测准确性 + for offset_y in [0, -1, 1, -2, 2]: # 检查当前行及上下2行 + check_y = scan_y + offset_y + if not (0 <= check_y < pic_height): + continue + + # 优化:从最优位置开始向左右两侧搜索 + found_x = None + search_range = max( + optimal_x_scaled - scan_x_start, + scan_x_end - optimal_x_scaled, + ) + + # 先检查最优位置本身 + try: + px = pix[optimal_x_scaled, check_y] + if isinstance(px, tuple) and len(px) >= 3: + if ( + 190 <= px[0] <= 206 + and 173 <= px[1] <= 196 + and 234 <= px[2] <= 250 + ): + found_x = optimal_x_scaled + except (IndexError, TypeError): + pass + + # 如果最优位置没找到,向左右两侧交替搜索 + if found_x is None: + for offset in range(1, search_range + 1): + # 先检查右侧 + right_x = optimal_x_scaled + offset + if right_x < scan_x_end: + try: + px = pix[right_x, check_y] + if isinstance(px, tuple) and len(px) >= 3: + if ( + 190 <= px[0] <= 206 + and 173 <= px[1] <= 196 + and 234 <= px[2] <= 250 + ): + found_x = right_x + break + except (IndexError, TypeError): + continue + + # 再检查左侧 + left_x = optimal_x_scaled - offset + if left_x >= scan_x_start: + try: + px = pix[left_x, check_y] + if isinstance(px, tuple) and len(px) >= 3: + if ( + 190 <= px[0] <= 206 + and 173 <= px[1] <= 196 + and 234 <= px[2] <= 250 + ): + found_x = left_x + break + except (IndexError, TypeError): + continue + + # 如果还是没找到,回退到原来的方法(从左到右) + if found_x is None: + for x_scaled in range(scan_x_start, scan_x_end): + try: + px = pix[x_scaled, check_y] + if isinstance(px, tuple) and len(px) >= 3: + if ( + 190 <= px[0] <= 206 + and 173 <= px[1] <= 196 + and 234 <= px[2] <= 250 + ): + found_x = x_scaled + break + except (IndexError, TypeError): + continue + + if found_x is not None: + # 转换回1080基准坐标 + detected_x = int(found_x * 1080.0 / max(1, rx)) + detected = True + + # 保存找到的位置作为下次的最优位置 + try: + analyser.last_fudai_x = detected_x + except Exception: + pass + + # 双截图记录:保存成功识别存档截图 + try: + from douyin.handlers import douyin_screenshot_manager + + detect_roi = pic_new.crop( + ( + scan_x_start, + max(0, scan_y - 3), + scan_x_end, + min(pic_height, scan_y + 4), + ) + ).convert("RGB") + douyin_screenshot_manager.save_last_success( + detect_roi, + operation_type="pixel_detect", + ) + + # 保存完整截图(检测成功) + pic_rgb_full = pic_new.convert("RGB") + debug_path = douyin_screenshot_manager.save_debug_image( + pic_rgb_full, + operation_type="pixel_detect", + picname="fudai_icon", + step="detected", + coords=( + scan_x_start, + max(0, scan_y - 3), + scan_x_end, + min(pic_height, scan_y + 4), + ), + extra_info={ + "detected_x": detected_x, + "detected_x_scaled": found_x, + "scan_range": f"x:{scan_x_start}-{scan_x_end}, y:{scan_y}", + "resolution": f"{rx}x{ry}", + "result": "success", + }, + ) + if debug_path: + try: + analyser._log_kv( + "[调试图像]", + "福袋图标检测成功保存完整截图", + path=debug_path, + detected_x=detected_x, + ) + except Exception: + pass + except Exception: + pass + + try: + analyser.last_find_fudai_time = time.time() + except Exception: + pass + break # 找到后退出offset_y循环 + + if detected: + break # 找到后退出外层循环 + except Exception: + # 像素检测失败,记录但不影响主流程 + pass + + # 像素检测未命中时,使用模板匹配作为兜底 + if not detected or detected_x is None: + try: + import cv2 + import numpy as np + + # 获取模板路径 + try: + from douyin.core import path_config + + template_path = path_config.get_assets_dir() + "/fudai.png" + except Exception: + # 回退到旧路径(向后兼容) + base_dir = os.path.dirname(__file__) + template_path = os.path.join(base_dir, "assets", "fudai.png") + + if os.path.exists(template_path): + # 获取匹配阈值 + 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 not None: + # 将PIL图片转换为OpenCV格式 + pic_rgb = pic_new.convert("RGB") + screenshot_bgr = cv2.cvtColor( + np.array(pic_rgb), cv2.COLOR_RGB2BGR + ) + + # 定义搜索区域: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 x1 < x2 and y_top < y_bottom: + search_roi = screenshot_bgr[y_top:y_bottom, x1:x2] # type: ignore[index] + + if search_roi.size > 0: + th, tw = template.shape[:2] + rh, rw = search_roi.shape[:2] + + # 保存搜索区域截图到 last_match.png + try: + base_dir = os.path.dirname(__file__) + pic_base = os.path.join(base_dir, "pic") + match_dir = os.path.join(pic_base, "template_match") + os.makedirs(match_dir, exist_ok=True) + last_match_path = os.path.join( + match_dir, "last_match.png" + ) + + # 保存截图并检查是否成功 + 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 + + # 如果模板太大,缩小模板 + 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 th <= rh and tw <= rw: + 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: + # 双截图记录:保存成功识别存档截图 + 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 + # 计算检测到的X坐标(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_find_fudai_time = time.time() + except Exception: + pass + else: + # 模板尺寸不匹配,记录调试信息 + 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 + else: + # 搜索区域为空,记录调试信息 + try: + analyser._log_kv( + "[找福袋图标] 模板匹配失败-搜索区域为空", + loop=loop, + 搜索区域=f"({x1},{y_top})-({x2},{y_bottom})", + 截图尺寸=f"{width}x{height}", + ) + except Exception: + pass + else: + # 搜索区域坐标无效,记录调试信息 + 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 + except (ImportError, Exception) as e: + # OpenCV不可用或模板匹配失败,继续使用像素检测结果 + try: + analyser._log_kv( + "[找福袋图标] 模板匹配异常", + loop=loop, + 错误类型=type(e).__name__, + 错误信息=str(e), + ) + except Exception: + pass + + if detected and detected_x is not None: + # 重置连续失败计数 + consecutive_failures = 0 + + # 统计:每次"进入直播间"后首次找到福袋图标耗时 + try: + enter_ts = float(getattr(analyser, "_enter_live_ts", 0.0) or 0.0) + except Exception: + enter_ts = 0.0 + try: + already = bool(getattr(analyser, "_enter_live_fudai_logged", False)) + except Exception: + already = False + if (enter_ts > 0) and (not already): + try: + dt = float(time.time() - enter_ts) + except Exception: + dt = -1.0 + if dt >= 0: + try: + reason = str(getattr(analyser, "_enter_live_reason", "") or "") + except Exception: + reason = "" + try: + analyser._enter_live_fudai_logged = True + except Exception: + pass + try: + analyser._log_kv( + "性能", + "进入直播间后找到福袋耗时", + seconds=round(dt, 2), + enter_reason=reason, + fudai_x=int(detected_x or 0), + ) + except Exception: + pass + + # 命中时记录调试信息 + try: + elapsed_ms = int((time.time() - func_start) * 1000) + scan_y_for_log = 403 * ry // 2400 + y_pianyi_val + analyser._log_kv( + "[找福袋图标] 命中", + loop=loop, + elapsed_ms=elapsed_ms, + scan_y=scan_y_for_log, + x=detected_x, + ) + except Exception: + pass + return int(detected_x or 0) + + loop += 1 + consecutive_failures += 1 + + # 如果连续失败太多次,提前退出(避免无效循环) + if consecutive_failures >= max_consecutive_failures: + try: + elapsed_ms = int((time.time() - func_start) * 1000) + analyser._log_kv( + "[找福袋图标] 警告", + "连续检测失败次数过多,提前退出", + consecutive_failures=consecutive_failures, + loop=loop, + elapsed_ms=elapsed_ms, + scan_y=scan_y, + ) + except Exception: + pass + break + + # 每轮记录一次轻量调试信息(帮助诊断为何未命中) + # 只在每3次循环记录一次,减少日志量 + if loop % 3 == 0: + try: + elapsed_ms = int((time.time() - func_start) * 1000) + sample_color = None + try: + mid_x = (scan_x_start + scan_x_end) // 2 + if 0 <= scan_y < height and 0 <= mid_x < width: + pix_sample = pic_new.load() + sample_color = pix_sample[mid_x, scan_y] + except Exception: + sample_color = None + analyser._log_kv( + "[找福袋图标] 循环中", + loop=loop, + elapsed_ms=elapsed_ms, + scan_y=scan_y, + scan_x_start=scan_x_start, + scan_x_end=scan_x_end, + sample_color=( + str(sample_color) if sample_color is not None else "None" + ), + consecutive_failures=consecutive_failures, + ) + except Exception: + pass + + # 检查是否还有剩余时间 + elapsed = time.time() - func_start + if elapsed < max_wait_s: + remaining_time = max_wait_s - elapsed + sleep_time = min(retry_delay_s, remaining_time) + if sleep_time > 0: + try: + analyser.operation.delay(sleep_time) + except Exception: + time.sleep(sleep_time) + else: + # 时间已到,退出循环 + break + + # 像素检测未找到时,返回 False,并记录总耗时与循环次数 + try: + total_elapsed_ms = int((time.time() - func_start) * 1000) + analyser._log_kv( + "[找福袋图标] 未找到福袋图标", + elapsed_ms=total_elapsed_ms, + total_loops=loop, + max_wait_s=max_wait_s, + retry_delay_s=retry_delay_s, + ) + except Exception: + pass + return False + + try: + retry_delay_s = float( + getattr(analyser, "fudai_icon_retry_delay_seconds", 0.3) or 0.3 + ) + except Exception: + retry_delay_s = 0.3 + try: + max_wait_s = float(getattr(analyser, "fudai_icon_max_wait_seconds", 2.5) or 2.5) + except Exception: + max_wait_s = 2.5 + if retry_delay_s < 0.15: + retry_delay_s = 0.15 + if max_wait_s < 0: + max_wait_s = 0.0 + + try: + analyser._log_kv( + "[找福袋图标] 开始", + f"最大等待时间{max_wait_s}秒,复用截图={screenshot_ready},使用模板匹配", + ) + except Exception: + pass + + detected_any = False + detected_x = None + + # 检查OpenCV是否可用 + try: + import cv2 + import numpy as np + except Exception: + try: + analyser._log_kv( + "[找福袋图标] 错误", "OpenCV/numpy不可用,无法使用模板匹配" + ) + except Exception: + pass + return False + + # 获取模板路径 + try: + from douyin.core import path_config + + template_path = path_config.get_assets_dir() + "/fudai.png" + except Exception: + # 回退到旧路径(向后兼容) + base_dir = os.path.dirname(__file__) + template_path = os.path.join(base_dir, "assets", "fudai.png") + if not os.path.exists(template_path): + try: + analyser._log_kv("[找福袋图标] 错误", f"模板文件不存在: {template_path}") + except Exception: + pass + return False + + # 获取匹配阈值 + try: + template_threshold = float( + getattr(analyser, "fudai_template_match_threshold", 0.7) or 0.7 + ) + except Exception: + template_threshold = 0.7 + + # 完全基于时间限制,不使用次数限制 + while (time.time() - func_start) < max_wait_s: + attempt_start = time.time() + attempt_count += 1 + # Reuse external screenshot only on first attempt if provided + if not (screenshot_ready and attempt_count == 1): + try: + analyser._shot_and_diagnose( + "找福袋图标", "判定是否存在福袋", analyser.device_id, diagnose=False + ) + except Exception: + pass + + try: + screenshot_img = cv2.imread(pic1_path, cv2.IMREAD_COLOR) + if screenshot_img is None: + try: + analyser.operation.get_screenshot( + analyser.device_id, use_service=False + ) + screenshot_img = cv2.imread(pic1_path, cv2.IMREAD_COLOR) + except Exception: + pass + if screenshot_img is None: + elapsed = time.time() - func_start + if elapsed < max_wait_s: + remaining_time = max_wait_s - elapsed + sleep_time = min(retry_delay_s, remaining_time) + if sleep_time > 0: + try: + analyser.operation.delay(sleep_time) + except Exception: + time.sleep(sleep_time) + continue + except Exception: + elapsed = time.time() - func_start + if elapsed < max_wait_s: + remaining_time = max_wait_s - elapsed + sleep_time = min(retry_delay_s, remaining_time) + if sleep_time > 0: + try: + analyser.operation.delay(sleep_time) + except Exception: + time.sleep(sleep_time) + continue + + # Resolution ratios + try: + rx = int(getattr(analyser, "resolution_ratio_x", 1080) or 1080) + except Exception: + rx = 1080 + try: + ry = int(getattr(analyser, "resolution_ratio_y", 2400) or 2400) + except Exception: + ry = 2400 + try: + y_pianyi_val = int(getattr(analyser, "y_pianyi", 0) or 0) + except Exception: + y_pianyi_val = 0 + + # 定义搜索区域(仅在更小的范围内寻找福袋图标) + # X范围:157-269(1080基准) # 左上角 X 改为图片中的坐标 x=157(右下不变) + # Y范围:354-451(2400基准) # 左上角 Y 改为图片中的坐标 y=354(右下不变) + search_x1 = max(0, 157 * rx // 1080) + search_x2 = min(screenshot_img.shape[1], 269 * rx // 1080) + search_y1_base = 354 * ry // 2400 + y_pianyi_val + search_y2_base = 451 * ry // 2400 + y_pianyi_val + search_y1 = max(0, search_y1_base) + search_y2 = min(screenshot_img.shape[0], search_y2_base) + + if search_x1 >= search_x2 or search_y1 >= search_y2: + elapsed = time.time() - func_start + if elapsed < max_wait_s: + remaining_time = max_wait_s - elapsed + sleep_time = min(retry_delay_s, remaining_time) + if sleep_time > 0: + try: + analyser.operation.delay(sleep_time) + except Exception: + time.sleep(sleep_time) + continue + + # 裁剪搜索区域 + search_roi = screenshot_img[search_y1:search_y2, search_x1:search_x2] + + # 读取模板 + try: + template = cv2.imread(template_path, cv2.IMREAD_COLOR) + if template is None: + try: + analyser._log_kv( + "[找福袋图标] 错误", f"无法读取模板文件: {template_path}" + ) + except Exception: + pass + elapsed = time.time() - func_start + if elapsed < max_wait_s: + remaining_time = max_wait_s - elapsed + sleep_time = min(retry_delay_s, remaining_time) + if sleep_time > 0: + try: + analyser.operation.delay(sleep_time) + except Exception: + time.sleep(sleep_time) + continue + except Exception as e: + try: + analyser._log_kv("[找福袋图标] 错误", f"读取模板文件异常: {str(e)}") + except Exception: + pass + elapsed = time.time() - func_start + if elapsed < max_wait_s: + remaining_time = max_wait_s - elapsed + sleep_time = min(retry_delay_s, remaining_time) + if sleep_time > 0: + try: + analyser.operation.delay(sleep_time) + except Exception: + time.sleep(sleep_time) + continue + + # 检查模板和搜索区域尺寸 + th, tw = template.shape[:2] + rh, rw = search_roi.shape[:2] + + 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] + + # 执行模板匹配(使用彩色匹配) + try: + template_match_start = time.time() + + # 详细日志:记录搜索区域和模板信息 + try: + analyser._log_kv( + "[找福袋图标] 模板匹配开始", + f"搜索区域", + search_x1=search_x1, + search_x2=search_x2, + search_y1=search_y1, + search_y2=search_y2, + search_width=search_x2 - search_x1, + search_height=search_y2 - search_y1, + template_width=tw, + template_height=th, + template_path=template_path, + 阈值=template_threshold, + 匹配模式="彩色(BGR)", + ) + except Exception: + pass + + # 使用彩色匹配(BGR三通道) + 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" + ) + + # 保存调试图像(匹配前)- 使用完整截图 + screenshot_pil = PILImage.fromarray( + cv2.cvtColor(screenshot_img, cv2.COLOR_BGR2RGB) + ) + debug_path = douyin_screenshot_manager.save_debug_image( + screenshot_pil, + operation_type="template_match", + picname="fudai_icon", + step="before_match", + coords=(search_x1, search_y1, search_x2, search_y2), + extra_info={ + "template": ( + os.path.basename(template_path) if template_path else "" + ), + "template_size": f"{tw}x{th}", + "search_size": f"{search_x2-search_x1}x{search_y2-search_y1}", + "threshold": template_threshold, + }, + ) + if debug_path: + try: + analyser._log_kv( + "[调试图像]", + "模板匹配前保存", + path=debug_path, + coords=f"({search_x1},{search_y1})-({search_x2},{search_y2})", + ) + except Exception: + pass + except Exception: + pass + + template_match_elapsed = (time.time() - template_match_start) * 1000 + + # 详细日志:记录匹配结果 + try: + analyser._log_kv( + "[找福袋图标] 模板匹配结果", + f"匹配分数={round(max_val, 4)},阈值={template_threshold}", + 耗时毫秒=round(template_match_elapsed, 2), + 是否通过=bool(max_val >= template_threshold), + 最小分数=round(_min_val, 4), + 最佳位置=f"({max_loc[0]}, {max_loc[1]})", + ) + except Exception: + pass + + if max_val >= template_threshold: + # 双截图记录:保存成功识别存档截图 + 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" + ) + + # 保存带匹配结果的调试图像(带匹配框)- 使用完整截图 + screenshot_pil = PILImage.fromarray( + cv2.cvtColor(screenshot_img, cv2.COLOR_BGR2RGB) + ) + # 计算匹配位置在完整截图中的坐标 + match_x_in_roi = max_loc[0] + match_y_in_roi = max_loc[1] + match_x1 = search_x1 + match_x_in_roi + match_y1 = search_y1 + match_y_in_roi + match_x2 = search_x1 + match_x_in_roi + tw + match_y2 = search_y1 + match_y_in_roi + th + match_coords = (match_x1, match_y1, match_x2, match_y2) + + debug_path = douyin_screenshot_manager.save_debug_image( + screenshot_pil, + operation_type="template_match", + picname="fudai_icon", + step="matched", + coords=( + search_x1, + search_y1, + search_x2, + search_y2, + ), # 搜索区域坐标 + extra_info={ + "match_score": round(max_val, 4), + "match_location": f"({max_loc[0]},{max_loc[1]})", + "match_coords": match_coords, # 匹配位置坐标 + "template_size": f"{tw}x{th}", + "threshold": template_threshold, + "passed": True, + }, + ) + if debug_path: + try: + analyser._log_kv( + "[调试图像]", + "模板匹配成功保存", + path=debug_path, + score=round(max_val, 4), + location=f"({max_loc[0]},{max_loc[1]})", + ) + except Exception: + pass + except Exception: + pass + + # 计算检测到的X坐标(1080基准) + detected_x_in_roi = max_loc[0] + detected_x = int( + (search_x1 + detected_x_in_roi + tw // 2) * 1080.0 / max(1, rx) + ) + detected_y_in_roi = max_loc[1] + detected_y = int( + (search_y1 + detected_y_in_roi + th // 2) * 2400.0 / max(1, ry) + ) + detected_any = True + + # 保存匹配位置的图片 + try: + # 计算匹配区域在原图中的坐标(扩大一些范围以便查看上下文) + margin = 20 # 扩大20像素的边距 + match_x1 = max(0, search_x1 + detected_x_in_roi - margin) + match_y1 = max(0, search_y1 + detected_y_in_roi - margin) + match_x2 = min( + screenshot_img.shape[1], + search_x1 + detected_x_in_roi + tw + margin, + ) + match_y2 = min( + screenshot_img.shape[0], + search_y1 + detected_y_in_roi + th + margin, + ) + + # 裁剪匹配区域 + match_roi = screenshot_img[match_y1:match_y2, match_x1:match_x2] + + # 保存到 pic/template_match/ 目录 + base_dir = os.path.dirname(__file__) + pic_base = os.path.join(base_dir, "pic") + match_dir = os.path.join(pic_base, "template_match") + os.makedirs(match_dir, exist_ok=True) + + # 文件名包含时间戳和匹配分数 + timestamp = int(time.time() * 1000) + score_str = f"{max_val:.4f}".replace(".", "_") + match_filename = f"fudai_match_{timestamp}_score_{score_str}.png" + match_path = os.path.join(match_dir, match_filename) + + # 检查裁剪区域是否有效 + if match_roi.size == 0: + analyser._log_kv( + "[找福袋图标] 警告", + "匹配区域裁剪后为空,跳过保存", + 区域坐标=f"({match_x1}, {match_y1}) - ({match_x2}, {match_y2})", + ) + else: + # 保存图片 + save_success = cv2.imwrite(match_path, match_roi) + + # 验证文件是否真的保存成功 + file_exists = os.path.exists(match_path) + file_size = os.path.getsize(match_path) if file_exists else 0 + + analyser._log_kv( + "[找福袋图标] 已保存匹配位置图片", + f"路径: {match_path}", + 匹配区域=f"({match_x1}, {match_y1}) - ({match_x2}, {match_y2})", + 区域大小=f"{match_x2 - match_x1}x{match_y2 - match_y1}", + ROI尺寸=f"{match_roi.shape[1]}x{match_roi.shape[0]}", + cv2保存返回值=save_success, + 文件存在=file_exists, + 文件大小字节=file_size, + ) + + if not file_exists or file_size == 0: + analyser._log_kv( + "[找福袋图标] 警告", + "保存的图片文件不存在或大小为0", + 路径=match_path, + ) + else: + # 保存成功,输出绝对路径方便查看 + abs_path = os.path.abspath(match_path) + analyser._log_kv( + "[找福袋图标] 图片保存成功", + f"绝对路径: {abs_path}", + ) + except Exception as e: + try: + analyser._log_kv( + "[找福袋图标] 保存匹配位置图片失败", + f"异常: {str(e)}", + ) + except Exception: + pass + + try: + analyser.last_find_fudai_time = time.time() + analyser._log_kv( + "[找福袋图标] 模板匹配命中", + f"检测到福袋图标", + x=detected_x, + y=detected_y, + 匹配分数=round(max_val, 4), + x_in_roi=detected_x_in_roi, + y_in_roi=detected_y_in_roi, + 模板中心偏移=f"({tw//2}, {th//2})", + 实际屏幕坐标=f"({detected_x}, {detected_y})", + ) + except Exception: + pass + break + else: + # 未命中时也记录日志 + try: + analyser._log_kv( + "[找福袋图标] 模板匹配未命中", + f"匹配分数={round(max_val, 4)} < 阈值={template_threshold}", + 差值=round(template_threshold - max_val, 4), + ) + except Exception: + pass + except Exception as e: + try: + analyser._log_kv( + "[找福袋图标] 模板匹配异常", + f"异常: {str(e)}", + 异常类型=type(e).__name__, + ) + except Exception: + pass + + # 检查是否还有剩余时间 + elapsed = time.time() - func_start + if elapsed < max_wait_s: + remaining_time = max_wait_s - elapsed + sleep_time = min(retry_delay_s, remaining_time) + if sleep_time > 0: + try: + analyser.operation.delay(sleep_time) + except Exception: + time.sleep(sleep_time) + + try: + total_elapsed = time.time() - func_start + analyser._log_kv( + "[找福袋图标] 结束", + "检测完成", + detected=bool(detected_any and detected_x is not None), + elapsed_ms=int(total_elapsed * 1000), + ) + except Exception: + pass + + # 与原始 fudai.py 的 check_have_fudai 行为保持一致: + # 返回福袋图标的 x 坐标(1080 基准坐标),未找到返回 False。 + return int(detected_x) if detected_x is not None else False + + +# ========== 以下为旧的像素检测代码,已暂时禁用 ========== +# 像素检测代码已删除,使用模板匹配替代 +# 如需恢复像素检测,请参考 git 历史记录 + + diff --git a/douyin/detection/douyin_detection.py b/douyin/detection/douyin_detection.py index 1bc523f..a613a64 100644 --- a/douyin/detection/douyin_detection.py +++ b/douyin/detection/douyin_detection.py @@ -1,1756 +1,22 @@ -from __future__ import annotations +"""douyin_detection - 兼容入口(保留外部 import 路径)。 -""" -Functional fudai detection (WebGUI version) -=========================================== - -This module reimplements the key detection helpers used in `douyin_fudai`: - -- check_detail_height(analyser) -> int -- check_have_fudai_with_ready_screenshot(analyser, screenshot_ready=False) -> bool -- check_in_fudai_detail(analyser) -> bool - -The logic is adapted from the original `axf/douyin_detection.py`, with extra -defensive checks to avoid crashes when certain analyser attributes are missing. +实际实现位于子包 ``douyin.detection.detection_pkg``。 +本文件仅 re-export 公共符号;详见各子模块文档: +- ``detection_pkg._common`` _ensure_detection_cache +- ``detection_pkg.detail`` check_detail_height / check_in_fudai_detail +- ``detection_pkg.finder`` check_have_fudai_with_ready_screenshot(最大头) """ -# pylint: disable=no-member - -from typing import Any -import os -import time - -from PIL import Image - - -def _ensure_detection_cache(analyser: Any) -> None: - """Ensure analyser has a simple detection cache structure.""" - if not hasattr(analyser, "_detection_cache"): - analyser._detection_cache = {} - if not hasattr(analyser, "_detection_cache_ttl"): - analyser._detection_cache_ttl = 2.0 - - -def check_detail_height(analyser: Any) -> int: - """Detect how many tasks are required in the fudai detail popup. - - Returns: - 0 / 1 / 2 / 3 (number of tasks). - """ - # 使用统一的截图路径配置 - try: - from douyin.core import path_config - - pic1_path = path_config.get_screenshot_path() - except Exception: - # 回退到旧路径(向后兼容) - from douyin.core import path_config - - pic1_path = path_config.get_screenshot_path() - - # 记录截图文件的时间戳(如果存在) - screenshot_timestamp = None - try: - if os.path.exists(pic1_path): - screenshot_timestamp = os.path.getmtime(pic1_path) - except Exception: - pass - - pic = None - for attempt in range(3): - try: - pic = Image.open(pic1_path) - pic.load() # force load to catch truncated files early - break - except Exception: - if attempt < 2: - try: - analyser.operation.get_screenshot( - analyser.device_id, use_service=False - ) - analyser.operation.delay(0.2) - # 重新获取截图后,更新时间戳 - try: - if os.path.exists(pic1_path): - screenshot_timestamp = os.path.getmtime(pic1_path) - except Exception: - pass - except Exception: - pass - pic = None - if pic is None: - try: - analyser._log_kv( - "检测", - "读取截图失败,默认不需要任务", - level="WARN", - screenshot_timestamp=screenshot_timestamp, - ) - except Exception: - pass - return 0 - - pic_new = pic.convert("RGBA") - pix = pic_new.load() - width, height = pic_new.size - - # X of “参与” button - try: - rx = int(getattr(analyser, "resolution_ratio_x", 1080) or 1080) - except Exception: - rx = 1080 - try: - ry = int(getattr(analyser, "resolution_ratio_y", 2400) or 2400) - except Exception: - ry = 2400 - - x = 536 * rx // 1080 - if not (0 <= x < width): - return 0 - - # (base_y, y_pianyi_offset, task_count) - try: - y_pianyi_val = int(getattr(analyser, "y_pianyi", 0) or 0) - except Exception: - y_pianyi_val = 0 - - check_points = [ - (983, 0, 2), - (983, y_pianyi_val, 2), - (983, -y_pianyi_val, 2), - (883, 0, 3), - (883, y_pianyi_val, 3), - (883, -y_pianyi_val, 3), - (1058, 0, 1), - (1058, y_pianyi_val, 1), - (1058, -y_pianyi_val, 1), - ] - - def _check_pixel_color(y_coord: int) -> bool: - if not (0 <= y_coord < height): - return False - try: - p = pix[x, y_coord] - if not (isinstance(p, tuple) and len(p) >= 3): - return False - r, g, b = int(p[0]), int(p[1]), int(p[2]) - return 28 <= r <= 40 and 32 <= g <= 42 and 76 <= b <= 86 - except Exception: - return False - - detected_task_count = None - detected_y = None - try: - for base_y, dy, task_count in check_points: - y = base_y * ry // 2400 + dy - if _check_pixel_color(y): - detected_task_count = task_count - detected_y = y - try: - import time - - current_time = time.time() - analyser._log_kv( - "检测", - f"识别到福袋任务数量:{task_count}个", - task_count=task_count, - detected_y=detected_y, - screenshot_timestamp=screenshot_timestamp, - current_time=current_time, - time_diff=( - round(current_time - screenshot_timestamp, 3) - if screenshot_timestamp - else None - ), - ) - except Exception: - pass - break - except Exception: - pass - - # 保存调试图片:标注检测区域和检测点 - try: - from douyin.handlers import douyin_screenshot_manager - - # 计算检测区域(参与按钮附近的区域) - check_region_x1 = max(0, x - 50) - check_region_x2 = min(width, x + 50) - check_region_y1 = max(0, (883 * ry // 2400) - 100) # 3个任务的Y坐标附近 - check_region_y2 = min(height, (1058 * ry // 2400) + 100) # 1个任务的Y坐标附近 - - # 标注检测点 - coords_list = [] - for base_y, dy, task_count_val in check_points: - check_y = base_y * ry // 2400 + dy - if 0 <= check_y < height: - # 标注检测点(小矩形区域) - coords_list.append( - ( - max(0, x - 10), - max(0, check_y - 5), - min(width, x + 10), - min(height, check_y + 5), - ) - ) - - # 保存调试图片 - pic_rgb = pic.convert("RGB") - debug_path = douyin_screenshot_manager.save_debug_image( - pic_rgb, - operation_type="detection", - picname="福袋任务数量", - step="task_count_detection", - coords=(check_region_x1, check_region_y1, check_region_x2, check_region_y2), - extra_info={ - "task_count": ( - detected_task_count if detected_task_count is not None else 0 - ), - "detected_y": detected_y, - "check_x": x, - "check_points_count": len(check_points), - "region": f"({check_region_x1},{check_region_y1})-({check_region_x2},{check_region_y2})", - }, - ) - if debug_path: - try: - analyser._log_kv( - "[调试图像]", - "福袋任务数量检测", - path=debug_path, - task_count=( - detected_task_count if detected_task_count is not None else 0 - ), - ) - except Exception: - pass - except Exception: - pass - - if detected_task_count is not None: - return detected_task_count - - # Optional robot-check path from original implementation - try: - if ( - hasattr(analyser, "check_have_robot_analyse") - and analyser.check_have_robot_analyse() - ): - analyser.deal_robot_analyse() - except Exception: - pass - try: - import time - - current_time = time.time() - analyser._log_kv( - "检测", - "识别福袋任务数量:0个(不需要任务)", - level="INFO", - task_count=0, - screenshot_timestamp=screenshot_timestamp, - current_time=current_time, - time_diff=( - current_time - screenshot_timestamp if screenshot_timestamp else None - ), - ) - except Exception: - pass - return 0 - - -def check_have_fudai_with_ready_screenshot( - analyser: Any, screenshot_ready: bool = False -): - """判定直播页面福袋的小图标是否存在(优化版,优先使用模块化查找器)。 - - 如果启用了优化查找器(fudai_finder_optimized_enabled=True),则使用新的模块化查找器。 - 否则使用原版像素检测实现。 - """ - # 检查是否启用优化查找器 - try: - use_optimized = bool(getattr(analyser, "fudai_finder_optimized_enabled", False)) - except Exception: - use_optimized = False - - if use_optimized: - try: - from douyin.fudai.douyin_fudai_finder import ( - check_have_fudai_with_ready_screenshot_optimized, - ) - - return check_have_fudai_with_ready_screenshot_optimized( - analyser, screenshot_ready - ) - except ImportError: - # 优化版不可用,回退到原版 - try: - analyser._log_kv( - "[找福袋图标]", "优化查找器不可用,使用原版", error="ImportError" - ) - except Exception: - pass - except Exception as e: - # 优化版异常,回退到原版 - try: - analyser._log_kv( - "[找福袋图标]", - "优化查找器异常,使用原版", - error=str(e), - error_type=type(e).__name__, - ) - except Exception: - pass - - # 原版实现(像素检测版) - func_start = time.time() - # 使用统一的截图路径配置 - try: - from douyin.core import path_config - - pic1_path = path_config.get_screenshot_path() - except Exception: - # 回退到旧路径(向后兼容) - from douyin.core import path_config - - pic1_path = path_config.get_screenshot_path() - attempt_count = 0 # 仅用于日志记录,不影响循环控制 - - # ===== 像素检测版(优先使用),不依赖 OpenCV ===== - # 改为按时间控制循环,而不是按次数限制 - try: - retry_delay_s = float( - getattr(analyser, "fudai_icon_retry_delay_seconds", 0.6) or 0.6 - ) - except Exception: - retry_delay_s = 0.6 - try: - max_wait_s = float(getattr(analyser, "fudai_icon_max_wait_seconds", 5.0) or 5.0) - except Exception: - max_wait_s = 5.0 - if retry_delay_s < 0.1: - retry_delay_s = 0.1 # 最小间隔改为0.1秒 - if max_wait_s < 0: - max_wait_s = 0.0 - # 防止无限等待:最大等待时间限制为30秒 - if max_wait_s > 30.0: - max_wait_s = 30.0 - - loop = 0 - max_loops = int(max_wait_s / retry_delay_s) + 10 # 额外的循环次数保护 - consecutive_failures = 0 # 连续失败计数 - max_consecutive_failures = 5 # 最大连续失败次数 - - # 额外的时间限制保护,防止死循环 - absolute_timeout = func_start + max( - max_wait_s * 1.5, 15 - ) # 最长15秒,但不超过 max_wait_s * 1.5 - - while (time.time() - func_start) < max_wait_s and time.time() < absolute_timeout: - # 额外的循环次数保护:防止无限循环 - if loop >= max_loops: - try: - analyser._log_kv( - "[找福袋图标] 警告", - "达到最大循环次数限制,提前退出", - max_loops=max_loops, - elapsed_s=round(time.time() - func_start, 2), - ) - except Exception: - pass - break - - # 每次循环都重新截图(除了第一次如果已经提供了截图) - if not (screenshot_ready and loop == 0): - try: - analyser._shot_and_diagnose( - "找福袋图标", "判定是否存在福袋", analyser.device_id, diagnose=False - ) - except Exception: - # 如果截图失败,尝试直接调用 get_screenshot 并验证文件可读性(避免半写入/截断图片导致后续 PIL 打开失败) - try: - analyser.operation.get_screenshot( - analyser.device_id, use_service=False - ) - # 简单验证:文件存在、大小足够且能由 PIL 打开/加载 - valid = False - try: - if ( - os.path.exists(pic1_path) - and os.path.getsize(pic1_path) >= 1024 - ): - from PIL import Image as PILImage - - with PILImage.open(pic1_path) as _im: - _im.verify() - valid = True - except Exception: - valid = False - if not valid: - # 再尝试一次直接截图作为最后回退 - try: - analyser.operation.get_screenshot( - analyser.device_id, use_service=False - ) - except Exception: - pass - except Exception: - pass - - # 读取截图 - pic = None - try: - pic = Image.open(pic1_path) - pic.load() - except Exception: - # 如果读取失败,尝试重新截图 - try: - analyser.operation.get_screenshot(analyser.device_id, use_service=False) - pic = Image.open(pic1_path) - pic.load() - except Exception: - pass - - # 如果截图读取失败,等待后继续下一次循环 - if pic is None: - loop += 1 - consecutive_failures += 1 - # 如果连续失败太多次,提前退出 - if consecutive_failures >= max_consecutive_failures: - try: - analyser._log_kv( - "[找福袋图标] 警告", - "连续截图失败次数过多,提前退出", - consecutive_failures=consecutive_failures, - elapsed_s=round(time.time() - func_start, 2), - ) - except Exception: - pass - break - # 检查是否还有剩余时间 - elapsed = time.time() - func_start - if elapsed < max_wait_s: - remaining_time = max_wait_s - elapsed - sleep_time = min(retry_delay_s, remaining_time) - if sleep_time > 0: - try: - analyser.operation.delay(sleep_time) - except Exception: - time.sleep(sleep_time) - continue - - pic_new = pic.convert("RGBA") - width, height = pic_new.size - try: - rx = int(getattr(analyser, "resolution_ratio_x", 1080) or 1080) - except Exception: - rx = 1080 - try: - ry = int(getattr(analyser, "resolution_ratio_y", 2400) or 2400) - except Exception: - ry = 2400 - try: - y_pianyi_val = int(getattr(analyser, "y_pianyi", 0) or 0) - except Exception: - y_pianyi_val = 0 - - detected = False - detected_x = None - # 初始化扫描范围变量(用于日志记录) - scan_x_start = 41 * rx // 1080 - scan_x_end = min(410 * rx // 1080, width) - scan_y = 403 * ry // 2400 + y_pianyi_val - - # 获取最优搜索起始位置(使用历史记录或默认值) - try: - # 尝试获取上次找到的福袋位置(1080基准) - last_fudai_x = int(getattr(analyser, "last_fudai_x", 0) or 0) - if last_fudai_x > 0 and scan_x_start <= last_fudai_x <= scan_x_end: - optimal_x_scaled = last_fudai_x * rx // 1080 - else: - # 使用默认最优位置:最热门区域中心(98-240的中心,约169) - optimal_x_scaled = 169 * rx // 1080 - except Exception: - # 使用默认最优位置 - optimal_x_scaled = 169 * rx // 1080 - - # 确保最优位置在搜索范围内 - optimal_x_scaled = max(scan_x_start, min(optimal_x_scaled, scan_x_end - 1)) - - # 保存完整截图用于调试(检测前) - try: - from douyin.handlers import douyin_screenshot_manager - - pic_rgb_full = pic_new.convert("RGB") - - # 保存完整截图 - debug_path = douyin_screenshot_manager.save_debug_image( - pic_rgb_full, - operation_type="pixel_detect", - picname="fudai_icon", - step="before_detect", - coords=( - (scan_x_start, scan_y - 3, scan_x_end, scan_y + 4) - if scan_y >= 3 - else None - ), - extra_info={ - "scan_range": f"x:{scan_x_start}-{scan_x_end}, y:{scan_y}", - "resolution": f"{rx}x{ry}", - "y_offset": y_pianyi_val, - "loop": loop, - }, - ) - if debug_path: - try: - analyser._log_kv( - "[调试图像]", "福袋图标检测前保存完整截图", path=debug_path - ) - except Exception: - pass - except Exception: - pass - - # 先尝试 numpy 加速扫描水平带(性能优化:批量处理) - try: - import numpy as _np - - # 计算检测区域,考虑分辨率缩放和偏移 - # 将像素检测的扫描带左上角改为图片坐标 x=157, y=354(右下不变) - y0 = 354 * ry // 2400 + y_pianyi_val - x1 = max(0, 157 * rx // 1080) - x2 = min(width, 410 * rx // 1080) - y_top = max(0, y0 - 3) - y_bottom = min(height, y0 + 4) - - # 边界检查 - if x1 < x2 and y_top < y_bottom: - roi = pic_new.crop((x1, y_top, x2, y_bottom)).convert("RGB") - - # 双截图记录:保存实时操作截图(像素检测区域) - try: - from douyin.handlers import douyin_screenshot_manager - - douyin_screenshot_manager.save_latest_attempt( - roi, operation_type="pixel_detect" - ) - except Exception: - pass - - arr = _np.array(roi) - if arr.size > 0: - r = arr[:, :, 0] # type: ignore[index] - g = arr[:, :, 1] # type: ignore[index] - b = arr[:, :, 2] # type: ignore[index] - # 精度优化:扩大颜色范围,提高容错性(考虑不同屏幕色温和亮度) - # 原来的范围:R[193-203], G[176-193], B[237-247] - # 优化后:适度放宽范围,但保持中心值权重 - mask_center = (r >= 190) & (r <= 206) & (g >= 173) & (g <= 196) & (b >= 234) & (b <= 250) # type: ignore[operator] - # 速度优化:使用更宽松的预筛选,减少计算量 - mask = mask_center - # 检查每一列是否有足够多的匹配像素(提高准确性,避免单个噪点误判) - cols = _np.sum(mask, axis=0) - # 至少需要2个像素匹配(避免单个噪点误判) - valid_cols = cols >= 2 - if valid_cols.any(): - # 优化:从最优位置开始向左右两侧搜索 - roi_width = x2 - x1 - optimal_col = optimal_x_scaled - x1 # 转换为ROI内的列索引 - optimal_col = max(0, min(optimal_col, roi_width - 1)) - - # 从最优位置开始,向左右两侧交替搜索 - found_col = None - search_range = max(optimal_col, roi_width - optimal_col) - - # 先检查最优位置本身 - if valid_cols[optimal_col]: - found_col = optimal_col - else: - # 向左右两侧交替搜索 - for offset in range(1, search_range + 1): - # 先检查右侧 - right_col = optimal_col + offset - if right_col < roi_width and valid_cols[right_col]: - found_col = right_col - break - # 再检查左侧 - left_col = optimal_col - offset - if left_col >= 0 and valid_cols[left_col]: - found_col = left_col - break - - # 如果没找到,回退到原来的方法(从左到右) - if found_col is None: - found_col = int(_np.argmax(valid_cols)) - - col_idx = found_col - # 计算检测到的x坐标:转换为1080基准坐标,确保精度 - detected_x = int((x1 + col_idx) * 1080.0 / max(1, rx)) - detected = True - - # 保存找到的位置作为下次的最优位置 - try: - analyser.last_fudai_x = detected_x - except Exception: - pass - - # 双截图记录:保存成功识别存档截图 - try: - from douyin.handlers import douyin_screenshot_manager - - douyin_screenshot_manager.save_last_success( - roi, operation_type="pixel_detect" - ) - - # 保存完整截图(检测成功) - pic_rgb_full = pic_new.convert("RGB") - debug_path = douyin_screenshot_manager.save_debug_image( - pic_rgb_full, - operation_type="pixel_detect", - picname="fudai_icon", - step="detected", - coords=(x1, y_top, x2, y_bottom), - extra_info={ - "detected_x": detected_x, - "detected_x_scaled": x1 + col_idx, - "scan_range": f"x:{x1}-{x2}, y:{y_top}-{y_bottom}", - "resolution": f"{rx}x{ry}", - "result": "success", - }, - ) - if debug_path: - try: - analyser._log_kv( - "[调试图像]", - "福袋图标检测成功保存完整截图", - path=debug_path, - detected_x=detected_x, - ) - except Exception: - pass - except Exception: - pass - - try: - analyser.last_find_fudai_time = time.time() - except Exception: - pass - except (ImportError, ValueError, IndexError, AttributeError): - # numpy不可用或计算错误,回退到逐像素检测 - detected = False - - # numpy 未命中/不可用时回退到原始逐像素检查 - if not detected: - try: - pix = pic_new.load() - pic_width, pic_height = pic_new.size - # 计算实际扫描范围,防止越界 - scan_x_start = 41 * rx // 1080 - scan_x_end = min(410 * rx // 1080, pic_width) - scan_y = 403 * ry // 2400 + y_pianyi_val - - # 获取最优搜索起始位置(使用历史记录或默认值) - try: - # 尝试获取上次找到的福袋位置(1080基准) - last_fudai_x = int(getattr(analyser, "last_fudai_x", 0) or 0) - if ( - last_fudai_x > 0 - and scan_x_start <= last_fudai_x * rx // 1080 <= scan_x_end - ): - optimal_x_scaled = last_fudai_x * rx // 1080 - else: - # 使用默认最优位置:最热门区域中心(98-240的中心,约169) - optimal_x_scaled = 169 * rx // 1080 - except Exception: - # 使用默认最优位置 - optimal_x_scaled = 169 * rx // 1080 - - # 确保最优位置在搜索范围内 - optimal_x_scaled = max( - scan_x_start, min(optimal_x_scaled, scan_x_end - 1) - ) - - # 确保y坐标在有效范围内 - if 0 <= scan_y < pic_height: - # 精度优化:同时检查上下几行像素,提高检测准确性 - for offset_y in [0, -1, 1, -2, 2]: # 检查当前行及上下2行 - check_y = scan_y + offset_y - if not (0 <= check_y < pic_height): - continue - - # 优化:从最优位置开始向左右两侧搜索 - found_x = None - search_range = max( - optimal_x_scaled - scan_x_start, - scan_x_end - optimal_x_scaled, - ) - - # 先检查最优位置本身 - try: - px = pix[optimal_x_scaled, check_y] - if isinstance(px, tuple) and len(px) >= 3: - if ( - 190 <= px[0] <= 206 - and 173 <= px[1] <= 196 - and 234 <= px[2] <= 250 - ): - found_x = optimal_x_scaled - except (IndexError, TypeError): - pass - - # 如果最优位置没找到,向左右两侧交替搜索 - if found_x is None: - for offset in range(1, search_range + 1): - # 先检查右侧 - right_x = optimal_x_scaled + offset - if right_x < scan_x_end: - try: - px = pix[right_x, check_y] - if isinstance(px, tuple) and len(px) >= 3: - if ( - 190 <= px[0] <= 206 - and 173 <= px[1] <= 196 - and 234 <= px[2] <= 250 - ): - found_x = right_x - break - except (IndexError, TypeError): - continue - - # 再检查左侧 - left_x = optimal_x_scaled - offset - if left_x >= scan_x_start: - try: - px = pix[left_x, check_y] - if isinstance(px, tuple) and len(px) >= 3: - if ( - 190 <= px[0] <= 206 - and 173 <= px[1] <= 196 - and 234 <= px[2] <= 250 - ): - found_x = left_x - break - except (IndexError, TypeError): - continue - - # 如果还是没找到,回退到原来的方法(从左到右) - if found_x is None: - for x_scaled in range(scan_x_start, scan_x_end): - try: - px = pix[x_scaled, check_y] - if isinstance(px, tuple) and len(px) >= 3: - if ( - 190 <= px[0] <= 206 - and 173 <= px[1] <= 196 - and 234 <= px[2] <= 250 - ): - found_x = x_scaled - break - except (IndexError, TypeError): - continue - - if found_x is not None: - # 转换回1080基准坐标 - detected_x = int(found_x * 1080.0 / max(1, rx)) - detected = True - - # 保存找到的位置作为下次的最优位置 - try: - analyser.last_fudai_x = detected_x - except Exception: - pass - - # 双截图记录:保存成功识别存档截图 - try: - from douyin.handlers import douyin_screenshot_manager - - detect_roi = pic_new.crop( - ( - scan_x_start, - max(0, scan_y - 3), - scan_x_end, - min(pic_height, scan_y + 4), - ) - ).convert("RGB") - douyin_screenshot_manager.save_last_success( - detect_roi, - operation_type="pixel_detect", - ) - - # 保存完整截图(检测成功) - pic_rgb_full = pic_new.convert("RGB") - debug_path = douyin_screenshot_manager.save_debug_image( - pic_rgb_full, - operation_type="pixel_detect", - picname="fudai_icon", - step="detected", - coords=( - scan_x_start, - max(0, scan_y - 3), - scan_x_end, - min(pic_height, scan_y + 4), - ), - extra_info={ - "detected_x": detected_x, - "detected_x_scaled": found_x, - "scan_range": f"x:{scan_x_start}-{scan_x_end}, y:{scan_y}", - "resolution": f"{rx}x{ry}", - "result": "success", - }, - ) - if debug_path: - try: - analyser._log_kv( - "[调试图像]", - "福袋图标检测成功保存完整截图", - path=debug_path, - detected_x=detected_x, - ) - except Exception: - pass - except Exception: - pass - - try: - analyser.last_find_fudai_time = time.time() - except Exception: - pass - break # 找到后退出offset_y循环 - - if detected: - break # 找到后退出外层循环 - except Exception: - # 像素检测失败,记录但不影响主流程 - pass - - # 像素检测未命中时,使用模板匹配作为兜底 - if not detected or detected_x is None: - try: - import cv2 - import numpy as np - - # 获取模板路径 - try: - from douyin.core import path_config - - template_path = path_config.get_assets_dir() + "/fudai.png" - except Exception: - # 回退到旧路径(向后兼容) - base_dir = os.path.dirname(__file__) - template_path = os.path.join(base_dir, "assets", "fudai.png") - - if os.path.exists(template_path): - # 获取匹配阈值 - 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 not None: - # 将PIL图片转换为OpenCV格式 - pic_rgb = pic_new.convert("RGB") - screenshot_bgr = cv2.cvtColor( - np.array(pic_rgb), cv2.COLOR_RGB2BGR - ) - - # 定义搜索区域: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 x1 < x2 and y_top < y_bottom: - search_roi = screenshot_bgr[y_top:y_bottom, x1:x2] # type: ignore[index] - - if search_roi.size > 0: - th, tw = template.shape[:2] - rh, rw = search_roi.shape[:2] - - # 保存搜索区域截图到 last_match.png - try: - base_dir = os.path.dirname(__file__) - pic_base = os.path.join(base_dir, "pic") - match_dir = os.path.join(pic_base, "template_match") - os.makedirs(match_dir, exist_ok=True) - last_match_path = os.path.join( - match_dir, "last_match.png" - ) - - # 保存截图并检查是否成功 - 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 - - # 如果模板太大,缩小模板 - 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 th <= rh and tw <= rw: - 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: - # 双截图记录:保存成功识别存档截图 - 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 - # 计算检测到的X坐标(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_find_fudai_time = time.time() - except Exception: - pass - else: - # 模板尺寸不匹配,记录调试信息 - 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 - else: - # 搜索区域为空,记录调试信息 - try: - analyser._log_kv( - "[找福袋图标] 模板匹配失败-搜索区域为空", - loop=loop, - 搜索区域=f"({x1},{y_top})-({x2},{y_bottom})", - 截图尺寸=f"{width}x{height}", - ) - except Exception: - pass - else: - # 搜索区域坐标无效,记录调试信息 - 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 - except (ImportError, Exception) as e: - # OpenCV不可用或模板匹配失败,继续使用像素检测结果 - try: - analyser._log_kv( - "[找福袋图标] 模板匹配异常", - loop=loop, - 错误类型=type(e).__name__, - 错误信息=str(e), - ) - except Exception: - pass - - if detected and detected_x is not None: - # 重置连续失败计数 - consecutive_failures = 0 - - # 统计:每次"进入直播间"后首次找到福袋图标耗时 - try: - enter_ts = float(getattr(analyser, "_enter_live_ts", 0.0) or 0.0) - except Exception: - enter_ts = 0.0 - try: - already = bool(getattr(analyser, "_enter_live_fudai_logged", False)) - except Exception: - already = False - if (enter_ts > 0) and (not already): - try: - dt = float(time.time() - enter_ts) - except Exception: - dt = -1.0 - if dt >= 0: - try: - reason = str(getattr(analyser, "_enter_live_reason", "") or "") - except Exception: - reason = "" - try: - analyser._enter_live_fudai_logged = True - except Exception: - pass - try: - analyser._log_kv( - "性能", - "进入直播间后找到福袋耗时", - seconds=round(dt, 2), - enter_reason=reason, - fudai_x=int(detected_x or 0), - ) - except Exception: - pass - - # 命中时记录调试信息 - try: - elapsed_ms = int((time.time() - func_start) * 1000) - scan_y_for_log = 403 * ry // 2400 + y_pianyi_val - analyser._log_kv( - "[找福袋图标] 命中", - loop=loop, - elapsed_ms=elapsed_ms, - scan_y=scan_y_for_log, - x=detected_x, - ) - except Exception: - pass - return int(detected_x or 0) - - loop += 1 - consecutive_failures += 1 - - # 如果连续失败太多次,提前退出(避免无效循环) - if consecutive_failures >= max_consecutive_failures: - try: - elapsed_ms = int((time.time() - func_start) * 1000) - analyser._log_kv( - "[找福袋图标] 警告", - "连续检测失败次数过多,提前退出", - consecutive_failures=consecutive_failures, - loop=loop, - elapsed_ms=elapsed_ms, - scan_y=scan_y, - ) - except Exception: - pass - break - - # 每轮记录一次轻量调试信息(帮助诊断为何未命中) - # 只在每3次循环记录一次,减少日志量 - if loop % 3 == 0: - try: - elapsed_ms = int((time.time() - func_start) * 1000) - sample_color = None - try: - mid_x = (scan_x_start + scan_x_end) // 2 - if 0 <= scan_y < height and 0 <= mid_x < width: - pix_sample = pic_new.load() - sample_color = pix_sample[mid_x, scan_y] - except Exception: - sample_color = None - analyser._log_kv( - "[找福袋图标] 循环中", - loop=loop, - elapsed_ms=elapsed_ms, - scan_y=scan_y, - scan_x_start=scan_x_start, - scan_x_end=scan_x_end, - sample_color=( - str(sample_color) if sample_color is not None else "None" - ), - consecutive_failures=consecutive_failures, - ) - except Exception: - pass - - # 检查是否还有剩余时间 - elapsed = time.time() - func_start - if elapsed < max_wait_s: - remaining_time = max_wait_s - elapsed - sleep_time = min(retry_delay_s, remaining_time) - if sleep_time > 0: - try: - analyser.operation.delay(sleep_time) - except Exception: - time.sleep(sleep_time) - else: - # 时间已到,退出循环 - break - - # 像素检测未找到时,返回 False,并记录总耗时与循环次数 - try: - total_elapsed_ms = int((time.time() - func_start) * 1000) - analyser._log_kv( - "[找福袋图标] 未找到福袋图标", - elapsed_ms=total_elapsed_ms, - total_loops=loop, - max_wait_s=max_wait_s, - retry_delay_s=retry_delay_s, - ) - except Exception: - pass - return False - - try: - retry_delay_s = float( - getattr(analyser, "fudai_icon_retry_delay_seconds", 0.3) or 0.3 - ) - except Exception: - retry_delay_s = 0.3 - try: - max_wait_s = float(getattr(analyser, "fudai_icon_max_wait_seconds", 2.5) or 2.5) - except Exception: - max_wait_s = 2.5 - if retry_delay_s < 0.15: - retry_delay_s = 0.15 - if max_wait_s < 0: - max_wait_s = 0.0 - - try: - analyser._log_kv( - "[找福袋图标] 开始", - f"最大等待时间{max_wait_s}秒,复用截图={screenshot_ready},使用模板匹配", - ) - except Exception: - pass - - detected_any = False - detected_x = None - - # 检查OpenCV是否可用 - try: - import cv2 - import numpy as np - except Exception: - try: - analyser._log_kv( - "[找福袋图标] 错误", "OpenCV/numpy不可用,无法使用模板匹配" - ) - except Exception: - pass - return False - - # 获取模板路径 - try: - from douyin.core import path_config - - template_path = path_config.get_assets_dir() + "/fudai.png" - except Exception: - # 回退到旧路径(向后兼容) - base_dir = os.path.dirname(__file__) - template_path = os.path.join(base_dir, "assets", "fudai.png") - if not os.path.exists(template_path): - try: - analyser._log_kv("[找福袋图标] 错误", f"模板文件不存在: {template_path}") - except Exception: - pass - return False - - # 获取匹配阈值 - try: - template_threshold = float( - getattr(analyser, "fudai_template_match_threshold", 0.7) or 0.7 - ) - except Exception: - template_threshold = 0.7 - - # 完全基于时间限制,不使用次数限制 - while (time.time() - func_start) < max_wait_s: - attempt_start = time.time() - attempt_count += 1 - # Reuse external screenshot only on first attempt if provided - if not (screenshot_ready and attempt_count == 1): - try: - analyser._shot_and_diagnose( - "找福袋图标", "判定是否存在福袋", analyser.device_id, diagnose=False - ) - except Exception: - pass - - try: - screenshot_img = cv2.imread(pic1_path, cv2.IMREAD_COLOR) - if screenshot_img is None: - try: - analyser.operation.get_screenshot( - analyser.device_id, use_service=False - ) - screenshot_img = cv2.imread(pic1_path, cv2.IMREAD_COLOR) - except Exception: - pass - if screenshot_img is None: - elapsed = time.time() - func_start - if elapsed < max_wait_s: - remaining_time = max_wait_s - elapsed - sleep_time = min(retry_delay_s, remaining_time) - if sleep_time > 0: - try: - analyser.operation.delay(sleep_time) - except Exception: - time.sleep(sleep_time) - continue - except Exception: - elapsed = time.time() - func_start - if elapsed < max_wait_s: - remaining_time = max_wait_s - elapsed - sleep_time = min(retry_delay_s, remaining_time) - if sleep_time > 0: - try: - analyser.operation.delay(sleep_time) - except Exception: - time.sleep(sleep_time) - continue - - # Resolution ratios - try: - rx = int(getattr(analyser, "resolution_ratio_x", 1080) or 1080) - except Exception: - rx = 1080 - try: - ry = int(getattr(analyser, "resolution_ratio_y", 2400) or 2400) - except Exception: - ry = 2400 - try: - y_pianyi_val = int(getattr(analyser, "y_pianyi", 0) or 0) - except Exception: - y_pianyi_val = 0 - - # 定义搜索区域(仅在更小的范围内寻找福袋图标) - # X范围:157-269(1080基准) # 左上角 X 改为图片中的坐标 x=157(右下不变) - # Y范围:354-451(2400基准) # 左上角 Y 改为图片中的坐标 y=354(右下不变) - search_x1 = max(0, 157 * rx // 1080) - search_x2 = min(screenshot_img.shape[1], 269 * rx // 1080) - search_y1_base = 354 * ry // 2400 + y_pianyi_val - search_y2_base = 451 * ry // 2400 + y_pianyi_val - search_y1 = max(0, search_y1_base) - search_y2 = min(screenshot_img.shape[0], search_y2_base) - - if search_x1 >= search_x2 or search_y1 >= search_y2: - elapsed = time.time() - func_start - if elapsed < max_wait_s: - remaining_time = max_wait_s - elapsed - sleep_time = min(retry_delay_s, remaining_time) - if sleep_time > 0: - try: - analyser.operation.delay(sleep_time) - except Exception: - time.sleep(sleep_time) - continue - - # 裁剪搜索区域 - search_roi = screenshot_img[search_y1:search_y2, search_x1:search_x2] - - # 读取模板 - try: - template = cv2.imread(template_path, cv2.IMREAD_COLOR) - if template is None: - try: - analyser._log_kv( - "[找福袋图标] 错误", f"无法读取模板文件: {template_path}" - ) - except Exception: - pass - elapsed = time.time() - func_start - if elapsed < max_wait_s: - remaining_time = max_wait_s - elapsed - sleep_time = min(retry_delay_s, remaining_time) - if sleep_time > 0: - try: - analyser.operation.delay(sleep_time) - except Exception: - time.sleep(sleep_time) - continue - except Exception as e: - try: - analyser._log_kv("[找福袋图标] 错误", f"读取模板文件异常: {str(e)}") - except Exception: - pass - elapsed = time.time() - func_start - if elapsed < max_wait_s: - remaining_time = max_wait_s - elapsed - sleep_time = min(retry_delay_s, remaining_time) - if sleep_time > 0: - try: - analyser.operation.delay(sleep_time) - except Exception: - time.sleep(sleep_time) - continue - - # 检查模板和搜索区域尺寸 - th, tw = template.shape[:2] - rh, rw = search_roi.shape[:2] - - 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] - - # 执行模板匹配(使用彩色匹配) - try: - template_match_start = time.time() - - # 详细日志:记录搜索区域和模板信息 - try: - analyser._log_kv( - "[找福袋图标] 模板匹配开始", - f"搜索区域", - search_x1=search_x1, - search_x2=search_x2, - search_y1=search_y1, - search_y2=search_y2, - search_width=search_x2 - search_x1, - search_height=search_y2 - search_y1, - template_width=tw, - template_height=th, - template_path=template_path, - 阈值=template_threshold, - 匹配模式="彩色(BGR)", - ) - except Exception: - pass - - # 使用彩色匹配(BGR三通道) - 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" - ) - - # 保存调试图像(匹配前)- 使用完整截图 - screenshot_pil = PILImage.fromarray( - cv2.cvtColor(screenshot_img, cv2.COLOR_BGR2RGB) - ) - debug_path = douyin_screenshot_manager.save_debug_image( - screenshot_pil, - operation_type="template_match", - picname="fudai_icon", - step="before_match", - coords=(search_x1, search_y1, search_x2, search_y2), - extra_info={ - "template": ( - os.path.basename(template_path) if template_path else "" - ), - "template_size": f"{tw}x{th}", - "search_size": f"{search_x2-search_x1}x{search_y2-search_y1}", - "threshold": template_threshold, - }, - ) - if debug_path: - try: - analyser._log_kv( - "[调试图像]", - "模板匹配前保存", - path=debug_path, - coords=f"({search_x1},{search_y1})-({search_x2},{search_y2})", - ) - except Exception: - pass - except Exception: - pass - - template_match_elapsed = (time.time() - template_match_start) * 1000 - - # 详细日志:记录匹配结果 - try: - analyser._log_kv( - "[找福袋图标] 模板匹配结果", - f"匹配分数={round(max_val, 4)},阈值={template_threshold}", - 耗时毫秒=round(template_match_elapsed, 2), - 是否通过=bool(max_val >= template_threshold), - 最小分数=round(_min_val, 4), - 最佳位置=f"({max_loc[0]}, {max_loc[1]})", - ) - except Exception: - pass - - if max_val >= template_threshold: - # 双截图记录:保存成功识别存档截图 - 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" - ) - - # 保存带匹配结果的调试图像(带匹配框)- 使用完整截图 - screenshot_pil = PILImage.fromarray( - cv2.cvtColor(screenshot_img, cv2.COLOR_BGR2RGB) - ) - # 计算匹配位置在完整截图中的坐标 - match_x_in_roi = max_loc[0] - match_y_in_roi = max_loc[1] - match_x1 = search_x1 + match_x_in_roi - match_y1 = search_y1 + match_y_in_roi - match_x2 = search_x1 + match_x_in_roi + tw - match_y2 = search_y1 + match_y_in_roi + th - match_coords = (match_x1, match_y1, match_x2, match_y2) - - debug_path = douyin_screenshot_manager.save_debug_image( - screenshot_pil, - operation_type="template_match", - picname="fudai_icon", - step="matched", - coords=( - search_x1, - search_y1, - search_x2, - search_y2, - ), # 搜索区域坐标 - extra_info={ - "match_score": round(max_val, 4), - "match_location": f"({max_loc[0]},{max_loc[1]})", - "match_coords": match_coords, # 匹配位置坐标 - "template_size": f"{tw}x{th}", - "threshold": template_threshold, - "passed": True, - }, - ) - if debug_path: - try: - analyser._log_kv( - "[调试图像]", - "模板匹配成功保存", - path=debug_path, - score=round(max_val, 4), - location=f"({max_loc[0]},{max_loc[1]})", - ) - except Exception: - pass - except Exception: - pass - - # 计算检测到的X坐标(1080基准) - detected_x_in_roi = max_loc[0] - detected_x = int( - (search_x1 + detected_x_in_roi + tw // 2) * 1080.0 / max(1, rx) - ) - detected_y_in_roi = max_loc[1] - detected_y = int( - (search_y1 + detected_y_in_roi + th // 2) * 2400.0 / max(1, ry) - ) - detected_any = True - - # 保存匹配位置的图片 - try: - # 计算匹配区域在原图中的坐标(扩大一些范围以便查看上下文) - margin = 20 # 扩大20像素的边距 - match_x1 = max(0, search_x1 + detected_x_in_roi - margin) - match_y1 = max(0, search_y1 + detected_y_in_roi - margin) - match_x2 = min( - screenshot_img.shape[1], - search_x1 + detected_x_in_roi + tw + margin, - ) - match_y2 = min( - screenshot_img.shape[0], - search_y1 + detected_y_in_roi + th + margin, - ) - - # 裁剪匹配区域 - match_roi = screenshot_img[match_y1:match_y2, match_x1:match_x2] - - # 保存到 pic/template_match/ 目录 - base_dir = os.path.dirname(__file__) - pic_base = os.path.join(base_dir, "pic") - match_dir = os.path.join(pic_base, "template_match") - os.makedirs(match_dir, exist_ok=True) - - # 文件名包含时间戳和匹配分数 - timestamp = int(time.time() * 1000) - score_str = f"{max_val:.4f}".replace(".", "_") - match_filename = f"fudai_match_{timestamp}_score_{score_str}.png" - match_path = os.path.join(match_dir, match_filename) - - # 检查裁剪区域是否有效 - if match_roi.size == 0: - analyser._log_kv( - "[找福袋图标] 警告", - "匹配区域裁剪后为空,跳过保存", - 区域坐标=f"({match_x1}, {match_y1}) - ({match_x2}, {match_y2})", - ) - else: - # 保存图片 - save_success = cv2.imwrite(match_path, match_roi) - - # 验证文件是否真的保存成功 - file_exists = os.path.exists(match_path) - file_size = os.path.getsize(match_path) if file_exists else 0 - - analyser._log_kv( - "[找福袋图标] 已保存匹配位置图片", - f"路径: {match_path}", - 匹配区域=f"({match_x1}, {match_y1}) - ({match_x2}, {match_y2})", - 区域大小=f"{match_x2 - match_x1}x{match_y2 - match_y1}", - ROI尺寸=f"{match_roi.shape[1]}x{match_roi.shape[0]}", - cv2保存返回值=save_success, - 文件存在=file_exists, - 文件大小字节=file_size, - ) - - if not file_exists or file_size == 0: - analyser._log_kv( - "[找福袋图标] 警告", - "保存的图片文件不存在或大小为0", - 路径=match_path, - ) - else: - # 保存成功,输出绝对路径方便查看 - abs_path = os.path.abspath(match_path) - analyser._log_kv( - "[找福袋图标] 图片保存成功", - f"绝对路径: {abs_path}", - ) - except Exception as e: - try: - analyser._log_kv( - "[找福袋图标] 保存匹配位置图片失败", - f"异常: {str(e)}", - ) - except Exception: - pass - - try: - analyser.last_find_fudai_time = time.time() - analyser._log_kv( - "[找福袋图标] 模板匹配命中", - f"检测到福袋图标", - x=detected_x, - y=detected_y, - 匹配分数=round(max_val, 4), - x_in_roi=detected_x_in_roi, - y_in_roi=detected_y_in_roi, - 模板中心偏移=f"({tw//2}, {th//2})", - 实际屏幕坐标=f"({detected_x}, {detected_y})", - ) - except Exception: - pass - break - else: - # 未命中时也记录日志 - try: - analyser._log_kv( - "[找福袋图标] 模板匹配未命中", - f"匹配分数={round(max_val, 4)} < 阈值={template_threshold}", - 差值=round(template_threshold - max_val, 4), - ) - except Exception: - pass - except Exception as e: - try: - analyser._log_kv( - "[找福袋图标] 模板匹配异常", - f"异常: {str(e)}", - 异常类型=type(e).__name__, - ) - except Exception: - pass - - # 检查是否还有剩余时间 - elapsed = time.time() - func_start - if elapsed < max_wait_s: - remaining_time = max_wait_s - elapsed - sleep_time = min(retry_delay_s, remaining_time) - if sleep_time > 0: - try: - analyser.operation.delay(sleep_time) - except Exception: - time.sleep(sleep_time) - - try: - total_elapsed = time.time() - func_start - analyser._log_kv( - "[找福袋图标] 结束", - "检测完成", - detected=bool(detected_any and detected_x is not None), - elapsed_ms=int(total_elapsed * 1000), - ) - except Exception: - pass - - # 与原始 fudai.py 的 check_have_fudai 行为保持一致: - # 返回福袋图标的 x 坐标(1080 基准坐标),未找到返回 False。 - return int(detected_x) if detected_x is not None else False - - -# ========== 以下为旧的像素检测代码,已暂时禁用 ========== -# 像素检测代码已删除,使用模板匹配替代 -# 如需恢复像素检测,请参考 git 历史记录 - - -def check_in_fudai_detail(analyser: Any) -> bool: - """Detect whether we are currently on the fudai detail page. - - 使用模板匹配检测,与 detect_fudai_detail_page 保持一致。 - """ - _ensure_detection_cache(analyser) - cache_key = "check_in_fudai_detail" - now = time.time() - cached = analyser._detection_cache.get(cache_key) - if cached: - cached_ts, cached_result = cached - ttl = float(getattr(analyser, "_detection_cache_ttl", 2.0) or 2.0) - if (now - cached_ts) < ttl: - return bool(cached_result) - - # 使用模板匹配检测,与 detect_fudai_detail_page 保持一致 - try: - from douyin.detection import douyin_pixel - from douyin.core import path_config - - # 获取截图 - pic_path = path_config.get_screenshot_path() - if not os.path.exists(pic_path): - try: - analyser.operation.get_screenshot(analyser.device_id) - except Exception: - analyser._detection_cache[cache_key] = (now, False) - return False - pic_path = path_config.get_screenshot_path() - if not os.path.exists(pic_path): - analyser._detection_cache[cache_key] = (now, False) - return False - - pic = Image.open(pic_path) - pic.load() - pic_rgb = pic.convert("RGB") - - # 获取分辨率比例和Y偏移 - rx = int(getattr(analyser, "resolution_ratio_x", 1080) or 1080) - ry = int(getattr(analyser, "resolution_ratio_y", 2400) or 2400) - y_pianyi = int(getattr(analyser, "y_pianyi", 0) or 0) - - # 获取模板匹配阈值(优先使用福袋详情页专用阈值) - template_threshold = float( - getattr(analyser, "fudai_detail_template_threshold", None) - or getattr(analyser, "fudai_template_match_threshold", 0.7) - or 0.7 - ) - if template_threshold < 0.0: - template_threshold = 0.0 - if template_threshold > 1.0: - template_threshold = 1.0 - - # 使用任务数量检测 - result = douyin_pixel.detect_fudai_detail_page( - pic_rgb, - rx, - ry, - y_pianyi, - template_threshold, - return_info=False, # 不需要详细信息 - analyser=analyser, - ) - - is_in_detail = bool(result) - analyser._detection_cache[cache_key] = (now, is_in_detail) - return is_in_detail - except Exception: - analyser._detection_cache[cache_key] = (now, False) - return False - - -# ========== 以下为旧的像素检测代码,已暂时禁用 ========== -# 像素检测代码已删除,使用模板匹配替代 -# 如需恢复像素检测,请参考 git 历史记录 +from douyin.detection.detection_pkg import ( + _ensure_detection_cache, + check_detail_height, + check_have_fudai_with_ready_screenshot, + check_in_fudai_detail, +) + +__all__ = [ + "_ensure_detection_cache", + "check_detail_height", + "check_have_fudai_with_ready_screenshot", + "check_in_fudai_detail", +]