diff --git a/douyin/stage/douyin_stage.py b/douyin/stage/douyin_stage.py index 8d5c87f..3a69636 100644 --- a/douyin/stage/douyin_stage.py +++ b/douyin/stage/douyin_stage.py @@ -333,445 +333,25 @@ def diagnose_stage_from_current_screen(analyser, trigger_reason="unknown"): # t step0_elapsed = (time.time() - step_start) * 1000 _log("步骤0[并行检测]-完成", 总耗时毫秒=step0_elapsed) - # ========== 如果不在直播间,执行步骤3(OCR检测推荐页/关注列表/直播列表)========== - if not in_live_room: - _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) - - # 步骤3:检测推荐页面、关注列表、直播列表(OCR标签栏,整合在一起检测) - step_start = time.time() - remaining_time_before = max_diag_time - (time.time() - diag_start) - _log( - "步骤3[OCR推荐页/关注列表/直播列表]-开始", 剩余时间秒=remaining_time_before - ) - t_recommend = "" - t_follow = "" - t_list = "" - try: - # 检测推荐页标签栏区域(包含"推荐"、"关注"、"直播"标签) - _log("步骤3[OCR推荐页标签栏]-区域", 区域="(400,140)-(900,200)") - if time.time() - step_start < max_step_time: - cut_start = time.time() - analyser.cut_pic(*RECOMMEND_TAB, "", "推荐页标签栏") - cut_elapsed = (time.time() - cut_start) * 1000 - _log("步骤3[OCR推荐页标签栏]-截图完成", 耗时毫秒=cut_elapsed) - - if time.time() - step_start < max_step_time: - ocr_start = time.time() - name0 = preprocess_for_ocr(analyser, "推荐页标签栏") - t_recommend = str( - analyser.operation.analyse_pic_word(name0, 0) or "" - ) - ocr_elapsed = (time.time() - ocr_start) * 1000 - _log( - "步骤3[OCR推荐页标签栏]-OCR模式0", - 耗时毫秒=ocr_elapsed, - 文本长度=len(t_recommend), - 文本预览=t_recommend[:50] if t_recommend else "", - ) - - if ( - enable_ocr_mode1 - and (time.time() - step_start < max_step_time) - and ( - not t_recommend - or ( - "推荐" not in t_recommend - and "关注" not in t_recommend - and "直播" not in t_recommend - ) - ) - ): - ocr_start_1 = time.time() - name1 = preprocess_for_ocr(analyser, "推荐页标签栏") - t_recommend = str( - analyser.operation.analyse_pic_word(name1, 1) or "" - ) - ocr_elapsed_1 = (time.time() - ocr_start_1) * 1000 - _log( - "步骤3[OCR推荐页标签栏]-OCR模式1", - 耗时毫秒=ocr_elapsed_1, - 文本长度=len(t_recommend), - 文本预览=t_recommend[:50] if t_recommend else "", - ) - elapsed_recommend = (time.time() - step_start) * 1000 - - # 如果识别到"推荐",直接返回推荐页面 - if "推荐" in t_recommend: - total_elapsed = (time.time() - diag_start) * 1000 - seen_text = t_recommend[:50] if t_recommend else "-" - _log( - "步骤3[OCR推荐页标签栏]-命中推荐", - 耗时毫秒=elapsed_recommend, - 总耗时毫秒=total_elapsed, - 识别文本=seen_text, - ) - _log_summary( - "阶段诊断完成", - 阶段="RECOMMEND_PAGE", - 原因=f"标题='{t_recommend}'", - 总耗时毫秒=total_elapsed, - ) - _log_separator("最终结果:RECOMMEND_PAGE(推荐页)") - return "RECOMMEND_PAGE", "标题='{}'".format(t_recommend) - - # 如果识别到"关注"但没有"推荐",可能是关注列表 - if "关注" in t_recommend and "推荐" not in t_recommend: - _log( - "步骤3[OCR关注列表]-检测到关注关键词,准备精确确认", - 精确区域="(658,147)-(765,194)", - ) - step_start_follow = time.time() - try: - if time.time() - step_start < max_step_time: - cut_start = time.time() - analyser.cut_pic(*FOLLOW_TAB, "", "关注列表标签") - cut_elapsed = (time.time() - cut_start) * 1000 - _log("步骤3[OCR关注列表]-精确截图完成", 耗时毫秒=cut_elapsed) - - if time.time() - step_start < max_step_time: - ocr_start = time.time() - namef0 = preprocess_for_ocr(analyser, "关注列表标签") - t_follow = str( - analyser.operation.analyse_pic_word(namef0, 0) or "" - ) - ocr_elapsed = (time.time() - ocr_start) * 1000 - _log( - "步骤3[OCR关注列表]-OCR模式0", - 耗时毫秒=ocr_elapsed, - 文本长度=len(t_follow), - 文本预览=t_follow[:50] if t_follow else "", - ) - - if ( - enable_ocr_mode1 - and (time.time() - step_start < max_step_time) - and ("关注" not in t_follow) - ): - ocr_start_1 = time.time() - namef1 = preprocess_for_ocr(analyser, "关注列表标签") - t_follow = str( - analyser.operation.analyse_pic_word(namef1, 1) or "" - ) - ocr_elapsed_1 = (time.time() - ocr_start_1) * 1000 - _log( - "步骤3[OCR关注列表]-OCR模式1", - 耗时毫秒=ocr_elapsed_1, - 文本长度=len(t_follow), - 文本预览=t_follow[:50] if t_follow else "", - ) - except Exception as e: - import traceback - - _log( - "步骤3[OCR关注列表]-异常", - 异常类型=type(e).__name__, - 异常信息=str(e), - 堆栈=traceback.format_exc(), - ) - - elapsed_follow = (time.time() - step_start_follow) * 1000 - if "关注" in t_follow: - total_elapsed = (time.time() - diag_start) * 1000 - follow_text = t_follow[:50] if t_follow else "-" - _log( - "步骤3[OCR关注列表]-命中", - 耗时毫秒=elapsed_follow, - 总耗时毫秒=total_elapsed, - 识别文本=follow_text, - ) - try: - analyser._last_follow_list_title_text = str( - t_follow or "" - ).strip() - except Exception: - analyser._last_follow_list_title_text = "" - _log_summary( - "阶段诊断完成", - 阶段="FOLLOW_LIST", - 原因=f"标题='{t_follow}'", - 总耗时毫秒=total_elapsed, - ) - _log_separator("最终结果:FOLLOW_LIST(关注列表)") - return "FOLLOW_LIST", "标题='{}'".format(t_follow) - - # 如果识别到"正在直播"标签,判定为直播列表 - if "正在直播" in t_recommend or ( - "直播" in t_recommend - and "推荐" not in t_recommend - and "关注" not in t_recommend - ): - _log( - "步骤3[OCR直播列表]-检测到直播关键词,准备精确确认", - 精确区域="(650,145)-(800,195)", - ) - step_start_list = time.time() - try: - ocr_start = time.time() # 初始化ocr_start - if time.time() - step_start < max_step_time: - cut_start = time.time() - analyser.cut_pic(*LIVE_LIST_TAB, "", "直播列表标签") - cut_elapsed = (time.time() - cut_start) * 1000 - _log("步骤3[OCR直播列表]-精确截图完成", 耗时毫秒=cut_elapsed) - - if time.time() - step_start < max_step_time: - ocr_start = time.time() - # else分支不需要,因为ocr_start已经在上面初始化了 - namel0 = preprocess_for_ocr(analyser, "直播列表标签") - t_list = str(analyser.operation.analyse_pic_word(namel0, 0) or "") - ocr_elapsed = (time.time() - ocr_start) * 1000 - _log( - "步骤3[OCR直播列表]-OCR模式0", - 耗时毫秒=ocr_elapsed, - 文本长度=len(t_list), - 文本预览=t_list[:50] if t_list else "", - ) - - if ( - enable_ocr_mode1 - and (time.time() - step_start < max_step_time) - and ("正在直播" not in t_list) - ): - ocr_start_1 = time.time() - namel1 = preprocess_for_ocr(analyser, "直播列表标签") - t_list = str( - analyser.operation.analyse_pic_word(namel1, 1) or "" - ) - ocr_elapsed_1 = (time.time() - ocr_start_1) * 1000 - _log( - "步骤3[OCR直播列表]-OCR模式1", - 耗时毫秒=ocr_elapsed_1, - 文本长度=len(t_list), - 文本预览=t_list[:50] if t_list else "", - ) - except Exception as e: - import traceback - - _log( - "步骤3[OCR直播列表]-异常", - 异常类型=type(e).__name__, - 异常信息=str(e), - 堆栈=traceback.format_exc(), - ) - - elapsed_list = (time.time() - step_start_list) * 1000 - if "正在直播" in t_list: - total_elapsed = (time.time() - diag_start) * 1000 - list_text = t_list[:50] if t_list else "-" - _log( - "步骤3[OCR直播列表]-命中", - 耗时毫秒=elapsed_list, - 总耗时毫秒=total_elapsed, - 识别文本=list_text, - ) - _log_summary( - "阶段诊断完成", - 阶段="LIVE_LIST", - 原因=f"标题='{t_list}'", - 总耗时毫秒=total_elapsed, - ) - _log_separator("最终结果:LIVE_LIST(直播列表)") - return "LIVE_LIST", "标题='{}'".format(t_list) - - # 记录诊断时看到的"关注页标题" - try: - analyser._last_follow_list_title_text = str(t_follow or "").strip() - except Exception: - analyser._last_follow_list_title_text = "" - - elapsed_total = (time.time() - step_start) * 1000 - if elapsed_total > max_step_time * 1000: - _log( - "步骤3[OCR推荐页/关注列表/直播列表]-步骤超时", - 耗时毫秒=elapsed_total, - 阈值毫秒=max_step_time * 1000, - ) - else: - _log( - "步骤3[OCR推荐页/关注列表/直播列表]-未命中", - 耗时毫秒=elapsed_total, - 推荐标签栏预览=t_recommend[:30] if t_recommend else "", - ) - except Exception as e: - elapsed_total = (time.time() - step_start) * 1000 - import traceback - - _log( - "步骤3[OCR推荐页/关注列表/直播列表]-异常", - 耗时毫秒=elapsed_total, - 异常类型=type(e).__name__, - 异常信息=str(e), - 堆栈=traceback.format_exc(), - ) - - # 步骤3结束计时 - step3_elapsed = (time.time() - step_start) * 1000 - _log("步骤3[OCR推荐页/关注列表/直播列表]-完成", 总耗时毫秒=step3_elapsed) - - # 如果不在直播间且OCR也未识别到特定页面,默认返回在直播间 - total_elapsed = (time.time() - diag_start) * 1000 - _log_summary( - "阶段诊断完成", - 阶段="IN_LIVE_ROOM", - 原因="不在直播间且未识别到特定页面,默认判定为在直播间", - 总耗时毫秒=total_elapsed, - ) - _log_separator("最终结果:IN_LIVE_ROOM(默认直播间)") - return "IN_LIVE_ROOM", "不在直播间且未识别到特定页面,默认判定为在直播间" + # 不在直播间时:跑 OCR 找推荐页/关注列表/直播列表/购物红包 + from douyin.stage.douyin_stage_handler_pkg.ocr_detection import ( + detect_pages_via_ocr, + ) + _ocr_result = detect_pages_via_ocr( + analyser=analyser, + in_live_room=in_live_room, + pixel_state=pixel_state, + pixel_details=pixel_details, + diag_start=diag_start, + max_diag_time=max_diag_time, + max_step_time=max_step_time, + enable_ocr_mode1=enable_ocr_mode1, + log=_log, + log_summary=_log_summary, + log_separator=_log_separator, + ) + if _ocr_result is not None: + return _ocr_result # ========== 在直播间:继续执行像素检测结果处理 ========== _log_separator("在直播间,继续执行像素检测结果处理") diff --git a/douyin/stage/douyin_stage_handler_pkg/ocr_detection.py b/douyin/stage/douyin_stage_handler_pkg/ocr_detection.py new file mode 100644 index 0000000..9413b3c --- /dev/null +++ b/douyin/stage/douyin_stage_handler_pkg/ocr_detection.py @@ -0,0 +1,581 @@ +"""douyin_stage_handler_pkg.ocr_detection - 不在直播间时的 OCR 检测 + +从 diagnose_stage_from_current_screen 抽出 L336-774 的整段 "if not in_live_room" +分支(438 行): +- 步骤 2.5:购物红包 OCR 复查(像素命中红包背景时再用 OCR 二次确认) +- 步骤 3:OCR 推荐页/关注列表/直播列表标签栏 + +入口:detect_pages_via_ocr(...) -> Optional[Tuple[str, str]] +返回值: +- ("SHOPPING_REDPACKET_POPUP", reason) | ("RECOMMEND_PAGE", reason) + | ("FOLLOW_LIST", reason) | ("LIVE_LIST", reason) | ("IN_LIVE_ROOM", reason) + → caller 立即 return("IN_LIVE_ROOM" 是兜底默认值,与原代码保持一致) +- None + → in_live_room=True,caller 应继续走"在直播间"分支 +""" +from __future__ import annotations + +import time +from typing import Any, Callable, Optional, Tuple + +from douyin.fudai.core.coords import ( + FOLLOW_TAB, + LIVE_LIST_TAB, + RECOMMEND_TAB, + SHOPPING_REDPACKET_AREA, +) + + +def detect_pages_via_ocr( + analyser: Any, + in_live_room: bool, + pixel_state: dict, + pixel_details: dict, + diag_start: float, + max_diag_time: float, + max_step_time: float, + enable_ocr_mode1: bool, + log: Callable[..., None], + log_summary: Callable[..., None], + log_separator: Callable[..., None], +) -> Optional[Tuple[str, str]]: + """如果不在直播间,跑 OCR 找推荐页 / 关注列表 / 直播列表 / 购物红包。 + + 返回 ("阶段", 原因) 让 caller 立即 return;返回 None 表示 in_live_room=True,继续后续逻辑。 + """ + if in_live_room: + return None + + try: + from douyin.utils.ocr_preprocess import preprocess_for_ocr # type: ignore + except Exception: + def preprocess_for_ocr(_analyser, picname: str) -> str: # type: ignore + return str(picname) + + log("不在直播间,执行步骤3(OCR检测推荐页/关注列表/直播列表)") + + # 购物红包 OCR 复查(不在直播间也必须执行) + redpacket_result = _check_redpacket_ocr_when_not_in_live_room( + analyser, + pixel_state, + pixel_details, + diag_start, + max_step_time, + enable_ocr_mode1, + preprocess_for_ocr, + log, + log_summary, + ) + if redpacket_result is not None: + return redpacket_result + + # 步骤 3:OCR 推荐页/关注列表/直播列表标签栏 + step_start = time.time() + remaining_time_before = max_diag_time - (time.time() - diag_start) + log( + "步骤3[OCR推荐页/关注列表/直播列表]-开始", 剩余时间秒=remaining_time_before + ) + t_recommend = "" + t_follow = "" + t_list = "" + try: + log("步骤3[OCR推荐页标签栏]-区域", 区域="(400,140)-(900,200)") + if time.time() - step_start < max_step_time: + cut_start = time.time() + analyser.cut_pic(*RECOMMEND_TAB, "", "推荐页标签栏") + cut_elapsed = (time.time() - cut_start) * 1000 + log("步骤3[OCR推荐页标签栏]-截图完成", 耗时毫秒=cut_elapsed) + + if time.time() - step_start < max_step_time: + ocr_start = time.time() + name0 = preprocess_for_ocr(analyser, "推荐页标签栏") + t_recommend = str( + analyser.operation.analyse_pic_word(name0, 0) or "" + ) + ocr_elapsed = (time.time() - ocr_start) * 1000 + log( + "步骤3[OCR推荐页标签栏]-OCR模式0", + 耗时毫秒=ocr_elapsed, + 文本长度=len(t_recommend), + 文本预览=t_recommend[:50] if t_recommend else "", + ) + + if ( + enable_ocr_mode1 + and (time.time() - step_start < max_step_time) + and ( + not t_recommend + or ( + "推荐" not in t_recommend + and "关注" not in t_recommend + and "直播" not in t_recommend + ) + ) + ): + ocr_start_1 = time.time() + name1 = preprocess_for_ocr(analyser, "推荐页标签栏") + t_recommend = str( + analyser.operation.analyse_pic_word(name1, 1) or "" + ) + ocr_elapsed_1 = (time.time() - ocr_start_1) * 1000 + log( + "步骤3[OCR推荐页标签栏]-OCR模式1", + 耗时毫秒=ocr_elapsed_1, + 文本长度=len(t_recommend), + 文本预览=t_recommend[:50] if t_recommend else "", + ) + elapsed_recommend = (time.time() - step_start) * 1000 + + # 命中"推荐" + if "推荐" in t_recommend: + total_elapsed = (time.time() - diag_start) * 1000 + seen_text = t_recommend[:50] if t_recommend else "-" + log( + "步骤3[OCR推荐页标签栏]-命中推荐", + 耗时毫秒=elapsed_recommend, + 总耗时毫秒=total_elapsed, + 识别文本=seen_text, + ) + log_summary( + "阶段诊断完成", + 阶段="RECOMMEND_PAGE", + 原因=f"标题='{t_recommend}'", + 总耗时毫秒=total_elapsed, + ) + log_separator("最终结果:RECOMMEND_PAGE(推荐页)") + return "RECOMMEND_PAGE", "标题='{}'".format(t_recommend) + + # 命中"关注"但没有"推荐" → 关注列表精确确认 + if "关注" in t_recommend and "推荐" not in t_recommend: + follow_result = _confirm_follow_list( + analyser, + t_recommend, + step_start, + diag_start, + max_step_time, + enable_ocr_mode1, + preprocess_for_ocr, + log, + log_summary, + log_separator, + ) + if follow_result is not None: + return follow_result + # follow_result 为 None:关注列表确认失败,但继续走直播列表分支 + t_follow = getattr(analyser, "_last_follow_list_title_text", "") or "" + + # 命中"正在直播"或"直播"标签 → 直播列表精确确认 + if "正在直播" in t_recommend or ( + "直播" in t_recommend + and "推荐" not in t_recommend + and "关注" not in t_recommend + ): + list_result = _confirm_live_list( + analyser, + step_start, + diag_start, + max_step_time, + enable_ocr_mode1, + preprocess_for_ocr, + log, + log_summary, + log_separator, + ) + if list_result is not None: + return list_result + + # 记录诊断时看到的"关注页标题" + try: + analyser._last_follow_list_title_text = str(t_follow or "").strip() + except Exception: + analyser._last_follow_list_title_text = "" + + elapsed_total = (time.time() - step_start) * 1000 + if elapsed_total > max_step_time * 1000: + log( + "步骤3[OCR推荐页/关注列表/直播列表]-步骤超时", + 耗时毫秒=elapsed_total, + 阈值毫秒=max_step_time * 1000, + ) + else: + log( + "步骤3[OCR推荐页/关注列表/直播列表]-未命中", + 耗时毫秒=elapsed_total, + 推荐标签栏预览=t_recommend[:30] if t_recommend else "", + ) + except Exception as e: + elapsed_total = (time.time() - step_start) * 1000 + import traceback + + log( + "步骤3[OCR推荐页/关注列表/直播列表]-异常", + 耗时毫秒=elapsed_total, + 异常类型=type(e).__name__, + 异常信息=str(e), + 堆栈=traceback.format_exc(), + ) + + step3_elapsed = (time.time() - step_start) * 1000 + log("步骤3[OCR推荐页/关注列表/直播列表]-完成", 总耗时毫秒=step3_elapsed) + + # 如果不在直播间且 OCR 也未识别到特定页面,默认返回在直播间 + total_elapsed = (time.time() - diag_start) * 1000 + log_summary( + "阶段诊断完成", + 阶段="IN_LIVE_ROOM", + 原因="不在直播间且未识别到特定页面,默认判定为在直播间", + 总耗时毫秒=total_elapsed, + ) + log_separator("最终结果:IN_LIVE_ROOM(默认直播间)") + return "IN_LIVE_ROOM", "不在直播间且未识别到特定页面,默认判定为在直播间" + + +def _check_redpacket_ocr_when_not_in_live_room( + analyser: Any, + pixel_state: dict, + pixel_details: dict, + diag_start: float, + max_step_time: float, + enable_ocr_mode1: bool, + preprocess_for_ocr: Callable, + log: Callable[..., None], + log_summary: Callable[..., None], +) -> Optional[Tuple[str, str]]: + """步骤 2.5:购物红包像素命中时的 OCR 二次确认(不在直播间分支专用)。""" + 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( + "步骤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 + redpacket_text = "" + 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("步骤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) + return None + + +def _confirm_follow_list( + analyser: Any, + t_recommend: str, + step_start: float, + diag_start: float, + max_step_time: float, + enable_ocr_mode1: bool, + preprocess_for_ocr: Callable, + log: Callable[..., None], + log_summary: Callable[..., None], + log_separator: Callable[..., None], +) -> Optional[Tuple[str, str]]: + """精确截图 + OCR 确认是否真的是关注列表。""" + log( + "步骤3[OCR关注列表]-检测到关注关键词,准备精确确认", + 精确区域="(658,147)-(765,194)", + ) + step_start_follow = time.time() + t_follow = "" + try: + if time.time() - step_start < max_step_time: + cut_start = time.time() + analyser.cut_pic(*FOLLOW_TAB, "", "关注列表标签") + cut_elapsed = (time.time() - cut_start) * 1000 + log("步骤3[OCR关注列表]-精确截图完成", 耗时毫秒=cut_elapsed) + + if time.time() - step_start < max_step_time: + ocr_start = time.time() + namef0 = preprocess_for_ocr(analyser, "关注列表标签") + t_follow = str( + analyser.operation.analyse_pic_word(namef0, 0) or "" + ) + ocr_elapsed = (time.time() - ocr_start) * 1000 + log( + "步骤3[OCR关注列表]-OCR模式0", + 耗时毫秒=ocr_elapsed, + 文本长度=len(t_follow), + 文本预览=t_follow[:50] if t_follow else "", + ) + + if ( + enable_ocr_mode1 + and (time.time() - step_start < max_step_time) + and ("关注" not in t_follow) + ): + ocr_start_1 = time.time() + namef1 = preprocess_for_ocr(analyser, "关注列表标签") + t_follow = str( + analyser.operation.analyse_pic_word(namef1, 1) or "" + ) + ocr_elapsed_1 = (time.time() - ocr_start_1) * 1000 + log( + "步骤3[OCR关注列表]-OCR模式1", + 耗时毫秒=ocr_elapsed_1, + 文本长度=len(t_follow), + 文本预览=t_follow[:50] if t_follow else "", + ) + except Exception as e: + import traceback + + log( + "步骤3[OCR关注列表]-异常", + 异常类型=type(e).__name__, + 异常信息=str(e), + 堆栈=traceback.format_exc(), + ) + + elapsed_follow = (time.time() - step_start_follow) * 1000 + if "关注" in t_follow: + total_elapsed = (time.time() - diag_start) * 1000 + follow_text = t_follow[:50] if t_follow else "-" + log( + "步骤3[OCR关注列表]-命中", + 耗时毫秒=elapsed_follow, + 总耗时毫秒=total_elapsed, + 识别文本=follow_text, + ) + try: + analyser._last_follow_list_title_text = str(t_follow or "").strip() + except Exception: + analyser._last_follow_list_title_text = "" + log_summary( + "阶段诊断完成", + 阶段="FOLLOW_LIST", + 原因=f"标题='{t_follow}'", + 总耗时毫秒=total_elapsed, + ) + log_separator("最终结果:FOLLOW_LIST(关注列表)") + return "FOLLOW_LIST", "标题='{}'".format(t_follow) + + # 关注列表确认失败:把 t_follow 存到 analyser,后续可读 + try: + analyser._last_follow_list_title_text = str(t_follow or "").strip() + except Exception: + analyser._last_follow_list_title_text = "" + return None + + +def _confirm_live_list( + analyser: Any, + step_start: float, + diag_start: float, + max_step_time: float, + enable_ocr_mode1: bool, + preprocess_for_ocr: Callable, + log: Callable[..., None], + log_summary: Callable[..., None], + log_separator: Callable[..., None], +) -> Optional[Tuple[str, str]]: + """精确截图 + OCR 确认是否真的是直播列表。""" + log( + "步骤3[OCR直播列表]-检测到直播关键词,准备精确确认", + 精确区域="(650,145)-(800,195)", + ) + step_start_list = time.time() + t_list = "" + try: + ocr_start = time.time() + if time.time() - step_start < max_step_time: + cut_start = time.time() + analyser.cut_pic(*LIVE_LIST_TAB, "", "直播列表标签") + cut_elapsed = (time.time() - cut_start) * 1000 + log("步骤3[OCR直播列表]-精确截图完成", 耗时毫秒=cut_elapsed) + + if time.time() - step_start < max_step_time: + ocr_start = time.time() + namel0 = preprocess_for_ocr(analyser, "直播列表标签") + t_list = str(analyser.operation.analyse_pic_word(namel0, 0) or "") + ocr_elapsed = (time.time() - ocr_start) * 1000 + log( + "步骤3[OCR直播列表]-OCR模式0", + 耗时毫秒=ocr_elapsed, + 文本长度=len(t_list), + 文本预览=t_list[:50] if t_list else "", + ) + + if ( + enable_ocr_mode1 + and (time.time() - step_start < max_step_time) + and ("正在直播" not in t_list) + ): + ocr_start_1 = time.time() + namel1 = preprocess_for_ocr(analyser, "直播列表标签") + t_list = str(analyser.operation.analyse_pic_word(namel1, 1) or "") + ocr_elapsed_1 = (time.time() - ocr_start_1) * 1000 + log( + "步骤3[OCR直播列表]-OCR模式1", + 耗时毫秒=ocr_elapsed_1, + 文本长度=len(t_list), + 文本预览=t_list[:50] if t_list else "", + ) + except Exception as e: + import traceback + + log( + "步骤3[OCR直播列表]-异常", + 异常类型=type(e).__name__, + 异常信息=str(e), + 堆栈=traceback.format_exc(), + ) + + elapsed_list = (time.time() - step_start_list) * 1000 + if "正在直播" in t_list: + total_elapsed = (time.time() - diag_start) * 1000 + list_text = t_list[:50] if t_list else "-" + log( + "步骤3[OCR直播列表]-命中", + 耗时毫秒=elapsed_list, + 总耗时毫秒=total_elapsed, + 识别文本=list_text, + ) + log_summary( + "阶段诊断完成", + 阶段="LIVE_LIST", + 原因=f"标题='{t_list}'", + 总耗时毫秒=total_elapsed, + ) + log_separator("最终结果:LIVE_LIST(直播列表)") + return "LIVE_LIST", "标题='{}'".format(t_list) + + return None