feat: 添加直播间标题识别功能,优化缓存机制以避免重复OCR

This commit is contained in:
Your Name committed 2026-01-16 13:08:26 +08:00
1 parent 13bf52f7a9
commit f5618d014d
4 files changed
+143 -55

No files matched your search

+118 -26
View File
@@ -508,8 +508,12 @@ def get_fudai_contain(analyser, renwu: int = 2) -> Tuple[str, str, int, int, flo
COUNTDOWN_1460_1580,
TASK_LIST,
PARTICIPATE_BUTTON,
LIVE_ROOM_TITLE,
)
# 切割直播间标题图片(用于并行OCR)
analyser.cut_pic(*LIVE_ROOM_TITLE, "", "live_room_title_mid", analyser.y_pianyi)
value_box = VALUE_BOX_TARGET
if renwu == 2: # 如果是2个任务的
# 优化:扩大Y范围向上,确保包含任务描述(如"观看X分钟"等)
@@ -1014,6 +1018,47 @@ def get_fudai_contain(analyser, renwu: int = 2) -> Tuple[str, str, int, int, flo
pass
return None
def ocr_live_room_title():
"""OCR识别直播间标题"""
start = time.time()
try:
# 导入直播间标题识别函数
from fudai_detail_modules.fudai_detail_room_title import _ocr_try_room_title
# 使用已切割的图片进行OCR识别
title, raw_text = _ocr_try_room_title(analyser, "live_room_title_mid", 0)
# 返回结果元组
return title, raw_text
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
return "", ""
finally:
try:
dur = round((time.time() - start) * 1000, 1)
with perf_task_lock:
perf_task_durations["ocr_live_room_title"] = dur
try:
analyser._log_kv(
"性能",
"并行OCR子任务单项耗时(ms)",
field="live_room_title",
duration_ms=dur,
)
except Exception:
pass
except Exception:
pass
# 使用线程池并行执行OCR(包括任务列表OCR,与其他OCR一起并行执行)
# 任务数量已在 detect_fudai_detail_page 中检测,直接使用传入的 renwu 参数
try:
@@ -1029,6 +1074,7 @@ def get_fudai_contain(analyser, renwu: int = 2) -> Tuple[str, str, int, int, flo
"countdown_ss": ocr_countdown_ss,
"value": ocr_value,
"button": ocr_button,
"live_room_title": ocr_live_room_title,
}
# 如果配置启用,避免把 content 任务加入并行队列,彻底不调度该OCR
if not disable_content_recognition:
@@ -1042,6 +1088,7 @@ def get_fudai_contain(analyser, renwu: int = 2) -> Tuple[str, str, int, int, flo
task_list_raw = fudai_content_raw = countdown_mm_raw = None
countdown_ss_raw = value_raw = button_text_raw = None
live_room_title_raw = live_room_title_text_raw = None
# 收集结果并记录耗时
durations = {}
@@ -1064,6 +1111,8 @@ def get_fudai_contain(analyser, renwu: int = 2) -> Tuple[str, str, int, int, flo
value_raw = res
elif name == "button":
button_text_raw = res
elif name == "live_room_title":
live_room_title_raw, live_room_title_text_raw = res
try:
analyser._log_kv(
@@ -2067,6 +2116,30 @@ def get_fudai_contain(analyser, renwu: int = 2) -> Tuple[str, str, int, int, flo
except Exception:
pass
# 处理直播间标题结果
if live_room_title_raw and live_room_title_text_raw:
try:
analyser.current_room_title = live_room_title_raw
analyser.current_room_title_raw = live_room_title_text_raw
analyser._log_kv(
"直播间标题",
"OCR识别直播间标题",
title=live_room_title_raw,
raw_text=live_room_title_text_raw,
log_type="INFO",
)
except Exception as e_inner:
try:
analyser._log_kv(
"错误",
"设置直播间标题结果失败",
error=str(e_inner),
error_type=type(e_inner).__name__,
level="WARNING",
)
except Exception:
pass
return fudai_content_text, time_text, participants, copies, prize_value_yuan
@@ -4553,38 +4626,57 @@ def basic_join_decision(
getattr(analyser, "high_value_room_revisit_enabled", False)
)
if revisit_enabled:
# 每次记录预约时,都重新读取直播间标题,不使用缓存
# 这样可以确保每个预约记录的是当时的真实直播间标题
try:
# 在函数开头导入,避免作用域问题
from douyin.fudai import (
douyin_fudai_detail as dfd_module,
)
y_pianyi_val = int(getattr(analyser, "y_pianyi", 0) or 0)
current_room_title, current_room_title_raw = (
dfd_module.read_live_room_title_core(analyser, y_pianyi_val)
)
# 更新 analyser 的属性,用于状态文件显示当前直播间
try:
analyser.current_room_title = current_room_title
analyser.current_room_title_raw = current_room_title_raw
except Exception:
pass
except Exception as e_inner:
# 优先使用福袋详情识别时已缓存的直播间标题,避免重复截图和OCR
current_room_title = str(
getattr(analyser, "current_room_title", "") or ""
).strip()
current_room_title_raw = str(
getattr(analyser, "current_room_title_raw", "") or ""
).strip()
# 如果缓存为空,才重新读取直播间标题
if not current_room_title:
try:
analyser._log_kv(
"预约",
"读取直播间标题失败,尝试使用缓存",
error=str(e_inner),
error_type=type(e_inner).__name__,
"缓存中无直播间标题,重新读取",
log_type="DEBUG",
)
# 在函数开头导入,避免作用域问题
from douyin.fudai import (
douyin_fudai_detail as dfd_module,
)
y_pianyi_val = int(getattr(analyser, "y_pianyi", 0) or 0)
current_room_title, current_room_title_raw = (
dfd_module.read_live_room_title_core(analyser, y_pianyi_val)
)
# 更新 analyser 的属性,用于状态文件显示当前直播间
try:
analyser.current_room_title = current_room_title
analyser.current_room_title_raw = current_room_title_raw
except Exception:
pass
except Exception as e_inner:
try:
analyser._log_kv(
"预约",
"读取直播间标题失败",
error=str(e_inner),
error_type=type(e_inner).__name__,
)
except Exception:
pass
else:
try:
analyser._log_kv(
"预约",
"复用福袋详情识别时已缓存的直播间标题",
title=current_room_title,
log_type="DEBUG",
)
except Exception:
pass
# 如果读取失败,尝试使用缓存(作为兜底)
current_room_title = str(
getattr(analyser, "current_room_title", "") or ""
)
current_room_title_raw = str(
getattr(analyser, "current_room_title_raw", "") or ""
)
@@ -224,6 +224,25 @@ class ReservationHandler:
try:
room_pos = int(getattr(analyser, "room_pos", 0) or 0)
# 检查是否已经为当前直播间识别过标题,避免重复截图和OCR
identified_positions = getattr(analyser, "_room_title_identified_positions", set())
if not isinstance(identified_positions, set):
identified_positions = set()
if room_pos in identified_positions:
# 已经识别过,直接使用缓存的标题
try:
analyser._log_kv(
"预约",
"[预约] 复用已缓存的直播间标题",
room_pos=room_pos,
title=getattr(analyser, "current_room_title", ""),
log_type="DEBUG",
)
except Exception:
pass
return
# 读取当前直播间标题
try:
y_pianyi_val = int(getattr(analyser, "y_pianyi", 0) or 0)
@@ -240,6 +259,10 @@ class ReservationHandler:
except Exception:
pass
# 标记该位置已识别过标题
identified_positions.add(room_pos)
analyser._room_title_identified_positions = identified_positions
# 记录到映射表(包含福袋信息)
try:
# 检查是否有福袋
+1 -28
View File
@@ -370,34 +370,7 @@ def read_fudai_detail_once(analyser: Any) -> Tuple[str, Tuple, dict]:
try:
ocr_start = time.time()
# 在开始OCR识别前,先识别直播间标题
try:
from fudai_detail_modules.fudai_detail_room_title import (
read_live_room_title_core,
)
y_pianyi_val = int(getattr(analyser, "y_pianyi", 0) or 0)
current_room_title, current_room_title_raw = read_live_room_title_core(
analyser, y_pianyi_val
)
# 更新 analyser 的属性
try:
analyser.current_room_title = current_room_title
analyser.current_room_title_raw = current_room_title_raw
except Exception:
pass
except Exception as e:
try:
analyser._log_kv(
"福袋详情",
"read_fudai_detail_once:直播间标题识别失败",
error=str(e),
log_type="WARNING",
)
except Exception:
pass
content, time_text, participants, copies, prize_value_yuan = get_fudai_contain(
analyser, renwu=task_count
)
@@ -207,7 +207,7 @@ def read_live_room_title_core(
t, r = _ocr_try_room_title(analyser, "live_room_title_mid", 0)
title = t
raw_text = r
# 输出识别结果日志
try:
analyser._log_kv(