修正福袋详情调试图与图标检测

This commit is contained in:
Your Name committed 2026-06-03 01:34:30 +08:00
1 parent 1d37d57c90
commit 098083a354
11 files changed
+244 -127

No files matched your search

+1 -1
View File
@@ -216,7 +216,7 @@
"monitor_critical_mb": 200.0
},
"debug_captures": {
"enabled": true,
"enabled": false,
"keep_days": 1,
"max_files": 1000,
"auto_cleanup": true,
-12
View File
@@ -37,18 +37,6 @@ def check_detail_height(analyser: Any) -> int:
if detected_v2_pixels:
return detected_v2_pixels
try:
from douyin.fudai.douyin_fudai_core_pkg.ocr_switches import (
resolve_fudai_detail_ocr_switches_from_analyser,
)
if resolve_fudai_detail_ocr_switches_from_analyser(analyser).task_count:
detected_v2 = _check_detail_task_count_v2(analyser)
if detected_v2:
return detected_v2
except Exception:
pass
return 0
+21 -4
View File
@@ -224,7 +224,13 @@ def check_have_fudai_with_ready_screenshot(
# 初始化扫描范围变量(用于日志记录)
scan_x_start = 41 * rx // 1080
scan_x_end = min(410 * rx // 1080, width)
scan_y = 403 * ry // 2400 + y_pianyi_val
scan_y_candidates = [
403 * ry // 2400 + y_pianyi_val,
503 * ry // 2400 + y_pianyi_val,
354 * ry // 2400 + y_pianyi_val,
]
scan_y_values = [y for y in scan_y_candidates if 0 <= y < height]
scan_y = scan_y_values[0] if scan_y_values else 403 * ry // 2400 + y_pianyi_val
# 获取最优搜索起始位置(使用历史记录或默认值)
try:
@@ -260,12 +266,23 @@ def check_have_fudai_with_ready_screenshot(
picname="fudai_icon",
step="before_detect",
coords=(
(scan_x_start, scan_y - 3, scan_x_end, scan_y + 4)
if scan_y >= 3
(
scan_x_start,
max(0, min(scan_y_values) - 3),
scan_x_end,
min(height, max(scan_y_values) + 4),
)
if scan_y_values
else None
),
extra_info={
"scan_range": f"x:{scan_x_start}-{scan_x_end}, y:{scan_y}",
"scan_range": (
f"x:{scan_x_start}-{scan_x_end}, "
f"y:{min(scan_y_values)}-{max(scan_y_values)}"
if scan_y_values
else f"x:{scan_x_start}-{scan_x_end}, y:{scan_y}"
),
"candidate_y": ",".join(str(y) for y in scan_y_values),
"resolution": f"{rx}x{ry}",
"y_offset": y_pianyi_val,
"loop": loop,
@@ -43,12 +43,19 @@ def scan_with_numpy(
try:
# 计算检测区域,考虑分辨率缩放和偏移。
# 新版福袋 icon 可能更靠左,numpy 快速扫描区域向左扩大。
y0 = 354 * ry // 2400 + y_pianyi_val
x1 = max(0, 120 * rx // 1080)
# 新版福袋 icon 可能更靠左,也可能出现在 403/503 两条高度带。
y_candidates = [
403 * ry // 2400 + y_pianyi_val,
503 * ry // 2400 + y_pianyi_val,
354 * ry // 2400 + y_pianyi_val,
]
x1 = max(0, 41 * rx // 1080)
x2 = min(width, 410 * rx // 1080)
y_top = max(0, y0 - 3)
y_bottom = min(height, y0 + 4)
y_values = [y for y in y_candidates if 0 <= y < height]
if not y_values:
return False, None
y_top = max(0, min(y_values) - 3)
y_bottom = min(height, max(y_values) + 4)
if not (x1 < x2 and y_top < y_bottom):
return False, None
@@ -142,6 +149,7 @@ def scan_with_numpy(
"detected_x": detected_x,
"detected_x_scaled": x1 + col_idx,
"scan_range": f"x:{x1}-{x2}, y:{y_top}-{y_bottom}",
"candidate_y": ",".join(str(y) for y in y_values),
"resolution": f"{rx}x{ry}",
"result": "success",
},
@@ -113,91 +113,100 @@ def scan_pixel_by_pixel(
# 计算实际扫描范围,防止越界
scan_x_start = 41 * rx // 1080
scan_x_end = min(410 * rx // 1080, pic_width)
scan_y = 403 * ry // 2400 + y_pianyi_val
scan_y_candidates = [
403 * ry // 2400 + y_pianyi_val,
503 * ry // 2400 + y_pianyi_val,
354 * ry // 2400 + y_pianyi_val,
]
optimal_x_scaled = _pick_optimal_x(analyser, scan_x_start, scan_x_end, rx)
# 确保最优位置在搜索范围内
optimal_x_scaled = max(scan_x_start, min(optimal_x_scaled, scan_x_end - 1))
if not (0 <= scan_y < pic_height):
scan_y_values = [y for y in scan_y_candidates if 0 <= y < pic_height]
if not scan_y_values:
return False, None
# 精度优化:同时检查上下几行像素
for offset_y in (0, -1, 1, -2, 2):
check_y = scan_y + offset_y
if not (0 <= check_y < pic_height):
continue
for scan_y in scan_y_values:
for offset_y in (0, -1, 1, -2, 2):
check_y = scan_y + offset_y
if not (0 <= check_y < pic_height):
continue
found_x = _search_along_y(
pix, scan_x_start, scan_x_end, optimal_x_scaled, check_y
)
if found_x is None:
continue
# 命中:转换回 1080 基准
detected_x = int(found_x * 1080.0 / max(1, rx))
detected = True
try:
analyser.last_fudai_x = detected_x
except Exception:
pass
# 双截图记录:成功识别存档
try:
from douyin.handlers import douyin_screenshot_manager
detect_roi = pic_new.crop(
(
scan_x_start,
max(0, scan_y - 3),
scan_x_end,
min(pic_height, scan_y + 4),
)
).convert("RGB")
douyin_screenshot_manager.save_last_success(
detect_roi,
operation_type="pixel_detect",
found_x = _search_along_y(
pix, scan_x_start, scan_x_end, optimal_x_scaled, check_y
)
if found_x is None:
continue
pic_rgb_full = pic_new.convert("RGB")
debug_path = douyin_screenshot_manager.save_debug_image(
pic_rgb_full,
operation_type="pixel_detect",
picname="fudai_icon",
step="detected",
coords=(
scan_x_start,
max(0, scan_y - 3),
scan_x_end,
min(pic_height, scan_y + 4),
),
extra_info={
"detected_x": detected_x,
"detected_x_scaled": found_x,
"scan_range": f"x:{scan_x_start}-{scan_x_end}, y:{scan_y}",
"resolution": f"{rx}x{ry}",
"result": "success",
},
)
if debug_path:
try:
analyser._log_kv(
"[调试图像]",
"福袋图标检测成功保存完整截图",
path=debug_path,
detected_x=detected_x,
# 命中:转换回 1080 基准
detected_x = int(found_x * 1080.0 / max(1, rx))
detected = True
try:
analyser.last_fudai_x = detected_x
except Exception:
pass
# 双截图记录:成功识别存档
try:
from douyin.handlers import douyin_screenshot_manager
detect_roi = pic_new.crop(
(
scan_x_start,
max(0, scan_y - 3),
scan_x_end,
min(pic_height, scan_y + 4),
)
except Exception:
pass
except Exception:
pass
).convert("RGB")
douyin_screenshot_manager.save_last_success(
detect_roi,
operation_type="pixel_detect",
)
try:
analyser.last_find_fudai_time = time.time()
except Exception:
pass
break # 找到后退出 offset_y 循环
pic_rgb_full = pic_new.convert("RGB")
debug_path = douyin_screenshot_manager.save_debug_image(
pic_rgb_full,
operation_type="pixel_detect",
picname="fudai_icon",
step="detected",
coords=(
scan_x_start,
max(0, scan_y - 3),
scan_x_end,
min(pic_height, scan_y + 4),
),
extra_info={
"detected_x": detected_x,
"detected_x_scaled": found_x,
"scan_range": f"x:{scan_x_start}-{scan_x_end}, y:{scan_y}",
"candidate_y": ",".join(str(y) for y in scan_y_values),
"resolution": f"{rx}x{ry}",
"result": "success",
},
)
if debug_path:
try:
analyser._log_kv(
"[调试图像]",
"福袋图标检测成功保存完整截图",
path=debug_path,
detected_x=detected_x,
)
except Exception:
pass
except Exception:
pass
try:
analyser.last_find_fudai_time = time.time()
except Exception:
pass
break
if detected:
break
except Exception:
# 像素检测失败,记录但不影响主流程
pass
+51
View File
@@ -23,6 +23,8 @@ FUDAI_DETAIL_FIELD_DEBUG_PICNAMES = {
"福袋任务列表",
"参与按钮",
"福袋参与人数",
"福袋任务数量_v2",
"福袋任务数量_v2_1task",
"live_room_title_mid",
}
@@ -38,6 +40,52 @@ def is_fudai_detail_field_debug_picname(picname: Any) -> bool:
return name.startswith("福袋倒计时_候选")
def _is_fudai_detail_debug_filename(filename: str) -> bool:
"""识别 debug_captures 中的福袋详情旧图文件名。"""
try:
name = str(filename or "")
except Exception:
return False
if "福袋详情综合标注" in name:
return True
if "福袋倒计时_候选" in name:
return True
return any(picname in name for picname in FUDAI_DETAIL_FIELD_DEBUG_PICNAMES)
def cleanup_fudai_detail_debug_captures(*, keep_path: str | None = None) -> int:
"""清理当天目录里的福袋详情旧调试图,避免新旧标注混在一起。"""
removed = 0
try:
from douyin.handlers import douyin_screenshot_manager
debug_dir = douyin_screenshot_manager.get_debug_captures_dir()
except Exception:
return 0
try:
keep_abs = os.path.abspath(keep_path) if keep_path else ""
except Exception:
keep_abs = ""
try:
for filename in os.listdir(debug_dir):
if not _is_fudai_detail_debug_filename(filename):
continue
path = os.path.join(debug_dir, filename)
try:
if keep_abs and os.path.abspath(path) == keep_abs:
continue
if os.path.isfile(path):
os.remove(path)
removed += 1
except Exception:
continue
except Exception:
return removed
return removed
def log_debug_error(
analyser: Any,
error: Exception,
@@ -398,6 +446,8 @@ def save_fudai_detail_debug_overview(
return None
try:
cleanup_fudai_detail_debug_captures()
width, height = pic_rgb.size
rx = int(getattr(analyser, "resolution_ratio_x", 1080) or 1080)
ry = int(getattr(analyser, "resolution_ratio_y", 2400) or 2400)
@@ -475,6 +525,7 @@ def save_fudai_detail_debug_overview(
},
annotate=False,
background=False,
force=True,
)
if debug_path:
try:
@@ -61,21 +61,20 @@ class FudaiDetector:
def _init_search_regions(self):
"""初始化搜索区域
实际福袋出现区域:(153, 348) 到 (561, 451) [基于1080x2400基准]
只在此区域内搜索,不扩大搜索范围
实际福袋出现区域按 2.0 直播间样式向左、向下扩展。
普通 finder 的调试样本已验证 icon 可能落在 x=41..410、y=403/503 两条带。
"""
# 实际福袋区域(X=153-561, Y=348-451)
# 这是用户确认的福袋实际出现区域,只在此区域内搜索
# 旧区域 X=153..561/Y=348..451 保留在扩展区域内;新版图标更靠左且高度可能下移。
self.search_regions = [
{
"name": "实际福袋区域",
"priority": 1,
"x1": 153 * self.rx // 1080,
"x1": 41 * self.rx // 1080,
"x2": 561 * self.rx // 1080,
"y_top": 348 * self.ry // 2400 + self.y_pianyi,
"y_bottom": 451 * self.ry // 2400 + self.y_pianyi,
"y": (348 + 451) // 2 * self.ry // 2400 + self.y_pianyi, # 中心Y坐标
"y_range": (451 - 348) // 2 + 1, # 覆盖整个Y范围
"y_bottom": 540 * self.ry // 2400 + self.y_pianyi,
"y": (348 + 540) // 2 * self.ry // 2400 + self.y_pianyi,
"y_range": (540 - 348) // 2 + 1,
},
]
@@ -53,6 +53,7 @@ def save_debug_image( # type: ignore
annotate: bool = True,
auto_cleanup: bool = True,
background: Optional[bool] = None,
force: bool = False,
) -> Optional[str]:
"""保存调试图像到存档目录
@@ -71,7 +72,7 @@ def save_debug_image( # type: ignore
保存的文件路径,失败返回None
"""
try:
if not is_debug_captures_enabled():
if not force and not is_debug_captures_enabled():
return None
maybe_auto_cleanup(auto_cleanup, _mgr_log)
+9
View File
@@ -136,6 +136,15 @@ class TestScanPixelByPixel:
detected, x = scan_pixel_by_pixel(a, pic, rx=1080, ry=2400, y_pianyi_val=0)
assert detected is True
def test_target_at_shifted_503_y_detects(self):
"""新版福袋 icon 可能下移到 y=503 候选带。"""
from douyin.detection.detection_pkg.finder_pixel_scan import scan_pixel_by_pixel
a = _mk_analyser()
pic = _make_with_target(TARGET, 200, 250, 503, 505)
detected, x = scan_pixel_by_pixel(a, pic, rx=1080, ry=2400, y_pianyi_val=0)
assert detected is True
assert 200 <= x <= 250
def test_target_outside_offset_y_misses(self):
"""目标在 scan_y+10(超过 offset 范围 ±2)→ 应失败。"""
from douyin.detection.detection_pkg.finder_pixel_scan import scan_pixel_by_pixel
+51
View File
@@ -57,4 +57,55 @@ def test_fudai_detail_debug_overview_saves_one_annotated_image(tmp_path, monkeyp
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_overview_cleans_old_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 not old_detail.exists()
assert not old_overview.exists()
assert unrelated.exists()
+8 -24
View File
@@ -121,35 +121,19 @@ def test_config_manager_loads_fudai_detail_ui_and_ocr_switches(tmp_path):
assert analyser.fudai_detail_ocr_participants_enabled is True
def test_v2_task_count_ocr_tries_one_task_shifted_region_first():
from douyin.detection.detection_pkg.detail import _check_detail_task_count_v2
def test_check_detail_height_v2_does_not_run_task_count_ocr(monkeypatch):
from douyin.detection.detection_pkg import detail
calls = []
monkeypatch.setattr(detail, "_check_detail_task_count_v2_pixels", lambda _analyser: 0)
class _Operation:
def analyse_pic_word(self, name, _mode):
calls.append(name)
if name == "福袋任务数量_v2_1task":
return "(0/1)"
return ""
def _ocr_should_not_run(_analyser):
raise AssertionError("v2 detail detection must not fallback to task-count OCR")
def analyse_pic_digits(self, name, _mode):
calls.append(name)
return ""
monkeypatch.setattr(detail, "_check_detail_task_count_v2", _ocr_should_not_run)
class _Analyser:
y_pianyi = 0
operation = _Operation()
analyser = SimpleNamespace(fudai_detail_ui_mode="v2_0")
def cut_pic(self, x1, y1, x2, y2, _prefix, name, _offset):
calls.append((name, (x1, y1, x2, y2)))
def _log_kv(self, *_args, **_kwargs):
return None
assert _check_detail_task_count_v2(_Analyser()) == 1
assert ("福袋任务数量_v2_1task", (890, 1855, 1040, 1950)) in calls
assert "福袋任务数量_v2" not in calls
assert detail.check_detail_height(analyser) == 0
def test_v2_task_count_pixels_choose_one_task_before_ocr(tmp_path, monkeypatch):