diff --git a/douyin/detection/detection_pkg/finder.py b/douyin/detection/detection_pkg/finder.py index 8495c03..2f503fa 100644 --- a/douyin/detection/detection_pkg/finder.py +++ b/douyin/detection/detection_pkg/finder.py @@ -302,53 +302,14 @@ def check_have_fudai_with_ready_screenshot( # 重置连续失败计数 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 + # 记录"进入直播间→找到福袋"耗时统计 + 命中调试信息 + from douyin.detection.detection_pkg.finder_loop_logging import ( + log_fudai_found_and_metrics, + ) - # 命中时记录调试信息 - 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 + log_fudai_found_and_metrics( + analyser, detected_x, func_start, loop, ry, y_pianyi_val + ) return int(detected_x or 0) loop += 1 @@ -370,33 +331,15 @@ def check_have_fudai_with_ready_screenshot( 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 + # 每轮记录一次轻量调试信息(每 3 次循环一次,减少日志量) + from douyin.detection.detection_pkg.finder_loop_logging import ( + log_loop_progress, + ) + + log_loop_progress( + analyser, loop, func_start, pic_new, width, height, + scan_x_start, scan_x_end, scan_y, consecutive_failures, + ) # 检查是否还有剩余时间 elapsed = time.time() - func_start @@ -413,17 +356,11 @@ def check_have_fudai_with_ready_screenshot( 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 + from douyin.detection.detection_pkg.finder_loop_logging import ( + log_no_fudai_found, + ) + + log_no_fudai_found(analyser, func_start, loop, max_wait_s, retry_delay_s) return False diff --git a/douyin/detection/detection_pkg/finder_loop_logging.py b/douyin/detection/detection_pkg/finder_loop_logging.py new file mode 100644 index 0000000..0d15c48 --- /dev/null +++ b/douyin/detection/detection_pkg/finder_loop_logging.py @@ -0,0 +1,131 @@ +"""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