add countdown OCR voting fallback
This commit is contained in:
1 parent
ab664e4da8
commit
3ace80dcca
2 files changed
+313
-19
No files matched your search
@@ -18,7 +18,7 @@ from __future__ import annotations
|
||||
|
||||
import threading
|
||||
import time
|
||||
from typing import Any, Dict, Optional, Tuple
|
||||
from typing import Any, Dict, Optional, Sequence, Tuple
|
||||
|
||||
|
||||
# OCR 按钮关键词列表(识别到任一即可短路返回,省 50% 时间)
|
||||
@@ -48,14 +48,197 @@ def _safe_preprocess(analyser, name: str) -> str:
|
||||
return name
|
||||
|
||||
|
||||
def _read_countdown_digits(analyser, picname: str) -> str:
|
||||
"""读取倒计时分/秒小图,并把 OCR 零形噪声归一成数字。"""
|
||||
def _valid_countdown_digits(text: Any) -> str:
|
||||
"""提取合法倒计时两位数字;非法或越界返回空。"""
|
||||
from douyin.fudai.douyin_fudai_core_pkg.countdown_parse import (
|
||||
extract_countdown_digits,
|
||||
)
|
||||
|
||||
raw = str(analyser.operation.analyse_pic_digits(picname, 0) or "")
|
||||
return extract_countdown_digits(raw, max_digits=2)
|
||||
digits = extract_countdown_digits(str(text or ""), max_digits=2)
|
||||
if not digits:
|
||||
return ""
|
||||
try:
|
||||
value = int(digits)
|
||||
except Exception:
|
||||
return ""
|
||||
return digits if 0 <= value <= 59 else ""
|
||||
|
||||
|
||||
def _choose_countdown_vote(votes: Sequence[str]) -> str:
|
||||
"""按多数票选择倒计时数字;最高票并列时返回空,避免错识别误参与。"""
|
||||
if not votes:
|
||||
return ""
|
||||
counts: Dict[str, int] = {}
|
||||
for value in votes:
|
||||
counts[value] = counts.get(value, 0) + 1
|
||||
max_count = max(counts.values())
|
||||
winners = [value for value, count in counts.items() if count == max_count]
|
||||
return winners[0] if len(winners) == 1 else ""
|
||||
|
||||
|
||||
def _cv_detect_two_zero_digits(picname: str) -> str:
|
||||
"""用简单像素组件检测倒计时分钟图中的粉色 00。"""
|
||||
try:
|
||||
from PIL import Image
|
||||
from douyin.core.path_config import get_cut_pic_path
|
||||
|
||||
path = get_cut_pic_path(picname)
|
||||
img_raw = Image.open(path)
|
||||
try:
|
||||
img_raw.load()
|
||||
img = img_raw.convert("RGB")
|
||||
finally:
|
||||
img_raw.close()
|
||||
w, h = img.size
|
||||
if w <= 0 or h <= 0:
|
||||
return ""
|
||||
|
||||
mask = set()
|
||||
for y in range(h):
|
||||
for x in range(w):
|
||||
r, g, b = img.getpixel((x, y))
|
||||
if r > 180 and g < 140 and b > 110 and (r - g) > 70:
|
||||
mask.add((x, y))
|
||||
|
||||
components = []
|
||||
while mask:
|
||||
start = mask.pop()
|
||||
stack = [start]
|
||||
xs = []
|
||||
ys = []
|
||||
while stack:
|
||||
x, y = stack.pop()
|
||||
xs.append(x)
|
||||
ys.append(y)
|
||||
for nx in (x - 1, x, x + 1):
|
||||
for ny in (y - 1, y, y + 1):
|
||||
if (nx, ny) in mask:
|
||||
mask.remove((nx, ny))
|
||||
stack.append((nx, ny))
|
||||
if len(xs) >= 20:
|
||||
components.append((len(xs), (min(xs), min(ys), max(xs), max(ys))))
|
||||
|
||||
components = sorted(components, key=lambda item: item[0], reverse=True)[:2]
|
||||
if len(components) != 2:
|
||||
return ""
|
||||
areas = [item[0] for item in components]
|
||||
boxes = [item[1] for item in components]
|
||||
if min(areas) / max(areas) < 0.70:
|
||||
return ""
|
||||
widths = [box[2] - box[0] + 1 for box in boxes]
|
||||
heights = [box[3] - box[1] + 1 for box in boxes]
|
||||
if min(widths) < 8 or min(heights) < 10:
|
||||
return ""
|
||||
if max(abs(heights[0] - heights[1]), abs(widths[0] - widths[1])) > 8:
|
||||
return ""
|
||||
return "00"
|
||||
except Exception:
|
||||
return ""
|
||||
|
||||
|
||||
def _read_countdown_digits(
|
||||
analyser,
|
||||
picname: str,
|
||||
*,
|
||||
fallback_picnames: Sequence[str] = (),
|
||||
) -> str:
|
||||
"""读取倒计时分/秒小图,使用多预处理 OCR 投票降低单次识别歪斜。"""
|
||||
picnames = []
|
||||
for name in (picname, *tuple(fallback_picnames or ())):
|
||||
if name and name not in picnames:
|
||||
picnames.append(str(name))
|
||||
|
||||
votes = []
|
||||
raw_results = []
|
||||
last_exc: Optional[BaseException] = None
|
||||
successful_call = False
|
||||
|
||||
for name in picnames:
|
||||
for mode in (0, 1, 2):
|
||||
try:
|
||||
raw = str(analyser.operation.analyse_pic_digits(name, mode) or "")
|
||||
successful_call = True
|
||||
except Exception as exc:
|
||||
last_exc = exc
|
||||
continue
|
||||
digits = _valid_countdown_digits(raw)
|
||||
raw_results.append((name, mode, raw, digits))
|
||||
if digits:
|
||||
votes.append(digits)
|
||||
|
||||
chosen = _choose_countdown_vote(votes)
|
||||
if chosen:
|
||||
if chosen != "00":
|
||||
for name in reversed(picnames):
|
||||
cv_digits = _cv_detect_two_zero_digits(name)
|
||||
if cv_digits == "00":
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"倒计时",
|
||||
"倒计时数字OCR疑似把00识别歪,CV检测覆盖为00",
|
||||
picname=name,
|
||||
ocr_chosen=chosen,
|
||||
votes=list(votes),
|
||||
raw_results=raw_results[:8],
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
return "00"
|
||||
if len(set(votes)) > 1:
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"倒计时",
|
||||
"倒计时数字OCR投票存在分歧,已选择多数/优先结果",
|
||||
picname=picname,
|
||||
chosen=chosen,
|
||||
votes=list(votes),
|
||||
raw_results=raw_results[:8],
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
return chosen
|
||||
|
||||
if not successful_call and last_exc is not None:
|
||||
raise last_exc
|
||||
|
||||
if votes:
|
||||
for name in reversed(picnames):
|
||||
cv_digits = _cv_detect_two_zero_digits(name)
|
||||
if cv_digits:
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"倒计时",
|
||||
"倒计时数字OCR分歧,CV检测命中00兜底",
|
||||
picname=name,
|
||||
votes=list(votes),
|
||||
raw_results=raw_results[:8],
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
return cv_digits
|
||||
|
||||
if picnames:
|
||||
primary_votes = [
|
||||
digits
|
||||
for name, _mode, _raw, digits in raw_results
|
||||
if name == picnames[0] and digits
|
||||
]
|
||||
if primary_votes and len(set(primary_votes)) == 1:
|
||||
primary_chosen = primary_votes[0]
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"倒计时",
|
||||
"倒计时数字OCR投票打平,优先采用预处理图一致结果",
|
||||
picname=picnames[0],
|
||||
chosen=primary_chosen,
|
||||
votes=list(votes),
|
||||
raw_results=raw_results[:8],
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
return primary_chosen
|
||||
|
||||
return ""
|
||||
|
||||
|
||||
class OcrTaskRunner:
|
||||
@@ -162,8 +345,13 @@ class OcrTaskRunner:
|
||||
"""OCR 识别福袋倒计时分钟。"""
|
||||
start = time.time()
|
||||
try:
|
||||
picname = _safe_preprocess(self.analyser, "福袋倒计时_分钟")
|
||||
return _read_countdown_digits(self.analyser, picname)
|
||||
raw_name = "福袋倒计时_分钟"
|
||||
picname = _safe_preprocess(self.analyser, raw_name)
|
||||
return _read_countdown_digits(
|
||||
self.analyser,
|
||||
picname,
|
||||
fallback_picnames=(raw_name,),
|
||||
)
|
||||
except Exception as e:
|
||||
self._log_warn("OCR识别倒计时分钟失败", e)
|
||||
return None
|
||||
@@ -174,8 +362,13 @@ class OcrTaskRunner:
|
||||
"""OCR 识别福袋倒计时秒。"""
|
||||
start = time.time()
|
||||
try:
|
||||
picname = _safe_preprocess(self.analyser, "福袋倒计时_秒")
|
||||
return _read_countdown_digits(self.analyser, picname)
|
||||
raw_name = "福袋倒计时_秒"
|
||||
picname = _safe_preprocess(self.analyser, raw_name)
|
||||
return _read_countdown_digits(
|
||||
self.analyser,
|
||||
picname,
|
||||
fallback_picnames=(raw_name,),
|
||||
)
|
||||
except Exception as e:
|
||||
self._log_warn("OCR识别倒计时秒失败", e)
|
||||
return None
|
||||
|
||||
Reference in new issue
Block a user