refactor(stage): 提取"步骤2.5 购物红包OCR复查"块到 verify_redpacket_outside_live_room
将 douyin_stage.py 中"不在直播间但像素检测到购物红包背景"的 OCR 复查 独立块(约 153 行)抽到 douyin_stage_helpers.verify_redpacket_outside_live_room。 逻辑: - 截图 SHOPPING_REDPACKET_AREA - OCR 模式0 → 关键词未命中且 enable_ocr_mode1 → 跑模式1 - 关键词匹配 OR 置信度 ≥ shopping_redpacket_ocr_confidence_threshold → 判定 依赖通过参数注入:pixel_state、pixel_details、enable_ocr_mode1、 max_step_time、diag_start、_log/_log_summary、preprocess_for_ocr。 douyin_stage.py: 1247 → 1111 行 (-136) douyin_stage_helpers.py: 199 → 383 行 验证: - pytest 1013 passed - compile OK Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
331860ee5c
commit
4e89fadd2e
2 files changed
+204
-156
No files matched your search
+16
-152
@@ -338,159 +338,23 @@ def diagnose_stage_from_current_screen(analyser, trigger_reason="unknown"): # t
|
||||
_log("不在直播间,执行步骤3(OCR检测推荐页/关注列表/直播列表)")
|
||||
|
||||
# 购物红包像素命中:不在直播间也必须进行OCR复查
|
||||
if pixel_state.get("has_redpacket_bg", False):
|
||||
step_redpacket_ocr_start = time.time()
|
||||
red_ratio = None
|
||||
red_threshold = None
|
||||
try:
|
||||
redpacket_detail = (
|
||||
pixel_details.get("has_redpacket_bg", {})
|
||||
if isinstance(pixel_details, dict)
|
||||
else {}
|
||||
)
|
||||
red_ratio = redpacket_detail.get("red_ratio")
|
||||
red_threshold = redpacket_detail.get("effective_threshold")
|
||||
except Exception:
|
||||
red_ratio = None
|
||||
red_threshold = None
|
||||
from douyin.stage.douyin_stage_helpers import (
|
||||
verify_redpacket_outside_live_room,
|
||||
)
|
||||
|
||||
_log(
|
||||
"步骤2.5[购物红包OCR复查]-开始",
|
||||
说明="不在直播间也执行红包OCR复查",
|
||||
红色比例=(
|
||||
round(float(red_ratio), 4) if red_ratio is not None else None
|
||||
),
|
||||
阈值=(
|
||||
round(float(red_threshold), 4)
|
||||
if red_threshold is not None
|
||||
else None
|
||||
),
|
||||
)
|
||||
|
||||
redpacket_verified = False
|
||||
try:
|
||||
if time.time() - step_redpacket_ocr_start < max_step_time:
|
||||
cut_start = time.time()
|
||||
analyser.cut_pic(
|
||||
*SHOPPING_REDPACKET_AREA,
|
||||
"",
|
||||
"診斷購物紅包-非直播间复查",
|
||||
)
|
||||
cut_elapsed = (time.time() - cut_start) * 1000
|
||||
_log("步骤2.5[购物红包OCR复查]-截图完成", 耗时毫秒=cut_elapsed)
|
||||
|
||||
if time.time() - step_redpacket_ocr_start < max_step_time:
|
||||
ocr_start = time.time()
|
||||
name_rp0 = preprocess_for_ocr(
|
||||
analyser, "診斷購物紅包-非直播间复查"
|
||||
)
|
||||
ocr_result = analyser.operation.analyse_pic_word(
|
||||
name_rp0, 0, return_confidence=True
|
||||
)
|
||||
if isinstance(ocr_result, tuple):
|
||||
redpacket_text, ocr_confidence_info = ocr_result
|
||||
else:
|
||||
redpacket_text = str(ocr_result or "")
|
||||
ocr_confidence_info = None
|
||||
ocr_elapsed = (time.time() - ocr_start) * 1000
|
||||
_log(
|
||||
"步骤2.5[购物红包OCR复查]-OCR模式0",
|
||||
耗时毫秒=ocr_elapsed,
|
||||
文本长度=len(redpacket_text),
|
||||
文本预览=redpacket_text[:50] if redpacket_text else "",
|
||||
平均置信度=(
|
||||
ocr_confidence_info.get("avg_confidence")
|
||||
if ocr_confidence_info
|
||||
else None
|
||||
),
|
||||
最大置信度=(
|
||||
ocr_confidence_info.get("max_confidence")
|
||||
if ocr_confidence_info
|
||||
else None
|
||||
),
|
||||
)
|
||||
|
||||
redpacket_keywords = ("购物红包", "邀你抢", "抢红包")
|
||||
if enable_ocr_mode1 and (
|
||||
not redpacket_text
|
||||
or not any(
|
||||
kw in redpacket_text for kw in redpacket_keywords
|
||||
)
|
||||
):
|
||||
if time.time() - step_redpacket_ocr_start < max_step_time:
|
||||
ocr_start_1 = time.time()
|
||||
name_rp1 = preprocess_for_ocr(
|
||||
analyser, "診斷購物紅包-非直播间复查"
|
||||
)
|
||||
ocr_result_1 = analyser.operation.analyse_pic_word(
|
||||
name_rp1, 1, return_confidence=True
|
||||
)
|
||||
if isinstance(ocr_result_1, tuple):
|
||||
redpacket_text, ocr_confidence_info = ocr_result_1
|
||||
else:
|
||||
redpacket_text = str(ocr_result_1 or "")
|
||||
ocr_confidence_info = None
|
||||
ocr_elapsed_1 = (time.time() - ocr_start_1) * 1000
|
||||
_log(
|
||||
"步骤2.5[购物红包OCR复查]-OCR模式1",
|
||||
耗时毫秒=ocr_elapsed_1,
|
||||
文本长度=len(redpacket_text),
|
||||
文本预览=(
|
||||
redpacket_text[:50] if redpacket_text else ""
|
||||
),
|
||||
平均置信度=(
|
||||
ocr_confidence_info.get("avg_confidence")
|
||||
if ocr_confidence_info
|
||||
else None
|
||||
),
|
||||
最大置信度=(
|
||||
ocr_confidence_info.get("max_confidence")
|
||||
if ocr_confidence_info
|
||||
else None
|
||||
),
|
||||
)
|
||||
|
||||
# 检查关键词匹配或置信度阈值
|
||||
has_keyword_match = redpacket_text and any(
|
||||
kw in redpacket_text for kw in redpacket_keywords
|
||||
)
|
||||
has_high_confidence = (
|
||||
ocr_confidence_info
|
||||
and ocr_confidence_info.get("max_confidence", 0)
|
||||
>= analyser.shopping_redpacket_ocr_confidence_threshold
|
||||
)
|
||||
|
||||
if has_keyword_match or has_high_confidence:
|
||||
redpacket_verified = True
|
||||
_log(
|
||||
"步骤2.5[购物红包OCR复查]-命中",
|
||||
关键词匹配=has_keyword_match,
|
||||
高置信度匹配=has_high_confidence,
|
||||
阈值=analyser.shopping_redpacket_ocr_confidence_threshold,
|
||||
)
|
||||
except Exception as e:
|
||||
redpacket_elapsed = (time.time() - step_redpacket_ocr_start) * 1000
|
||||
_log(
|
||||
"步骤2.5[购物红包OCR复查]-异常",
|
||||
耗时毫秒=redpacket_elapsed,
|
||||
异常类型=type(e).__name__,
|
||||
异常信息=str(e),
|
||||
)
|
||||
|
||||
if redpacket_verified:
|
||||
total_elapsed = (time.time() - diag_start) * 1000
|
||||
redpacket_elapsed = (time.time() - step_redpacket_ocr_start) * 1000
|
||||
_log_summary(
|
||||
"阶段诊断完成(购物红包弹窗-非直播间复查)",
|
||||
阶段="SHOPPING_REDPACKET_POPUP",
|
||||
原因="像素检测到背景+OCR确认(不在直播间)",
|
||||
总耗时毫秒=total_elapsed,
|
||||
红包检测耗时毫秒=redpacket_elapsed,
|
||||
)
|
||||
return "SHOPPING_REDPACKET_POPUP", "像素检测+OCR确认(不在直播间)"
|
||||
else:
|
||||
redpacket_elapsed = (time.time() - step_redpacket_ocr_start) * 1000
|
||||
_log("步骤2.5[购物红包OCR复查]-未命中", 耗时毫秒=redpacket_elapsed)
|
||||
redpacket_result = verify_redpacket_outside_live_room(
|
||||
analyser,
|
||||
pixel_state,
|
||||
pixel_details,
|
||||
enable_ocr_mode1,
|
||||
max_step_time,
|
||||
diag_start,
|
||||
_log,
|
||||
_log_summary,
|
||||
preprocess_for_ocr,
|
||||
)
|
||||
if redpacket_result is not None:
|
||||
return redpacket_result
|
||||
|
||||
# 步骤3:检测推荐页面、关注列表、直播列表(OCR标签栏,整合在一起检测)
|
||||
step_start = time.time()
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
"""阶段诊断辅助:步骤3 OCR弹窗确认
|
||||
"""阶段诊断辅助:步骤3 OCR弹窗确认 + 步骤2.5 购物红包OCR复查
|
||||
|
||||
从 douyin/stage/douyin_stage.py:diagnose_stage_from_current_screen 中提取的
|
||||
"步骤3 OCR确认弹窗"块。当所有像素检测都未命中特定阶段,但像素检测到弹窗
|
||||
背景特征(优惠券/购物红包)时,用 OCR 再次确认是否为弹窗,避免直播间
|
||||
背景色误判。
|
||||
两个相对独立的 OCR 复查块:
|
||||
|
||||
- verify_popup_via_ocr:步骤3,所有像素检测都未命中特定阶段、但像素检测到弹窗
|
||||
背景特征(优惠券/购物红包)时,OCR 二次确认是否为弹窗。
|
||||
- verify_redpacket_outside_live_room:步骤2.5,不在直播间但检测到购物红包背景
|
||||
时,OCR 复查(支持模式0/模式1,置信度阈值)。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
@@ -15,6 +18,187 @@ from douyin.fudai.core.coords import (
|
||||
)
|
||||
|
||||
|
||||
def verify_redpacket_outside_live_room(
|
||||
analyser,
|
||||
pixel_state: dict,
|
||||
pixel_details: dict,
|
||||
enable_ocr_mode1: bool,
|
||||
max_step_time: float,
|
||||
diag_start: float,
|
||||
log_fn,
|
||||
log_summary_fn,
|
||||
preprocess_for_ocr,
|
||||
):
|
||||
"""步骤2.5:不在直播间,但像素检测到购物红包背景时的 OCR 复查。
|
||||
|
||||
Args:
|
||||
analyser: fudai_analyse 实例
|
||||
pixel_state: 像素检测结果字典
|
||||
pixel_details: 像素检测详细信息
|
||||
enable_ocr_mode1: 是否启用 OCR 模式1(关键词未命中时再跑模式1)
|
||||
max_step_time: 单步最大耗时(秒)
|
||||
diag_start: 阶段诊断起始时间戳
|
||||
log_fn: 详细日志函数(_log)
|
||||
log_summary_fn: 概要日志函数(_log_summary)
|
||||
preprocess_for_ocr: OCR 预处理函数
|
||||
|
||||
Returns:
|
||||
- (stage, reason) 二元组:检测到购物红包弹窗时
|
||||
- None:未检测到(调用方继续后续逻辑)
|
||||
"""
|
||||
if not pixel_state.get("has_redpacket_bg", False):
|
||||
return None
|
||||
|
||||
step_redpacket_ocr_start = time.time()
|
||||
red_ratio = None
|
||||
red_threshold = None
|
||||
try:
|
||||
redpacket_detail = (
|
||||
pixel_details.get("has_redpacket_bg", {})
|
||||
if isinstance(pixel_details, dict)
|
||||
else {}
|
||||
)
|
||||
red_ratio = redpacket_detail.get("red_ratio")
|
||||
red_threshold = redpacket_detail.get("effective_threshold")
|
||||
except Exception:
|
||||
red_ratio = None
|
||||
red_threshold = None
|
||||
|
||||
log_fn(
|
||||
"步骤2.5[购物红包OCR复查]-开始",
|
||||
说明="不在直播间也执行红包OCR复查",
|
||||
红色比例=(round(float(red_ratio), 4) if red_ratio is not None else None),
|
||||
阈值=(
|
||||
round(float(red_threshold), 4) if red_threshold is not None else None
|
||||
),
|
||||
)
|
||||
|
||||
redpacket_verified = False
|
||||
ocr_confidence_info = None
|
||||
try:
|
||||
if time.time() - step_redpacket_ocr_start < max_step_time:
|
||||
cut_start = time.time()
|
||||
analyser.cut_pic(
|
||||
*SHOPPING_REDPACKET_AREA,
|
||||
"",
|
||||
"診斷購物紅包-非直播间复查",
|
||||
)
|
||||
cut_elapsed = (time.time() - cut_start) * 1000
|
||||
log_fn("步骤2.5[购物红包OCR复查]-截图完成", 耗时毫秒=cut_elapsed)
|
||||
|
||||
if time.time() - step_redpacket_ocr_start < max_step_time:
|
||||
ocr_start = time.time()
|
||||
name_rp0 = preprocess_for_ocr(
|
||||
analyser, "診斷購物紅包-非直播间复查"
|
||||
)
|
||||
ocr_result = analyser.operation.analyse_pic_word(
|
||||
name_rp0, 0, return_confidence=True
|
||||
)
|
||||
if isinstance(ocr_result, tuple):
|
||||
redpacket_text, ocr_confidence_info = ocr_result
|
||||
else:
|
||||
redpacket_text = str(ocr_result or "")
|
||||
ocr_confidence_info = None
|
||||
ocr_elapsed = (time.time() - ocr_start) * 1000
|
||||
log_fn(
|
||||
"步骤2.5[购物红包OCR复查]-OCR模式0",
|
||||
耗时毫秒=ocr_elapsed,
|
||||
文本长度=len(redpacket_text),
|
||||
文本预览=redpacket_text[:50] if redpacket_text else "",
|
||||
平均置信度=(
|
||||
ocr_confidence_info.get("avg_confidence")
|
||||
if ocr_confidence_info
|
||||
else None
|
||||
),
|
||||
最大置信度=(
|
||||
ocr_confidence_info.get("max_confidence")
|
||||
if ocr_confidence_info
|
||||
else None
|
||||
),
|
||||
)
|
||||
|
||||
redpacket_keywords = ("购物红包", "邀你抢", "抢红包")
|
||||
if enable_ocr_mode1 and (
|
||||
not redpacket_text
|
||||
or not any(kw in redpacket_text for kw in redpacket_keywords)
|
||||
):
|
||||
if time.time() - step_redpacket_ocr_start < max_step_time:
|
||||
ocr_start_1 = time.time()
|
||||
name_rp1 = preprocess_for_ocr(
|
||||
analyser, "診斷購物紅包-非直播间复查"
|
||||
)
|
||||
ocr_result_1 = analyser.operation.analyse_pic_word(
|
||||
name_rp1, 1, return_confidence=True
|
||||
)
|
||||
if isinstance(ocr_result_1, tuple):
|
||||
redpacket_text, ocr_confidence_info = ocr_result_1
|
||||
else:
|
||||
redpacket_text = str(ocr_result_1 or "")
|
||||
ocr_confidence_info = None
|
||||
ocr_elapsed_1 = (time.time() - ocr_start_1) * 1000
|
||||
log_fn(
|
||||
"步骤2.5[购物红包OCR复查]-OCR模式1",
|
||||
耗时毫秒=ocr_elapsed_1,
|
||||
文本长度=len(redpacket_text),
|
||||
文本预览=(
|
||||
redpacket_text[:50] if redpacket_text else ""
|
||||
),
|
||||
平均置信度=(
|
||||
ocr_confidence_info.get("avg_confidence")
|
||||
if ocr_confidence_info
|
||||
else None
|
||||
),
|
||||
最大置信度=(
|
||||
ocr_confidence_info.get("max_confidence")
|
||||
if ocr_confidence_info
|
||||
else None
|
||||
),
|
||||
)
|
||||
|
||||
# 检查关键词匹配或置信度阈值
|
||||
has_keyword_match = redpacket_text and any(
|
||||
kw in redpacket_text for kw in redpacket_keywords
|
||||
)
|
||||
has_high_confidence = (
|
||||
ocr_confidence_info
|
||||
and ocr_confidence_info.get("max_confidence", 0)
|
||||
>= analyser.shopping_redpacket_ocr_confidence_threshold
|
||||
)
|
||||
|
||||
if has_keyword_match or has_high_confidence:
|
||||
redpacket_verified = True
|
||||
log_fn(
|
||||
"步骤2.5[购物红包OCR复查]-命中",
|
||||
关键词匹配=has_keyword_match,
|
||||
高置信度匹配=has_high_confidence,
|
||||
阈值=analyser.shopping_redpacket_ocr_confidence_threshold,
|
||||
)
|
||||
except Exception as e:
|
||||
redpacket_elapsed = (time.time() - step_redpacket_ocr_start) * 1000
|
||||
log_fn(
|
||||
"步骤2.5[购物红包OCR复查]-异常",
|
||||
耗时毫秒=redpacket_elapsed,
|
||||
异常类型=type(e).__name__,
|
||||
异常信息=str(e),
|
||||
)
|
||||
|
||||
if redpacket_verified:
|
||||
total_elapsed = (time.time() - diag_start) * 1000
|
||||
redpacket_elapsed = (time.time() - step_redpacket_ocr_start) * 1000
|
||||
log_summary_fn(
|
||||
"阶段诊断完成(购物红包弹窗-非直播间复查)",
|
||||
阶段="SHOPPING_REDPACKET_POPUP",
|
||||
原因="像素检测到背景+OCR确认(不在直播间)",
|
||||
总耗时毫秒=total_elapsed,
|
||||
红包检测耗时毫秒=redpacket_elapsed,
|
||||
)
|
||||
return "SHOPPING_REDPACKET_POPUP", "像素检测+OCR确认(不在直播间)"
|
||||
else:
|
||||
redpacket_elapsed = (time.time() - step_redpacket_ocr_start) * 1000
|
||||
log_fn("步骤2.5[购物红包OCR复查]-未命中", 耗时毫秒=redpacket_elapsed)
|
||||
return None
|
||||
|
||||
|
||||
def verify_popup_via_ocr(
|
||||
analyser,
|
||||
pixel_state: dict,
|
||||
|
||||
Reference in new issue
Block a user