From 4cb5daa79e7b985f14fee79c5befbffc78bdc979 Mon Sep 17 00:00:00 2001 From: Codex Date: Fri, 22 May 2026 04:21:35 +0800 Subject: [PATCH] =?UTF-8?q?refactor(stage):=20=E6=8B=86=E5=87=BA=E6=8A=BD?= =?UTF-8?q?=E5=A5=96=E7=BB=93=E6=9E=9C=E5=BC=B9=E7=AA=97=20+=20=E8=B4=AD?= =?UTF-8?q?=E7=89=A9=E7=BA=A2=E5=8C=85=E5=BC=B9=E7=AA=97=20OCR=20=E6=A3=80?= =?UTF-8?q?=E6=B5=8B=EF=BC=881284=E2=86=921204=EF=BC=8C-80=20=E8=A1=8C?= =?UTF-8?q?=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 把 douyin_stage 中两个独立的"像素初筛 + OCR 二次确认"块抽到 douyin_stage_helpers: - detect_lottery_result_popup:优先级1,抽奖结果弹窗(FUDai_RESULT) - detect_shopping_redpacket_popup:优先级1.5,购物红包弹窗(SHOPPING_REDPACKET_POPUP) 两者都返回 (matched, stage, reason),命中时主流程直接 return。 新增 6 个单元测试,1273 → 1279 用例全过。 Co-Authored-By: Claude Opus 4.7 (1M context) --- douyin/stage/douyin_stage.py | 118 ++++-------------------- douyin/stage/douyin_stage_helpers.py | 133 +++++++++++++++++++++++++++ tests/test_douyin_stage_helpers.py | 99 +++++++++++++++++++- 3 files changed, 250 insertions(+), 100 deletions(-) diff --git a/douyin/stage/douyin_stage.py b/douyin/stage/douyin_stage.py index bf0a807..c0cab6b 100644 --- a/douyin/stage/douyin_stage.py +++ b/douyin/stage/douyin_stage.py @@ -15,6 +15,8 @@ from douyin.fudai.core.coords import ( COUPON_PARTICIPATE_BUTTON_ALT, ) from douyin.stage.douyin_stage_helpers import ( + detect_lottery_result_popup, + detect_shopping_redpacket_popup, log_pixel_detection_details, summarize_pixel_state, ) @@ -681,109 +683,27 @@ def diagnose_stage_from_current_screen(analyser, trigger_reason="unknown"): # t # 优先级1:抽奖结果弹窗(像素检测 + 按钮OCR确认) if pixel_state.get("has_lucky_draw_result", False): - step1_start = time.time() - _log_separator("步骤1:检测抽奖结果弹窗(简化)") - try: - from douyin.utils.douyin_helpers import HelperFunctions - - detected = HelperFunctions.check_lottery_result_by_ocr(analyser) - if detected: - total_elapsed = (time.time() - diag_start) * 1000 - _log_summary( - "阶段诊断完成(抽奖结果弹窗)", - 阶段="FUDai_RESULT", - 原因="像素检测+按钮OCR确认", - 总耗时毫秒=total_elapsed, - ) - _log_separator("最终结果:FUDai_RESULT(抽奖结果弹窗)") - return "FUDai_RESULT", "像素检测+按钮OCR确认" - else: - _log("步骤1:像素检测到弹窗但未识别出按钮,继续后续检测") - except Exception as e: - _log( - "步骤1[检测抽奖结果弹窗]-简化OCR异常", - 异常类型=type(e).__name__, - 异常信息=str(e), - ) - - # 步骤1结束计时 - step1_elapsed = (time.time() - step1_start) * 1000 - _log("步骤1[检测抽奖结果弹窗]-完成", 总耗时毫秒=step1_elapsed) + matched, stage, reason = detect_lottery_result_popup( + analyser, diag_start, _log, _log_summary, _log_separator + ) + if matched: + return stage, reason # 优先级1.5:购物红包弹窗检测(保持现有检测方式) if pixel_state.get("has_redpacket_bg", False): - _log_separator("步骤1.5:检测购物红包弹窗") - step_redpacket_start = time.time() - remaining_time_before = max_diag_time - (time.time() - diag_start) - _log( - "步骤1.5[检测购物红包弹窗]-开始", - 剩余时间秒=remaining_time_before, - 说明="像素检测到购物红包背景", + matched, stage, reason = detect_shopping_redpacket_popup( + analyser, + SHOPPING_REDPACKET_AREA, + diag_start, + max_diag_time, + max_step_time, + preprocess_for_ocr, + _log, + _log_summary, + _log_separator, ) - - redpacket_detected = False - try: - if time.time() - step_redpacket_start < max_step_time: - cut_start = time.time() - analyser.cut_pic(*SHOPPING_REDPACKET_AREA, "", "診斷購物紅包") - cut_elapsed = (time.time() - cut_start) * 1000 - _log("步骤1.5[检测购物红包弹窗]-截图完成", 耗时毫秒=cut_elapsed) - - if time.time() - step_redpacket_start < max_step_time: - ocr_start = time.time() - name_rp = preprocess_for_ocr(analyser, "診斷購物紅包") - redpacket_text = str( - analyser.operation.analyse_pic_word(name_rp, 0) or "" - ) - ocr_elapsed = (time.time() - ocr_start) * 1000 - _log( - "步骤1.5[检测购物红包弹窗]-OCR模式0", - 耗时毫秒=ocr_elapsed, - 文本长度=len(redpacket_text), - 文本预览=redpacket_text[:50] if redpacket_text else "", - ) - - # 检查是否包含购物红包关键词 - redpacket_keywords = ("购物红包", "邀你抢", "抢红包") - if redpacket_text and any( - kw in redpacket_text for kw in redpacket_keywords - ): - redpacket_detected = True - else: - # OCR未识别到关键词,不判定为购物红包弹窗(以OCR为准) - _log( - "步骤1.5[检测购物红包弹窗]-OCR未识别到关键词", - 说明="OCR未匹配,不判定为购物红包弹窗", - ) - except Exception as e: - redpacket_elapsed = (time.time() - step_redpacket_start) * 1000 - _log( - "步骤1.5[检测购物红包弹窗]-异常", - 耗时毫秒=redpacket_elapsed, - 异常类型=type(e).__name__, - 异常信息=str(e), - ) - # OCR异常时,不判定为购物红包弹窗(以OCR为准) - _log( - "步骤1.5[检测购物红包弹窗]-OCR异常,不判定为购物红包弹窗", - 说明="需要OCR确认才能判定", - ) - - if redpacket_detected: - total_elapsed = (time.time() - diag_start) * 1000 - redpacket_elapsed = (time.time() - step_redpacket_start) * 1000 - _log_summary( - "阶段诊断完成(购物红包弹窗)", - 阶段="SHOPPING_REDPACKET_POPUP", - 原因="检测到购物红包弹窗", - 总耗时毫秒=total_elapsed, - 红包检测耗时毫秒=redpacket_elapsed, - ) - return "SHOPPING_REDPACKET_POPUP", "像素检测+OCR确认(购物红包弹窗)" - - # 步骤1.5结束计时 - step15_elapsed = (time.time() - step_redpacket_start) * 1000 - _log("步骤1.5[检测购物红包弹窗]-完成", 总耗时毫秒=step15_elapsed) + if matched: + return stage, reason # ========== 步骤1:像素检测福袋详情页(从并行检测结果获取)========== step1_fudai_start = time.time() diff --git a/douyin/stage/douyin_stage_helpers.py b/douyin/stage/douyin_stage_helpers.py index 582095b..2cf0792 100644 --- a/douyin/stage/douyin_stage_helpers.py +++ b/douyin/stage/douyin_stage_helpers.py @@ -89,6 +89,139 @@ def _format_detail_log_fields(key: str, detail: Dict[str, Any], analyser: Any) - return log_fields +def detect_lottery_result_popup( + analyser: Any, + diag_start: float, + log_callback: Callable[..., None], + log_summary_callback: Callable[..., None], + log_separator_callback: Callable[..., None], +) -> tuple[bool, str, str]: + """优先级1:通过 OCR 确认抽奖结果弹窗。 + + 返回 (matched, stage, reason): + - matched=True:已确认是 FUDai_RESULT,主流程应直接 return (stage, reason) + - matched=False:继续后续检测 + """ + import time + + step1_start = time.time() + log_separator_callback("步骤1:检测抽奖结果弹窗(简化)") + try: + from douyin.utils.douyin_helpers import HelperFunctions + + detected = HelperFunctions.check_lottery_result_by_ocr(analyser) + if detected: + total_elapsed = (time.time() - diag_start) * 1000 + log_summary_callback( + "阶段诊断完成(抽奖结果弹窗)", + 阶段="FUDai_RESULT", + 原因="像素检测+按钮OCR确认", + 总耗时毫秒=total_elapsed, + ) + log_separator_callback("最终结果:FUDai_RESULT(抽奖结果弹窗)") + return True, "FUDai_RESULT", "像素检测+按钮OCR确认" + else: + log_callback("步骤1:像素检测到弹窗但未识别出按钮,继续后续检测") + except Exception as e: + log_callback( + "步骤1[检测抽奖结果弹窗]-简化OCR异常", + 异常类型=type(e).__name__, + 异常信息=str(e), + ) + + step1_elapsed = (time.time() - step1_start) * 1000 + log_callback("步骤1[检测抽奖结果弹窗]-完成", 总耗时毫秒=step1_elapsed) + return False, "", "" + + +def detect_shopping_redpacket_popup( + analyser: Any, + shopping_redpacket_area: tuple, + diag_start: float, + max_diag_time: float, + max_step_time: float, + preprocess_for_ocr: Callable[[Any, str], str], + log_callback: Callable[..., None], + log_summary_callback: Callable[..., None], + log_separator_callback: Callable[..., None], +) -> tuple[bool, str, str]: + """优先级1.5:通过 OCR 确认购物红包弹窗。 + + 返回 (matched, stage, reason)。 + """ + import time + + log_separator_callback("步骤1.5:检测购物红包弹窗") + step_redpacket_start = time.time() + remaining_time_before = max_diag_time - (time.time() - diag_start) + log_callback( + "步骤1.5[检测购物红包弹窗]-开始", + 剩余时间秒=remaining_time_before, + 说明="像素检测到购物红包背景", + ) + + redpacket_detected = False + try: + if time.time() - step_redpacket_start < max_step_time: + cut_start = time.time() + analyser.cut_pic(*shopping_redpacket_area, "", "診斷購物紅包") + cut_elapsed = (time.time() - cut_start) * 1000 + log_callback("步骤1.5[检测购物红包弹窗]-截图完成", 耗时毫秒=cut_elapsed) + + if time.time() - step_redpacket_start < max_step_time: + ocr_start = time.time() + name_rp = preprocess_for_ocr(analyser, "診斷購物紅包") + redpacket_text = str( + analyser.operation.analyse_pic_word(name_rp, 0) or "" + ) + ocr_elapsed = (time.time() - ocr_start) * 1000 + log_callback( + "步骤1.5[检测购物红包弹窗]-OCR模式0", + 耗时毫秒=ocr_elapsed, + 文本长度=len(redpacket_text), + 文本预览=redpacket_text[:50] if redpacket_text else "", + ) + + redpacket_keywords = ("购物红包", "邀你抢", "抢红包") + if redpacket_text and any( + kw in redpacket_text for kw in redpacket_keywords + ): + redpacket_detected = True + else: + log_callback( + "步骤1.5[检测购物红包弹窗]-OCR未识别到关键词", + 说明="OCR未匹配,不判定为购物红包弹窗", + ) + except Exception as e: + redpacket_elapsed = (time.time() - step_redpacket_start) * 1000 + log_callback( + "步骤1.5[检测购物红包弹窗]-异常", + 耗时毫秒=redpacket_elapsed, + 异常类型=type(e).__name__, + 异常信息=str(e), + ) + log_callback( + "步骤1.5[检测购物红包弹窗]-OCR异常,不判定为购物红包弹窗", + 说明="需要OCR确认才能判定", + ) + + if redpacket_detected: + total_elapsed = (time.time() - diag_start) * 1000 + redpacket_elapsed = (time.time() - step_redpacket_start) * 1000 + log_summary_callback( + "阶段诊断完成(购物红包弹窗)", + 阶段="SHOPPING_REDPACKET_POPUP", + 原因="检测到购物红包弹窗", + 总耗时毫秒=total_elapsed, + 红包检测耗时毫秒=redpacket_elapsed, + ) + return True, "SHOPPING_REDPACKET_POPUP", "像素检测+OCR确认(购物红包弹窗)" + + step15_elapsed = (time.time() - step_redpacket_start) * 1000 + log_callback("步骤1.5[检测购物红包弹窗]-完成", 总耗时毫秒=step15_elapsed) + return False, "", "" + + def log_pixel_detection_details( pixel_details: Dict[str, Dict[str, Any]], analyser: Any, diff --git a/tests/test_douyin_stage_helpers.py b/tests/test_douyin_stage_helpers.py index 1773451..a390cd4 100644 --- a/tests/test_douyin_stage_helpers.py +++ b/tests/test_douyin_stage_helpers.py @@ -7,7 +7,8 @@ from __future__ import annotations -from unittest.mock import MagicMock +import time as _time +from unittest.mock import MagicMock, patch from douyin.stage import douyin_stage_helpers as h @@ -103,3 +104,99 @@ def test_summarize_pixel_state_swallows_exceptions(): """log_callback 抛错也不应传播""" bad_log = MagicMock(side_effect=RuntimeError("boom")) h.summarize_pixel_state({}, 0.0, bad_log) + + +# ---- detect_lottery_result_popup ---- # + +def test_detect_lottery_result_matched_returns_true(): + a = MagicMock() + log = MagicMock() + log_summary = MagicMock() + log_sep = MagicMock() + from douyin.utils import douyin_helpers + + with patch.object( + douyin_helpers.HelperFunctions, + "check_lottery_result_by_ocr", + return_value=True, + ): + matched, stage, reason = h.detect_lottery_result_popup( + a, _time.time(), log, log_summary, log_sep + ) + assert matched is True + assert stage == "FUDai_RESULT" + + +def test_detect_lottery_result_not_matched_returns_false(): + a = MagicMock() + log = MagicMock() + log_summary = MagicMock() + log_sep = MagicMock() + from douyin.utils import douyin_helpers + + with patch.object( + douyin_helpers.HelperFunctions, + "check_lottery_result_by_ocr", + return_value=False, + ): + matched, _, _ = h.detect_lottery_result_popup( + a, _time.time(), log, log_summary, log_sep + ) + assert matched is False + + +def test_detect_lottery_result_exception_returns_false(): + a = MagicMock() + log = MagicMock() + log_summary = MagicMock() + log_sep = MagicMock() + from douyin.utils import douyin_helpers + + with patch.object( + douyin_helpers.HelperFunctions, + "check_lottery_result_by_ocr", + side_effect=RuntimeError("boom"), + ): + matched, _, _ = h.detect_lottery_result_popup( + a, _time.time(), log, log_summary, log_sep + ) + assert matched is False + + +# ---- detect_shopping_redpacket_popup ---- # + +def test_detect_shopping_redpacket_keyword_match(): + """OCR 文本含关键词 → 命中""" + a = MagicMock() + a.operation.analyse_pic_word.return_value = "邀你抢红包啦" + log = MagicMock() + log_summary = MagicMock() + log_sep = MagicMock() + matched, stage, _ = h.detect_shopping_redpacket_popup( + a, (10, 20, 30, 40), _time.time(), 30.0, 5.0, + lambda _x, _y: "preprocessed", log, log_summary, log_sep, + ) + assert matched is True + assert stage == "SHOPPING_REDPACKET_POPUP" + + +def test_detect_shopping_redpacket_no_keyword_returns_false(): + a = MagicMock() + a.operation.analyse_pic_word.return_value = "随便的文本" + log = MagicMock() + matched, _, _ = h.detect_shopping_redpacket_popup( + a, (10, 20, 30, 40), _time.time(), 30.0, 5.0, + lambda _x, _y: "p", log, MagicMock(), MagicMock(), + ) + assert matched is False + + +def test_detect_shopping_redpacket_ocr_exception_returns_false(): + a = MagicMock() + a.cut_pic.side_effect = RuntimeError("boom") + log = MagicMock() + matched, _, _ = h.detect_shopping_redpacket_popup( + a, (10, 20, 30, 40), _time.time(), 30.0, 5.0, + lambda _x, _y: "p", log, MagicMock(), MagicMock(), + ) + assert matched is False