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
|
||||
|
||||
+111
-10
@@ -19,6 +19,9 @@ from douyin.fudai.douyin_fudai_core_pkg.ocr_tasks import (
|
||||
OcrTaskRunner,
|
||||
OcrTaskRunnerFallback,
|
||||
_BUTTON_KEYWORDS,
|
||||
_choose_countdown_vote,
|
||||
_cv_detect_two_zero_digits,
|
||||
_read_countdown_digits,
|
||||
_safe_preprocess,
|
||||
)
|
||||
|
||||
@@ -137,28 +140,41 @@ class TestOcrCountdown:
|
||||
runner.analyser.operation.analyse_pic_digits = MagicMock(
|
||||
return_value="abc23def"
|
||||
)
|
||||
assert runner.ocr_countdown_mm() == "23"
|
||||
with patch(
|
||||
"douyin.fudai.douyin_fudai_core_pkg.ocr_tasks._cv_detect_two_zero_digits",
|
||||
return_value="",
|
||||
):
|
||||
assert runner.ocr_countdown_mm() == "23"
|
||||
assert "ocr_countdown_mm" in durations
|
||||
|
||||
def test_mm_uses_preprocessed_countdown_image(self):
|
||||
runner, durations = _mk_runner()
|
||||
runner.analyser.operation.analyse_pic_digits = MagicMock(return_value="23")
|
||||
runner.analyser.operation.analyse_pic_digits = MagicMock(
|
||||
side_effect=["23", "", "", "", "", ""]
|
||||
)
|
||||
|
||||
with patch(
|
||||
"douyin.fudai.douyin_fudai_core_pkg.ocr_tasks._safe_preprocess",
|
||||
return_value="福袋倒计时_分钟_preprocessed",
|
||||
) as preprocess:
|
||||
) as preprocess, patch(
|
||||
"douyin.fudai.douyin_fudai_core_pkg.ocr_tasks._cv_detect_two_zero_digits",
|
||||
return_value="",
|
||||
):
|
||||
assert runner.ocr_countdown_mm() == "23"
|
||||
|
||||
preprocess.assert_called_once_with(runner.analyser, "福袋倒计时_分钟")
|
||||
runner.analyser.operation.analyse_pic_digits.assert_called_once_with(
|
||||
"福袋倒计时_分钟_preprocessed",
|
||||
0,
|
||||
runner.analyser.operation.analyse_pic_digits.assert_any_call(
|
||||
"福袋倒计时_分钟_preprocessed", 0
|
||||
)
|
||||
runner.analyser.operation.analyse_pic_digits.assert_any_call(
|
||||
"福袋倒计时_分钟", 0
|
||||
)
|
||||
|
||||
def test_ss_uses_preprocessed_countdown_image(self):
|
||||
runner, durations = _mk_runner()
|
||||
runner.analyser.operation.analyse_pic_digits = MagicMock(return_value="45")
|
||||
runner.analyser.operation.analyse_pic_digits = MagicMock(
|
||||
side_effect=["45", "", "", "", "", ""]
|
||||
)
|
||||
|
||||
with patch(
|
||||
"douyin.fudai.douyin_fudai_core_pkg.ocr_tasks._safe_preprocess",
|
||||
@@ -167,9 +183,11 @@ class TestOcrCountdown:
|
||||
assert runner.ocr_countdown_ss() == "45"
|
||||
|
||||
preprocess.assert_called_once_with(runner.analyser, "福袋倒计时_秒")
|
||||
runner.analyser.operation.analyse_pic_digits.assert_called_once_with(
|
||||
"福袋倒计时_秒_preprocessed",
|
||||
0,
|
||||
runner.analyser.operation.analyse_pic_digits.assert_any_call(
|
||||
"福袋倒计时_秒_preprocessed", 0
|
||||
)
|
||||
runner.analyser.operation.analyse_pic_digits.assert_any_call(
|
||||
"福袋倒计时_秒", 0
|
||||
)
|
||||
|
||||
def test_mm_normalizes_ocr_zero_noise(self):
|
||||
@@ -199,6 +217,89 @@ class TestOcrCountdown:
|
||||
)
|
||||
assert runner.ocr_countdown_mm() is None
|
||||
|
||||
def test_countdown_vote_prefers_majority(self):
|
||||
assert _choose_countdown_vote(["08", "09", "08"]) == "08"
|
||||
|
||||
def test_countdown_vote_tie_returns_empty_to_avoid_wrong_join(self):
|
||||
assert _choose_countdown_vote(["09", "08"]) == ""
|
||||
|
||||
def test_read_countdown_digits_uses_modes_and_fallback_pic(self):
|
||||
analyser = _mk_analyser()
|
||||
analyser.operation.analyse_pic_digits = MagicMock(
|
||||
side_effect=["", "99", "08", "8", "08", ""]
|
||||
)
|
||||
|
||||
with patch(
|
||||
"douyin.fudai.douyin_fudai_core_pkg.ocr_tasks._cv_detect_two_zero_digits",
|
||||
return_value="",
|
||||
):
|
||||
result = _read_countdown_digits(
|
||||
analyser,
|
||||
"福袋倒计时_分钟_preprocessed",
|
||||
fallback_picnames=("福袋倒计时_分钟",),
|
||||
)
|
||||
|
||||
assert result == "08"
|
||||
assert analyser.operation.analyse_pic_digits.call_args_list[0].args == (
|
||||
"福袋倒计时_分钟_preprocessed",
|
||||
0,
|
||||
)
|
||||
assert analyser.operation.analyse_pic_digits.call_args_list[-1].args == (
|
||||
"福袋倒计时_分钟",
|
||||
2,
|
||||
)
|
||||
|
||||
def test_read_countdown_digits_uses_cv_zero_when_ocr_ties(self):
|
||||
analyser = _mk_analyser()
|
||||
analyser.operation.analyse_pic_digits = MagicMock(
|
||||
side_effect=["09", "09", "09", "00", "00", "00"]
|
||||
)
|
||||
|
||||
with patch(
|
||||
"douyin.fudai.douyin_fudai_core_pkg.ocr_tasks._cv_detect_two_zero_digits",
|
||||
side_effect=["00"],
|
||||
):
|
||||
assert _read_countdown_digits(
|
||||
analyser,
|
||||
"福袋倒计时_分钟_preprocessed",
|
||||
fallback_picnames=("福袋倒计时_分钟",),
|
||||
) == "00"
|
||||
|
||||
def test_read_countdown_digits_tie_prefers_consistent_preprocessed(self):
|
||||
analyser = _mk_analyser()
|
||||
analyser.operation.analyse_pic_digits = MagicMock(
|
||||
side_effect=["41", "41", "41", "50", "50", "50"]
|
||||
)
|
||||
|
||||
with patch(
|
||||
"douyin.fudai.douyin_fudai_core_pkg.ocr_tasks._cv_detect_two_zero_digits",
|
||||
return_value="",
|
||||
):
|
||||
assert _read_countdown_digits(
|
||||
analyser,
|
||||
"福袋倒计时_秒_preprocessed",
|
||||
fallback_picnames=("福袋倒计时_秒",),
|
||||
) == "41"
|
||||
|
||||
def test_read_countdown_digits_cv_zero_can_override_wrong_ocr_majority(self):
|
||||
analyser = _mk_analyser()
|
||||
analyser.operation.analyse_pic_digits = MagicMock(
|
||||
side_effect=["09", "09", "09", "", "", ""]
|
||||
)
|
||||
|
||||
with patch(
|
||||
"douyin.fudai.douyin_fudai_core_pkg.ocr_tasks._cv_detect_two_zero_digits",
|
||||
side_effect=["00"],
|
||||
):
|
||||
assert _read_countdown_digits(
|
||||
analyser,
|
||||
"福袋倒计时_分钟_preprocessed",
|
||||
fallback_picnames=("福袋倒计时_分钟",),
|
||||
) == "00"
|
||||
|
||||
def test_cv_detect_two_zero_digits_on_current_minute_crop(self):
|
||||
assert _cv_detect_two_zero_digits("福袋倒计时_分钟") in ("", "00")
|
||||
|
||||
|
||||
# ============================================================
|
||||
# ocr_value
|
||||
|
||||
Reference in new issue
Block a user