Improve screenshot fallback and miss samples

This commit is contained in:
Your Name committed 2026-06-19 12:54:57 +08:00
1 parent 040ccd8161
commit 207748a901
5 files changed
+135

No files matched your search

@@ -468,6 +468,38 @@ def capture_screenshot_direct(
return True
return False
except Exception:
try:
stdout_fallback_start = time_module.time()
result = subprocess.run(
[adb, "-s", device_id, "shell", "screencap", "-p"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
check=True,
timeout=8,
)
with open(tmp_file, "wb", buffering=262144) as f:
f.write(result.stdout or b"")
if _valid_png(tmp_file) and _safe_replace_file(tmp_file, out_file):
_mirror_screenshot_to_external(out_file)
if _valid_png(out_file):
overall_dur = (time_module.time() - overall_start) * 1000.0
fallback_total_dur = (
time_module.time() - stdout_fallback_start
) * 1000.0
logging.info(
"[截图] ✓ 获取屏幕截图成功 | 总耗时(ms)=%.1f (shell stdout回退成功, 回退部分耗时=%.1fms)",
overall_dur,
fallback_total_dur,
)
return True
except Exception as stdout_error:
try:
_uo_log(
f"[截图] shell stdout回退失败:{type(stdout_error).__name__}: {stdout_error}",
"WARNING",
)
except Exception:
pass
# 再次回退:尝试用 screenshot1.png 覆盖 screenshot.png
try:
subprocess.run(
@@ -1039,6 +1039,33 @@ class FudaiDetector:
template_ok = self.confirm_current_icon_template(pic, detected_x)
shape_ok = self.confirm_pixel_candidate_shape(pic, detected_x)
if not (template_ok or shape_ok):
try:
if douyin_screenshot_manager is not None:
detected_x_scaled = int(detected_x * self.rx / 1080)
debug_y = int(403 * self.ry / 2400) + self.y_pianyi
douyin_screenshot_manager.save_debug_image(
pic.convert("RGB"),
operation_type="pixel_detect",
picname="fdicon_confirm_failed",
step="miss_or_not_found",
coords=(
max(0, detected_x_scaled - 80),
max(0, debug_y - 100),
min(pic.size[0], detected_x_scaled + 80),
min(pic.size[1], debug_y + 140),
),
extra_info={
"reason": "pixel_hit_but_template_and_shape_failed",
"region": region["name"],
"x": detected_x,
"template_ok": template_ok,
"shape_ok": shape_ok,
},
background=True,
force=True,
)
except Exception:
pass
try:
self.analyser._log_kv(
"[福袋查找] 检测完成",
+1
View File
@@ -177,6 +177,7 @@ def _classify_long_term_sample(path: Path, log_text: str) -> str | None:
"miss",
"not_found",
"no_fudai",
"confirm_failed",
)
):
return "miss_or_not_found"
+28
View File
@@ -233,3 +233,31 @@ def test_collect_run_regression_artifacts_does_not_promote_normal_stage_images_f
assert result["counts"]["long_term_samples"] == 0
assert not (root / "artifacts" / "fudai_regression_samples" / "index.jsonl").exists()
def test_collect_run_regression_artifacts_promotes_pixel_confirm_failed_as_miss_sample(
tmp_path, monkeypatch
):
from douyin.testing.regression_collector import collect_run_regression_artifacts
root = tmp_path
debug = root / "pic" / "debug_captures" / "2026-06-19"
debug.mkdir(parents=True)
(debug / "20260619_px_fdicon_confirm_failed_x162.png").write_bytes(b"confirm-failed")
monkeypatch.setattr(
"douyin.testing.regression_collector.path_config.get_project_root",
lambda: str(root),
)
result = collect_run_regression_artifacts(
run_id="confirm_failed",
started_at=0.0,
full_run_package=False,
max_debug_images=10,
)
assert result["counts"]["long_term_samples"] == 1
sample_root = root / "artifacts" / "fudai_regression_samples"
index_text = (sample_root / "index.jsonl").read_text(encoding="utf-8")
assert '"category": "miss_or_not_found"' in index_text
+47
View File
@@ -30,3 +30,50 @@ def test_capture_screenshot_direct_invalid_fallback_png_returns_false(tmp_path):
result = capture_screenshot_direct(fake_self, "MOCK", "pic")
assert result is False
def test_capture_screenshot_direct_uses_shell_stdout_fallback_when_execout_and_pull_fail(tmp_path):
from douyin.core.underlying_operations_pkg.screenshot_direct import (
capture_screenshot_direct,
)
fake_self = SimpleNamespace(
_adb_path=lambda: "adb",
get_screenshot=lambda *_a, **_k: False,
)
pic_dir = tmp_path / "pic"
pic_dir.mkdir()
png_bytes = b"\x89PNG\r\n\x1a\n" + (b"x" * 2048) + b"IEND"
calls: list[list[str]] = []
class FakePopen:
returncode = 1
def __init__(self, cmd, **_kwargs):
calls.append(list(cmd))
def communicate(self, timeout=None): # noqa: ARG002
return b"", None
def fake_run(cmd, **_kwargs):
calls.append(list(cmd))
if cmd[:5] == ["adb", "-s", "MOCK", "shell", "screencap"] and "-p" in cmd and len(cmd) == 6:
return SimpleNamespace(returncode=0, stdout=png_bytes, stderr=b"")
raise RuntimeError("file fallback unavailable")
with patch(
"douyin.core.underlying_operations_pkg.screenshot_direct.path_config.get_pic_dir",
return_value=str(pic_dir),
), patch(
"douyin.core.underlying_operations_pkg.screenshot_direct.subprocess.Popen",
FakePopen,
), patch(
"douyin.core.underlying_operations_pkg.screenshot_direct.subprocess.run",
side_effect=fake_run,
):
result = capture_screenshot_direct(fake_self, "MOCK", "pic")
assert result is True
assert (pic_dir / "screenshot.png").exists()
assert any(cmd[:5] == ["adb", "-s", "MOCK", "shell", "screencap"] and len(cmd) == 6 for cmd in calls)