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

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

+14 -15
View File
@@ -134,22 +134,20 @@ def _save_pixel_stage_overview(
detail_profile = get_fudai_detail_profile(mode, task_count=task_count)
lottery_profile = get_lottery_result_confirm_profile(mode)
regions: dict[str, tuple[str, tuple[int, int, int, int]]] = {}
regions: dict[str, tuple[str, tuple[int, int, int, int]]] = {
"lottery": ("has_lucky_draw_result", tuple(lottery_profile.get("pixel") or (181, 1261, 890, 1389))),
"attend": ("has_attend_button_red", (200, 2000, 880, 2120)),
"fansclub": ("has_fansclub_join_red", (200, 1940, 880, 2140)),
}
if state.get("has_fudai_detail"):
regions["detail_body"] = ("has_fudai_detail", (0, 1160, 1080, 2250))
if task_count not in (None, 0):
regions["task_count"] = ("has_fudai_detail", tuple(detail_profile.get("task_count") or (860, 1770, 1035, 1875)))
else:
if state.get("has_coupon_bg"):
if details.get("has_coupon_bg", {}).get("method") != "disabled":
regions["coupon"] = ("has_coupon_bg", (9, 1173, 1079, 2255))
if state.get("has_redpacket_bg"):
if details.get("has_redpacket_bg", {}).get("method") != "disabled":
regions["redpacket"] = ("has_redpacket_bg", (200, 300, 880, 1400))
if state.get("has_lucky_draw_result"):
regions["lottery"] = ("has_lucky_draw_result", tuple(lottery_profile.get("pixel") or (181, 1261, 890, 1389)))
if state.get("has_attend_button_red"):
regions["attend"] = ("has_attend_button_red", (200, 2000, 880, 2120))
if state.get("has_fansclub_join_red"):
regions["fansclub"] = ("has_fansclub_join_red", (200, 1940, 880, 2140))
def _color_for(key: str) -> tuple[int, int, int]:
info = details.get(key, {})
@@ -162,14 +160,15 @@ def _save_pixel_stage_overview(
if x1 >= x2 or y1 >= y2:
continue
color = _color_for(key)
draw.rectangle((x1, y1, x2, y2), outline=color, width=4)
line_width = 4 if state.get(key) else 2
draw.rectangle((x1, y1, x2, y2), outline=color, width=line_width)
draw.text((x1 + 6, max(0, y1 - 26)), label, fill=color, font=small_font)
if state.get("has_live_room_pixel"):
x1, y1, x2, y2 = _live_room_icon_bbox(width, height)
color = _color_for("has_live_room_pixel")
draw.rectangle((x1, y1, x2, y2), outline=color, width=4)
draw.text((max(0, x1 - 120), max(0, y1 - 26)), "liveicon", fill=color, font=small_font)
x1, y1, x2, y2 = _live_room_icon_bbox(width, height)
color = _color_for("has_live_room_pixel")
line_width = 4 if state.get("has_live_room_pixel") else 2
draw.rectangle((x1, y1, x2, y2), outline=color, width=line_width)
draw.text((max(0, x1 - 120), max(0, y1 - 26)), "liveicon", fill=color, font=small_font)
panel_x = 24
lines = ["pixel stage overview"]
+22 -6
View File
@@ -28,6 +28,17 @@ FUDAI_DETAIL_FIELD_DEBUG_PICNAMES = {
"live_room_title_mid",
}
DETAIL_DEBUG_LABELS = {
"live_title": "live",
"content": "content",
"countdown": "time",
"value": "value",
"task": "task",
"task_count": "count",
"participants": "people",
"button": "button",
}
def is_fudai_detail_field_debug_picname(picname: Any) -> bool:
"""福袋详情字段级调试图统一跳过,只保留详情综合标注图。"""
@@ -478,7 +489,7 @@ def save_fudai_detail_debug_overview(
]
pad = 12
legend_h = 34 * len(legend_lines) + pad * 2
legend_y = max(0, height - legend_h - 78)
legend_y = max(0, height - legend_h - 12)
draw.rectangle((18, legend_y, min(width - 18, 1040), legend_y + legend_h), fill=(0, 0, 0))
for idx, line in enumerate(legend_lines):
draw.text((30, legend_y + 12 + idx * 34), line, fill=(255, 255, 255), font=small_font)
@@ -506,11 +517,16 @@ def save_fudai_detail_debug_overview(
(x1 - offset, y1 - offset, x2 + offset, y2 + offset),
outline=color,
)
label = f"{name} {'on' if enabled else 'off'}"
label_y = max(0, y1 - 28)
label_w = min(width - x1 - 2, max(120, len(label) * 13))
draw.rectangle((x1, label_y, x1 + label_w, label_y + 26), fill=(0, 0, 0))
draw.text((x1 + 4, label_y + 3), label, fill=color, font=small_font)
label_key = DETAIL_DEBUG_LABELS.get(key, key)
label = f"{label_key}:{'on' if enabled else 'off'}"
label_w = min(max(96, len(label) * 11), max(32, x2 - x1))
label_h = 24
label_x = x1 + 3
label_y = y1 + 3
if y2 - y1 < 42:
label_y = max(0, y1 - label_h - 2)
draw.rectangle((label_x, label_y, label_x + label_w, label_y + label_h), fill=(0, 0, 0))
draw.text((label_x + 4, label_y + 3), label, fill=color, font=small_font)
debug_path = douyin_screenshot_manager.save_debug_image(
pic_rgb,
@@ -158,10 +158,25 @@ def read_fudai_detail_once(analyser: Any) -> Tuple[str, Tuple, dict]:
# 步骤2:获取截图(仅用于后续OCR识别内容/倒计时/价值,不是为了获取任务数量)
# 注意:此时后台截图服务已被停止,直接调用底层截图避免递归
# 检查是否可以复用阶段诊断时的截图
# 检查是否可以复用阶段诊断时的截图。默认关闭:阶段诊断截图通常拍得更早,
# 用它做正式 OCR 容易读到未渲染完整的详情页,导致首轮 OCR 为空并触发重试。
screenshot_reused = False
try:
if getattr(analyser, "_screenshot_ready_for_ocr", False):
allow_stage_screenshot_reuse = bool(
getattr(analyser, "fudai_detail_reuse_stage_screenshot_enabled", False)
)
if getattr(analyser, "_screenshot_ready_for_ocr", False) and not allow_stage_screenshot_reuse:
analyser._screenshot_ready_for_ocr = False
try:
analyser._log_kv(
"福袋详情",
"read_fudai_detail_once:跳过复用阶段诊断截图,重新截图用于OCR",
log_type="SCREENSHOT",
reason="stage_screenshot_reuse_disabled",
)
except Exception:
pass
if getattr(analyser, "_screenshot_ready_for_ocr", False) and allow_stage_screenshot_reuse:
try:
pic_path = path_config.get_screenshot_path()
except Exception:
@@ -596,7 +611,7 @@ def read_fudai_detail_once(analyser: Any) -> Tuple[str, Tuple, dict]:
image_load_ms=image_load_ms,
service_stop_ms=service_stop_ms,
countdown_parse_ms=countdown_parse_ms,
screenshot_reused=bool(getattr(analyser, "_screenshot_ready_for_ocr", False)),
screenshot_reused=bool(screenshot_reused),
content_preview=(content or "")[:60],
prize_value_yuan=float(prize_value_yuan or 0.0),
time_text=time_text or "",
+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(