refactor(stage): 拆出"非直播间购物红包 OCR 复查"块(1204→1066,-138 行)
把 douyin_stage 步骤2.5 中的"非直播间购物红包 OCR 复查"逻辑(含 mode0+mode1 双 OCR、置信度兜底、154 行)抽到 detect_redpacket_ocr_recheck_outside_live。 新增 5 个单元测试(无背景跳过 / 关键词命中 / 高置信度命中 / 无匹配 / 异常吞噬)。 1279 → 1284 用例全过。 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
4cb5daa79e
commit
c2654d60aa
3 files changed
+267
-153
No files matched your search
+15
-153
@@ -16,6 +16,7 @@ from douyin.fudai.core.coords import (
|
||||
)
|
||||
from douyin.stage.douyin_stage_helpers import (
|
||||
detect_lottery_result_popup,
|
||||
detect_redpacket_ocr_recheck_outside_live,
|
||||
detect_shopping_redpacket_popup,
|
||||
log_pixel_detection_details,
|
||||
summarize_pixel_state,
|
||||
@@ -239,159 +240,20 @@ 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
|
||||
|
||||
_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)
|
||||
matched, stage, reason = detect_redpacket_ocr_recheck_outside_live(
|
||||
analyser,
|
||||
pixel_state,
|
||||
pixel_details,
|
||||
SHOPPING_REDPACKET_AREA,
|
||||
diag_start,
|
||||
max_step_time,
|
||||
enable_ocr_mode1,
|
||||
preprocess_for_ocr,
|
||||
_log,
|
||||
_log_summary,
|
||||
)
|
||||
if matched:
|
||||
return stage, reason
|
||||
|
||||
# 步骤3:检测推荐页面、关注列表、直播列表(OCR标签栏,整合在一起检测)
|
||||
step_start = time.time()
|
||||
|
||||
@@ -89,6 +89,184 @@ def _format_detail_log_fields(key: str, detail: Dict[str, Any], analyser: Any) -
|
||||
return log_fields
|
||||
|
||||
|
||||
def detect_redpacket_ocr_recheck_outside_live(
|
||||
analyser: Any,
|
||||
pixel_state: Dict[str, Any],
|
||||
pixel_details: Dict[str, Any],
|
||||
shopping_redpacket_area: tuple,
|
||||
diag_start: 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]:
|
||||
"""步骤2.5:不在直播间时的"购物红包"像素 + OCR 复查(含 mode0 / mode1 双 OCR + 置信度兜底)。
|
||||
|
||||
若 ``pixel_state.has_redpacket_bg`` 为 False,直接返回 (False, "", "")。
|
||||
返回 (matched, stage, reason):matched=True 时主流程应直接 return。
|
||||
"""
|
||||
import time
|
||||
|
||||
if not pixel_state.get("has_redpacket_bg", False):
|
||||
return 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
|
||||
|
||||
log_callback(
|
||||
"步骤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_callback(
|
||||
"步骤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_callback(
|
||||
"步骤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_callback(
|
||||
"步骤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_callback(
|
||||
"步骤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_callback(
|
||||
"步骤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_callback(
|
||||
"阶段诊断完成(购物红包弹窗-非直播间复查)",
|
||||
阶段="SHOPPING_REDPACKET_POPUP",
|
||||
原因="像素检测到背景+OCR确认(不在直播间)",
|
||||
总耗时毫秒=total_elapsed,
|
||||
红包检测耗时毫秒=redpacket_elapsed,
|
||||
)
|
||||
return True, "SHOPPING_REDPACKET_POPUP", "像素检测+OCR确认(不在直播间)"
|
||||
|
||||
redpacket_elapsed = (time.time() - step_redpacket_ocr_start) * 1000
|
||||
log_callback("步骤2.5[购物红包OCR复查]-未命中", 耗时毫秒=redpacket_elapsed)
|
||||
return False, "", ""
|
||||
|
||||
|
||||
def detect_lottery_result_popup(
|
||||
analyser: Any,
|
||||
diag_start: float,
|
||||
|
||||
@@ -200,3 +200,77 @@ def test_detect_shopping_redpacket_ocr_exception_returns_false():
|
||||
lambda _x, _y: "p", log, MagicMock(), MagicMock(),
|
||||
)
|
||||
assert matched is False
|
||||
|
||||
|
||||
# ---- detect_redpacket_ocr_recheck_outside_live ---- #
|
||||
|
||||
def test_redpacket_recheck_no_pixel_bg_returns_false():
|
||||
"""像素未命中红包背景 → 直接返回 False,不做 OCR"""
|
||||
a = MagicMock()
|
||||
log = MagicMock()
|
||||
matched, _, _ = h.detect_redpacket_ocr_recheck_outside_live(
|
||||
a, {"has_redpacket_bg": False}, {},
|
||||
(10, 20, 30, 40), _time.time(), 5.0, True,
|
||||
lambda _x, _y: "p", log, MagicMock(),
|
||||
)
|
||||
assert matched is False
|
||||
a.cut_pic.assert_not_called()
|
||||
|
||||
|
||||
def test_redpacket_recheck_keyword_match_returns_true():
|
||||
a = MagicMock()
|
||||
a.operation.analyse_pic_word.return_value = ("邀你抢红包", None)
|
||||
a.shopping_redpacket_ocr_confidence_threshold = 0.99
|
||||
log = MagicMock()
|
||||
matched, stage, _ = h.detect_redpacket_ocr_recheck_outside_live(
|
||||
a, {"has_redpacket_bg": True}, {"has_redpacket_bg": {}},
|
||||
(10, 20, 30, 40), _time.time(), 30.0, True,
|
||||
lambda _x, _y: "p", log, MagicMock(),
|
||||
)
|
||||
assert matched is True
|
||||
assert stage == "SHOPPING_REDPACKET_POPUP"
|
||||
|
||||
|
||||
def test_redpacket_recheck_high_confidence_match():
|
||||
"""无关键词但置信度高 → 命中"""
|
||||
a = MagicMock()
|
||||
a.operation.analyse_pic_word.return_value = (
|
||||
"无关文本",
|
||||
{"avg_confidence": 0.9, "max_confidence": 0.95},
|
||||
)
|
||||
a.shopping_redpacket_ocr_confidence_threshold = 0.9
|
||||
log = MagicMock()
|
||||
matched, _, _ = h.detect_redpacket_ocr_recheck_outside_live(
|
||||
a, {"has_redpacket_bg": True}, {"has_redpacket_bg": {}},
|
||||
(10, 20, 30, 40), _time.time(), 30.0, False,
|
||||
lambda _x, _y: "p", log, MagicMock(),
|
||||
)
|
||||
assert matched is True
|
||||
|
||||
|
||||
def test_redpacket_recheck_no_match_returns_false():
|
||||
a = MagicMock()
|
||||
a.operation.analyse_pic_word.return_value = (
|
||||
"无关文本",
|
||||
{"avg_confidence": 0.3, "max_confidence": 0.4},
|
||||
)
|
||||
a.shopping_redpacket_ocr_confidence_threshold = 0.9
|
||||
log = MagicMock()
|
||||
matched, _, _ = h.detect_redpacket_ocr_recheck_outside_live(
|
||||
a, {"has_redpacket_bg": True}, {"has_redpacket_bg": {}},
|
||||
(10, 20, 30, 40), _time.time(), 30.0, False,
|
||||
lambda _x, _y: "p", log, MagicMock(),
|
||||
)
|
||||
assert matched is False
|
||||
|
||||
|
||||
def test_redpacket_recheck_exception_returns_false():
|
||||
a = MagicMock()
|
||||
a.cut_pic.side_effect = RuntimeError("adb")
|
||||
log = MagicMock()
|
||||
matched, _, _ = h.detect_redpacket_ocr_recheck_outside_live(
|
||||
a, {"has_redpacket_bg": True}, {"has_redpacket_bg": {}},
|
||||
(10, 20, 30, 40), _time.time(), 30.0, True,
|
||||
lambda _x, _y: "p", log, MagicMock(),
|
||||
)
|
||||
assert matched is False
|
||||
Reference in new issue
Block a user