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
@@ -20,6 +20,7 @@ FUDAI_DETAIL_FIELD_DEBUG_PICNAMES = {
|
||||
"福袋倒计时_分钟",
|
||||
"福袋倒计时_秒",
|
||||
"福袋倒计时_整行",
|
||||
"福袋人数倒计时信息",
|
||||
"福袋任务列表",
|
||||
"参与按钮",
|
||||
"福袋参与人数",
|
||||
|
||||
@@ -33,6 +33,7 @@ from douyin.fudai.douyin_fudai_core_pkg import (
|
||||
)
|
||||
from douyin.fudai.douyin_fudai_core_pkg.contain_helpers import (
|
||||
extract_participants_and_clean,
|
||||
extract_participants_from_info_line,
|
||||
is_blank_text,
|
||||
run_heavy_ocr,
|
||||
should_use_heavy,
|
||||
@@ -210,8 +211,8 @@ def _parse_mmss_from_full_text(text: Any) -> Tuple[str, str]:
|
||||
return f"{mm_i:02d}", f"{ss_i:02d}"
|
||||
return "", ""
|
||||
|
||||
# 1) 标准格式:13:40 / 13:40 / 13 40
|
||||
m = re.search(r"(\d{1,2})\D+(\d{1,2})", raw)
|
||||
# 1) 标准格式:13:40 / 13:40
|
||||
m = re.search(r"(\d{1,2})\s*[::]\s*(\d{1,2})", raw)
|
||||
if m:
|
||||
mm, ss = _fmt_pair(m.group(1), m.group(2))
|
||||
if mm and ss:
|
||||
@@ -233,6 +234,16 @@ def _parse_mmss_from_full_text(text: Any) -> Tuple[str, str]:
|
||||
if mm and ss:
|
||||
return mm, ss
|
||||
|
||||
# 3) 仅识别到秒数残片:35后开奖 -> 00:35
|
||||
seconds_only = re.search(r"(?<!\d)(\d{1,2})(?=\D*(?:后开奖|开奖|后|$))", raw)
|
||||
if seconds_only:
|
||||
try:
|
||||
ss_i = int(seconds_only.group(1))
|
||||
except Exception:
|
||||
ss_i = -1
|
||||
if 0 <= ss_i <= 59:
|
||||
return "00", f"{ss_i:02d}"
|
||||
|
||||
return "", ""
|
||||
|
||||
|
||||
@@ -586,6 +597,12 @@ def get_fudai_contain(analyser, renwu: int = 2) -> Tuple[str, str, int, int, flo
|
||||
)
|
||||
detail_debug_regions.extend(
|
||||
[
|
||||
{
|
||||
"key": "task_count",
|
||||
"name": "任务数",
|
||||
"coords": tuple(profile.get("task_count") or ()),
|
||||
"enabled": bool(ocr_switches.task_count),
|
||||
},
|
||||
{
|
||||
"key": "content",
|
||||
"name": "内容",
|
||||
@@ -606,6 +623,16 @@ def get_fudai_contain(analyser, renwu: int = 2) -> Tuple[str, str, int, int, flo
|
||||
},
|
||||
]
|
||||
)
|
||||
try:
|
||||
task_count_box = tuple(profile.get("task_count") or ())
|
||||
if len(task_count_box) == 4:
|
||||
from douyin.detection.detection_pkg.detail import get_detail_task_count_digit_box
|
||||
|
||||
detail_debug_regions[-4]["coords"] = get_detail_task_count_digit_box(
|
||||
tuple(int(v) for v in task_count_box)
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
# 额外整行倒计时图:用于优先解析 mm:ss,降低分钟框被文案污染的风险。
|
||||
try:
|
||||
if ocr_switches.countdown:
|
||||
@@ -778,6 +805,7 @@ def get_fudai_contain(analyser, renwu: int = 2) -> Tuple[str, str, int, int, flo
|
||||
ocr_value = _ocr_runner.ocr_value
|
||||
ocr_button = _ocr_runner.ocr_button
|
||||
ocr_live_room_title = _ocr_runner.ocr_live_room_title
|
||||
ocr_info_line = _ocr_runner.ocr_info_line
|
||||
|
||||
# 使用线程池并行执行OCR(包括任务列表OCR,与其他OCR一起并行执行)
|
||||
# 任务数量已在 detect_fudai_detail_page 中检测,直接使用传入的 renwu 参数
|
||||
@@ -808,6 +836,12 @@ def get_fudai_contain(analyser, renwu: int = 2) -> Tuple[str, str, int, int, flo
|
||||
preprocess_for_ocr(analyser, "福袋参与人数"), 0
|
||||
)
|
||||
)
|
||||
info_line_raw = None
|
||||
if renwu == 1 and (ocr_switches.participants or ocr_switches.countdown):
|
||||
info_line_box = tuple(profile.get("info_line") or ())
|
||||
if len(info_line_box) == 4:
|
||||
analyser.cut_pic(*info_line_box, "", "福袋人数倒计时信息", analyser.y_pianyi)
|
||||
task_funcs["info_line"] = ocr_info_line
|
||||
# 并行补充倒计时额外候选(保持并行,不降级)
|
||||
for meta in countdown_candidate_meta:
|
||||
if meta.get("is_primary"):
|
||||
@@ -866,6 +900,8 @@ def get_fudai_contain(analyser, renwu: int = 2) -> Tuple[str, str, int, int, flo
|
||||
button_text_raw = res
|
||||
elif name == "participants":
|
||||
participants_raw = res
|
||||
elif name == "info_line":
|
||||
info_line_raw = res
|
||||
elif name == "live_room_title":
|
||||
live_room_title_raw, live_room_title_text_raw = res
|
||||
elif name.startswith("countdown_mm_c") or name.startswith("countdown_ss_c"):
|
||||
@@ -896,6 +932,21 @@ def get_fudai_contain(analyser, renwu: int = 2) -> Tuple[str, str, int, int, flo
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
elif renwu == 1 and info_line_raw:
|
||||
mm_line, ss_line = _parse_mmss_from_full_text(info_line_raw)
|
||||
if mm_line and ss_line:
|
||||
countdown_mm_raw, countdown_ss_raw = mm_line, ss_line
|
||||
if _detail_verbose_logs(analyser):
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"倒计时",
|
||||
"one-task 信息行命中,覆盖倒计时结果",
|
||||
full_text=str(info_line_raw or "")[:80],
|
||||
mm=mm_line,
|
||||
ss=ss_line,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
if picked:
|
||||
picked_idx = int(picked.get("idx", -1))
|
||||
if _detail_verbose_logs(analyser):
|
||||
@@ -1402,6 +1453,8 @@ def get_fudai_contain(analyser, renwu: int = 2) -> Tuple[str, str, int, int, flo
|
||||
participants = participants_only
|
||||
if copies_from_participants and not copies:
|
||||
copies = copies_from_participants
|
||||
if not participants and renwu == 1:
|
||||
participants = extract_participants_from_info_line(info_line_raw or "")
|
||||
|
||||
# 清理后再次检查观看任务
|
||||
if handle_content_watch_task(analyser, fudai_content_text, cleaned=True):
|
||||
|
||||
@@ -39,6 +39,26 @@ def extract_participants_and_clean(text: str):
|
||||
return participants, copies, cleaned
|
||||
|
||||
|
||||
def extract_participants_from_info_line(text: str) -> int:
|
||||
"""从人数+倒计时同一行文本中尽量提取参与人数。"""
|
||||
if not text:
|
||||
return 0
|
||||
compact = re.sub(r"\s+", "", str(text))
|
||||
m = re.search(r"(\d+(?:\.\d+)?)万人已参与", compact)
|
||||
if m:
|
||||
try:
|
||||
return int(float(m.group(1)) * 10000)
|
||||
except Exception:
|
||||
return 0
|
||||
m = re.search(r"(\d+)人已参与", compact)
|
||||
if m:
|
||||
try:
|
||||
return int(m.group(1))
|
||||
except Exception:
|
||||
return 0
|
||||
return 0
|
||||
|
||||
|
||||
def is_blank_text(text: Any) -> bool:
|
||||
return len(str(text or "").strip()) == 0
|
||||
|
||||
|
||||
@@ -68,9 +68,9 @@ def cut_detail_regions(
|
||||
x1, y1, x2, y2 = countdown_box
|
||||
w = max(1, int(x2 - x1))
|
||||
h = max(1, int(y2 - y1))
|
||||
# 纵向只取数字行:上方常有“超级福袋”等标题,容易让分钟误识别成 601。
|
||||
digit_y1 = min(y2 - 2, y1 + max(1, int(h * 0.34)))
|
||||
digit_y2 = min(y2, max(digit_y1 + 2, y1 + max(2, int(h * 0.92))))
|
||||
# 纵向只取倒计时 chip 的数字行,避免把下方商品标题混入分钟/秒 OCR。
|
||||
digit_y1 = min(y2 - 2, y1 + max(1, int(h * 0.18)))
|
||||
digit_y2 = min(y2, max(digit_y1 + 2, y1 + max(2, int(h * 0.62))))
|
||||
mm_x1 = x1
|
||||
mm_x2 = min(x2, x1 + max(1, int(w * 0.26)))
|
||||
ss_x1 = min(x2 - 1, x1 + max(1, int(w * 0.38)))
|
||||
|
||||
@@ -188,6 +188,18 @@ class OcrTaskRunner:
|
||||
finally:
|
||||
self._record_duration("ocr_countdown_full", start)
|
||||
|
||||
def ocr_info_line(self) -> Optional[str]:
|
||||
"""OCR 识别 one-task 的人数+倒计时同一行文本。"""
|
||||
start = time.time()
|
||||
try:
|
||||
name_line = _safe_preprocess(self.analyser, "福袋人数倒计时信息")
|
||||
return self.analyser.operation.analyse_pic_word(name_line, 0)
|
||||
except Exception as e:
|
||||
self._log_warn("OCR识别人数倒计时信息失败", e)
|
||||
return None
|
||||
finally:
|
||||
self._record_duration("ocr_info_line", start)
|
||||
|
||||
def ocr_value(self) -> Optional[str]:
|
||||
"""OCR 识别福袋价值(普通文本模型,支持小数点)。"""
|
||||
start = time.time()
|
||||
|
||||
@@ -13,6 +13,9 @@ from douyin.core import path_config
|
||||
from .cleanup import (
|
||||
cleanup_debug_captures,
|
||||
count_debug_captures_files,
|
||||
_EXTRA_INFO_KEY_CN,
|
||||
_PICNAME_CN,
|
||||
_STEP_CN,
|
||||
)
|
||||
from .paths import get_debug_captures_base_dir, get_debug_captures_dir
|
||||
from ._common import _mgr_log
|
||||
@@ -349,6 +352,20 @@ def annotate_debug_image(
|
||||
if x1 < x2 and y1 < y2:
|
||||
draw.rectangle((float(x1), float(y1), float(x2), float(y2)), outline="red", width=2)
|
||||
|
||||
def _draw_green_arrow(draw_ctx, target_x: int, target_y: int) -> None:
|
||||
try:
|
||||
img_w, img_h = annotated_image.size
|
||||
tx = max(0, min(int(target_x), img_w - 1))
|
||||
ty = max(0, min(int(target_y), img_h - 1))
|
||||
start_x = max(10, tx - 120)
|
||||
start_y = max(10, ty - 80)
|
||||
draw_ctx.line((start_x, start_y, tx, ty), fill=(0, 220, 80, 255), width=5)
|
||||
head = 16
|
||||
draw_ctx.line((tx, ty, tx - head, ty - 4), fill=(0, 220, 80, 255), width=5)
|
||||
draw_ctx.line((tx, ty, tx - 4, ty - head), fill=(0, 220, 80, 255), width=5)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
info_lines = []
|
||||
if picname:
|
||||
info_lines.append(str(_PICNAME_CN.get(picname, picname)))
|
||||
@@ -435,6 +452,12 @@ def annotate_debug_image(
|
||||
|
||||
try:
|
||||
if extra_info:
|
||||
is_fudai_icon_success = (
|
||||
picname == "fudai_icon"
|
||||
and step == "detected"
|
||||
and "detected_x_scaled" in extra_info
|
||||
and coords
|
||||
)
|
||||
if (
|
||||
"match_coords" in extra_info
|
||||
and isinstance(extra_info["match_coords"], (list, tuple))
|
||||
@@ -442,11 +465,13 @@ def annotate_debug_image(
|
||||
):
|
||||
mx1, my1, mx2, my2 = extra_info["match_coords"]
|
||||
odraw.rectangle((mx1, my1, mx2, my2), outline=(255, 0, 0, 200), width=2)
|
||||
elif "detected_x_scaled" in extra_info and coords:
|
||||
elif "detected_x_scaled" in extra_info and coords and not is_fudai_icon_success:
|
||||
dx = int(extra_info["detected_x_scaled"])
|
||||
cy = (coords[1] + coords[3]) // 2
|
||||
r = max(4, int(min(annotated_image.size) * 0.01))
|
||||
odraw.ellipse([dx - r, cy - r, dx + r, cy + r], fill=(0, 128, 255, 220), outline=(0, 64, 192, 220))
|
||||
if is_fudai_icon_success:
|
||||
_draw_green_arrow(odraw, int(extra_info["detected_x_scaled"]), (coords[1] + coords[3]) // 2)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
@@ -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