fix task count digit detection

This commit is contained in:
Your Name committed 2026-06-04 11:15:45 +08:00
1 parent 0c77bf9e0e
commit dc6ae7d1e4
6 files changed
+400 -74

No files matched your search

+6 -4
View File
@@ -270,7 +270,7 @@ class TestPixelStage:
"has_lucky_draw_result": False,
"has_attend_button_red": False,
"has_fansclub_join_red": False,
"has_fudai_detail": False,
"has_fudai_detail": True,
"has_live_room_pixel": True,
},
{
@@ -287,10 +287,12 @@ class TestPixelStage:
assert saved_images
overview = saved_images[0]
# Candidate boxes are visible again: red means checked but not matched.
assert overview.getpixel((9, 1173)) == (255, 70, 70)
assert overview.getpixel((290, 1300)) == (255, 70, 70)
# Task-count digit candidate boxes are visible when the detail page is seen
# but the task count itself has not matched yet.
assert overview.getpixel((9, 1173)) != (255, 70, 70)
assert overview.getpixel((200, 300)) != (255, 70, 70)
assert overview.getpixel((977, 1874)) == (0, 220, 80)
assert overview.getpixel((975, 1817)) == (0, 220, 80)
# Successful live-room icon detection is still annotated.
assert overview.getpixel((1002, 172)) == (0, 220, 80)
+107 -18
View File
@@ -184,32 +184,35 @@ def test_config_manager_loads_fudai_detail_ui_and_ocr_switches(tmp_path):
assert analyser.fudai_detail_ocr_participants_enabled is True
def test_check_detail_height_v2_does_not_run_task_count_ocr(monkeypatch):
def test_check_detail_height_v2_uses_template_match_path(monkeypatch):
from douyin.detection.detection_pkg import detail
monkeypatch.setattr(detail, "_check_detail_task_count_v2_pixels", lambda _analyser: 0)
def _ocr_should_not_run(_analyser):
raise AssertionError("v2 detail detection must not fallback to task-count OCR")
monkeypatch.setattr(detail, "_check_detail_task_count_v2", _ocr_should_not_run)
monkeypatch.setattr(detail, "_check_detail_task_count_v2", lambda _analyser: 2)
analyser = SimpleNamespace(fudai_detail_ui_mode="v2_0")
assert detail.check_detail_height(analyser) == 0
assert detail.check_detail_height(analyser) == 2
def test_v2_task_count_pixels_choose_one_task_before_ocr(tmp_path, monkeypatch):
from douyin.detection.detection_pkg.detail import _check_detail_task_count_v2_pixels
from douyin.detection.detection_pkg.detail import _check_detail_task_count_v2
from douyin.core import path_config
import numpy as np
import cv2
screenshot_path = tmp_path / "one_task.png"
img = Image.new("RGB", (1080, 2400), (10, 15, 46))
img.putpixel((536, 1970), (255, 255, 255))
img.save(screenshot_path)
assets_dir = tmp_path / "assets"
assets_dir.mkdir()
Image.new("RGB", (1080, 2400), (255, 255, 255)).save(screenshot_path)
Image.new("L", (40, 80), 255).save(assets_dir / "fudai_task_count_digit_1.png")
Image.new("L", (40, 80), 255).save(assets_dir / "fudai_task_count_digit_2.png")
monkeypatch.setattr(
"douyin.core.path_config.get_screenshot_path",
lambda: str(screenshot_path),
)
monkeypatch.setattr(path_config, "get_assets_dir", lambda: str(assets_dir))
scores = iter([0.99, 0.98, 0.81, 0.80])
monkeypatch.setattr(cv2, "matchTemplate", lambda *_a, **_k: np.array([[next(scores)]], dtype=np.float32))
analyser = SimpleNamespace(
fudai_detail_ui_mode="auto",
@@ -218,20 +221,29 @@ def test_v2_task_count_pixels_choose_one_task_before_ocr(tmp_path, monkeypatch):
_log_kv=lambda *_args, **_kwargs: None,
)
assert _check_detail_task_count_v2_pixels(analyser) == 1
assert analyser._fudai_detail_task_count_source == "v2_pixels"
assert _check_detail_task_count_v2(analyser) == 1
assert analyser._fudai_detail_task_count_source == "v2_template"
def test_v2_task_count_pixels_choose_two_task_from_dark_card(tmp_path, monkeypatch):
from douyin.detection.detection_pkg.detail import _check_detail_task_count_v2_pixels
from douyin.detection.detection_pkg.detail import _check_detail_task_count_v2
from douyin.core import path_config
import numpy as np
import cv2
screenshot_path = tmp_path / "two_task.png"
img = Image.new("RGB", (1080, 2400), (10, 15, 46))
img.save(screenshot_path)
assets_dir = tmp_path / "assets"
assets_dir.mkdir()
Image.new("RGB", (1080, 2400), (255, 255, 255)).save(screenshot_path)
Image.new("L", (40, 80), 255).save(assets_dir / "fudai_task_count_digit_1.png")
Image.new("L", (40, 80), 255).save(assets_dir / "fudai_task_count_digit_2.png")
monkeypatch.setattr(
"douyin.core.path_config.get_screenshot_path",
lambda: str(screenshot_path),
)
monkeypatch.setattr(path_config, "get_assets_dir", lambda: str(assets_dir))
scores = iter([0.81, 0.80, 0.99, 1.0])
monkeypatch.setattr(cv2, "matchTemplate", lambda *_a, **_k: np.array([[next(scores)]], dtype=np.float32))
analyser = SimpleNamespace(
fudai_detail_ui_mode="v2_0",
@@ -240,7 +252,84 @@ def test_v2_task_count_pixels_choose_two_task_from_dark_card(tmp_path, monkeypat
_log_kv=lambda *_args, **_kwargs: None,
)
assert _check_detail_task_count_v2_pixels(analyser) == 2
assert _check_detail_task_count_v2(analyser) == 2
def test_v2_task_count_pixels_does_not_misread_two_task_when_lower_text_is_white(tmp_path, monkeypatch):
from douyin.detection.detection_pkg.detail import _check_detail_task_count_v2
from douyin.core import path_config
import numpy as np
import cv2
screenshot_path = tmp_path / "two_task_with_white_text.png"
assets_dir = tmp_path / "assets"
assets_dir.mkdir()
Image.new("RGB", (1080, 2400), (255, 255, 255)).save(screenshot_path)
Image.new("L", (40, 80), 255).save(assets_dir / "fudai_task_count_digit_1.png")
Image.new("L", (40, 80), 255).save(assets_dir / "fudai_task_count_digit_2.png")
monkeypatch.setattr(
"douyin.core.path_config.get_screenshot_path",
lambda: str(screenshot_path),
)
monkeypatch.setattr(path_config, "get_assets_dir", lambda: str(assets_dir))
scores = iter([0.97, 0.96, 0.98, 0.99])
monkeypatch.setattr(cv2, "matchTemplate", lambda *_a, **_k: np.array([[next(scores)]], dtype=np.float32))
analyser = SimpleNamespace(
fudai_detail_ui_mode="v2_0",
resolution_ratio_x=1080,
resolution_ratio_y=2400,
_log_kv=lambda *_args, **_kwargs: None,
)
assert _check_detail_task_count_v2(analyser) == 2
def test_v2_task_count_uses_digit_templates_instead_of_full_card_templates(tmp_path, monkeypatch):
from douyin.detection.detection_pkg.detail import _check_detail_task_count_v2
from douyin.core import path_config
import numpy as np
import cv2
screenshot_path = tmp_path / "digit_based.png"
assets_dir = tmp_path / "assets"
assets_dir.mkdir()
Image.new("RGB", (1080, 2400), (255, 255, 255)).save(screenshot_path)
Image.new("L", (40, 80), 255).save(assets_dir / "fudai_task_count_digit_1.png")
Image.new("L", (40, 80), 255).save(assets_dir / "fudai_task_count_digit_2.png")
monkeypatch.setattr(
"douyin.core.path_config.get_screenshot_path",
lambda: str(screenshot_path),
)
monkeypatch.setattr(path_config, "get_assets_dir", lambda: str(assets_dir))
seen_paths = []
def _fake_imread(path, _flag):
seen_paths.append(path)
return np.full((80, 40), 255, dtype=np.uint8)
scores = iter([0.80, 0.79, 0.99, 0.98])
monkeypatch.setattr(cv2, "imread", _fake_imread)
monkeypatch.setattr(cv2, "matchTemplate", lambda *_a, **_k: np.array([[next(scores)]], dtype=np.float32))
analyser = SimpleNamespace(
fudai_detail_ui_mode="v2_0",
resolution_ratio_x=1080,
resolution_ratio_y=2400,
_log_kv=lambda *_args, **_kwargs: None,
)
assert _check_detail_task_count_v2(analyser) == 2
assert any(path.endswith("fudai_task_count_digit_1.png") for path in seen_paths)
assert any(path.endswith("fudai_task_count_digit_2.png") for path in seen_paths)
def test_get_detail_task_count_digit_box_targets_trailing_digit_for_one_and_two_task():
from douyin.detection.detection_pkg.detail import get_detail_task_count_digit_box
assert get_detail_task_count_digit_box((890, 1855, 1040, 1960)) == (976, 1871, 988, 1901)
assert get_detail_task_count_digit_box((860, 1770, 1035, 1875)) == (974, 1814, 995, 1845)
def test_v2_task_count_pixels_disabled_for_legacy(tmp_path, monkeypatch):