Files
douyi_guaji_main/douyin/fudai/douyin_fudai_contain_impl.py
T

1468 lines
56 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from __future__ import annotations
import os
import time
import threading
from typing import Tuple, Any, Optional, Dict, List
from douyin.utils.douyin_utils import extract_prize_value_yuan
from douyin.core import path_config
from douyin.fudai.core.fudai_filter import (
is_watch_task_fudai,
_is_short_countdown_text,
)
# 从新模块导入调试工具函数
from douyin.fudai.core.fudai_debug_utils import (
save_fudai_detail_debug_overview,
)
# 从子包 re-export(保持兼容):薄壳 forwarding
from douyin.fudai.douyin_fudai_core_pkg import (
attend_choujiang,
basic_join_decision,
check_have_reward_notice_confirm,
check_in_order_confirm_page,
check_lucky_draw_result,
get_reward,
_get_fudai_ocr_executor,
is_watch_task_fudai,
should_skip_fudai_by_config,
_is_short_countdown_text,
)
from douyin.fudai.douyin_fudai_core_pkg.contain_helpers import (
extract_participants_and_clean,
is_blank_text,
run_heavy_ocr,
should_use_heavy,
)
from douyin.fudai.douyin_fudai_core_pkg.button_state import (
apply_button_ocr_result,
decide_button_status,
)
from douyin.fudai.douyin_fudai_core_pkg.parallel_exec import run_parallel_tasks
from douyin.fudai.douyin_fudai_core_pkg.watch_task import (
handle_content_watch_task,
handle_task_list_watch_task,
)
from douyin.fudai.douyin_fudai_core_pkg.countdown_parse import (
extract_countdown_digits,
parse_countdown_time_text,
)
from douyin.fudai.douyin_fudai_core_pkg.contain_postprocess import (
log_enter_live_latency,
persist_room_detail_map,
update_live_room_title,
validate_value_ocr,
)
try:
from douyin.utils.ocr_preprocess import preprocess_for_ocr # type: ignore
except Exception: # pyright: ignore
# 在运行环境中可能缺少 OCR 预处理依赖,提供回退实现以避免导入失败导致程序中断。
def preprocess_for_ocr(_analyser, picname: str) -> str: # type: ignore # pyright: ignore
"""回退的 preprocess_for_ocr:直接返回 picname(不做预处理)。"""
try:
return str(picname)
except TypeError: # pyright: ignore
return picname
# 模块级线程池状态,用于复用 OCR 并行执行,避免频繁创建/销毁
def _detail_verbose_logs(analyser: Any) -> bool:
"""福袋详情 OCR 的中间态日志开关;默认关闭,避免日志刷屏和重复。"""
try:
return bool(getattr(analyser, "fudai_detail_verbose_logs", False))
except Exception:
return False
def _short_ocr_text(value: Any, limit: int = 80) -> str:
text = str(value or "").strip()
if not text:
return "未识别"
text = " ".join(text.split())
if len(text) <= limit:
return text
return f"{text[:limit]}..."
def _format_enabled_ocr(ocr_switches: Any) -> str:
fields = (
("content", "内容"),
("value", "价值"),
("countdown", "倒计时"),
("task", "任务"),
("task_count", "任务数"),
("button", "按钮"),
("participants", "人数"),
("live_title", "标题"),
)
enabled = []
for attr, label in fields:
try:
if bool(getattr(ocr_switches, attr, False)):
enabled.append(label)
except Exception:
continue
return ",".join(enabled) if enabled else "无"
def _slowest_ocr_task(durations: Dict[str, Any]) -> str:
if not durations:
return ""
try:
key, value = max(
durations.items(),
key=lambda item: float(item[1] or 0),
)
return f"{key}:{round(float(value or 0), 1)}ms"
except Exception:
return ""
def _log_detail_ocr_summary(
analyser: Any,
*,
total_ms: float,
renwu: int,
ui_version: str,
task_count_source: str,
ocr_switches: Any,
durations: Dict[str, Any],
fudai_content_raw: Any,
task_list_raw: Any,
countdown_mm_raw: Any,
countdown_ss_raw: Any,
countdown_full_raw: Any,
value_raw: Any,
button_text_raw: Any,
button_status: Any,
live_room_title_raw: Any,
participants_raw: Any,
heavy_fields: Any,
fallback: bool = False,
) -> None:
"""统一输出一条 OCR 摘要,替代分散的字段/耗时重复日志。"""
try:
mm, ss = _normalize_countdown_pair(countdown_mm_raw, countdown_ss_raw)
countdown = f"{mm}:{ss}" if mm and ss else _short_ocr_text(countdown_full_raw, 40)
heavy = ",".join(sorted(str(x) for x in (heavy_fields or []))) or "无"
payload = {
"ui_version": str(ui_version or "unknown"),
"task_count": int(renwu or 0),
"task_count_source": str(task_count_source or "unknown"),
"enabled_ocr": _format_enabled_ocr(ocr_switches),
"content": _short_ocr_text(fudai_content_raw),
"value": _short_ocr_text(value_raw, 40),
"countdown": countdown,
"task": _short_ocr_text(task_list_raw),
"button": _short_ocr_text(button_text_raw, 40),
"button_status": str(button_status or "未识别"),
"live_title": _short_ocr_text(live_room_title_raw, 60),
"participants": _short_ocr_text(participants_raw, 40),
"total_ms": round(float(total_ms or 0), 1),
"slowest_task": _slowest_ocr_task(durations),
"heavy_fields": heavy,
"fallback": bool(fallback),
}
analyser._log_kv("福袋详情", "OCR摘要", log_type="OCR_SUMMARY", **payload)
except Exception:
pass
def _emit_flow_flag(analyser: Any, flag: str, status: str, **extra) -> None:
try:
payload = {
"flag": str(flag or ""),
"status": str(status or ""),
"run_id": str(getattr(analyser, "_run_id", "") or ""),
}
payload.update(extra or {})
analyser._log_kv("流程标志", f"[FLOW_FLAG] {flag}:{status}", **payload)
except Exception:
pass
def _extract_two_digits(text: Any) -> str:
"""提取最多两位数字;与倒计时最终解析保持同一策略。"""
return extract_countdown_digits(str(text or ""), max_digits=2)
def _parse_mmss_from_full_text(text: Any) -> Tuple[str, str]:
"""从整行倒计时文本中提取 mm:ss,失败返回空。"""
import re
raw = str(text or "").strip()
if not raw:
return "", ""
def _fmt_pair(mm_s: str, ss_s: str) -> Tuple[str, str]:
try:
mm_i = int(mm_s)
ss_i = int(ss_s)
except Exception:
return "", ""
if 0 <= mm_i <= 59 and 0 <= ss_i <= 59:
return f"{mm_i:02d}", f"{ss_i:02d}"
return "", ""
# 1) 标准格式:13:40 / 13:40 / 13 40
m = re.search(r"(\d{1,2})\D+(\d{1,2})", raw)
if m:
mm, ss = _fmt_pair(m.group(1), m.group(2))
if mm and ss:
return mm, ss
# 2) 紧凑格式:1340后开奖 / 940后开奖
compact = re.search(
r"(?<!\d)(\d{3,4})(?=\D*(?:后开奖|开奖|后|$))",
raw,
)
if compact:
digits = compact.group(1)
if len(digits) == 4:
mm, ss = _fmt_pair(digits[:2], digits[2:])
if mm and ss:
return mm, ss
elif len(digits) == 3:
mm, ss = _fmt_pair(digits[:1], digits[1:])
if mm and ss:
return mm, ss
return "", ""
def _build_countdown_candidate_boxes(renwu: int) -> List[Tuple[int, int, int, int]]:
"""构建倒计时候选框列表:主候选优先,限制在同一纵向带,避免扫到价值/文案行。"""
from douyin.fudai.core.coords import (
COUNTDOWN_1010_1130,
COUNTDOWN_1110_1230,
COUNTDOWN_1140_1290,
COUNTDOWN_1460_1580,
)
if renwu == 1:
primary = COUNTDOWN_1140_1290
elif renwu == 2:
primary = COUNTDOWN_1110_1230
elif renwu == 3:
primary = COUNTDOWN_1010_1130
else:
primary = COUNTDOWN_1460_1580
def _center_y(box: Tuple[int, int, int, int]) -> int:
return int((int(box[1]) + int(box[3])) / 2)
# 经验阈值:仅在主候选上下约 220px 内补充候选,避免混入下方“参考价/价值”行。
primary_cy = _center_y(primary)
max_center_delta = 220
ordered: List[Tuple[int, int, int, int]] = []
for box in (
primary,
COUNTDOWN_1140_1290,
COUNTDOWN_1110_1230,
COUNTDOWN_1010_1130,
COUNTDOWN_1460_1580,
):
if box != primary and abs(_center_y(box) - primary_cy) > max_center_delta:
continue
if box not in ordered:
ordered.append(box)
return ordered
def _cut_countdown_candidate_regions(
analyser: Any,
box: Tuple[int, int, int, int],
mm_name: str,
ss_name: str,
*,
save_debug_images: bool = False,
) -> None:
"""按稳定分割比例切分钟/秒候选区域,降低混入“后开奖”等文本的概率。"""
x1, y1, x2, y2 = box
w = max(1, int(x2 - x1))
h = max(1, int(y2 - y1))
# 只截取倒计时数字行,降低把标题文案识别进分钟框的概率。
digit_y1 = min(y2 - 2, y1 + max(1, int(h * 0.34)))
digit_y2 = min(y2, max(digit_y1 + 2, y1 + max(2, int(h * 0.92))))
# 经验分割:分钟在左 0~26%,秒在中部 38%~64%,避开中间冒号与右侧“后开奖”文本。
mm_x1 = x1
mm_x2 = min(x2, x1 + max(1, int(w * 0.26)))
ss_x1 = min(x2 - 1, x1 + max(1, int(w * 0.38)))
ss_x2 = min(x2, max(ss_x1 + 1, x1 + max(2, int(w * 0.64))))
_ = save_debug_images
analyser.cut_pic(
mm_x1,
digit_y1,
mm_x2,
digit_y2,
"",
mm_name,
analyser.y_pianyi,
)
analyser.cut_pic(
ss_x1,
digit_y1,
ss_x2,
digit_y2,
"",
ss_name,
analyser.y_pianyi,
)
def _pick_best_countdown_candidate(
analyser: Any,
candidates: List[Dict[str, Any]],
*,
room_title_hint: str = "",
) -> Optional[Dict[str, Any]]:
"""从多个倒计时候选中挑选最可信的一个(数值合法 + 时间连续性)。"""
room_title_now = str(
room_title_hint or getattr(analyser, "current_room_title_raw", "") or ""
)
prev_room_title = str(getattr(analyser, "_countdown_last_room_title", "") or "")
same_room = bool(room_title_now and room_title_now == prev_room_title)
prev_total: Optional[int] = None
if same_room:
try:
prev_total_val = getattr(analyser, "_last_countdown_total_seconds", None)
if prev_total_val is not None:
prev_total = int(prev_total_val)
except Exception:
prev_total = None
best: Optional[Dict[str, Any]] = None
best_score = -10**9
for cand in candidates:
mm = _extract_two_digits(cand.get("mm_raw"))
ss = _extract_two_digits(cand.get("ss_raw"))
cand["mm"] = mm
cand["ss"] = ss
cand["valid"] = False
cand["total_seconds"] = None
score = 0
if cand.get("is_primary"):
score += 8
if mm and ss:
try:
mm_i = int(mm)
ss_i = int(ss)
if 0 <= mm_i <= 59 and 0 <= ss_i <= 59:
cand["valid"] = True
total = mm_i * 60 + ss_i
cand["total_seconds"] = total
score += 100
# 更偏好两段都完整为两位数字的候选。
if len(str(cand.get("mm_raw") or "")) >= 2:
score += 3
if len(str(cand.get("ss_raw") or "")) >= 2:
score += 3
# 同直播间内优先选择“倒计时递减”的候选,避免错行跳数。
if prev_total is not None:
if total <= prev_total + 3:
score += 24
else:
score -= 16
if abs(total - prev_total) <= 120:
score += 8
else:
score -= 12
except Exception:
score -= 12
else:
score -= 20
cand["score"] = score
if score > best_score:
best = cand
best_score = score
return best
def _normalize_countdown_pair(mm_raw: Any, ss_raw: Any) -> Tuple[str, str]:
"""统一净化倒计时分秒,保证输出仅为 00~59 两位数字;非法则返回空。"""
mm = _extract_two_digits(mm_raw)
ss = _extract_two_digits(ss_raw)
if not mm or not ss:
return "", ""
try:
mm_i = int(mm)
ss_i = int(ss)
except Exception:
return "", ""
if 0 <= mm_i <= 59 and 0 <= ss_i <= 59:
return f"{mm_i:02d}", f"{ss_i:02d}"
return "", ""
def _select_countdown_from_candidates(
analyser: Any,
candidate_meta: List[Dict[str, Any]],
primary_mm_raw: Any,
primary_ss_raw: Any,
extra_results: Dict[str, Any],
*,
room_title_hint: str = "",
) -> Tuple[Any, Any, Optional[Dict[str, Any]]]:
"""根据候选 OCR 结果选择最可信倒计时,返回 (mm_raw, ss_raw, best_candidate)。"""
candidates: List[Dict[str, Any]] = []
for meta in candidate_meta:
is_primary = bool(meta.get("is_primary"))
if is_primary:
mm_raw = primary_mm_raw
ss_raw = primary_ss_raw
else:
mm_raw = extra_results.get(str(meta.get("mm_result_key") or ""), None)
ss_raw = extra_results.get(str(meta.get("ss_result_key") or ""), None)
candidates.append(
{
"idx": int(meta.get("idx", -1)),
"box": tuple(meta.get("box") or ()),
"is_primary": is_primary,
"mm_raw": mm_raw,
"ss_raw": ss_raw,
}
)
best = _pick_best_countdown_candidate(
analyser,
candidates,
room_title_hint=room_title_hint,
)
if _detail_verbose_logs(analyser):
try:
snapshot = []
for cand in candidates:
box = cand.get("box") or ()
snapshot.append(
{
"idx": int(cand.get("idx", -1) or -1),
"primary": bool(cand.get("is_primary")),
"mm_raw": str(cand.get("mm_raw") or ""),
"ss_raw": str(cand.get("ss_raw") or ""),
"mm": str(cand.get("mm") or ""),
"ss": str(cand.get("ss") or ""),
"valid": bool(cand.get("valid")),
"score": int(cand.get("score", 0) or 0),
"box": tuple(box)[:4],
}
)
analyser._log_kv(
"倒计时",
"倒计时候选评分",
room_title_hint=str(room_title_hint or "")[:40],
candidate_count=len(snapshot),
candidates=snapshot,
)
except Exception:
pass
if best and bool(best.get("valid")):
mm_sel, ss_sel = _normalize_countdown_pair(best.get("mm"), best.get("ss"))
if mm_sel and ss_sel:
return mm_sel, ss_sel, best
mm_primary, ss_primary = _normalize_countdown_pair(primary_mm_raw, primary_ss_raw)
if mm_primary and ss_primary:
return mm_primary, ss_primary, best
return "", "", best
def get_fudai_contain(analyser, renwu: int = 2) -> Tuple[str, str, int, int, float]:
"""获取福袋的内容和倒计时(从 `fudai_analyse.get_fudai_contain` 抽离)。"""
_emit_flow_flag(analyser, "fudai_contain", "start", renwu=int(renwu or 0))
# 检查是否启用调试截图保存(默认False,正常运行时跳过以提升性能)
try:
save_debug_images = bool(
getattr(analyser, "fudai_detail_save_debug_images", False)
)
except Exception:
save_debug_images = False
# 如果analyser未提供配置,回退到全局配置文件
if not save_debug_images:
try:
import json
cfg_path = path_config.get_config_path()
if os.path.exists(cfg_path):
with open(cfg_path, "r", encoding="utf-8") as f:
cfg = json.load(f)
save_debug_images = bool(
cfg.get("fudai_detail_save_debug_images", False)
)
except Exception: # pyright: ignore[reportTooBroadExceptionType]
pass
# 轻重分流模式:auto(缺失字段才跑重)、light(只跑轻)、heavy(轻后必跑重)
try:
ocr_mode = str(
getattr(analyser, "fudai_detail_ocr_mode", "light") or "light"
).lower() # 优化:默认使用light模式,提升性能
except Exception:
ocr_mode = "light"
force_light = ocr_mode == "light"
force_heavy = ocr_mode == "heavy"
try:
from douyin.fudai.core.coords import get_fudai_detail_profile
from douyin.fudai.douyin_fudai_core_pkg.ocr_switches import (
resolve_fudai_detail_ocr_switches_from_analyser,
)
ui_mode = str(getattr(analyser, "fudai_detail_ui_mode", "auto") or "auto")
profile = get_fudai_detail_profile(ui_mode, task_count=renwu)
ui_version = str(profile.get("version") or "legacy")
ocr_switches = resolve_fudai_detail_ocr_switches_from_analyser(analyser)
except Exception:
from douyin.fudai.core.coords import get_fudai_detail_profile
from douyin.fudai.douyin_fudai_core_pkg.ocr_switches import (
FudaiDetailOcrSwitches,
)
ui_mode = "auto"
profile = get_fudai_detail_profile("auto", task_count=renwu)
ui_version = str(profile.get("version") or "v2_0")
ocr_switches = FudaiDetailOcrSwitches()
try:
setattr(analyser, "fudai_detail_ui_version_active", ui_version)
setattr(analyser, "fudai_detail_ocr_enabled_fields", sorted(ocr_switches.enabled_fields()))
except Exception:
pass
# 切割直播间标题图片(用于并行 OCR)
detail_debug_regions: List[Dict[str, Any]] = []
if ocr_switches.live_title:
live_title_box = tuple(profile.get("live_title") or (144, 144, 329, 185))
detail_debug_regions.append(
{
"key": "live_title",
"name": "直播间标题",
"coords": live_title_box,
"enabled": bool(ocr_switches.live_title),
}
)
analyser.cut_pic(
*live_title_box,
"",
"live_room_title_mid",
analyser.y_pianyi,
)
else:
detail_debug_regions.append(
{
"key": "live_title",
"name": "直播间标题",
"coords": tuple(profile.get("live_title") or (144, 144, 329, 185)),
"enabled": False,
}
)
# 切割内容/倒计时/价值(按 renwu 选 countdown_box)
from douyin.fudai.douyin_fudai_core_pkg.cut_regions import cut_detail_regions
countdown_box = cut_detail_regions(
analyser,
renwu,
save_debug_images=save_debug_images,
profile=profile,
ocr_switches=ocr_switches,
)
detail_debug_regions.extend(
[
{
"key": "content",
"name": "内容",
"coords": tuple(profile.get("content") or (390, 1240, 980, 1470)),
"enabled": bool(ocr_switches.content),
},
{
"key": "countdown",
"name": "倒计时",
"coords": tuple(countdown_box),
"enabled": bool(ocr_switches.countdown),
},
{
"key": "value",
"name": "价值",
"coords": tuple(profile.get("value") or (390, 1500, 780, 1600)),
"enabled": bool(ocr_switches.value),
},
]
)
# 额外整行倒计时图:用于优先解析 mm:ss,降低分钟框被文案污染的风险。
try:
if ocr_switches.countdown:
analyser.cut_pic(*countdown_box, "", "福袋倒计时_整行", analyser.y_pianyi)
except Exception:
pass
# 额外切割倒计时候选区域(用于并行多候选OCR,根治分钟框偏移)
countdown_candidate_meta: List[Dict[str, Any]] = []
try:
candidate_boxes = [tuple(countdown_box)] if ui_version == "v2_0" else _build_countdown_candidate_boxes(renwu)
for idx, box in enumerate(candidate_boxes):
is_primary = (tuple(box) == tuple(countdown_box))
if is_primary:
# 主候选沿用原始“福袋倒计时_分钟/秒”命名,兼容现有OCR与调试图像链路
mm_name = "福袋倒计时_分钟"
ss_name = "福袋倒计时_秒"
mm_key = "countdown_mm"
ss_key = "countdown_ss"
else:
mm_name = f"福袋倒计时_候选{idx}_分钟"
ss_name = f"福袋倒计时_候选{idx}_秒"
mm_key = f"countdown_mm_c{idx}"
ss_key = f"countdown_ss_c{idx}"
if ocr_switches.countdown:
_cut_countdown_candidate_regions(
analyser,
tuple(box),
mm_name,
ss_name,
save_debug_images=False,
)
countdown_candidate_meta.append(
{
"idx": idx,
"box": tuple(box),
"is_primary": is_primary,
"mm_name": mm_name,
"ss_name": ss_name,
"mm_result_key": mm_key,
"ss_result_key": ss_key,
}
)
except Exception:
countdown_candidate_meta = [
{
"idx": 0,
"box": tuple(countdown_box),
"is_primary": True,
"mm_name": "福袋倒计时_分钟",
"ss_name": "福袋倒计时_秒",
"mm_result_key": "countdown_mm",
"ss_result_key": "countdown_ss",
}
]
# 准备任务列表区域的截图(用于并行OCR)
try:
# 保存任务列表区域的调试截图(仅在配置启用时)
task_list_box = tuple(profile.get("task_list") or (50, 1621, 1032, 1960))
detail_debug_regions.append(
{
"key": "task",
"name": "任务",
"coords": task_list_box,
"enabled": bool(ocr_switches.task),
}
)
if ocr_switches.task:
analyser.cut_pic(*task_list_box, "", "福袋任务列表", analyser.y_pianyi)
except Exception as e_inner: # pyright: ignore[reportTooBroadExceptionType]
# 任务列表截图失败不影响后续流程,记录日志即可
try:
analyser._log_kv( # pyright: ignore[reportPrivateUsage]
"福袋",
"任务列表截图失败,继续后续流程",
error=str(e_inner),
)
except Exception: # pyright: ignore[reportTooBroadExceptionType]
pass
# 准备参与按钮区域的截图(用于并行OCR识别按钮状态)
try:
# 保存参与按钮区域的调试截图(仅在配置启用时)
button_box = tuple(profile.get("button") or (306, 2030, 780, 2180))
detail_debug_regions.append(
{
"key": "button",
"name": "按钮",
"coords": button_box,
"enabled": bool(ocr_switches.button),
}
)
if ocr_switches.button:
analyser.cut_pic(*button_box, "", "参与按钮", analyser.y_pianyi)
except Exception as e_inner: # pyright: ignore[reportTooBroadExceptionType]
# 按钮截图失败不影响后续流程,记录日志即可
try:
analyser._log_kv( # pyright: ignore[reportPrivateUsage]
"福袋",
"参与按钮截图失败,继续后续流程",
error=str(e_inner),
)
except Exception:
pass
# 准备参与人数区域(默认关闭,仅开关开启时切图和OCR)
try:
participants_box = tuple(profile.get("participants") or ())
if len(participants_box) == 4:
detail_debug_regions.append(
{
"key": "participants",
"name": "人数",
"coords": participants_box,
"enabled": bool(ocr_switches.participants),
}
)
if ocr_switches.participants and len(participants_box) == 4:
analyser.cut_pic(*participants_box, "", "福袋参与人数", analyser.y_pianyi)
except Exception:
pass
if save_debug_images:
try:
task_count_source = str(
getattr(analyser, "_fudai_detail_task_count_source", "") or ""
)
save_fudai_detail_debug_overview(
analyser,
detail_debug_regions,
ui_version=ui_version,
ocr_enabled=sorted(ocr_switches.enabled_fields()),
task_count=int(renwu or 0),
task_count_source=task_count_source,
y_pianyi=analyser.y_pianyi,
)
except Exception:
pass
# 并行执行六个操作:OCR识别任务列表/内容/倒计时分钟/倒计时秒/价值/参与按钮
# 任务数量已在 detect_fudai_detail_page 中检测,直接使用传入的 renwu 参数
ocr_start_time = time.time()
task_list_raw = None
fudai_content_raw = None
countdown_mm_raw = None
countdown_ss_raw = None
value_raw = None
button_text_raw = None
participants_raw = None
# 并行子任务耗时收集(ms),由各子任务在 finally 中写入
perf_task_durations: Dict[str, float] = {}
perf_task_lock = threading.Lock()
disable_content_recognition = not bool(ocr_switches.content)
# 7 个并行 OCR 子任务封装到 OcrTaskRunner
from douyin.fudai.douyin_fudai_core_pkg.ocr_tasks import OcrTaskRunner
_ocr_runner = OcrTaskRunner(
analyser,
perf_task_durations,
perf_task_lock,
disable_content_recognition=disable_content_recognition,
)
ocr_task_list = _ocr_runner.ocr_task_list
ocr_content = _ocr_runner.ocr_content
ocr_countdown_mm = _ocr_runner.ocr_countdown_mm
ocr_countdown_ss = _ocr_runner.ocr_countdown_ss
ocr_countdown_full = _ocr_runner.ocr_countdown_full
ocr_value = _ocr_runner.ocr_value
ocr_button = _ocr_runner.ocr_button
ocr_live_room_title = _ocr_runner.ocr_live_room_title
# 使用线程池并行执行OCR(包括任务列表OCR,与其他OCR一起并行执行)
# 任务数量已在 detect_fudai_detail_page 中检测,直接使用传入的 renwu 参数
try:
_emit_flow_flag(analyser, "fudai_contain_ocr", "parallel_start")
# 使用动态线程池大小,根据CPU核心数优化,并复用线程池
from operations.screenshot_service import get_optimal_workers
optimal_workers = get_optimal_workers()
executor = _get_fudai_ocr_executor(optimal_workers)
task_funcs = {}
if ocr_switches.task:
task_funcs["task_list"] = ocr_task_list
if ocr_switches.countdown:
task_funcs["countdown_mm"] = ocr_countdown_mm
task_funcs["countdown_ss"] = ocr_countdown_ss
task_funcs["countdown_full"] = ocr_countdown_full
if ocr_switches.value:
task_funcs["value"] = ocr_value
if ocr_switches.button:
task_funcs["button"] = ocr_button
if ocr_switches.live_title:
task_funcs["live_room_title"] = ocr_live_room_title
if ocr_switches.participants:
task_funcs["participants"] = (
lambda: analyser.operation.analyse_pic_word(
preprocess_for_ocr(analyser, "福袋参与人数"), 0
)
)
# 并行补充倒计时额外候选(保持并行,不降级)
for meta in countdown_candidate_meta:
if meta.get("is_primary"):
continue
mm_name = str(meta.get("mm_name") or "")
ss_name = str(meta.get("ss_name") or "")
mm_key = str(meta.get("mm_result_key") or "")
ss_key = str(meta.get("ss_result_key") or "")
if not mm_name or not ss_name or not mm_key or not ss_key:
continue
if ocr_switches.countdown:
task_funcs[mm_key] = (
lambda name=mm_name: "".join(
ch
for ch in str(
analyser.operation.analyse_pic_digits(name, 0) or ""
)
if ch.isdigit()
)
)
task_funcs[ss_key] = (
lambda name=ss_name: "".join(
ch
for ch in str(
analyser.operation.analyse_pic_digits(name, 0) or ""
)
if ch.isdigit()
)
)
# 如果配置启用,避免把 content 任务加入并行队列,彻底不调度该OCR
if ocr_switches.content:
task_funcs["content"] = ocr_content
task_list_raw = fudai_content_raw = countdown_mm_raw = None
countdown_ss_raw = value_raw = None
participants_raw = None
countdown_full_raw = None
live_room_title_raw = live_room_title_text_raw = None
# 收集结果并记录耗时
results, durations = run_parallel_tasks(task_funcs, executor)
extra_countdown_results: Dict[str, Any] = {}
for name, res in results.items():
if name == "task_list":
task_list_raw = res
elif name == "content":
fudai_content_raw = res
elif name == "countdown_mm":
countdown_mm_raw = res
elif name == "countdown_ss":
countdown_ss_raw = res
elif name == "countdown_full":
countdown_full_raw = res
elif name == "value":
value_raw = res
elif name == "button":
button_text_raw = res
elif name == "participants":
participants_raw = res
elif name == "live_room_title":
live_room_title_raw, live_room_title_text_raw = res
elif name.startswith("countdown_mm_c") or name.startswith("countdown_ss_c"):
extra_countdown_results[name] = res
try:
if not ocr_switches.countdown:
raise RuntimeError("countdown_ocr_disabled")
countdown_mm_raw, countdown_ss_raw, picked = _select_countdown_from_candidates(
analyser,
countdown_candidate_meta,
countdown_mm_raw,
countdown_ss_raw,
extra_countdown_results,
room_title_hint=str(live_room_title_text_raw or live_room_title_raw or ""),
)
mm_full, ss_full = _parse_mmss_from_full_text(countdown_full_raw)
if mm_full and ss_full:
countdown_mm_raw, countdown_ss_raw = mm_full, ss_full
if _detail_verbose_logs(analyser):
try:
analyser._log_kv(
"倒计时",
"倒计时整行优先命中,覆盖分割候选结果",
full_text=str(countdown_full_raw or "")[:80],
mm=mm_full,
ss=ss_full,
)
except Exception:
pass
if picked:
picked_idx = int(picked.get("idx", -1))
if _detail_verbose_logs(analyser):
analyser._log_kv(
"倒计时",
(
"倒计时候选选择结果"
f" idx={picked_idx}"
f" mm={str(picked.get('mm') or '')}"
f" ss={str(picked.get('ss') or '')}"
f" valid={bool(picked.get('valid'))}"
),
chosen_idx=picked_idx,
chosen_is_primary=bool(picked.get("is_primary")),
chosen_mm=str(picked.get("mm") or ""),
chosen_ss=str(picked.get("ss") or ""),
chosen_score=int(picked.get("score", 0) or 0),
chosen_valid=bool(picked.get("valid")),
candidates=len(countdown_candidate_meta),
)
# 回写连续性状态:供下一次候选评分使用(同直播间递减约束)。
try:
room_for_state = str(live_room_title_text_raw or live_room_title_raw or "").strip()
mm_state, ss_state = _normalize_countdown_pair(countdown_mm_raw, countdown_ss_raw)
if room_for_state and mm_state and ss_state:
analyser._countdown_last_room_title = room_for_state
analyser._last_countdown_total_seconds = int(mm_state) * 60 + int(ss_state)
except Exception:
pass
except Exception as e_pick:
try:
if str(e_pick) != "countdown_ocr_disabled":
analyser._log_kv(
"倒计时",
"倒计时候选选择异常,回退主候选",
error=str(e_pick),
error_type=type(e_pick).__name__,
level="WARNING",
)
except Exception:
pass
if _detail_verbose_logs(analyser):
try:
analyser._log_kv(
"性能",
"并行OCR子任务耗时(ms)",
**{f"ocr_{k}": v for k, v in durations.items()},
)
except Exception:
pass
_emit_flow_flag(
analyser,
"fudai_contain_ocr",
"parallel_done",
has_content=bool(fudai_content_raw),
has_button=bool(button_text_raw),
has_countdown_mm=bool(countdown_mm_raw),
has_countdown_ss=bool(countdown_ss_raw),
has_countdown_full=bool(countdown_full_raw),
)
# 轻重分流:轻量OCR跑完后,仅在缺字段或强制时补充重OCR
try:
heavy_fields = should_use_heavy(
force_light,
force_heavy,
fudai_content_raw,
countdown_mm_raw,
countdown_ss_raw,
value_raw,
task_list_raw,
button_text_raw,
)
except Exception:
heavy_fields = set()
allowed_heavy_fields: set[str] = set()
if ocr_switches.content:
allowed_heavy_fields.add("content")
if ocr_switches.countdown:
allowed_heavy_fields.update({"countdown_mm", "countdown_ss"})
if ocr_switches.value:
allowed_heavy_fields.add("value")
if ocr_switches.task:
allowed_heavy_fields.add("task_list")
if ocr_switches.button:
allowed_heavy_fields.add("button")
heavy_fields = set(heavy_fields) & allowed_heavy_fields
if heavy_fields:
heavy_res = run_heavy_ocr(analyser, heavy_fields)
picked_heavy = None
def _prefer(new_val: Any, old_val: Any):
return new_val if not is_blank_text(new_val) else old_val
task_list_raw = _prefer(heavy_res.get("task_list"), task_list_raw)
fudai_content_raw = _prefer(heavy_res.get("content"), fudai_content_raw)
countdown_mm_raw = _prefer(heavy_res.get("countdown_mm"), countdown_mm_raw)
countdown_ss_raw = _prefer(heavy_res.get("countdown_ss"), countdown_ss_raw)
value_raw = _prefer(heavy_res.get("value"), value_raw)
button_text_raw = _prefer(heavy_res.get("button"), button_text_raw)
try:
if ("countdown_mm" in heavy_fields) or ("countdown_ss" in heavy_fields):
countdown_mm_raw, countdown_ss_raw, picked_heavy = _select_countdown_from_candidates(
analyser,
countdown_candidate_meta,
countdown_mm_raw,
countdown_ss_raw,
extra_countdown_results,
room_title_hint=str(live_room_title_text_raw or live_room_title_raw or ""),
)
mm_full_h, ss_full_h = _parse_mmss_from_full_text(countdown_full_raw)
if mm_full_h and ss_full_h:
countdown_mm_raw, countdown_ss_raw = mm_full_h, ss_full_h
if picked_heavy:
picked_heavy_idx = int(picked_heavy.get("idx", -1))
if _detail_verbose_logs(analyser):
analyser._log_kv(
"倒计时",
(
"重OCR后倒计时候选复选结果"
f" idx={picked_heavy_idx}"
f" mm={str(picked_heavy.get('mm') or '')}"
f" ss={str(picked_heavy.get('ss') or '')}"
f" valid={bool(picked_heavy.get('valid'))}"
),
chosen_idx=picked_heavy_idx,
chosen_is_primary=bool(picked_heavy.get("is_primary")),
chosen_mm=str(picked_heavy.get("mm") or ""),
chosen_ss=str(picked_heavy.get("ss") or ""),
chosen_score=int(picked_heavy.get("score", 0) or 0),
chosen_valid=bool(picked_heavy.get("valid")),
candidates=len(countdown_candidate_meta),
)
except Exception:
pass
if _detail_verbose_logs(analyser):
try:
analyser._log_kv(
"性能",
"福袋详情OCR轻重分流:触发重OCR补齐",
mode=ocr_mode,
heavy_fields=sorted(list(heavy_fields)),
heavy_duration_ms=heavy_res.get("_duration_ms"),
)
except Exception:
pass
# 检查任务列表中是否包含"观看X分钟"任务(在并行OCR完成后检查)
if handle_task_list_watch_task(analyser, task_list_raw, fallback=False):
return "", "", 0, 0, 0.0
# 处理按钮识别结果,判断参与状态并做决策
button_status = None
if button_text_raw:
button_text = str(button_text_raw).strip()
if _detail_verbose_logs(analyser):
try:
analyser._log_kv(
"参与",
"主并行OCR识别参与按钮",
button_text=button_text if button_text else "",
)
except Exception:
pass
button_status = decide_button_status(button_text)
# 将按钮状态存储到 analyser 对象中
try:
apply_button_ocr_result(
analyser,
button_text_raw=button_text_raw,
button_status=button_status,
fudai_content_raw=fudai_content_raw,
task_list_raw=task_list_raw,
countdown_mm_raw=countdown_mm_raw,
countdown_ss_raw=countdown_ss_raw,
value_raw=value_raw,
live_room_title_raw=live_room_title_raw,
log_prefix="主",
record_room_pos=True,
)
except Exception:
pass
ocr_elapsed = time.time() - ocr_start_time
try:
task_count_source = str(
getattr(analyser, "_fudai_detail_task_count_source", "") or ""
)
_log_detail_ocr_summary(
analyser,
total_ms=ocr_elapsed * 1000,
renwu=renwu,
ui_version=ui_version,
task_count_source=task_count_source,
ocr_switches=ocr_switches,
durations=durations,
fudai_content_raw=fudai_content_raw,
task_list_raw=task_list_raw,
countdown_mm_raw=countdown_mm_raw,
countdown_ss_raw=countdown_ss_raw,
countdown_full_raw=countdown_full_raw,
value_raw=value_raw,
button_text_raw=button_text_raw,
button_status=button_status,
live_room_title_raw=live_room_title_raw,
participants_raw=participants_raw,
heavy_fields=heavy_fields,
)
except Exception as e_inner:
try:
analyser._log_kv(
"错误",
"记录OCR性能日志失败",
error=str(e_inner),
error_type=type(e_inner).__name__,
level="WARNING",
)
except Exception:
pass
except Exception as e_inner:
# 如果并行执行失败,按需决定是否走备用并行(可跳过以节省时间)
_emit_flow_flag(
analyser,
"fudai_contain_ocr",
"parallel_exception",
error_type=type(e_inner).__name__,
error=str(e_inner),
)
try:
analyser._log_kv(
"错误",
"并行OCR失败",
error=str(e_inner),
error_type=type(e_inner).__name__,
level="ERROR",
)
except Exception:
pass
# 可选跳过备用并行,加快失败路径(默认启用备用;可通过 analyser.enable_fudai_ocr_fallback 控制)
try:
enable_fallback_parallel = bool(
getattr(analyser, "enable_fudai_ocr_fallback", True)
)
except Exception:
enable_fallback_parallel = True
if not enable_fallback_parallel:
try:
analyser._log_kv(
"性能",
"跳过备用并行OCR以节省时间",
reason="fallback_disabled",
level="WARNING",
)
except Exception:
pass
return "", "", 0, 0, 0.0
# 使用线程池并行执行OCR(包括任务列表OCR,与其他OCR一起并行执行)
try:
# 6 个备用并行 OCR 子任务封装到 OcrTaskRunnerFallback
from douyin.fudai.douyin_fudai_core_pkg.ocr_tasks import (
OcrTaskRunnerFallback,
)
_fb_runner = OcrTaskRunnerFallback(
analyser,
disable_content_recognition=disable_content_recognition,
)
ocr_task_list_fallback = _fb_runner.ocr_task_list
ocr_content_fallback = _fb_runner.ocr_content
ocr_countdown_mm_fallback = _fb_runner.ocr_countdown_mm
ocr_countdown_ss_fallback = _fb_runner.ocr_countdown_ss
ocr_countdown_full_fallback = _fb_runner.ocr_countdown_full
ocr_value_fallback = _fb_runner.ocr_value
ocr_button_fallback = _fb_runner.ocr_button
# 使用动态线程池大小,并复用线程池
from operations.screenshot_service import get_optimal_workers
optimal_workers = get_optimal_workers()
executor = _get_fudai_ocr_executor(optimal_workers)
task_funcs = {}
if ocr_switches.task:
task_funcs["task_list"] = ocr_task_list_fallback
if ocr_switches.countdown:
task_funcs["countdown_mm"] = ocr_countdown_mm_fallback
task_funcs["countdown_ss"] = ocr_countdown_ss_fallback
task_funcs["countdown_full"] = ocr_countdown_full_fallback
if ocr_switches.value:
task_funcs["value"] = ocr_value_fallback
if ocr_switches.button:
task_funcs["button"] = ocr_button_fallback
if ocr_switches.participants:
task_funcs["participants"] = (
lambda: analyser.operation.analyse_pic_word("福袋参与人数", 0)
)
# 备用并行中也加入倒计时额外候选
for meta in countdown_candidate_meta:
if meta.get("is_primary"):
continue
mm_name = str(meta.get("mm_name") or "")
ss_name = str(meta.get("ss_name") or "")
mm_key = str(meta.get("mm_result_key") or "")
ss_key = str(meta.get("ss_result_key") or "")
if not mm_name or not ss_name or not mm_key or not ss_key:
continue
if ocr_switches.countdown:
task_funcs[mm_key] = (
lambda name=mm_name: "".join(
ch
for ch in str(
analyser.operation.analyse_pic_digits(name, 0) or ""
)
if ch.isdigit()
)
)
task_funcs[ss_key] = (
lambda name=ss_name: "".join(
ch
for ch in str(
analyser.operation.analyse_pic_digits(name, 0) or ""
)
if ch.isdigit()
)
)
# 如果配置允许,则加入备用 content OCR,否则不调度
if ocr_switches.content:
task_funcs["content"] = ocr_content_fallback
durations = {}
task_list_raw = fudai_content_raw = countdown_mm_raw = None
countdown_ss_raw = value_raw = button_text_raw = None
participants_raw = None
countdown_full_raw_fb = None
results, durations = run_parallel_tasks(task_funcs, executor)
extra_countdown_results_fb: Dict[str, Any] = {}
for name, res in results.items():
if name == "task_list":
task_list_raw = res
elif name == "content":
fudai_content_raw = res
elif name == "countdown_mm":
countdown_mm_raw = res
elif name == "countdown_ss":
countdown_ss_raw = res
elif name == "countdown_full":
countdown_full_raw_fb = res
elif name == "value":
value_raw = res
elif name == "button":
button_text_raw = res
elif name == "participants":
participants_raw = res
elif name.startswith("countdown_mm_c") or name.startswith("countdown_ss_c"):
extra_countdown_results_fb[name] = res
try:
if not ocr_switches.countdown:
raise RuntimeError("countdown_ocr_disabled")
countdown_mm_raw, countdown_ss_raw, picked_fb = _select_countdown_from_candidates(
analyser,
countdown_candidate_meta,
countdown_mm_raw,
countdown_ss_raw,
extra_countdown_results_fb,
room_title_hint=str(
getattr(analyser, "current_room_title_raw", "") or ""
),
)
mm_fb, ss_fb = _parse_mmss_from_full_text(countdown_full_raw_fb)
if mm_fb and ss_fb:
countdown_mm_raw, countdown_ss_raw = mm_fb, ss_fb
if picked_fb:
picked_fb_idx = int(picked_fb.get("idx", -1))
if _detail_verbose_logs(analyser):
analyser._log_kv(
"倒计时",
(
"备用并行倒计时候选选择结果"
f" idx={picked_fb_idx}"
f" mm={str(picked_fb.get('mm') or '')}"
f" ss={str(picked_fb.get('ss') or '')}"
f" valid={bool(picked_fb.get('valid'))}"
),
chosen_idx=picked_fb_idx,
chosen_is_primary=bool(picked_fb.get("is_primary")),
chosen_mm=str(picked_fb.get("mm") or ""),
chosen_ss=str(picked_fb.get("ss") or ""),
chosen_score=int(picked_fb.get("score", 0) or 0),
chosen_valid=bool(picked_fb.get("valid")),
candidates=len(countdown_candidate_meta),
)
try:
mm_state, ss_state = _normalize_countdown_pair(countdown_mm_raw, countdown_ss_raw)
room_for_state = str(
getattr(analyser, "current_room_title_raw", "") or ""
).strip()
if room_for_state and mm_state and ss_state:
analyser._countdown_last_room_title = room_for_state
analyser._last_countdown_total_seconds = int(mm_state) * 60 + int(ss_state)
except Exception:
pass
except Exception as e_fb_pick:
if str(e_fb_pick) != "countdown_ocr_disabled":
pass
if _detail_verbose_logs(analyser):
try:
analyser._log_kv(
"性能",
"备用并行OCR子任务耗时(ms)",
**{f"ocr_{k}": v for k, v in durations.items()},
)
except Exception:
pass
# 检查任务列表中是否包含"观看X分钟"任务(在并行OCR完成后检查)
if handle_task_list_watch_task(analyser, task_list_raw, fallback=True):
return "", "", 0, 0, 0.0
# 处理备用并行执行的按钮识别结果
button_status = None
if button_text_raw:
button_text = str(button_text_raw).strip()
if _detail_verbose_logs(analyser):
try:
analyser._log_kv(
"参与",
"备用并行OCR识别参与按钮",
button_text=button_text if button_text else "",
)
except Exception:
pass
button_status = decide_button_status(button_text)
# 将按钮状态存储到 analyser 对象中
try:
apply_button_ocr_result(
analyser,
button_text_raw=button_text_raw,
button_status=button_status,
fudai_content_raw=fudai_content_raw,
task_list_raw=task_list_raw,
countdown_mm_raw=countdown_mm_raw,
countdown_ss_raw=countdown_ss_raw,
value_raw=value_raw,
live_room_title_raw=live_room_title_raw,
log_prefix="备用",
record_room_pos=True,
)
except Exception:
pass
try:
task_count_source = str(
getattr(analyser, "_fudai_detail_task_count_source", "") or ""
)
_log_detail_ocr_summary(
analyser,
total_ms=(time.time() - ocr_start_time) * 1000,
renwu=renwu,
ui_version=ui_version,
task_count_source=task_count_source,
ocr_switches=ocr_switches,
durations=durations,
fudai_content_raw=fudai_content_raw,
task_list_raw=task_list_raw,
countdown_mm_raw=countdown_mm_raw,
countdown_ss_raw=countdown_ss_raw,
countdown_full_raw=countdown_full_raw_fb,
value_raw=value_raw,
button_text_raw=button_text_raw,
button_status=button_status,
live_room_title_raw=live_room_title_raw,
participants_raw=participants_raw,
heavy_fields=set(),
fallback=True,
)
except Exception:
pass
except Exception:
pass
# 先检查原始OCR内容是否是"观看X分钟"任务(在清理之前检查,避免丢失关键信息)
if handle_content_watch_task(analyser, fudai_content_raw, cleaned=False):
return "", "", 0, 0, 0.0
participants, copies, fudai_content_text = extract_participants_and_clean(
fudai_content_raw if fudai_content_raw is not None else ""
)
if ocr_switches.participants:
participants_only, copies_from_participants, _cleaned_participants = (
extract_participants_and_clean(participants_raw or "")
)
if participants_only:
participants = participants_only
if copies_from_participants and not copies:
copies = copies_from_participants
# 清理后再次检查观看任务
if handle_content_watch_task(analyser, fudai_content_text, cleaned=True):
return "", "", 0, 0, 0.0
# 输出福袋内容(使用明确的格式,便于GUI解析)
content_for_log = fudai_content_text if fudai_content_text else "未识别到"
# 移除重复输出,因为并行OCR详情中已经包含了此信息
# analyser._log_kv(
# "福袋",
# f"识别福袋内容:{content_for_log[:100]}", # 在消息中直接包含内容,确保可见
# level="DEBUG",
# content=content_for_log, # 同时作为字段输出
# )
log_enter_live_latency(analyser, copies, participants)
# 倒计时:直接使用分割OCR结果(分钟+秒),不再使用完整OCR
time_text = parse_countdown_time_text(analyser, countdown_mm_raw, countdown_ss_raw)
# 移除重复输出,因为并行OCR详情中已经包含了倒计时信息
# analyser._log_kv(
# "福袋", "识别倒计时", countdown=time_text if time_text else "未识别到"
# )
prize_value_yuan = extract_prize_value_yuan(
value_raw if value_raw is not None else ""
)
validate_value_ocr(analyser, value_raw, prize_value_yuan)
update_live_room_title(analyser, live_room_title_raw, live_room_title_text_raw)
persist_room_detail_map(
analyser,
fudai_content_text,
task_list_raw,
countdown_mm_raw,
countdown_ss_raw,
time_text,
participants,
copies,
prize_value_yuan,
value_raw,
button_text_raw,
live_room_title_raw,
live_room_title_text_raw,
)
_emit_flow_flag(
analyser,
"fudai_contain",
"return",
content_nonempty=bool(fudai_content_text),
time_text=str(time_text or ""),
prize_value_yuan=float(prize_value_yuan or 0.0),
)
return fudai_content_text, time_text, participants, copies, prize_value_yuan
# check_contain 已迁移到 douyin.fudai.core.fudai_filter(顶部导入)。
# 此处保留模块级名称以兼容历史 from douyin_fudai_core import check_contain。