add countdown OCR voting fallback

This commit is contained in:
Your Name committed 2026-06-23 19:18:12 +08:00
1 parent ab664e4da8
commit 3ace80dcca
2 files changed
+313 -19

No files matched your search

+111 -10
View File
@@ -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