"""find_fudai 辅助:循环中的调试日志 从 douyin/detection/detection_pkg/finder.py:check_have_fudai_with_ready_screenshot 中提取。 - log_fudai_found_and_metrics:命中福袋后记录"进入直播间→找到福袋"耗时统计 + 命中调试信息 - log_loop_progress:每 3 次循环记录一次轻量调试信息 - log_no_fudai_found:未找到时记录总耗时与循环次数 """ from __future__ import annotations import time from typing import Any def log_fudai_found_and_metrics( analyser: Any, detected_x, func_start: float, loop: int, ry: int, y_pianyi_val: int, ) -> None: """命中福袋后:记录"进入直播间→找到福袋"耗时统计 + 命中调试信息。""" # 统计:每次"进入直播间"后首次找到福袋图标耗时 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 def log_loop_progress( analyser: Any, loop: int, func_start: float, pic_new, width: int, height: int, scan_x_start: int, scan_x_end: int, scan_y: int, consecutive_failures: int, ) -> None: """每 3 次循环记录一次轻量调试信息(帮助诊断为何未命中)。""" if loop % 3 != 0: return 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 def log_no_fudai_found( analyser: Any, func_start: float, loop: int, max_wait_s: float, retry_delay_s: float, ) -> None: """像素检测未找到时记录总耗时与循环次数。""" 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