improve fudai ocr debug captures
This commit is contained in:
1 parent
62c84939ee
commit
207cf0eadb
10 files changed
+382
-117
No files matched your search
@@ -96,6 +96,19 @@ class TestCutDetailRegions:
|
||||
assert ss_call.args[0] == ss_x1
|
||||
assert ss_call.args[2] == ss_x2
|
||||
|
||||
def test_profile_countdown_vertical_crop_is_narrower_and_higher(self):
|
||||
a = _mk_analyser()
|
||||
profile = {"countdown_full": (760, 1328, 1038, 1398)}
|
||||
|
||||
cut_detail_regions(a, renwu=2, profile=profile)
|
||||
|
||||
mm_call = a.cut_pic.call_args_list[1]
|
||||
ss_call = a.cut_pic.call_args_list[2]
|
||||
assert mm_call.args[1] == 1340
|
||||
assert mm_call.args[3] == 1371
|
||||
assert ss_call.args[1] == 1340
|
||||
assert ss_call.args[3] == 1371
|
||||
|
||||
def test_save_debug_images_disabled_default(self):
|
||||
a = _mk_analyser()
|
||||
with patch(
|
||||
@@ -119,6 +132,12 @@ class TestCutDetailRegions:
|
||||
result = cut_detail_regions(a, renwu=1)
|
||||
assert result == COUNTDOWN_1140_1290
|
||||
|
||||
def test_one_task_profile_countdown_box_uses_new_coords(self):
|
||||
a = _mk_analyser()
|
||||
profile = {"countdown_full": (730, 1338, 1038, 1418)}
|
||||
result = cut_detail_regions(a, renwu=1, profile=profile)
|
||||
assert result == (730, 1338, 1038, 1418)
|
||||
|
||||
def test_renwu_3_uses_correct_box(self):
|
||||
a = _mk_analyser()
|
||||
from douyin.fudai.core.coords import COUNTDOWN_1010_1130
|
||||
|
||||
@@ -169,6 +169,56 @@ def test_fudai_detail_debug_overview_supports_two_task_positions(tmp_path, monke
|
||||
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
|
||||
@@ -219,6 +269,31 @@ def test_fudai_detail_debug_overview_keeps_existing_detail_images(tmp_path, monk
|
||||
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
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from douyin.fudai.douyin_fudai_contain_impl import _parse_mmss_from_full_text
|
||||
from douyin.fudai.douyin_fudai_core_pkg.contain_helpers import (
|
||||
extract_participants_from_info_line,
|
||||
)
|
||||
|
||||
|
||||
def test_parse_mmss_from_info_line_extracts_standard_countdown():
|
||||
mm, ss = _parse_mmss_from_full_text("149人已参与/01:35后开奖")
|
||||
assert (mm, ss) == ("01", "35")
|
||||
|
||||
|
||||
def test_parse_mmss_from_info_line_extracts_seconds_fragment():
|
||||
mm, ss = _parse_mmss_from_full_text("Z5三工骏")
|
||||
assert (mm, ss) == ("00", "05")
|
||||
|
||||
|
||||
def test_extract_participants_from_info_line_reads_plain_people():
|
||||
assert extract_participants_from_info_line("149人已参与/01:35后开奖") == 149
|
||||
|
||||
|
||||
def test_extract_participants_from_info_line_reads_wan_unit():
|
||||
assert extract_participants_from_info_line("0.4万人已参与/00:00后开奖") == 4000
|
||||
@@ -1,93 +1,94 @@
|
||||
"""douyin/handlers/screenshot_manager 子模块单元测试。
|
||||
|
||||
覆盖:
|
||||
- save_image: _sanitize_filename_part 纯函数
|
||||
- save_files: 函数签名验证
|
||||
"""
|
||||
|
||||
"""douyin/handlers/screenshot_manager 子模块单元测试。
|
||||
|
||||
覆盖:
|
||||
- save_image: _sanitize_filename_part 纯函数
|
||||
- save_files: 函数签名验证
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from PIL import Image
|
||||
import pytest
|
||||
|
||||
|
||||
class TestSanitizeFilenamePart:
|
||||
"""_sanitize_filename_part 纯函数测试。"""
|
||||
|
||||
def test_empty_string(self):
|
||||
from douyin.handlers.screenshot_manager.save_image import _sanitize_filename_part
|
||||
|
||||
result = _sanitize_filename_part("")
|
||||
assert result == ""
|
||||
|
||||
def test_none_input(self):
|
||||
from douyin.handlers.screenshot_manager.save_image import _sanitize_filename_part
|
||||
|
||||
result = _sanitize_filename_part(None)
|
||||
assert result == ""
|
||||
|
||||
def test_removes_invalid_chars(self):
|
||||
from douyin.handlers.screenshot_manager.save_image import _sanitize_filename_part
|
||||
|
||||
result = _sanitize_filename_part('file<>:"/\\|?*name')
|
||||
assert "<" not in result
|
||||
assert ">" not in result
|
||||
assert ":" not in result
|
||||
assert '"' not in result
|
||||
assert "/" not in result
|
||||
assert "\\" not in result
|
||||
assert "|" not in result
|
||||
assert "?" not in result
|
||||
assert "*" not in result
|
||||
|
||||
def test_normal_filename(self):
|
||||
from douyin.handlers.screenshot_manager.save_image import _sanitize_filename_part
|
||||
|
||||
result = _sanitize_filename_part("normal_filename")
|
||||
assert "normal" in result
|
||||
assert "filename" in result
|
||||
|
||||
def test_chinese_filename(self):
|
||||
from douyin.handlers.screenshot_manager.save_image import _sanitize_filename_part
|
||||
|
||||
result = _sanitize_filename_part("福袋截图")
|
||||
assert "福袋" in result or len(result) > 0
|
||||
|
||||
def test_extracts_basename_from_path(self):
|
||||
from douyin.handlers.screenshot_manager.save_image import _sanitize_filename_part
|
||||
|
||||
result = _sanitize_filename_part("/path/to/file.png")
|
||||
assert "path" not in result or "file" in result
|
||||
|
||||
|
||||
class TestSaveFilesModule:
|
||||
"""save_files 模块测试。"""
|
||||
|
||||
def test_cleanup_ocr_dir_exists(self):
|
||||
from douyin.handlers.screenshot_manager.save_files import cleanup_ocr_dir
|
||||
|
||||
assert callable(cleanup_ocr_dir)
|
||||
|
||||
def test_save_latest_attempt_exists(self):
|
||||
from douyin.handlers.screenshot_manager.save_files import save_latest_attempt
|
||||
|
||||
assert callable(save_latest_attempt)
|
||||
|
||||
def test_save_last_success_exists(self):
|
||||
from douyin.handlers.screenshot_manager.save_files import save_last_success
|
||||
|
||||
assert callable(save_last_success)
|
||||
|
||||
def test_save_attempt_from_file_exists(self):
|
||||
from douyin.handlers.screenshot_manager.save_files import save_attempt_from_file
|
||||
|
||||
assert callable(save_attempt_from_file)
|
||||
|
||||
def test_save_success_from_file_exists(self):
|
||||
from douyin.handlers.screenshot_manager.save_files import save_success_from_file
|
||||
|
||||
assert callable(save_success_from_file)
|
||||
|
||||
|
||||
|
||||
|
||||
class TestSanitizeFilenamePart:
|
||||
"""_sanitize_filename_part 纯函数测试。"""
|
||||
|
||||
def test_empty_string(self):
|
||||
from douyin.handlers.screenshot_manager.save_image import _sanitize_filename_part
|
||||
|
||||
result = _sanitize_filename_part("")
|
||||
assert result == ""
|
||||
|
||||
def test_none_input(self):
|
||||
from douyin.handlers.screenshot_manager.save_image import _sanitize_filename_part
|
||||
|
||||
result = _sanitize_filename_part(None)
|
||||
assert result == ""
|
||||
|
||||
def test_removes_invalid_chars(self):
|
||||
from douyin.handlers.screenshot_manager.save_image import _sanitize_filename_part
|
||||
|
||||
result = _sanitize_filename_part('file<>:"/\\|?*name')
|
||||
assert "<" not in result
|
||||
assert ">" not in result
|
||||
assert ":" not in result
|
||||
assert '"' not in result
|
||||
assert "/" not in result
|
||||
assert "\\" not in result
|
||||
assert "|" not in result
|
||||
assert "?" not in result
|
||||
assert "*" not in result
|
||||
|
||||
def test_normal_filename(self):
|
||||
from douyin.handlers.screenshot_manager.save_image import _sanitize_filename_part
|
||||
|
||||
result = _sanitize_filename_part("normal_filename")
|
||||
assert "normal" in result
|
||||
assert "filename" in result
|
||||
|
||||
def test_chinese_filename(self):
|
||||
from douyin.handlers.screenshot_manager.save_image import _sanitize_filename_part
|
||||
|
||||
result = _sanitize_filename_part("福袋截图")
|
||||
assert "福袋" in result or len(result) > 0
|
||||
|
||||
def test_extracts_basename_from_path(self):
|
||||
from douyin.handlers.screenshot_manager.save_image import _sanitize_filename_part
|
||||
|
||||
result = _sanitize_filename_part("/path/to/file.png")
|
||||
assert "path" not in result or "file" in result
|
||||
|
||||
|
||||
class TestSaveFilesModule:
|
||||
"""save_files 模块测试。"""
|
||||
|
||||
def test_cleanup_ocr_dir_exists(self):
|
||||
from douyin.handlers.screenshot_manager.save_files import cleanup_ocr_dir
|
||||
|
||||
assert callable(cleanup_ocr_dir)
|
||||
|
||||
def test_save_latest_attempt_exists(self):
|
||||
from douyin.handlers.screenshot_manager.save_files import save_latest_attempt
|
||||
|
||||
assert callable(save_latest_attempt)
|
||||
|
||||
def test_save_last_success_exists(self):
|
||||
from douyin.handlers.screenshot_manager.save_files import save_last_success
|
||||
|
||||
assert callable(save_last_success)
|
||||
|
||||
def test_save_attempt_from_file_exists(self):
|
||||
from douyin.handlers.screenshot_manager.save_files import save_attempt_from_file
|
||||
|
||||
assert callable(save_attempt_from_file)
|
||||
|
||||
def test_save_success_from_file_exists(self):
|
||||
from douyin.handlers.screenshot_manager.save_files import save_success_from_file
|
||||
|
||||
assert callable(save_success_from_file)
|
||||
|
||||
|
||||
class TestSaveImageModule:
|
||||
"""save_image 模块测试。"""
|
||||
|
||||
@@ -167,7 +168,7 @@ class TestSaveImageModule:
|
||||
)
|
||||
|
||||
assert filename.endswith(".png")
|
||||
assert len(filename.split("_", 3)[2]) == 6
|
||||
assert len(filename.split("_", 3)[2]) == 6
|
||||
|
||||
def test_cleanup_single_operation_debug_images_cleans_detection_only(self, tmp_path, monkeypatch):
|
||||
from douyin.handlers.screenshot_manager.save_image_helpers import (
|
||||
@@ -188,31 +189,66 @@ class TestSaveImageModule:
|
||||
assert not old_det.exists()
|
||||
assert old_px.exists()
|
||||
|
||||
def test_annotate_debug_image_uses_cn_maps_without_nameerror(self):
|
||||
from douyin.handlers.screenshot_manager.save_image_helpers import annotate_debug_image
|
||||
|
||||
class TestScreenshotManagerSubmodules:
|
||||
"""screenshot_manager 子模块存在性测试。"""
|
||||
image = Image.new("RGB", (200, 120), "white")
|
||||
annotated = annotate_debug_image(
|
||||
image=image,
|
||||
picname="fudai_content",
|
||||
step="after_ocr",
|
||||
coords=(10, 10, 100, 80),
|
||||
extra_info={"text_preview": "儿童装饰手表生活"},
|
||||
)
|
||||
|
||||
def test_paths_importable(self):
|
||||
from douyin.handlers.screenshot_manager import paths
|
||||
assert annotated.size == image.size
|
||||
|
||||
assert paths is not None
|
||||
def test_annotate_debug_image_draws_green_arrow_for_fudai_icon_detected(self):
|
||||
from douyin.handlers.screenshot_manager.save_image_helpers import annotate_debug_image
|
||||
|
||||
def test_cleanup_importable(self):
|
||||
from douyin.handlers.screenshot_manager import cleanup
|
||||
image = Image.new("RGB", (240, 160), "white")
|
||||
annotated = annotate_debug_image(
|
||||
image=image,
|
||||
picname="fudai_icon",
|
||||
step="detected",
|
||||
coords=(80, 60, 180, 100),
|
||||
extra_info={"detected_x_scaled": 150, "result": "success"},
|
||||
)
|
||||
|
||||
assert cleanup is not None
|
||||
|
||||
def test_async_save_importable(self):
|
||||
from douyin.handlers.screenshot_manager import async_save
|
||||
|
||||
assert async_save is not None
|
||||
|
||||
def test_save_files_importable(self):
|
||||
from douyin.handlers.screenshot_manager import save_files
|
||||
|
||||
assert save_files is not None
|
||||
|
||||
def test_save_image_importable(self):
|
||||
from douyin.handlers.screenshot_manager import save_image
|
||||
|
||||
assert save_image is not None
|
||||
assert annotated.size == image.size
|
||||
green_pixels = 0
|
||||
for x in range(30, 151):
|
||||
for y in range(10, 81):
|
||||
r, g, b = annotated.getpixel((x, y))
|
||||
if g >= 160 and r <= 80 and b <= 120:
|
||||
green_pixels += 1
|
||||
assert green_pixels > 20
|
||||
|
||||
|
||||
class TestScreenshotManagerSubmodules:
|
||||
"""screenshot_manager 子模块存在性测试。"""
|
||||
|
||||
def test_paths_importable(self):
|
||||
from douyin.handlers.screenshot_manager import paths
|
||||
|
||||
assert paths is not None
|
||||
|
||||
def test_cleanup_importable(self):
|
||||
from douyin.handlers.screenshot_manager import cleanup
|
||||
|
||||
assert cleanup is not None
|
||||
|
||||
def test_async_save_importable(self):
|
||||
from douyin.handlers.screenshot_manager import async_save
|
||||
|
||||
assert async_save is not None
|
||||
|
||||
def test_save_files_importable(self):
|
||||
from douyin.handlers.screenshot_manager import save_files
|
||||
|
||||
assert save_files is not None
|
||||
|
||||
def test_save_image_importable(self):
|
||||
from douyin.handlers.screenshot_manager import save_image
|
||||
|
||||
assert save_image is not None
|
||||
Reference in new issue
Block a user