233 lines
6.6 KiB
Python
233 lines
6.6 KiB
Python
"""
|
||
直播间标题处理模块
|
||
==================
|
||
|
||
处理直播间标题的OCR识别、噪音过滤、规范化等功能。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
from typing import Any, Tuple, List
|
||
from douyin.fudai.core.coords import LIVE_ROOM_TITLE
|
||
import re
|
||
import time
|
||
|
||
|
||
def _room_title_has_ellipsis(s: str) -> bool:
|
||
"""检查是否包含省略号。"""
|
||
if "…" in s:
|
||
return True
|
||
if re.search(r"\.{3,}", s):
|
||
return True
|
||
return False
|
||
|
||
|
||
def _strip_room_noise_text(s: str) -> str:
|
||
"""移除直播间名 OCR 常见噪音(点赞/关注/粉丝等)。"""
|
||
try:
|
||
t = str(s or "")
|
||
except Exception:
|
||
# 字符串转换失败,返回空字符串
|
||
return ""
|
||
t = t.replace("\r", "\n").replace("\t", " ")
|
||
t = re.sub(r"[\s\u3000]+", " ", t).strip()
|
||
# 计数噪音:例如 1.2万点赞 / 234点赞 / 3W点赞
|
||
t = re.sub(r"\d+(?:\.\d+)?\s*[万wW]?\s*(?:点赞|赞)", " ", t)
|
||
t = re.sub(r"\d+(?:\.\d+)?\s*[万wW]?\s*关注", " ", t)
|
||
t = re.sub(r"\d+(?:\.\d+)?\s*[万wW]?\s*粉丝", " ", t)
|
||
t = re.sub(r"\d+(?:\.\d+)?\s*[万wW]?\s*人(?:在线|在看)?", " ", t)
|
||
t = re.sub(r"\d+(?:\.\d+)?\s*[万wW]?\s*本场\s*(?:点赞|点\s*赞|点贊|点)", " ", t)
|
||
t = re.sub(r"本场\s*(?:点赞|点\s*赞|点贊|点)", " ", t)
|
||
for w in (
|
||
"点赞",
|
||
"赞",
|
||
"关注",
|
||
"粉丝",
|
||
"直播中",
|
||
"正在直播",
|
||
"进入直播间",
|
||
"推荐",
|
||
"人在线",
|
||
"在线",
|
||
):
|
||
t = t.replace(w, " ")
|
||
t = re.sub(r"\s+", " ", t).strip()
|
||
return t
|
||
|
||
|
||
def _extract_room_title_candidate(raw_text: str) -> str:
|
||
"""从 OCR 原文中提取更像"直播间名"的片段。"""
|
||
t = _strip_room_noise_text(raw_text)
|
||
if not t:
|
||
return ""
|
||
|
||
segs = [x.strip() for x in re.split(r"[\n ]+", t) if x.strip()]
|
||
best = ""
|
||
best_score = -999
|
||
for seg in segs:
|
||
if not seg:
|
||
continue
|
||
seg2 = re.sub(
|
||
r"\d+(?:\.\d+)?\s*[万wW]?\s*(?:点赞|赞|关注|粉丝)$", "", seg
|
||
).strip()
|
||
if not seg2:
|
||
continue
|
||
seg = seg2
|
||
score = len(seg)
|
||
if _room_title_has_ellipsis(seg):
|
||
score += 30
|
||
if re.search(r"[\u4e00-\u9fffA-Za-z]", seg):
|
||
score += 5
|
||
if score > best_score:
|
||
best_score = score
|
||
best = seg
|
||
|
||
if best:
|
||
m = re.search(r"(.*?)(…|\.{3,})", best)
|
||
if m:
|
||
cand = (m.group(1) + m.group(2)).strip()
|
||
return cand if cand else best.strip()
|
||
return best.strip()
|
||
|
||
m2 = re.search(r"(.*?)(…|\.{3,})", t)
|
||
if m2:
|
||
cand2 = (m2.group(1) + m2.group(2)).strip()
|
||
if cand2:
|
||
return cand2
|
||
return t.strip()
|
||
|
||
|
||
def _normalize_room_title(t: str) -> str:
|
||
"""规范化直播间标题。"""
|
||
s = _extract_room_title_candidate(str(t or ""))
|
||
s = s.replace("\r", " ").replace("\n", " ").strip()
|
||
s = re.sub(r"\s+", "", s)
|
||
for w in (
|
||
"点赞",
|
||
"赞",
|
||
"关注",
|
||
"粉丝",
|
||
"直播中",
|
||
"正在直播",
|
||
"进入直播间",
|
||
"推荐",
|
||
"在线",
|
||
"人在线",
|
||
):
|
||
s = s.replace(w, "")
|
||
s = re.sub(r"\d+(?:\.\d+)?[万wW]?(?:点赞|赞|关注|粉丝)", "", s)
|
||
s = re.sub(r"\d+(?:\.\d+)?[万wW]?本场(?:点赞|点赞|点贊|点)", "", s)
|
||
s = re.sub(r"本场(?:点赞|点赞|点贊|点)", "", s)
|
||
s = re.sub(r"[^0-9A-Za-z\u4e00-\u9fff]", "", s)
|
||
return s
|
||
|
||
|
||
def _ocr_try_room_title(
|
||
analyser: Any, cut_name: str, change_color: int
|
||
) -> Tuple[str, str]:
|
||
"""尝试 OCR 识别直播间标题。"""
|
||
try:
|
||
raw0 = analyser.operation.analyse_pic_word(cut_name, change_color)
|
||
except Exception as e:
|
||
try:
|
||
analyser._log_kv(
|
||
"错误",
|
||
"OCR识别直播间标题失败",
|
||
error=str(e),
|
||
error_type=type(e).__name__,
|
||
cut_name=cut_name,
|
||
level="WARNING",
|
||
)
|
||
except Exception:
|
||
pass
|
||
raw0 = ""
|
||
raw_text0 = str(raw0 or "").replace("\r", "\n").strip()
|
||
cand_raw0 = _extract_room_title_candidate(raw_text0)
|
||
title0 = _normalize_room_title(cand_raw0)
|
||
return title0, cand_raw0
|
||
|
||
|
||
def _pick_best_room_title(
|
||
cands: List[Tuple[str, str, str]], min_len: int = 3
|
||
) -> Tuple[str, str]:
|
||
"""从多个候选结果中选择最佳的一个。"""
|
||
best = ("", "")
|
||
best_score = -1
|
||
for t_norm, t_raw, tag in cands:
|
||
if not t_raw and not t_norm:
|
||
continue
|
||
ln = len(t_norm or "")
|
||
score = ln
|
||
if ln >= min_len:
|
||
score += 10
|
||
if ln == 1:
|
||
score -= 4
|
||
if ln == 2:
|
||
score -= 1
|
||
if _room_title_has_ellipsis(t_raw or ""):
|
||
score += 8
|
||
if tag.endswith("_cc0"):
|
||
score += 2
|
||
if tag.startswith("large"):
|
||
score += 2
|
||
if tag.startswith("mid"):
|
||
score += 1
|
||
if score > best_score:
|
||
best_score = score
|
||
best = (t_norm, t_raw)
|
||
return best
|
||
|
||
|
||
def read_live_room_title_core(
|
||
analyser: Any,
|
||
y_pianyi: int,
|
||
min_len: int = 3,
|
||
) -> Tuple[str, str]:
|
||
"""读取当前直播间顶部区域的"主播名/直播间名"(OCR,尽量轻量)。
|
||
|
||
只使用指定区域 (144,144) 到 (329,185) 进行识别,避免多次截图。
|
||
"""
|
||
try:
|
||
analyser.operation.get_screenshot(analyser.device_id)
|
||
except Exception as e:
|
||
try:
|
||
analyser._log_kv(
|
||
"错误",
|
||
"获取截图失败(读取直播间标题)",
|
||
error=str(e),
|
||
error_type=type(e).__name__,
|
||
level="WARNING",
|
||
)
|
||
except Exception:
|
||
pass
|
||
|
||
# 只使用指定区域 (144,144) 到 (329,185),只尝试一次OCR(mode 0)
|
||
try:
|
||
analyser.cut_pic(*LIVE_ROOM_TITLE, "", "live_room_title_mid", y_pianyi)
|
||
t, r = _ocr_try_room_title(analyser, "live_room_title_mid", 0)
|
||
title = t
|
||
raw_text = r
|
||
except Exception as e:
|
||
try:
|
||
analyser._log_kv(
|
||
"错误",
|
||
"OCR识别直播间标题失败",
|
||
error=str(e),
|
||
error_type=type(e).__name__,
|
||
level="WARNING",
|
||
)
|
||
except Exception:
|
||
pass
|
||
title = ""
|
||
raw_text = ""
|
||
|
||
# 更新 analyser 的属性
|
||
try:
|
||
analyser.current_room_title_raw = raw_text
|
||
if title:
|
||
analyser.current_room_title = title
|
||
except Exception:
|
||
pass
|
||
|
||
return title, raw_text
|