fix: stabilize fudai countdown OCR crops

This commit is contained in:
Your Name committed 2026-06-23 15:11:29 +08:00
1 parent 5ebca337eb
commit 7086ab1d6e
9 files changed
+106 -26

No files matched your search

+3
View File
@@ -159,6 +159,9 @@ STALE_DETAIL_REGION_DEFAULTS = {
"value": (300, 1585, 650, 1685),
"countdown_full": (760, 1328, 1038, 1398),
},
"v2_0_one_task": {
"countdown_full": (769, 1384, 1029, 1442),
},
}
PIXEL_REGION_DEFAULTS = {
@@ -5,7 +5,9 @@ from __future__ import annotations
def extract_countdown_digits(text: str, max_digits: int = 2) -> str:
"""提取倒计时数字,最多 max_digits 位。"""
digits = "".join(ch for ch in str(text or "") if ch.isdigit())
raw = str(text or "")
normalized = raw.translate(str.maketrans({"O": "0", "o": "0", "U": "0", "u": "0"}))
digits = "".join(ch for ch in normalized if ch.isdigit())
if len(digits) > max_digits:
# 倒计时分钟/秒区域偶发会混入噪声数字(如 "601"、"0123")。
# 对 2 位提取场景,优先尝试滑窗中第一个“合法”候选(00~59),
@@ -16,17 +16,12 @@ from typing import Tuple
def _extract_digits(text: str, max_digits: int = 2) -> str:
"""提取数字,最多 max_digits 位,取前 max_digits 位优先。"""
digits = "".join(ch for ch in str(text or "") if ch.isdigit())
if len(digits) > max_digits:
candidate = digits[:max_digits]
if set(candidate) == {"0"} and digits.lstrip("0"):
actual = digits.lstrip("0")[:max_digits]
return actual.zfill(max_digits)
return candidate
if len(digits) == 1:
return "0" + digits
return digits
"""提取倒计时数字,兼容 OCR 把 00 识别成 UU/OO 的噪声。"""
from douyin.fudai.douyin_fudai_core_pkg.countdown_parse import (
extract_countdown_digits,
)
return extract_countdown_digits(text, max_digits=max_digits)
def read_countdown_split(
@@ -150,16 +150,20 @@ def _ocr_digits_with_fallback(analyser, picname: str) -> str:
返回提取到的数字字符串;都失败返回 ""。
"""
from douyin.fudai.douyin_fudai_core_pkg.countdown_parse import (
extract_countdown_digits,
)
digits = ""
try:
raw = analyser.operation.analyse_pic_digits(picname, 1)
digits = "".join(ch for ch in str(raw or "") if ch.isdigit())
digits = extract_countdown_digits(raw, max_digits=2)
except Exception:
pass
if not digits:
try:
raw = analyser.operation.analyse_pic_word(picname, 1)
digits = "".join(ch for ch in str(raw or "") if ch.isdigit())
digits = extract_countdown_digits(raw, max_digits=2)
except Exception:
pass
return digits
+16 -10
View File
@@ -48,6 +48,16 @@ def _safe_preprocess(analyser, name: str) -> str:
return name
def _read_countdown_digits(analyser, picname: str) -> str:
"""读取倒计时分/秒小图,并把 OCR 零形噪声归一成数字。"""
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)
class OcrTaskRunner:
"""7 个并行 OCR 子任务的运行器。
@@ -149,13 +159,11 @@ class OcrTaskRunner:
self._record_duration("ocr_content", start)
def ocr_countdown_mm(self) -> Optional[str]:
"""OCR 识别福袋倒计时分钟(纯数字模型,无回退)。"""
"""OCR 识别福袋倒计时分钟。"""
start = time.time()
try:
raw = str(
self.analyser.operation.analyse_pic_digits("福袋倒计时_分钟", 0) or ""
)
return "".join(ch for ch in raw if ch.isdigit())
picname = _safe_preprocess(self.analyser, "福袋倒计时_分钟")
return _read_countdown_digits(self.analyser, picname)
except Exception as e:
self._log_warn("OCR识别倒计时分钟失败", e)
return None
@@ -163,13 +171,11 @@ class OcrTaskRunner:
self._record_duration("ocr_countdown_mm", start)
def ocr_countdown_ss(self) -> Optional[str]:
"""OCR 识别福袋倒计时秒(纯数字模型,无回退)。"""
"""OCR 识别福袋倒计时秒。"""
start = time.time()
try:
raw = str(
self.analyser.operation.analyse_pic_digits("福袋倒计时_秒", 0) or ""
)
return "".join(ch for ch in raw if ch.isdigit())
picname = _safe_preprocess(self.analyser, "福袋倒计时_秒")
return _read_countdown_digits(self.analyser, picname)
except Exception as e:
self._log_warn("OCR识别倒计时秒失败", e)
return None
+6 -2
View File
@@ -47,11 +47,15 @@ def preprocess_for_ocr(analyser: Any, cut_pic_name: str) -> str:
img = _raw.convert("L")
finally:
_raw.close()
# 放大两倍(Lanczos)
img = img.resize((img.width * 2, img.height * 2), Image.LANCZOS)
is_countdown_digit = str(cut_pic_name or "").startswith("福袋倒计时_")
# 倒计时分秒小图通常只有几十像素宽,2x 仍容易把 00 识别成空。
scale = 4 if is_countdown_digit else 2
img = img.resize((img.width * scale, img.height * scale), Image.LANCZOS)
# 自动对比并锐化
img = ImageOps.autocontrast(img)
img = img.filter(ImageFilter.SHARPEN)
if is_countdown_digit:
img = ImageOps.expand(img, border=max(4, scale * 2), fill=255)
# 保存为目标路径(覆盖或新文件)
img.save(dst)
return pre_name
@@ -97,6 +97,14 @@ def test_parse_countdown_sliding_window_filters_noise_digits():
assert time_text == "01:53"
def test_parse_countdown_treats_ocr_uo_noise_as_zero_for_minute():
analyser = _mk_analyser()
time_text = parse_countdown_time_text(analyser, "UU", "41")
assert time_text == "00:41"
def test_select_countdown_uses_sliding_window_for_candidate_validity():
analyser = _mk_analyser()
candidate_meta = [
+20
View File
@@ -130,6 +130,26 @@ def test_detail_profile_ignores_stale_v2_two_task_runtime_defaults(monkeypatch):
assert profile["countdown_full"] == coords.DETAIL_UI_V2_0["countdown_full"]
def test_detail_profile_ignores_stale_v2_one_task_narrow_countdown(monkeypatch):
from douyin.fudai.core import coords
monkeypatch.setattr(
coords,
"_read_runtime_region_config",
lambda: {
"fudai_detail_regions": {
"v2_0_one_task": {
"countdown_full": [769, 1384, 1029, 1442],
}
}
},
)
profile = coords.get_fudai_detail_profile("v2_0", task_count=1)
assert profile["countdown_full"] == coords.DETAIL_UI_V2_0_ONE_TASK["countdown_full"]
def test_v2_two_task_profile_covers_batu_4999_sample_regions():
from PIL import Image
from pathlib import Path
+38
View File
@@ -140,6 +140,44 @@ class TestOcrCountdown:
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")
with patch(
"douyin.fudai.douyin_fudai_core_pkg.ocr_tasks._safe_preprocess",
return_value="福袋倒计时_分钟_preprocessed",
) as preprocess:
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,
)
def test_ss_uses_preprocessed_countdown_image(self):
runner, durations = _mk_runner()
runner.analyser.operation.analyse_pic_digits = MagicMock(return_value="45")
with patch(
"douyin.fudai.douyin_fudai_core_pkg.ocr_tasks._safe_preprocess",
return_value="福袋倒计时_秒_preprocessed",
) as preprocess:
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,
)
def test_mm_normalizes_ocr_zero_noise(self):
runner, durations = _mk_runner()
runner.analyser.operation.analyse_pic_digits = MagicMock(return_value="UU")
assert runner.ocr_countdown_mm() == "00"
runner.analyser.operation.analyse_pic_word.assert_not_called()
def test_ss_extracts_digits(self):
runner, durations = _mk_runner()
runner.analyser.operation.analyse_pic_digits = MagicMock(