refactor(stage): 拆出可疑弹窗 OCR 二次确认(814→676,-138 行)
把 douyin_stage 末尾"像素检测到弹窗背景但未匹配 → OCR 二次确认"的 156 行块抽到 detect_suspicious_popup_via_ocr: - _detect_coupon_popup_via_ocr:优惠券弹窗 OCR 确认(含并行 mode 0+1) - _detect_redpacket_popup_via_ocr_simple:购物红包简单 OCR 确认(仅 mode 0) 新增 4 个单元测试覆盖:无背景 / 优惠券命中 / 红包命中 / 关键词未匹配。 1339 → 1343 用例全过。 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
9423ddbfef
commit
30a66e955c
3 files changed
+281
-154
No files matched your search
+16
-154
@@ -19,6 +19,7 @@ from douyin.stage.douyin_stage_helpers import (
|
||||
detect_recommend_follow_live_tabs,
|
||||
detect_redpacket_ocr_recheck_outside_live,
|
||||
detect_shopping_redpacket_popup,
|
||||
detect_suspicious_popup_via_ocr,
|
||||
log_pixel_detection_details,
|
||||
summarize_pixel_state,
|
||||
)
|
||||
@@ -631,160 +632,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)
|
||||
|
||||
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)
|
||||
matched, stage, reason = detect_suspicious_popup_via_ocr(
|
||||
analyser,
|
||||
pixel_state,
|
||||
COUPON_PARTICIPATE_BUTTON_ALT,
|
||||
SHOPPING_REDPACKET_AREA,
|
||||
diag_start,
|
||||
max_diag_time,
|
||||
max_step_time,
|
||||
enable_ocr_mode1,
|
||||
preprocess_for_ocr,
|
||||
_log,
|
||||
_log_summary,
|
||||
)
|
||||
if matched:
|
||||
return stage, reason
|
||||
|
||||
# 如果识别到在直播间,并且其他阶段都没识别到,返回在直播间阶段
|
||||
if in_live_room:
|
||||
|
||||
@@ -142,6 +142,214 @@ def _ocr_with_dual_modes(
|
||||
return text
|
||||
|
||||
|
||||
def _detect_coupon_popup_via_ocr(
|
||||
analyser: Any,
|
||||
coupon_button_area: tuple,
|
||||
step_ocr_verify_start: float,
|
||||
max_step_time: float,
|
||||
enable_ocr_mode1: bool,
|
||||
preprocess_for_ocr: Callable[[Any, str], str],
|
||||
log_callback: Callable[..., None],
|
||||
) -> bool:
|
||||
"""OCR 确认优惠券弹窗("开心收下" 关键词)。返回是否命中。"""
|
||||
import time
|
||||
|
||||
coupon_verified = False
|
||||
try:
|
||||
if time.time() - step_ocr_verify_start >= max_step_time:
|
||||
return False
|
||||
|
||||
cut_start = time.time()
|
||||
analyser.cut_pic(*coupon_button_area, "", "参与按钮")
|
||||
cut_elapsed = (time.time() - cut_start) * 1000
|
||||
log_callback("步骤3[OCR确认弹窗]-优惠券截图完成", 耗时毫秒=cut_elapsed)
|
||||
|
||||
if time.time() - step_ocr_verify_start >= max_step_time:
|
||||
return False
|
||||
|
||||
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_callback(
|
||||
"步骤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_callback(
|
||||
"步骤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_callback(
|
||||
"步骤3[OCR确认弹窗]-优惠券异常",
|
||||
耗时毫秒=coupon_elapsed,
|
||||
异常类型=type(e).__name__,
|
||||
异常信息=str(e),
|
||||
)
|
||||
return coupon_verified
|
||||
|
||||
|
||||
def _detect_redpacket_popup_via_ocr_simple(
|
||||
analyser: Any,
|
||||
shopping_redpacket_area: tuple,
|
||||
step_ocr_verify_start: float,
|
||||
max_step_time: float,
|
||||
preprocess_for_ocr: Callable[[Any, str], str],
|
||||
log_callback: Callable[..., None],
|
||||
) -> bool:
|
||||
"""OCR 简单确认购物红包弹窗(仅 mode 0)。返回是否命中。"""
|
||||
import time
|
||||
|
||||
redpacket_verified = False
|
||||
try:
|
||||
if time.time() - step_ocr_verify_start >= max_step_time:
|
||||
return False
|
||||
|
||||
cut_start = time.time()
|
||||
analyser.cut_pic(*shopping_redpacket_area, "", "診斷購物紅包")
|
||||
cut_elapsed = (time.time() - cut_start) * 1000
|
||||
log_callback("步骤3[OCR确认弹窗]-购物红包截图完成", 耗时毫秒=cut_elapsed)
|
||||
|
||||
if time.time() - step_ocr_verify_start >= max_step_time:
|
||||
return False
|
||||
|
||||
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_callback(
|
||||
"步骤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_callback(
|
||||
"步骤3[OCR确认弹窗]-购物红包异常",
|
||||
耗时毫秒=redpacket_elapsed,
|
||||
异常类型=type(e).__name__,
|
||||
异常信息=str(e),
|
||||
)
|
||||
return redpacket_verified
|
||||
|
||||
|
||||
def detect_suspicious_popup_via_ocr(
|
||||
analyser: Any,
|
||||
pixel_state: Dict[str, Any],
|
||||
coupon_button_area: tuple,
|
||||
shopping_redpacket_area: tuple,
|
||||
diag_start: float,
|
||||
max_diag_time: float,
|
||||
max_step_time: float,
|
||||
enable_ocr_mode1: bool,
|
||||
preprocess_for_ocr: Callable[[Any, str], str],
|
||||
log_callback: Callable[..., None],
|
||||
log_summary_callback: Callable[..., None],
|
||||
) -> tuple[bool, str, str]:
|
||||
"""像素检测到弹窗背景但未匹配到弹窗阶段时,OCR 二次确认。
|
||||
|
||||
返回 (matched, stage, reason)。matched=True 时主流程应直接 return。
|
||||
"""
|
||||
import time
|
||||
|
||||
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 False, "", ""
|
||||
|
||||
step_ocr_verify_start = time.time()
|
||||
remaining_time_before = max_diag_time - (time.time() - diag_start)
|
||||
log_callback(
|
||||
"步骤3[OCR确认弹窗]-开始",
|
||||
剩余时间秒=remaining_time_before,
|
||||
说明="像素检测到弹窗背景,但未匹配到弹窗阶段,OCR再次确认",
|
||||
)
|
||||
|
||||
# 优惠券弹窗确认
|
||||
if pixel_state.get("has_coupon_bg", False):
|
||||
if _detect_coupon_popup_via_ocr(
|
||||
analyser, coupon_button_area, step_ocr_verify_start,
|
||||
max_step_time, enable_ocr_mode1, preprocess_for_ocr, log_callback,
|
||||
):
|
||||
total_elapsed = (time.time() - diag_start) * 1000
|
||||
coupon_elapsed = (time.time() - step_ocr_verify_start) * 1000
|
||||
log_summary_callback(
|
||||
"阶段诊断完成(优惠券弹窗-OCR确认)",
|
||||
阶段="COUPON_POPUP",
|
||||
原因="像素检测到背景+OCR确认(开心收下)",
|
||||
总耗时毫秒=total_elapsed,
|
||||
优惠券检测耗时毫秒=coupon_elapsed,
|
||||
)
|
||||
return True, "COUPON_POPUP", "OCR确认(开心收下)"
|
||||
|
||||
# 购物红包弹窗确认
|
||||
if pixel_state.get("has_redpacket_bg", False):
|
||||
if _detect_redpacket_popup_via_ocr_simple(
|
||||
analyser, shopping_redpacket_area, step_ocr_verify_start,
|
||||
max_step_time, preprocess_for_ocr, log_callback,
|
||||
):
|
||||
total_elapsed = (time.time() - diag_start) * 1000
|
||||
redpacket_elapsed = (time.time() - step_ocr_verify_start) * 1000
|
||||
log_summary_callback(
|
||||
"阶段诊断完成(购物红包弹窗-OCR确认)",
|
||||
阶段="SHOPPING_REDPACKET_POPUP",
|
||||
原因="像素检测到背景+OCR确认(购物红包)",
|
||||
总耗时毫秒=total_elapsed,
|
||||
红包检测耗时毫秒=redpacket_elapsed,
|
||||
)
|
||||
return True, "SHOPPING_REDPACKET_POPUP", "OCR确认(购物红包弹窗)"
|
||||
|
||||
# OCR确认后仍未匹配到弹窗,说明是直播间背景颜色误判
|
||||
ocr_verify_elapsed = (time.time() - step_ocr_verify_start) * 1000
|
||||
log_callback(
|
||||
"步骤3[OCR确认弹窗]-未匹配到弹窗",
|
||||
耗时毫秒=ocr_verify_elapsed,
|
||||
说明="OCR确认后未匹配,判定为直播间背景颜色",
|
||||
)
|
||||
log_callback(
|
||||
"步骤3[OCR确认弹窗]-完成", 总耗时毫秒=ocr_verify_elapsed
|
||||
)
|
||||
return False, "", ""
|
||||
|
||||
|
||||
def detect_recommend_follow_live_tabs(
|
||||
analyser: Any,
|
||||
recommend_tab_area: tuple,
|
||||
|
||||
@@ -352,3 +352,60 @@ def test_recommend_follow_live_tabs_outer_exception_returns_false():
|
||||
lambda _a, name: name, log, log_sum, log_sep,
|
||||
)
|
||||
assert matched is False
|
||||
|
||||
|
||||
# ---- detect_suspicious_popup_via_ocr ---- #
|
||||
|
||||
def test_suspicious_popup_no_pixel_returns_false():
|
||||
"""has_coupon_bg=False 且 has_redpacket_bg=False → 直接返回 False"""
|
||||
a = MagicMock()
|
||||
matched, _, _ = h.detect_suspicious_popup_via_ocr(
|
||||
a, {"has_coupon_bg": False, "has_redpacket_bg": False},
|
||||
(1, 2, 3, 4), (5, 6, 7, 8),
|
||||
_time.time(), 30.0, 5.0, False,
|
||||
lambda _a, name: name,
|
||||
MagicMock(), MagicMock(),
|
||||
)
|
||||
assert matched is False
|
||||
|
||||
|
||||
def test_suspicious_popup_coupon_match():
|
||||
"""coupon_bg=True + OCR 命中"开心收下" → 返回 COUPON_POPUP"""
|
||||
a = MagicMock()
|
||||
a.operation.analyse_pic_word.return_value = "请开心收下你的红包"
|
||||
matched, stage, _ = h.detect_suspicious_popup_via_ocr(
|
||||
a, {"has_coupon_bg": True, "has_redpacket_bg": False},
|
||||
(1, 2, 3, 4), (5, 6, 7, 8),
|
||||
_time.time(), 30.0, 5.0, False, # enable_ocr_mode1=False 走简单路径
|
||||
lambda _a, name: name,
|
||||
MagicMock(), MagicMock(),
|
||||
)
|
||||
assert matched is True
|
||||
assert stage == "COUPON_POPUP"
|
||||
|
||||
|
||||
def test_suspicious_popup_redpacket_match():
|
||||
a = MagicMock()
|
||||
a.operation.analyse_pic_word.return_value = "邀你抢红包"
|
||||
matched, stage, _ = h.detect_suspicious_popup_via_ocr(
|
||||
a, {"has_coupon_bg": False, "has_redpacket_bg": True},
|
||||
(1, 2, 3, 4), (5, 6, 7, 8),
|
||||
_time.time(), 30.0, 5.0, False,
|
||||
lambda _a, name: name,
|
||||
MagicMock(), MagicMock(),
|
||||
)
|
||||
assert matched is True
|
||||
assert stage == "SHOPPING_REDPACKET_POPUP"
|
||||
|
||||
|
||||
def test_suspicious_popup_no_keyword_returns_false():
|
||||
a = MagicMock()
|
||||
a.operation.analyse_pic_word.return_value = "无关文本"
|
||||
matched, _, _ = h.detect_suspicious_popup_via_ocr(
|
||||
a, {"has_coupon_bg": True, "has_redpacket_bg": True},
|
||||
(1, 2, 3, 4), (5, 6, 7, 8),
|
||||
_time.time(), 30.0, 5.0, False,
|
||||
lambda _a, name: name,
|
||||
MagicMock(), MagicMock(),
|
||||
)
|
||||
assert matched is False
|
||||
Reference in new issue
Block a user