Files
douyi_guaji_main/douyin/handlers/douyin_screenshot_handler.py
T

323 lines
12 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from __future__ import annotations
"""
截图与图像处理模块
==================
职责:截图、图像处理和坐标转换
- 截图裁剪
- 保存步骤截图
- 坐标缩放
"""
import os
import shutil
import time
from typing import Any, Tuple
class ScreenshotHandler:
"""截图处理器:统一管理截图操作"""
@staticmethod # type: ignore[misc]
def cut_pic(
analyser: Any,
x_top: int = 1,
y_top: int = 1,
x_bottom: int = 2,
y_bottom: int = 2,
target: str = "",
cut_pic_name: str = "",
y_pianyi: int = 0,
) -> None:
"""裁剪图片:保持与原 fudai1.cut_pic 相同的坐标语义(1080x2400 基准)。
参数:
analyser: 分析器对象
x_top: 顶部X坐标(基准1080)
y_top: 顶部Y坐标(基准2400)
x_bottom: 底部X坐标(基准1080)
y_bottom: 底部Y坐标(基准2400)
target: 目标路径
cut_pic_name: 裁剪图片名称
y_pianyi: Y轴偏移
"""
try:
x1 = x_top * analyser.resolution_ratio_x // 1080
y1 = y_top * analyser.resolution_ratio_y // 2400 + int(y_pianyi or 0)
x2 = x_bottom * analyser.resolution_ratio_x // 1080
y2 = y_bottom * analyser.resolution_ratio_y // 2400 + int(y_pianyi or 0)
analyser.operation.cut_pic((x1, y1), (x2, y2), target, cut_pic_name)
except Exception as e:
try:
analyser._log_kv( # type: ignore[attr-defined]
"截图",
"cut_pic 失败",
error=str(e),
x_top=x_top,
y_top=y_top,
x_bottom=x_bottom,
y_bottom=y_bottom,
y_pianyi=y_pianyi,
log_type="SCREENSHOT",
)
except Exception:
pass
@staticmethod # type: ignore[misc]
def scale_from_base(analyser: Any, base_x: int, base_y: int) -> Tuple[int, int]:
"""将以 1080x2400 为基准的坐标缩放到当前设备分辨率并应用 y 偏移。
参数:
analyser: 分析器对象
base_x: 基准X坐标(1080基准)
base_y: 基准Y坐标(2400基准)
返回:
(scaled_x, scaled_y) 缩放后的坐标
"""
try:
rx = int(getattr(analyser, "resolution_ratio_x", 1080) or 1080)
ry = int(getattr(analyser, "resolution_ratio_y", 2400) or 2400)
y_off = int(getattr(analyser, "y_pianyi", 0) or 0)
sx = int(base_x * rx // 1080)
sy = int(base_y * ry // 2400) + y_off
return sx, sy
except Exception:
try:
return int(base_x), int(base_y)
except Exception:
return 0, 0
@staticmethod # type: ignore[misc]
def save_step_screenshot(analyser: Any, label: str) -> None:
"""保存当前截图到 `pic/steps/` 目录,文件名包含 run_id 与序号,供调试使用。
参数:
analyser: 分析器对象
label: 标签(用于文件名)
"""
try:
from douyin.core import path_config
pic_dir = path_config.get_pic_dir()
# 确保有最新截图
try:
analyser.operation.get_screenshot(analyser.device_id, use_service=False)
except Exception:
pass
src = os.path.join(pic_dir, "screenshot.png")
if not os.path.exists(src):
return
steps_dir = os.path.join(pic_dir, "steps")
os.makedirs(steps_dir, exist_ok=True)
fname = (
f"{getattr(analyser, '_run_id', 'run')}_"
f"{getattr(analyser, '_log_seq', 0):04d}_{label}.png"
)
dst = os.path.join(steps_dir, fname)
try:
shutil.copy(src, dst)
except Exception:
# 最后降级为重命名保存
try:
shutil.copy(src, dst)
except Exception:
pass
except Exception:
pass
@staticmethod # type: ignore[misc]
def capture_roi_screenshot(
analyser: Any, label: str, sx: int, sy: int, size: int, screenshot_path: str = None
) -> str:
"""捕获以 (sx,sy) 为中心,边长为 size 的 ROI 截图,保存到 `pic/` 并返回路径。"""
try:
from douyin.core import path_config
from PIL import Image, UnidentifiedImageError # type: ignore[import]
import io
if screenshot_path is None:
try:
analyser.operation.get_screenshot(analyser.device_id, use_service=False)
except Exception:
pass
pic_path = path_config.get_screenshot_path()
else:
pic_path = screenshot_path
if not os.path.exists(pic_path):
return ""
half = int(size // 2)
left = int(max(0, sx - half))
top = int(max(0, sy - half))
right = int(sx + half)
bottom = int(sy + half)
data = None
for _attempt in range(6):
try:
try:
analyser.operation.get_screenshot(analyser.device_id, use_service=False)
except Exception:
pass
if not os.path.exists(pic_path):
time.sleep(0.08)
continue
if os.path.getsize(pic_path) < 1024:
time.sleep(0.08)
continue
with open(pic_path, "rb") as f:
data = f.read()
if not data or not data.startswith(b"\x89PNG") or b"IEND" not in data[-512:]:
data = None
time.sleep(0.08)
continue
try:
img_test = Image.open(io.BytesIO(data))
img_test.verify()
except Exception:
data = None
time.sleep(0.08)
continue
break
except Exception:
data = None
time.sleep(0.08)
if not data:
return ""
try:
img = Image.open(io.BytesIO(data))
img.load()
except UnidentifiedImageError:
return ""
except Exception:
try:
try:
analyser.operation.get_screenshot(analyser.device_id, use_service=False)
except Exception:
pass
with open(pic_path, "rb") as f:
data2 = f.read()
if not (data2 and data2.startswith(b"\x89PNG") and b"IEND" in data2[-512:]):
return ""
img = Image.open(io.BytesIO(data2))
img.load()
except Exception:
return ""
w, h = img.size
left = max(0, min(left, w - 1))
top = max(0, min(top, h - 1))
right = max(1, min(right, w))
bottom = max(1, min(bottom, h))
roi = img.crop((left, top, right, bottom))
img.close()
out_dir = path_config.get_pic_dir()
os.makedirs(out_dir, exist_ok=True)
fname = f"roi_{label}_{getattr(analyser, '_run_id', 'run')}_{int(time.time())}.png"
out_path = os.path.join(out_dir, fname)
roi.save(out_path)
return out_path
except Exception:
try:
analyser._log_kv(
"截图",
"_capture_roi_screenshot 失败",
label=label,
sx=sx,
sy=sy,
size=size,
)
except Exception:
pass
return ""
@staticmethod # type: ignore[misc]
def match_icon_template(
analyser: Any, roi_path: str, template_path: str, threshold: float | None = None
) -> Tuple[bool, float]:
"""对比 ROI 与模板,返回 (detected, score)。"""
try:
if not roi_path or not os.path.exists(roi_path):
return False, 0.0
if not template_path or not os.path.exists(template_path):
return False, 0.0
from PIL import Image # type: ignore[import]
import numpy as np # type: ignore[import]
_roi_raw = Image.open(roi_path)
_tpl_raw = Image.open(template_path)
try:
roi_img = _roi_raw.convert("RGB")
tpl_img = _tpl_raw.convert("RGB")
finally:
_roi_raw.close()
_tpl_raw.close()
rw, rh = roi_img.size
tw, th = tpl_img.size
if tw > rw or th > rh:
scale = min(rw / tw, rh / th, 1.0)
tpl_img = tpl_img.resize((max(1, int(tw * scale)), max(1, int(th * scale))), Image.Resampling.LANCZOS)
roi_gray = np.asarray(roi_img.convert("L"), dtype=np.float32)
tpl_gray = np.asarray(tpl_img.convert("L"), dtype=np.float32)
if roi_gray.shape == tpl_gray.shape:
A = roi_gray.flatten() - roi_gray.flatten().mean()
B = tpl_gray.flatten() - tpl_gray.flatten().mean()
denom = np.linalg.norm(A) * np.linalg.norm(B)
if denom <= 1e-6:
return False, 0.0
score = float(np.dot(A, B) / denom)
used_thr = float(threshold) if threshold is not None else float(getattr(analyser, "yanghao_icon_match_threshold", 0.70) or 0.70)
return bool(score >= used_thr), float(score)
ih, iw = roi_gray.shape
th, tw = tpl_gray.shape
if ih >= th and iw >= tw:
cy = max(0, min(ih // 2 - th // 2, ih - th))
cx = max(0, min(iw // 2 - tw // 2, iw - tw))
window = roi_gray[cy:cy + th, cx:cx + tw]
a = window.flatten() - window.flatten().mean()
b = tpl_gray.flatten() - tpl_gray.flatten().mean()
denom = np.linalg.norm(a) * np.linalg.norm(b)
score = float(np.dot(a, b) / denom) if denom > 1e-6 else -1.0
if score >= 0.55:
used_thr = float(threshold) if threshold is not None else float(getattr(analyser, "yanghao_icon_match_threshold", 0.70) or 0.70)
return bool(score >= used_thr), float(score)
best = -1.0
step_y = max(1, th // 6)
step_x = max(1, tw // 6)
for y in range(0, ih - th + 1, step_y):
for x in range(0, iw - tw + 1, step_x):
window = roi_gray[y:y + th, x:x + tw]
a = window.flatten() - window.flatten().mean()
b = tpl_gray.flatten() - tpl_gray.flatten().mean()
denom = np.linalg.norm(a) * np.linalg.norm(b)
if denom <= 1e-6:
continue
score = float(np.dot(a, b) / denom)
if score > best:
best = score
if best < -0.5:
return False, 0.0
used_thr = float(threshold) if threshold is not None else float(getattr(analyser, "yanghao_icon_match_threshold", 0.70) or 0.70)
return bool(best >= used_thr), float(best)
except Exception:
try:
analyser._log_kv(
"检测",
"_match_icon_template 异常",
roi_path=roi_path,
template_path=template_path,
)
except Exception:
pass
return False, 0.0