Files
douyi_guaji_main/tests/test_fudai_debug_overview.py
T

345 lines
11 KiB
Python

from __future__ import annotations
from types import SimpleNamespace
from PIL import Image
def test_fudai_detail_debug_overview_saves_one_annotated_image(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": "content",
"name": "内容",
"coords": (360, 1460, 1035, 1545),
"enabled": True,
},
{
"key": "participants",
"name": "人数",
"coords": (516, 1396, 743, 1443),
"enabled": False,
},
],
ui_version="v2_0",
ocr_enabled={"content"},
task_count=1,
task_count_source="v2_pixels",
)
assert result
assert len(saved) == 1
_image, kwargs = saved[0]
assert kwargs["picname"] == "福袋详情综合标注"
assert kwargs["step"] == "detail_regions"
assert kwargs["annotate"] is False
assert kwargs["force"] is True
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": (730, 1338, 1038, 1418),
"enabled": True,
},
{
"key": "content",
"name": "内容",
"coords": (360, 1460, 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((767, 1348)) == (0, 0, 0)
assert image.getpixel((363, 1463)) == (0, 0, 0)
assert image.getpixel((363, 1438)) != (0, 0, 0)
def test_fudai_detail_debug_overview_supports_two_task_positions(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, 1328, 1038, 1398),
"enabled": True,
},
{
"key": "content",
"name": "内容",
"coords": (360, 1410, 1035, 1495),
"enabled": True,
},
],
ui_version="v2_0",
ocr_enabled={"countdown", "content"},
task_count=2,
task_count_source="v2_pixels",
)
assert result
image, _kwargs = saved[0]
assert image.getpixel((763, 1331)) == (0, 0, 0)
assert image.getpixel((363, 1413)) == (0, 0, 0)
def test_fudai_detail_debug_overview_marks_task_count_digit_box(tmp_path, monkeypatch):
from douyin.core import path_config
from douyin.detection.detection_pkg.detail import get_detail_task_count_digit_box
from douyin.fudai.core import coords as coords_mod
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,
)
digit_box = get_detail_task_count_digit_box(coords_mod.DETAIL_UI_V2_0["task_count"])
result = fudai_debug_utils.save_fudai_detail_debug_overview(
analyser,
[
{
"key": "task_count",
"name": "任务数",
"coords": digit_box,
"enabled": True,
},
],
ui_version="v2_0",
ocr_enabled={"task_count"},
task_count=2,
task_count_source="v2_digit_template",
)
assert result
image, _kwargs = saved[0]
assert image.getpixel((digit_box[0], digit_box[1])) != (255, 255, 255)
assert image.getpixel((digit_box[0] + 6, digit_box[1])) != (255, 255, 255)
assert _kwargs["extra_info"]["task_count_source"] == "v2_digit_template"
def test_fudai_detail_debug_overview_keeps_existing_detail_images(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)
debug_dir = tmp_path / "debug"
debug_dir.mkdir()
old_detail = debug_dir / "20260603_ocr_福袋任务数量_v2_识别后.png"
old_overview = debug_dir / "20260603_ocr_福袋详情综合标注_detail_regions.png"
unrelated = debug_dir / "20260603_像素检测_抽奖结果_检测前.png"
old_detail.write_bytes(b"old")
old_overview.write_bytes(b"old")
unrelated.write_bytes(b"keep")
def fake_save_debug_image(image, **kwargs):
out = debug_dir / "new_overview.png"
image.save(out)
return str(out)
monkeypatch.setattr(path_config, "get_screenshot_path", lambda: str(screenshot))
monkeypatch.setattr(
"douyin.handlers.douyin_screenshot_manager.get_debug_captures_dir",
lambda: str(debug_dir),
)
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,
[],
ui_version="v2_0",
ocr_enabled=set(),
task_count=1,
)
assert result
assert old_detail.exists()
assert old_overview.exists()
assert unrelated.exists()
def test_cleanup_fudai_detail_debug_captures_removes_info_line_debug_image(tmp_path, monkeypatch):
from douyin.fudai.core import fudai_debug_utils
debug_dir = tmp_path / "debug"
debug_dir.mkdir()
info_line = debug_dir / "20260603_ocr_福袋人数倒计时信息_postocr.png"
overview = debug_dir / "20260603_ocr_福袋详情综合标注_detail_regions.png"
keep = debug_dir / "20260603_px_pxstage_ov_ok_1.png"
info_line.write_bytes(b"old")
overview.write_bytes(b"old")
keep.write_bytes(b"keep")
monkeypatch.setattr(
"douyin.handlers.douyin_screenshot_manager.get_debug_captures_dir",
lambda: str(debug_dir),
)
removed = fudai_debug_utils.cleanup_fudai_detail_debug_captures()
assert removed >= 2
assert not info_line.exists()
assert not overview.exists()
assert keep.exists()
def test_fudai_detail_debug_overview_uses_configured_profile_coords(tmp_path, monkeypatch):
from douyin.core import path_config
from douyin.fudai.core import coords as coords_mod
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,
)
monkeypatch.setattr(
coords_mod,
"_read_runtime_region_config",
lambda: {
"fudai_detail_regions": {
"v2_0_two_task": {
"content": [111, 222, 333, 444],
}
}
},
)
analyser = SimpleNamespace(
resolution_ratio_x=1080,
resolution_ratio_y=2400,
_log_kv=lambda *args, **kwargs: None,
)
profile = coords_mod.get_fudai_detail_profile("v2_0", task_count=2)
result = fudai_debug_utils.save_fudai_detail_debug_overview(
analyser,
[{"key": "content", "name": "内容", "coords": profile["content"], "enabled": True}],
ui_version="v2_0",
ocr_enabled={"content"},
task_count=2,
)
assert result
image, _kwargs = saved[0]
assert image.getpixel((114, 225)) == (0, 0, 0)