优化详情调试标注与截图复用

This commit is contained in:
Your Name committed 2026-06-03 11:36:47 +08:00
1 parent 42aa0f214d
commit 3bcb5cb259
6 files changed
+169 -42

No files matched your search

+5 -4
View File
@@ -209,7 +209,7 @@ class TestPixelStage:
assert not (debug_dir / "20260603_像素检测_抽奖结果_检测前.png").exists()
assert not (debug_dir / "20260603_px_attend_pre.png").exists()
def test_pixel_stage_overview_does_not_draw_failed_candidate_boxes(self, tmp_path, monkeypatch):
def test_pixel_stage_overview_draws_candidate_boxes(self, tmp_path, monkeypatch):
from douyin.detection.pixel.stage import _save_pixel_stage_overview
debug_dir = tmp_path / "debug"
@@ -259,9 +259,10 @@ class TestPixelStage:
assert saved_images
overview = saved_images[0]
# Old failed candidate boxes used to draw red outlines here.
assert overview.getpixel((10, 1173)) != (255, 70, 70)
assert overview.getpixel((250, 1261)) != (255, 70, 70)
# 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)
assert overview.getpixel((200, 300)) != (255, 70, 70)
# Successful live-room icon detection is still annotated.
assert overview.getpixel((1002, 172)) == (0, 220, 80)
+56
View File
@@ -61,6 +61,62 @@ def test_fudai_detail_debug_overview_saves_one_annotated_image(tmp_path, monkeyp
assert kwargs["extra_info"]["ui_version"] == "v2_0"
def test_fudai_detail_debug_labels_are_inside_region_edges(tmp_path, monkeypatch):
from douyin.core import path_config
from douyin.fudai.core import fudai_debug_utils
screenshot = tmp_path / "screenshot.png"
Image.new("RGB", (1080, 2400), "white").save(screenshot)
saved = []
def fake_save_debug_image(image, **kwargs):
saved.append((image.copy(), kwargs))
return str(tmp_path / "overview.png")
monkeypatch.setattr(path_config, "get_screenshot_path", lambda: str(screenshot))
monkeypatch.setattr(
"douyin.handlers.douyin_screenshot_manager.save_debug_image",
fake_save_debug_image,
)
analyser = SimpleNamespace(
resolution_ratio_x=1080,
resolution_ratio_y=2400,
_log_kv=lambda *args, **kwargs: None,
)
result = fudai_debug_utils.save_fudai_detail_debug_overview(
analyser,
[
{
"key": "countdown",
"name": "倒计时",
"coords": (760, 1355, 1035, 1455),
"enabled": True,
},
{
"key": "content",
"name": "内容",
"coords": (360, 1472, 1035, 1545),
"enabled": True,
},
],
ui_version="v2_0",
ocr_enabled={"countdown", "content"},
task_count=1,
task_count_source="v2_pixels",
)
assert result
assert saved
image, _kwargs = saved[0]
# Labels are now drawn adjacent to their own boxes, not floating above
# another OCR field.
assert image.getpixel((763, 1358)) == (0, 0, 0)
assert image.getpixel((363, 1475)) == (0, 0, 0)
assert image.getpixel((363, 1444)) != (0, 0, 0)
def test_fudai_detail_debug_overview_cleans_old_detail_images(tmp_path, monkeypatch):
from douyin.core import path_config
from douyin.fudai.core import fudai_debug_utils
+54 -14
View File
@@ -84,7 +84,7 @@ def test_read_once_restarts_screenshot_service_after_ocr(tmp_path, monkeypatch):
assert starts == [("MOCK_DEVICE", 1.5)]
def test_read_once_retries_fresh_screenshot_when_reused_file_is_unreadable(
def test_read_once_skips_stage_screenshot_reuse_by_default(
tmp_path, monkeypatch
):
from douyin.fudai.core.fudai_detail_orchestrator_pkg.read_once import (
@@ -93,9 +93,58 @@ def test_read_once_retries_fresh_screenshot_when_reused_file_is_unreadable(
analyser = _mk_analyser()
analyser._screenshot_ready_for_ocr = True
screenshot_path = tmp_path / "stage_screenshot.png"
screenshot_path.write_bytes(b"x" * 2048)
good_png = (
b"\x89PNG\r\n\x1a\n"
b"\x00\x00\x00\rIHDR"
b"\x00\x00\x00\x01\x00\x00\x00\x01\x08\x02\x00\x00\x00"
b"\x90wS\xde"
b"\x00\x00\x00\x0cIDATx\x9cc\xf8\xcf\xc0\x00\x00\x03\x01\x01\x00\xc9\xfe\x92\xef"
b"\x00\x00\x00\x00IEND\xaeB`\x82"
)
def _fresh_screenshot(*_args, **_kwargs):
screenshot_path.write_bytes(good_png)
return True
analyser.operation.get_screenshot = MagicMock(side_effect=_fresh_screenshot)
with patch(
"douyin.fudai.core.fudai_detail_orchestrator_pkg.read_once.path_config.get_screenshot_path",
return_value=str(screenshot_path),
), patch(
"douyin.fudai.core.fudai_detail_orchestrator_pkg.read_once.get_fudai_contain",
return_value=("内容", "00:30", 1, 1, 10.0),
), patch(
"operations.screenshot_service.ScreenshotService.get_instance",
return_value=SimpleNamespace(is_running=lambda: False),
):
status, detail, extra = read_fudai_detail_once(analyser)
assert status == "ok"
assert detail[0] == "内容"
assert extra["task_count"] == 1
assert analyser._screenshot_ready_for_ocr is False
analyser.operation.get_screenshot.assert_called_once_with(
analyser.device_id, use_service=False
)
def test_read_once_can_reuse_stage_screenshot_when_explicitly_enabled(
tmp_path, monkeypatch
):
from douyin.fudai.core.fudai_detail_orchestrator_pkg.read_once import (
read_fudai_detail_once,
)
analyser = _mk_analyser()
analyser._screenshot_ready_for_ocr = True
analyser.fudai_detail_reuse_stage_screenshot_enabled = True
analyser.operation.get_screenshot = MagicMock(return_value=True)
screenshot_path = tmp_path / "broken_reused.png"
screenshot_path = tmp_path / "stage_reuse.png"
screenshot_path.write_bytes(b"x" * 2048)
class _ImgObj:
@@ -108,16 +157,8 @@ def test_read_once_retries_fresh_screenshot_when_reused_file_is_unreadable(
def close(self):
return None
call_state = {"count": 0}
def _open(_path):
call_state["count"] += 1
if call_state["count"] == 1:
raise OSError("broken png")
return _ImgObj()
pil_mod = ModuleType("PIL")
pil_mod.Image = SimpleNamespace(open=_open)
pil_mod.Image = SimpleNamespace(open=lambda _path: _ImgObj())
with patch(
"douyin.fudai.core.fudai_detail_orchestrator_pkg.read_once.path_config.get_screenshot_path",
@@ -135,9 +176,8 @@ def test_read_once_retries_fresh_screenshot_when_reused_file_is_unreadable(
assert status == "ok"
assert detail[0] == "内容"
assert extra["task_count"] == 1
analyser.operation.get_screenshot.assert_called_once_with(
analyser.device_id, use_service=False
)
assert analyser._screenshot_ready_for_ocr is False
analyser.operation.get_screenshot.assert_not_called()
def test_read_once_clears_reuse_flag_when_screenshot_still_unreadable(