refactor(stage): 抽 diagnose_stage_from_current_screen 中"步骤 1+1.5 福袋详情页/邀你抢红包"块到独立模块
将 313 行的"福袋详情页(任务数量像素检测)+ 邀你抢红包(OCR 检测)"块抽到
douyin_stage_step1.py:detect_fudai_detail_or_invite_redpacket。
逻辑:
- 从 pixel_details 提取任务数量并保存到 analyser._fudai_detail_task_count_from_stage
- has_fudai_detail=True → return ("FUDai_DETAIL", "任务数量检测确认")
- 否则在指定区域 OCR 检测"邀你抢红包"文字 → return ("FUDai_DETAIL", "OCR检测确认")
- 都未命中 → return None
依赖通过参数注入:analyser、pixel_state、pixel_details、trigger_reason、
max_step_time、diag_start、max_diag_time、log_fn/log_summary_fn/log_separator_fn、
preprocess_for_ocr。
douyin_stage.py: 757 → 462 行 (-295)
新增 douyin_stage_step1.py: 347 行
douyin_stage.py 跌破 500 行(1385 → 462,本会话累计削减 923 行 = -67%)
验证:
- pytest 1013 passed
- compile OK
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
cec4a2d692
commit
0bd71a5e76
2 files changed
+363
-311
No files matched your search
+16
-311
@@ -396,319 +396,24 @@ def diagnose_stage_from_current_screen(analyser, trigger_reason="unknown"): # t
|
||||
if lottery_or_redpacket_result is not None:
|
||||
return lottery_or_redpacket_result
|
||||
|
||||
# ========== 步骤1:像素检测福袋详情页(从并行检测结果获取)==========
|
||||
step1_fudai_start = time.time()
|
||||
# 从并行检测结果中获取福袋详情页检测结果(任务数量检测)
|
||||
# 注意:即使 has_fudai_detail 为 False,pixel_details 中可能仍然有任务数量信息
|
||||
detail_detected = bool(pixel_state.get("has_fudai_detail", False))
|
||||
detail_task_count = None
|
||||
# 步骤1+1.5:福袋详情页(像素检测)+ 邀你抢红包(OCR 检测)
|
||||
from douyin.stage.douyin_stage_step1 import detect_fudai_detail_or_invite_redpacket
|
||||
|
||||
# 尝试从 pixel_details 中获取任务数量(无论 has_fudai_detail 是否为 True)
|
||||
if "has_fudai_detail" in pixel_details:
|
||||
detail_task_count = pixel_details["has_fudai_detail"].get("task_count")
|
||||
# 保存任务数量到 analyser 对象,供后续读取详情时使用
|
||||
# 特殊处理:如果是点击验证触发的原因,总是保存检测结果(包括0和None)
|
||||
if trigger_reason == "find_and_open_fudai_detail_click_verification":
|
||||
try:
|
||||
analyser._fudai_detail_task_count_from_stage = detail_task_count
|
||||
try:
|
||||
_log( # type: ignore[misc]
|
||||
"阶段检测:设置任务数量到 analyser(点击验证)",
|
||||
任务数量=detail_task_count,
|
||||
触发原因=trigger_reason,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
# 如果任务数量 > 0,标记截图可以复用,避免OCR时重复截图
|
||||
if detail_task_count is not None and detail_task_count > 0:
|
||||
analyser._screenshot_ready_for_ocr = True
|
||||
try:
|
||||
_log( # type: ignore[misc]
|
||||
"标记截图可复用(任务数量>0)",
|
||||
任务数量=detail_task_count,
|
||||
screenshot_ready=True,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
except Exception:
|
||||
pass
|
||||
elif detail_task_count is not None and detail_task_count > 0:
|
||||
# 其他情况下,只有检测到有效的任务数量才保存
|
||||
try:
|
||||
analyser._fudai_detail_task_count_from_stage = detail_task_count
|
||||
try:
|
||||
_log( # type: ignore[misc]
|
||||
"阶段检测:设置任务数量到 analyser",
|
||||
任务数量=detail_task_count,
|
||||
触发原因=trigger_reason,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
# 如果任务数量 > 0,标记截图可以复用,避免OCR时重复截图
|
||||
if detail_task_count > 0:
|
||||
analyser._screenshot_ready_for_ocr = True
|
||||
try:
|
||||
_log( # type: ignore[misc]
|
||||
"标记截图可复用(任务数量>0)",
|
||||
任务数量=detail_task_count,
|
||||
screenshot_ready=True,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
except Exception:
|
||||
pass
|
||||
pass
|
||||
try:
|
||||
_log( # type: ignore[misc]
|
||||
"保存任务数量到 analyser 对象",
|
||||
任务数量=detail_task_count,
|
||||
has_fudai_detail=detail_detected,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 如果任务数量为 None,记录警告日志
|
||||
if detail_task_count is None:
|
||||
try:
|
||||
_log( # type: ignore[misc]
|
||||
"警告:任务数量为 None,无法保存到 analyser 对象",
|
||||
任务数量=None,
|
||||
has_fudai_detail=detail_detected,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
else:
|
||||
# pixel_details 中没有 has_fudai_detail 信息,记录警告
|
||||
try:
|
||||
_log( # type: ignore[misc]
|
||||
"警告:pixel_details 中没有 has_fudai_detail 信息",
|
||||
has_fudai_detail=detail_detected,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 如果并行检测到福袋详情页,直接返回
|
||||
if detail_detected:
|
||||
total_elapsed = (time.time() - diag_start) * 1000
|
||||
_log_summary(
|
||||
"阶段诊断完成(任务数量检测)",
|
||||
阶段="FUDai_DETAIL",
|
||||
原因="并行检测任务数量检测确认",
|
||||
任务数量=detail_task_count,
|
||||
总耗时毫秒=total_elapsed,
|
||||
)
|
||||
_log_separator("最终结果:FUDai_DETAIL(福袋详情页)")
|
||||
return "FUDai_DETAIL", f"任务数量检测确认(任务数量={detail_task_count})"
|
||||
|
||||
# 额外检测:在指定区域检测"邀你抢红包"文字(用于检测红包/福袋详情页)
|
||||
# 区域:(238,454) 到 (847,545) - 基准坐标(1080x2400)
|
||||
# 优化:当模板匹配分数很低(<0.4)时,说明明显不在福袋详情页,跳过OCR检测以节省时间
|
||||
# 暂时禁用:将默认值改为 False
|
||||
enable_invite_redpacket_ocr = bool(
|
||||
getattr(analyser, "enable_invite_redpacket_ocr", False)
|
||||
fudai_detail_result = detect_fudai_detail_or_invite_redpacket(
|
||||
analyser,
|
||||
pixel_state,
|
||||
pixel_details,
|
||||
trigger_reason,
|
||||
max_step_time,
|
||||
diag_start,
|
||||
max_diag_time,
|
||||
_log,
|
||||
_log_summary,
|
||||
_log_separator,
|
||||
preprocess_for_ocr,
|
||||
)
|
||||
template_match_score_threshold = float(
|
||||
getattr(analyser, "invite_redpacket_ocr_template_threshold", 0.4) or 0.4
|
||||
)
|
||||
# 获取模板匹配分数(如果存在),用于判断是否跳过OCR
|
||||
template_match_score = float(
|
||||
getattr(analyser, "_last_template_match_score", 0.0) or 0.0
|
||||
)
|
||||
should_skip_redpacket_ocr = (
|
||||
not detail_detected
|
||||
and template_match_score > 0
|
||||
and template_match_score < template_match_score_threshold
|
||||
)
|
||||
|
||||
if should_skip_redpacket_ocr:
|
||||
_log(
|
||||
"步骤1.5[OCR检测邀你抢红包]-跳过(模板匹配分数过低)",
|
||||
模板匹配分数=template_match_score,
|
||||
阈值=template_match_score_threshold,
|
||||
说明="模板匹配分数过低,明显不在福袋详情页,跳过OCR检测以节省时间",
|
||||
)
|
||||
|
||||
# 步骤1结束计时(像素检测福袋详情页)
|
||||
step1_fudai_elapsed = (time.time() - step1_fudai_start) * 1000
|
||||
_log("步骤1[像素检测福袋详情页]-完成", 总耗时毫秒=step1_fudai_elapsed)
|
||||
|
||||
if (
|
||||
not detail_detected
|
||||
and enable_invite_redpacket_ocr
|
||||
and not should_skip_redpacket_ocr
|
||||
):
|
||||
step_ocr_redpacket_start = time.time()
|
||||
remaining_time_before = max_diag_time - (time.time() - diag_start)
|
||||
# 为OCR检测设置更小的超时时间(默认1秒),避免长时间等待
|
||||
ocr_max_step_time = float(
|
||||
getattr(analyser, "invite_redpacket_ocr_max_step_time", 1.0) or 1.0
|
||||
)
|
||||
if remaining_time_before > ocr_max_step_time:
|
||||
try:
|
||||
_log(
|
||||
"步骤1.5[OCR检测邀你抢红包]-开始",
|
||||
剩余时间秒=remaining_time_before,
|
||||
说明="在指定区域检测邀你抢红包文字",
|
||||
)
|
||||
|
||||
if time.time() - step_ocr_redpacket_start < ocr_max_step_time:
|
||||
cut_start = time.time()
|
||||
y_pianyi = int(getattr(analyser, "y_pianyi", 0) or 0)
|
||||
# 保存完整截图并标注OCR区域
|
||||
try:
|
||||
from douyin.handlers import douyin_screenshot_manager
|
||||
from douyin.core import path_config
|
||||
import os
|
||||
from PIL import Image
|
||||
|
||||
pic_path = path_config.get_screenshot_path()
|
||||
if os.path.exists(pic_path):
|
||||
pic = Image.open(pic_path)
|
||||
pic.load()
|
||||
pic_rgb = pic.convert("RGB")
|
||||
|
||||
width, height = pic_rgb.size
|
||||
rx = int(
|
||||
getattr(analyser, "resolution_ratio_x", 1080) or 1080
|
||||
)
|
||||
ry = int(
|
||||
getattr(analyser, "resolution_ratio_y", 2400) or 2400
|
||||
)
|
||||
|
||||
x1_base, y1_base, x2_base, y2_base = 238, 454, 847, 545
|
||||
x1 = int(x1_base * width / rx)
|
||||
y1 = int((y1_base + y_pianyi) * height / ry)
|
||||
x2 = int(x2_base * width / rx)
|
||||
y2 = int((y2_base + y_pianyi) * height / ry)
|
||||
|
||||
x1 = max(0, min(x1, width - 1))
|
||||
y1 = max(0, min(y1, height - 1))
|
||||
x2 = max(0, min(x2, width - 1))
|
||||
y2 = max(0, min(y2, height - 1))
|
||||
|
||||
if x1 < x2 and y1 < y2:
|
||||
debug_path = douyin_screenshot_manager.save_debug_image(
|
||||
pic_rgb,
|
||||
operation_type="ocr",
|
||||
picname="阶段诊断邀你抢红包区域",
|
||||
step="before_ocr",
|
||||
coords=(x1, y1, x2, y2),
|
||||
extra_info={
|
||||
"region": f"({x1_base},{y1_base})-({x2_base},{y2_base})",
|
||||
"size": f"{x2_base-x1_base}x{y2_base-y1_base}",
|
||||
"y_offset": y_pianyi,
|
||||
},
|
||||
)
|
||||
if debug_path:
|
||||
try:
|
||||
_log(
|
||||
"OCR识别前保存完整截图(阶段诊断邀你抢红包区域)",
|
||||
path=debug_path,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
analyser.cut_pic(
|
||||
*STAGE_RED_PACKET_OCR, "", "阶段诊断邀你抢红包区域", y_pianyi
|
||||
)
|
||||
cut_elapsed = (time.time() - cut_start) * 1000
|
||||
_log("步骤1.5[OCR检测邀你抢红包]-截图完成", 耗时毫秒=cut_elapsed)
|
||||
|
||||
if time.time() - step_ocr_redpacket_start < ocr_max_step_time:
|
||||
ocr_start = time.time()
|
||||
name_red0 = preprocess_for_ocr(
|
||||
analyser, "阶段诊断邀你抢红包区域"
|
||||
)
|
||||
ocr_text = str(
|
||||
analyser.operation.analyse_pic_word(name_red0, 0) or ""
|
||||
)
|
||||
ocr_elapsed = (time.time() - ocr_start) * 1000
|
||||
_log(
|
||||
"步骤1.5[OCR检测邀你抢红包]-OCR模式0",
|
||||
耗时毫秒=ocr_elapsed,
|
||||
文本长度=len(ocr_text),
|
||||
文本预览=ocr_text[:50] if ocr_text else "",
|
||||
)
|
||||
|
||||
# 如果模式0未识别到,尝试模式1
|
||||
enable_ocr_mode1 = bool(
|
||||
getattr(analyser, "enable_ocr_mode1", False)
|
||||
)
|
||||
if enable_ocr_mode1 and (
|
||||
not ocr_text
|
||||
or not any(
|
||||
kw in ocr_text for kw in ("邀你抢", "抢红包", "抢手气")
|
||||
)
|
||||
):
|
||||
if (
|
||||
time.time() - step_ocr_redpacket_start
|
||||
< ocr_max_step_time
|
||||
):
|
||||
name_red1 = preprocess_for_ocr(
|
||||
analyser, "阶段诊断邀你抢红包区域"
|
||||
)
|
||||
ocr_text = str(
|
||||
analyser.operation.analyse_pic_word(name_red1, 1)
|
||||
or ""
|
||||
)
|
||||
_log(
|
||||
"步骤1.5[OCR检测邀你抢红包]-OCR模式1",
|
||||
文本长度=len(ocr_text),
|
||||
文本预览=ocr_text[:50] if ocr_text else "",
|
||||
)
|
||||
|
||||
# 检测是否包含"邀你抢红包"相关关键词(支持变体)
|
||||
redpacket_keywords = (
|
||||
"邀你抢红包",
|
||||
"邀你抢手气红包",
|
||||
"邀你抢",
|
||||
"抢红包",
|
||||
"抢手气",
|
||||
)
|
||||
if ocr_text and any(
|
||||
kw in ocr_text for kw in redpacket_keywords
|
||||
):
|
||||
redpacket_elapsed = (
|
||||
time.time() - step_ocr_redpacket_start
|
||||
) * 1000
|
||||
total_elapsed = (time.time() - diag_start) * 1000
|
||||
_log(
|
||||
"步骤1.5[OCR检测邀你抢红包]-命中",
|
||||
耗时毫秒=redpacket_elapsed,
|
||||
总耗时毫秒=total_elapsed,
|
||||
OCR文本=ocr_text[:50],
|
||||
)
|
||||
_log_summary(
|
||||
"阶段诊断完成(OCR检测邀你抢红包)",
|
||||
阶段="FUDai_DETAIL",
|
||||
原因="检测到邀你抢红包文字",
|
||||
OCR文本=ocr_text[:50],
|
||||
总耗时毫秒=total_elapsed,
|
||||
)
|
||||
return "FUDai_DETAIL", "OCR检测确认(邀你抢红包)"
|
||||
else:
|
||||
redpacket_elapsed = (
|
||||
time.time() - step_ocr_redpacket_start
|
||||
) * 1000
|
||||
_log(
|
||||
"步骤1.5[OCR检测邀你抢红包]-未命中",
|
||||
耗时毫秒=redpacket_elapsed,
|
||||
OCR文本=ocr_text[:50] if ocr_text else "",
|
||||
)
|
||||
except Exception as e:
|
||||
redpacket_elapsed = (time.time() - step_ocr_redpacket_start) * 1000
|
||||
_log(
|
||||
"步骤1.5[OCR检测邀你抢红包]-异常",
|
||||
耗时毫秒=redpacket_elapsed,
|
||||
异常类型=type(e).__name__,
|
||||
异常信息=str(e),
|
||||
)
|
||||
|
||||
# 步骤1.5结束计时(OCR检测邀你抢红包)
|
||||
step15_ocr_elapsed = (time.time() - step_ocr_redpacket_start) * 1000
|
||||
_log("步骤1.5[OCR检测邀你抢红包]-完成", 总耗时毫秒=step15_ocr_elapsed)
|
||||
if fudai_detail_result is not None:
|
||||
return fudai_detail_result
|
||||
|
||||
# ========== 所有像素检测完成,未命中任何特定阶段 ==========
|
||||
# 在返回"在直播间"之前,如果检测到弹窗相关的像素特征,需要OCR再次确认
|
||||
|
||||
@@ -0,0 +1,347 @@
|
||||
"""阶段诊断辅助:步骤 1(像素检测福袋详情页)+ 步骤 1.5(OCR 检测邀你抢红包)
|
||||
|
||||
从 douyin/stage/douyin_stage.py:diagnose_stage_from_current_screen 中提取。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import time
|
||||
|
||||
from douyin.fudai.core.coords import (
|
||||
STAGE_RED_PACKET_OCR,
|
||||
)
|
||||
|
||||
|
||||
def detect_fudai_detail_or_invite_redpacket(
|
||||
analyser,
|
||||
pixel_state: dict,
|
||||
pixel_details: dict,
|
||||
trigger_reason: str,
|
||||
max_step_time: float,
|
||||
diag_start: float,
|
||||
max_diag_time: float,
|
||||
log_fn,
|
||||
log_summary_fn,
|
||||
log_separator_fn,
|
||||
preprocess_for_ocr,
|
||||
):
|
||||
"""步骤 1:从并行检测结果获取福袋详情页(任务数量检测)+ 步骤 1.5:OCR 检测邀你抢红包文字。
|
||||
|
||||
返回:
|
||||
(stage, reason) 二元组:检测到福袋详情页时
|
||||
None:未检测到,调用方继续后续流程
|
||||
"""
|
||||
# ========== 步骤1:像素检测福袋详情页(从并行检测结果获取)==========
|
||||
step1_fudai_start = time.time()
|
||||
# 从并行检测结果中获取福袋详情页检测结果(任务数量检测)
|
||||
# 注意:即使 has_fudai_detail 为 False,pixel_details 中可能仍然有任务数量信息
|
||||
detail_detected = bool(pixel_state.get("has_fudai_detail", False))
|
||||
detail_task_count = None
|
||||
|
||||
# 尝试从 pixel_details 中获取任务数量(无论 has_fudai_detail 是否为 True)
|
||||
if "has_fudai_detail" in pixel_details:
|
||||
detail_task_count = pixel_details["has_fudai_detail"].get("task_count")
|
||||
# 保存任务数量到 analyser 对象,供后续读取详情时使用
|
||||
# 特殊处理:如果是点击验证触发的原因,总是保存检测结果(包括0和None)
|
||||
if trigger_reason == "find_and_open_fudai_detail_click_verification":
|
||||
try:
|
||||
analyser._fudai_detail_task_count_from_stage = detail_task_count
|
||||
try:
|
||||
log_fn( # type: ignore[misc]
|
||||
"阶段检测:设置任务数量到 analyser(点击验证)",
|
||||
任务数量=detail_task_count,
|
||||
触发原因=trigger_reason,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
# 如果任务数量 > 0,标记截图可以复用,避免OCR时重复截图
|
||||
if detail_task_count is not None and detail_task_count > 0:
|
||||
analyser._screenshot_ready_for_ocr = True
|
||||
try:
|
||||
log_fn( # type: ignore[misc]
|
||||
"标记截图可复用(任务数量>0)",
|
||||
任务数量=detail_task_count,
|
||||
screenshot_ready=True,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
except Exception:
|
||||
pass
|
||||
elif detail_task_count is not None and detail_task_count > 0:
|
||||
# 其他情况下,只有检测到有效的任务数量才保存
|
||||
try:
|
||||
analyser._fudai_detail_task_count_from_stage = detail_task_count
|
||||
try:
|
||||
log_fn( # type: ignore[misc]
|
||||
"阶段检测:设置任务数量到 analyser",
|
||||
任务数量=detail_task_count,
|
||||
触发原因=trigger_reason,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
# 如果任务数量 > 0,标记截图可以复用,避免OCR时重复截图
|
||||
if detail_task_count > 0:
|
||||
analyser._screenshot_ready_for_ocr = True
|
||||
try:
|
||||
log_fn( # type: ignore[misc]
|
||||
"标记截图可复用(任务数量>0)",
|
||||
任务数量=detail_task_count,
|
||||
screenshot_ready=True,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
except Exception:
|
||||
pass
|
||||
pass
|
||||
try:
|
||||
log_fn( # type: ignore[misc]
|
||||
"保存任务数量到 analyser 对象",
|
||||
任务数量=detail_task_count,
|
||||
has_fudai_detail=detail_detected,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 如果任务数量为 None,记录警告日志
|
||||
if detail_task_count is None:
|
||||
try:
|
||||
log_fn( # type: ignore[misc]
|
||||
"警告:任务数量为 None,无法保存到 analyser 对象",
|
||||
任务数量=None,
|
||||
has_fudai_detail=detail_detected,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
else:
|
||||
# pixel_details 中没有 has_fudai_detail 信息,记录警告
|
||||
try:
|
||||
log_fn( # type: ignore[misc]
|
||||
"警告:pixel_details 中没有 has_fudai_detail 信息",
|
||||
has_fudai_detail=detail_detected,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 如果并行检测到福袋详情页,直接返回
|
||||
if detail_detected:
|
||||
total_elapsed = (time.time() - diag_start) * 1000
|
||||
log_summary_fn(
|
||||
"阶段诊断完成(任务数量检测)",
|
||||
阶段="FUDai_DETAIL",
|
||||
原因="并行检测任务数量检测确认",
|
||||
任务数量=detail_task_count,
|
||||
总耗时毫秒=total_elapsed,
|
||||
)
|
||||
log_separator_fn("最终结果:FUDai_DETAIL(福袋详情页)")
|
||||
return "FUDai_DETAIL", f"任务数量检测确认(任务数量={detail_task_count})"
|
||||
|
||||
# 额外检测:在指定区域检测"邀你抢红包"文字(用于检测红包/福袋详情页)
|
||||
# 区域:(238,454) 到 (847,545) - 基准坐标(1080x2400)
|
||||
# 优化:当模板匹配分数很低(<0.4)时,说明明显不在福袋详情页,跳过OCR检测以节省时间
|
||||
# 暂时禁用:将默认值改为 False
|
||||
enable_invite_redpacket_ocr = bool(
|
||||
getattr(analyser, "enable_invite_redpacket_ocr", False)
|
||||
)
|
||||
template_match_score_threshold = float(
|
||||
getattr(analyser, "invite_redpacket_ocr_template_threshold", 0.4) or 0.4
|
||||
)
|
||||
# 获取模板匹配分数(如果存在),用于判断是否跳过OCR
|
||||
template_match_score = float(
|
||||
getattr(analyser, "_last_template_match_score", 0.0) or 0.0
|
||||
)
|
||||
should_skip_redpacket_ocr = (
|
||||
not detail_detected
|
||||
and template_match_score > 0
|
||||
and template_match_score < template_match_score_threshold
|
||||
)
|
||||
|
||||
if should_skip_redpacket_ocr:
|
||||
log_fn(
|
||||
"步骤1.5[OCR检测邀你抢红包]-跳过(模板匹配分数过低)",
|
||||
模板匹配分数=template_match_score,
|
||||
阈值=template_match_score_threshold,
|
||||
说明="模板匹配分数过低,明显不在福袋详情页,跳过OCR检测以节省时间",
|
||||
)
|
||||
|
||||
# 步骤1结束计时(像素检测福袋详情页)
|
||||
step1_fudai_elapsed = (time.time() - step1_fudai_start) * 1000
|
||||
log_fn("步骤1[像素检测福袋详情页]-完成", 总耗时毫秒=step1_fudai_elapsed)
|
||||
|
||||
if (
|
||||
not detail_detected
|
||||
and enable_invite_redpacket_ocr
|
||||
and not should_skip_redpacket_ocr
|
||||
):
|
||||
step_ocr_redpacket_start = time.time()
|
||||
remaining_time_before = max_diag_time - (time.time() - diag_start)
|
||||
# 为OCR检测设置更小的超时时间(默认1秒),避免长时间等待
|
||||
ocr_max_step_time = float(
|
||||
getattr(analyser, "invite_redpacket_ocr_max_step_time", 1.0) or 1.0
|
||||
)
|
||||
if remaining_time_before > ocr_max_step_time:
|
||||
try:
|
||||
log_fn(
|
||||
"步骤1.5[OCR检测邀你抢红包]-开始",
|
||||
剩余时间秒=remaining_time_before,
|
||||
说明="在指定区域检测邀你抢红包文字",
|
||||
)
|
||||
|
||||
if time.time() - step_ocr_redpacket_start < ocr_max_step_time:
|
||||
cut_start = time.time()
|
||||
y_pianyi = int(getattr(analyser, "y_pianyi", 0) or 0)
|
||||
# 保存完整截图并标注OCR区域
|
||||
try:
|
||||
from douyin.handlers import douyin_screenshot_manager
|
||||
from douyin.core import path_config
|
||||
import os
|
||||
from PIL import Image
|
||||
|
||||
pic_path = path_config.get_screenshot_path()
|
||||
if os.path.exists(pic_path):
|
||||
pic = Image.open(pic_path)
|
||||
pic.load()
|
||||
pic_rgb = pic.convert("RGB")
|
||||
|
||||
width, height = pic_rgb.size
|
||||
rx = int(
|
||||
getattr(analyser, "resolution_ratio_x", 1080) or 1080
|
||||
)
|
||||
ry = int(
|
||||
getattr(analyser, "resolution_ratio_y", 2400) or 2400
|
||||
)
|
||||
|
||||
x1_base, y1_base, x2_base, y2_base = 238, 454, 847, 545
|
||||
x1 = int(x1_base * width / rx)
|
||||
y1 = int((y1_base + y_pianyi) * height / ry)
|
||||
x2 = int(x2_base * width / rx)
|
||||
y2 = int((y2_base + y_pianyi) * height / ry)
|
||||
|
||||
x1 = max(0, min(x1, width - 1))
|
||||
y1 = max(0, min(y1, height - 1))
|
||||
x2 = max(0, min(x2, width - 1))
|
||||
y2 = max(0, min(y2, height - 1))
|
||||
|
||||
if x1 < x2 and y1 < y2:
|
||||
debug_path = douyin_screenshot_manager.save_debug_image(
|
||||
pic_rgb,
|
||||
operation_type="ocr",
|
||||
picname="阶段诊断邀你抢红包区域",
|
||||
step="before_ocr",
|
||||
coords=(x1, y1, x2, y2),
|
||||
extra_info={
|
||||
"region": f"({x1_base},{y1_base})-({x2_base},{y2_base})",
|
||||
"size": f"{x2_base-x1_base}x{y2_base-y1_base}",
|
||||
"y_offset": y_pianyi,
|
||||
},
|
||||
)
|
||||
if debug_path:
|
||||
try:
|
||||
log_fn(
|
||||
"OCR识别前保存完整截图(阶段诊断邀你抢红包区域)",
|
||||
path=debug_path,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
analyser.cut_pic(
|
||||
*STAGE_RED_PACKET_OCR, "", "阶段诊断邀你抢红包区域", y_pianyi
|
||||
)
|
||||
cut_elapsed = (time.time() - cut_start) * 1000
|
||||
log_fn("步骤1.5[OCR检测邀你抢红包]-截图完成", 耗时毫秒=cut_elapsed)
|
||||
|
||||
if time.time() - step_ocr_redpacket_start < ocr_max_step_time:
|
||||
ocr_start = time.time()
|
||||
name_red0 = preprocess_for_ocr(
|
||||
analyser, "阶段诊断邀你抢红包区域"
|
||||
)
|
||||
ocr_text = str(
|
||||
analyser.operation.analyse_pic_word(name_red0, 0) or ""
|
||||
)
|
||||
ocr_elapsed = (time.time() - ocr_start) * 1000
|
||||
log_fn(
|
||||
"步骤1.5[OCR检测邀你抢红包]-OCR模式0",
|
||||
耗时毫秒=ocr_elapsed,
|
||||
文本长度=len(ocr_text),
|
||||
文本预览=ocr_text[:50] if ocr_text else "",
|
||||
)
|
||||
|
||||
# 如果模式0未识别到,尝试模式1
|
||||
enable_ocr_mode1 = bool(
|
||||
getattr(analyser, "enable_ocr_mode1", False)
|
||||
)
|
||||
if enable_ocr_mode1 and (
|
||||
not ocr_text
|
||||
or not any(
|
||||
kw in ocr_text for kw in ("邀你抢", "抢红包", "抢手气")
|
||||
)
|
||||
):
|
||||
if (
|
||||
time.time() - step_ocr_redpacket_start
|
||||
< ocr_max_step_time
|
||||
):
|
||||
name_red1 = preprocess_for_ocr(
|
||||
analyser, "阶段诊断邀你抢红包区域"
|
||||
)
|
||||
ocr_text = str(
|
||||
analyser.operation.analyse_pic_word(name_red1, 1)
|
||||
or ""
|
||||
)
|
||||
log_fn(
|
||||
"步骤1.5[OCR检测邀你抢红包]-OCR模式1",
|
||||
文本长度=len(ocr_text),
|
||||
文本预览=ocr_text[:50] if ocr_text else "",
|
||||
)
|
||||
|
||||
# 检测是否包含"邀你抢红包"相关关键词(支持变体)
|
||||
redpacket_keywords = (
|
||||
"邀你抢红包",
|
||||
"邀你抢手气红包",
|
||||
"邀你抢",
|
||||
"抢红包",
|
||||
"抢手气",
|
||||
)
|
||||
if ocr_text and any(
|
||||
kw in ocr_text for kw in redpacket_keywords
|
||||
):
|
||||
redpacket_elapsed = (
|
||||
time.time() - step_ocr_redpacket_start
|
||||
) * 1000
|
||||
total_elapsed = (time.time() - diag_start) * 1000
|
||||
log_fn(
|
||||
"步骤1.5[OCR检测邀你抢红包]-命中",
|
||||
耗时毫秒=redpacket_elapsed,
|
||||
总耗时毫秒=total_elapsed,
|
||||
OCR文本=ocr_text[:50],
|
||||
)
|
||||
log_summary_fn(
|
||||
"阶段诊断完成(OCR检测邀你抢红包)",
|
||||
阶段="FUDai_DETAIL",
|
||||
原因="检测到邀你抢红包文字",
|
||||
OCR文本=ocr_text[:50],
|
||||
总耗时毫秒=total_elapsed,
|
||||
)
|
||||
return "FUDai_DETAIL", "OCR检测确认(邀你抢红包)"
|
||||
else:
|
||||
redpacket_elapsed = (
|
||||
time.time() - step_ocr_redpacket_start
|
||||
) * 1000
|
||||
log_fn(
|
||||
"步骤1.5[OCR检测邀你抢红包]-未命中",
|
||||
耗时毫秒=redpacket_elapsed,
|
||||
OCR文本=ocr_text[:50] if ocr_text else "",
|
||||
)
|
||||
except Exception as e:
|
||||
redpacket_elapsed = (time.time() - step_ocr_redpacket_start) * 1000
|
||||
log_fn(
|
||||
"步骤1.5[OCR检测邀你抢红包]-异常",
|
||||
耗时毫秒=redpacket_elapsed,
|
||||
异常类型=type(e).__name__,
|
||||
异常信息=str(e),
|
||||
)
|
||||
|
||||
# 步骤1.5结束计时(OCR检测邀你抢红包)
|
||||
step15_ocr_elapsed = (time.time() - step_ocr_redpacket_start) * 1000
|
||||
log_fn("步骤1.5[OCR检测邀你抢红包]-完成", 总耗时毫秒=step15_ocr_elapsed)
|
||||
|
||||
return None
|
||||
Reference in new issue
Block a user