refactor(stage): 提取 diagnose_stage_from_current_screen 中"步骤3 OCR确认弹窗"块到 douyin_stage_helpers
将 1203-1359 行(约 157 行)的"OCR弹窗确认"独立块抽到 douyin/stage/douyin_stage_helpers.py:verify_popup_via_ocr。 该块当所有像素检测都未命中特定阶段、但像素检测到弹窗背景特征时 执行 OCR 二次确认(优惠券"开心收下"/购物红包关键词)。 依赖通过参数注入:pixel_state、enable_ocr_mode1、max_step_time、 diag_start、max_diag_time、_log/_log_summary 闭包、preprocess_for_ocr。 douyin_stage.py: 1385 → 1247 行 (-138) 新增 douyin_stage_helpers.py: 199 行 验证: - pytest 1013 passed - compile OK Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
a3fe7b3ef0
commit
d7201866a5
2 files changed
+213
-152
No files matched your search
+14
-152
@@ -1203,159 +1203,21 @@ def diagnose_stage_from_current_screen(analyser, trigger_reason="unknown"): # t
|
||||
# ========== 所有像素检测完成,未命中任何特定阶段 ==========
|
||||
# 在返回"在直播间"之前,如果检测到弹窗相关的像素特征,需要OCR再次确认
|
||||
# 因为有些直播间背景颜色可能与弹窗颜色相似,导致误判
|
||||
has_suspicious_popup_pixel = pixel_state.get(
|
||||
"has_coupon_bg", False
|
||||
) or pixel_state.get("has_redpacket_bg", False)
|
||||
from douyin.stage.douyin_stage_helpers import verify_popup_via_ocr
|
||||
|
||||
if has_suspicious_popup_pixel:
|
||||
step_ocr_verify_start = time.time()
|
||||
remaining_time_before = max_diag_time - (time.time() - diag_start)
|
||||
_log(
|
||||
"步骤3[OCR确认弹窗]-开始",
|
||||
剩余时间秒=remaining_time_before,
|
||||
说明="像素检测到弹窗背景,但未匹配到弹窗阶段,OCR再次确认",
|
||||
)
|
||||
|
||||
# 检测优惠券弹窗(使用参与按钮区域,检测"开心收下")
|
||||
if pixel_state.get("has_coupon_bg", False):
|
||||
coupon_verified = False
|
||||
try:
|
||||
if time.time() - step_ocr_verify_start < max_step_time:
|
||||
cut_start = time.time()
|
||||
analyser.cut_pic(*COUPON_PARTICIPATE_BUTTON_ALT, "", "参与按钮")
|
||||
cut_elapsed = (time.time() - cut_start) * 1000
|
||||
_log("步骤3[OCR确认弹窗]-优惠券截图完成", 耗时毫秒=cut_elapsed)
|
||||
|
||||
if time.time() - step_ocr_verify_start < max_step_time:
|
||||
ocr_start = time.time()
|
||||
# 如果启用模式1,并行执行两个OCR模式
|
||||
if enable_ocr_mode1:
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
|
||||
with ThreadPoolExecutor(max_workers=2) as executor:
|
||||
name_part0 = preprocess_for_ocr(analyser, "参与按钮")
|
||||
future_mode0 = executor.submit(
|
||||
analyser.operation.analyse_pic_word, name_part0, 0
|
||||
)
|
||||
name_part1 = preprocess_for_ocr(analyser, "参与按钮")
|
||||
future_mode1 = executor.submit(
|
||||
analyser.operation.analyse_pic_word, name_part1, 1
|
||||
)
|
||||
text_mode0 = str(future_mode0.result() or "")
|
||||
text_mode1 = str(future_mode1.result() or "")
|
||||
# 优先使用模式0的结果,如果模式0没有"开心收下"且模式1有,则使用模式1
|
||||
if text_mode0 and "开心收下" in text_mode0:
|
||||
coupon_text = text_mode0
|
||||
elif text_mode1 and "开心收下" in text_mode1:
|
||||
coupon_text = text_mode1
|
||||
elif text_mode0:
|
||||
coupon_text = text_mode0
|
||||
else:
|
||||
coupon_text = text_mode1
|
||||
ocr_elapsed = (time.time() - ocr_start) * 1000
|
||||
_log(
|
||||
"步骤3[OCR确认弹窗]-优惠券OCR并行模式0+1",
|
||||
耗时毫秒=ocr_elapsed,
|
||||
文本长度=len(coupon_text),
|
||||
文本预览=coupon_text[:50] if coupon_text else "",
|
||||
)
|
||||
else:
|
||||
pname = preprocess_for_ocr(analyser, "参与按钮")
|
||||
coupon_text = str(
|
||||
analyser.operation.analyse_pic_word(pname, 0) or ""
|
||||
)
|
||||
ocr_elapsed = (time.time() - ocr_start) * 1000
|
||||
_log(
|
||||
"步骤3[OCR确认弹窗]-优惠券OCR模式0",
|
||||
耗时毫秒=ocr_elapsed,
|
||||
文本长度=len(coupon_text),
|
||||
文本预览=coupon_text[:50] if coupon_text else "",
|
||||
)
|
||||
|
||||
if coupon_text and "开心收下" in coupon_text:
|
||||
coupon_verified = True
|
||||
except Exception as e:
|
||||
coupon_elapsed = (time.time() - step_ocr_verify_start) * 1000
|
||||
_log(
|
||||
"步骤3[OCR确认弹窗]-优惠券异常",
|
||||
耗时毫秒=coupon_elapsed,
|
||||
异常类型=type(e).__name__,
|
||||
异常信息=str(e),
|
||||
)
|
||||
|
||||
if coupon_verified:
|
||||
total_elapsed = (time.time() - diag_start) * 1000
|
||||
coupon_elapsed = (time.time() - step_ocr_verify_start) * 1000
|
||||
_log_summary(
|
||||
"阶段诊断完成(优惠券弹窗-OCR确认)",
|
||||
阶段="COUPON_POPUP",
|
||||
原因="像素检测到背景+OCR确认(开心收下)",
|
||||
总耗时毫秒=total_elapsed,
|
||||
优惠券检测耗时毫秒=coupon_elapsed,
|
||||
)
|
||||
return "COUPON_POPUP", "OCR确认(开心收下)"
|
||||
|
||||
# 检测购物红包弹窗
|
||||
if pixel_state.get("has_redpacket_bg", False):
|
||||
redpacket_verified = False
|
||||
try:
|
||||
if time.time() - step_ocr_verify_start < max_step_time:
|
||||
cut_start = time.time()
|
||||
analyser.cut_pic(*SHOPPING_REDPACKET_AREA, "", "診斷購物紅包")
|
||||
cut_elapsed = (time.time() - cut_start) * 1000
|
||||
_log("步骤3[OCR确认弹窗]-购物红包截图完成", 耗时毫秒=cut_elapsed)
|
||||
|
||||
if time.time() - step_ocr_verify_start < max_step_time:
|
||||
ocr_start = time.time()
|
||||
name_rp2 = preprocess_for_ocr(analyser, "診斷購物紅包")
|
||||
redpacket_text = str(
|
||||
analyser.operation.analyse_pic_word(name_rp2, 0) or ""
|
||||
)
|
||||
ocr_elapsed = (time.time() - ocr_start) * 1000
|
||||
_log(
|
||||
"步骤3[OCR确认弹窗]-购物红包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_verified = True
|
||||
except Exception as e:
|
||||
redpacket_elapsed = (time.time() - step_ocr_verify_start) * 1000
|
||||
_log(
|
||||
"步骤3[OCR确认弹窗]-购物红包异常",
|
||||
耗时毫秒=redpacket_elapsed,
|
||||
异常类型=type(e).__name__,
|
||||
异常信息=str(e),
|
||||
)
|
||||
|
||||
if redpacket_verified:
|
||||
total_elapsed = (time.time() - diag_start) * 1000
|
||||
redpacket_elapsed = (time.time() - step_ocr_verify_start) * 1000
|
||||
_log_summary(
|
||||
"阶段诊断完成(购物红包弹窗-OCR确认)",
|
||||
阶段="SHOPPING_REDPACKET_POPUP",
|
||||
原因="像素检测到背景+OCR确认(购物红包)",
|
||||
总耗时毫秒=total_elapsed,
|
||||
红包检测耗时毫秒=redpacket_elapsed,
|
||||
)
|
||||
return "SHOPPING_REDPACKET_POPUP", "OCR确认(购物红包弹窗)"
|
||||
|
||||
# OCR确认后仍未匹配到弹窗,说明是直播间背景颜色误判
|
||||
ocr_verify_elapsed = (time.time() - step_ocr_verify_start) * 1000
|
||||
_log(
|
||||
"步骤3[OCR确认弹窗]-未匹配到弹窗",
|
||||
耗时毫秒=ocr_verify_elapsed,
|
||||
说明="OCR确认后未匹配,判定为直播间背景颜色",
|
||||
)
|
||||
|
||||
# 步骤3结束计时(OCR确认弹窗)
|
||||
step3_ocr_elapsed = (time.time() - step_ocr_verify_start) * 1000
|
||||
_log("步骤3[OCR确认弹窗]-完成", 总耗时毫秒=step3_ocr_elapsed)
|
||||
popup_result = verify_popup_via_ocr(
|
||||
analyser,
|
||||
pixel_state,
|
||||
enable_ocr_mode1,
|
||||
max_step_time,
|
||||
diag_start,
|
||||
max_diag_time,
|
||||
_log,
|
||||
_log_summary,
|
||||
preprocess_for_ocr,
|
||||
)
|
||||
if popup_result is not None:
|
||||
return popup_result
|
||||
|
||||
# 如果识别到在直播间,并且其他阶段都没识别到,返回在直播间阶段
|
||||
if in_live_room:
|
||||
|
||||
@@ -0,0 +1,199 @@
|
||||
"""阶段诊断辅助:步骤3 OCR弹窗确认
|
||||
|
||||
从 douyin/stage/douyin_stage.py:diagnose_stage_from_current_screen 中提取的
|
||||
"步骤3 OCR确认弹窗"块。当所有像素检测都未命中特定阶段,但像素检测到弹窗
|
||||
背景特征(优惠券/购物红包)时,用 OCR 再次确认是否为弹窗,避免直播间
|
||||
背景色误判。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import time
|
||||
|
||||
from douyin.fudai.core.coords import (
|
||||
SHOPPING_REDPACKET_AREA,
|
||||
COUPON_PARTICIPATE_BUTTON_ALT,
|
||||
)
|
||||
|
||||
|
||||
def verify_popup_via_ocr(
|
||||
analyser,
|
||||
pixel_state: dict,
|
||||
enable_ocr_mode1: bool,
|
||||
max_step_time: float,
|
||||
diag_start: float,
|
||||
max_diag_time: float,
|
||||
log_fn,
|
||||
log_summary_fn,
|
||||
preprocess_for_ocr,
|
||||
):
|
||||
"""步骤3:OCR确认弹窗。
|
||||
|
||||
Args:
|
||||
analyser: fudai_analyse 实例
|
||||
pixel_state: 像素检测结果字典
|
||||
enable_ocr_mode1: 是否启用 OCR 模式1(并行执行模式0+模式1)
|
||||
max_step_time: 单步最大耗时(秒)
|
||||
diag_start: 阶段诊断起始时间戳
|
||||
max_diag_time: 阶段诊断最大耗时(秒)
|
||||
log_fn: 详细日志函数(_log)
|
||||
log_summary_fn: 概要日志函数(_log_summary)
|
||||
preprocess_for_ocr: OCR 预处理函数
|
||||
|
||||
Returns:
|
||||
- (stage, reason) 二元组:检测到弹窗时
|
||||
- None:未检测到(调用方继续后续逻辑)
|
||||
"""
|
||||
has_suspicious_popup_pixel = pixel_state.get(
|
||||
"has_coupon_bg", False
|
||||
) or pixel_state.get("has_redpacket_bg", False)
|
||||
|
||||
if not has_suspicious_popup_pixel:
|
||||
return None
|
||||
|
||||
step_ocr_verify_start = time.time()
|
||||
remaining_time_before = max_diag_time - (time.time() - diag_start)
|
||||
log_fn(
|
||||
"步骤3[OCR确认弹窗]-开始",
|
||||
剩余时间秒=remaining_time_before,
|
||||
说明="像素检测到弹窗背景,但未匹配到弹窗阶段,OCR再次确认",
|
||||
)
|
||||
|
||||
# 检测优惠券弹窗(使用参与按钮区域,检测"开心收下")
|
||||
if pixel_state.get("has_coupon_bg", False):
|
||||
coupon_verified = False
|
||||
try:
|
||||
if time.time() - step_ocr_verify_start < max_step_time:
|
||||
cut_start = time.time()
|
||||
analyser.cut_pic(*COUPON_PARTICIPATE_BUTTON_ALT, "", "参与按钮")
|
||||
cut_elapsed = (time.time() - cut_start) * 1000
|
||||
log_fn("步骤3[OCR确认弹窗]-优惠券截图完成", 耗时毫秒=cut_elapsed)
|
||||
|
||||
if time.time() - step_ocr_verify_start < max_step_time:
|
||||
ocr_start = time.time()
|
||||
if enable_ocr_mode1:
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
|
||||
with ThreadPoolExecutor(max_workers=2) as executor:
|
||||
name_part0 = preprocess_for_ocr(analyser, "参与按钮")
|
||||
future_mode0 = executor.submit(
|
||||
analyser.operation.analyse_pic_word, name_part0, 0
|
||||
)
|
||||
name_part1 = preprocess_for_ocr(analyser, "参与按钮")
|
||||
future_mode1 = executor.submit(
|
||||
analyser.operation.analyse_pic_word, name_part1, 1
|
||||
)
|
||||
text_mode0 = str(future_mode0.result() or "")
|
||||
text_mode1 = str(future_mode1.result() or "")
|
||||
if text_mode0 and "开心收下" in text_mode0:
|
||||
coupon_text = text_mode0
|
||||
elif text_mode1 and "开心收下" in text_mode1:
|
||||
coupon_text = text_mode1
|
||||
elif text_mode0:
|
||||
coupon_text = text_mode0
|
||||
else:
|
||||
coupon_text = text_mode1
|
||||
ocr_elapsed = (time.time() - ocr_start) * 1000
|
||||
log_fn(
|
||||
"步骤3[OCR确认弹窗]-优惠券OCR并行模式0+1",
|
||||
耗时毫秒=ocr_elapsed,
|
||||
文本长度=len(coupon_text),
|
||||
文本预览=coupon_text[:50] if coupon_text else "",
|
||||
)
|
||||
else:
|
||||
pname = preprocess_for_ocr(analyser, "参与按钮")
|
||||
coupon_text = str(
|
||||
analyser.operation.analyse_pic_word(pname, 0) or ""
|
||||
)
|
||||
ocr_elapsed = (time.time() - ocr_start) * 1000
|
||||
log_fn(
|
||||
"步骤3[OCR确认弹窗]-优惠券OCR模式0",
|
||||
耗时毫秒=ocr_elapsed,
|
||||
文本长度=len(coupon_text),
|
||||
文本预览=coupon_text[:50] if coupon_text else "",
|
||||
)
|
||||
|
||||
if coupon_text and "开心收下" in coupon_text:
|
||||
coupon_verified = True
|
||||
except Exception as e:
|
||||
coupon_elapsed = (time.time() - step_ocr_verify_start) * 1000
|
||||
log_fn(
|
||||
"步骤3[OCR确认弹窗]-优惠券异常",
|
||||
耗时毫秒=coupon_elapsed,
|
||||
异常类型=type(e).__name__,
|
||||
异常信息=str(e),
|
||||
)
|
||||
|
||||
if coupon_verified:
|
||||
total_elapsed = (time.time() - diag_start) * 1000
|
||||
coupon_elapsed = (time.time() - step_ocr_verify_start) * 1000
|
||||
log_summary_fn(
|
||||
"阶段诊断完成(优惠券弹窗-OCR确认)",
|
||||
阶段="COUPON_POPUP",
|
||||
原因="像素检测到背景+OCR确认(开心收下)",
|
||||
总耗时毫秒=total_elapsed,
|
||||
优惠券检测耗时毫秒=coupon_elapsed,
|
||||
)
|
||||
return "COUPON_POPUP", "OCR确认(开心收下)"
|
||||
|
||||
# 检测购物红包弹窗
|
||||
if pixel_state.get("has_redpacket_bg", False):
|
||||
redpacket_verified = False
|
||||
try:
|
||||
if time.time() - step_ocr_verify_start < max_step_time:
|
||||
cut_start = time.time()
|
||||
analyser.cut_pic(*SHOPPING_REDPACKET_AREA, "", "診斷購物紅包")
|
||||
cut_elapsed = (time.time() - cut_start) * 1000
|
||||
log_fn("步骤3[OCR确认弹窗]-购物红包截图完成", 耗时毫秒=cut_elapsed)
|
||||
|
||||
if time.time() - step_ocr_verify_start < max_step_time:
|
||||
ocr_start = time.time()
|
||||
name_rp2 = preprocess_for_ocr(analyser, "診斷購物紅包")
|
||||
redpacket_text = str(
|
||||
analyser.operation.analyse_pic_word(name_rp2, 0) or ""
|
||||
)
|
||||
ocr_elapsed = (time.time() - ocr_start) * 1000
|
||||
log_fn(
|
||||
"步骤3[OCR确认弹窗]-购物红包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_verified = True
|
||||
except Exception as e:
|
||||
redpacket_elapsed = (time.time() - step_ocr_verify_start) * 1000
|
||||
log_fn(
|
||||
"步骤3[OCR确认弹窗]-购物红包异常",
|
||||
耗时毫秒=redpacket_elapsed,
|
||||
异常类型=type(e).__name__,
|
||||
异常信息=str(e),
|
||||
)
|
||||
|
||||
if redpacket_verified:
|
||||
total_elapsed = (time.time() - diag_start) * 1000
|
||||
redpacket_elapsed = (time.time() - step_ocr_verify_start) * 1000
|
||||
log_summary_fn(
|
||||
"阶段诊断完成(购物红包弹窗-OCR确认)",
|
||||
阶段="SHOPPING_REDPACKET_POPUP",
|
||||
原因="像素检测到背景+OCR确认(购物红包)",
|
||||
总耗时毫秒=total_elapsed,
|
||||
红包检测耗时毫秒=redpacket_elapsed,
|
||||
)
|
||||
return "SHOPPING_REDPACKET_POPUP", "OCR确认(购物红包弹窗)"
|
||||
|
||||
# OCR确认后仍未匹配到弹窗,说明是直播间背景颜色误判
|
||||
ocr_verify_elapsed = (time.time() - step_ocr_verify_start) * 1000
|
||||
log_fn(
|
||||
"步骤3[OCR确认弹窗]-未匹配到弹窗",
|
||||
耗时毫秒=ocr_verify_elapsed,
|
||||
说明="OCR确认后未匹配,判定为直播间背景颜色",
|
||||
)
|
||||
|
||||
# 步骤3结束计时(OCR确认弹窗)
|
||||
step3_ocr_elapsed = (time.time() - step_ocr_verify_start) * 1000
|
||||
log_fn("步骤3[OCR确认弹窗]-完成", 总耗时毫秒=step3_ocr_elapsed)
|
||||
return None
|
||||
Reference in new issue
Block a user