import subprocess from datetime import datetime, timedelta import os import warnings # PaddleOCR 3.x 会在初始化/导入时进行模型源连通性检查;在国内网络环境下容易卡住。 # 这里提前设置环境变量,避免启动耗时/卡死。 os.environ.setdefault("DISABLE_MODEL_SOURCE_CHECK", "True") # 额外抑制 paddle/oneDNN/glog 的冗余信息(INFO/WARNING) os.environ.setdefault("GLOG_minloglevel", "2") os.environ.setdefault("OMP_NUM_THREADS", "1") # 抑制 PyTorch DataLoader 的 pin_memory 警告(当没有 GPU 时,EasyOCR 会触发此警告) warnings.filterwarnings("ignore", message=".*pin_memory.*", category=UserWarning) warnings.filterwarnings("ignore", message=".*accelerator.*", category=UserWarning) import time as time_module # alias for existing code that uses `time` name time = time_module from PIL import Image, ImageFilter # type: ignore[import] from PIL import UnidentifiedImageError # type: ignore[import] import numpy as np # type: ignore[import] import logging import re import random import xml.etree.ElementTree as ET import shlex import contextlib import base64 from typing import Tuple, Optional, Any # Pillow 版本兼容:使用 Image.Resampling 或回退到 Image 常量 try: _RESAMPLE = Image.Resampling except Exception: _RESAMPLE = Image # 外部完整版 fudai 逻辑所在的工作树路径,用于镜像 screenshot 给那边使用 # 使用环境变量或默认路径,避免硬编码用户路径 _EXTERNAL_AXF_SCREENSHOT = os.path.join( os.environ.get( "EXTERNAL_DOUYIN_FUDAI_DIR", os.path.join( os.path.expanduser("~"), ".cursor", "worktrees", "douyin_guaji-main", "axf" ), ), "pic", "screenshot.png", ) def _safe_replace_file(tmp_file: str, out_file: str, max_retries: int = 2) -> bool: """安全地替换文件,处理Windows文件锁定问题。 根本优化:优先使用直接复制而不是替换,避免文件锁定问题。 如果文件被占用,直接复制内容而不是等待释放。 Args: tmp_file: 临时文件路径 out_file: 目标文件路径 max_retries: 最大重试次数(已优化为2次) Returns: True if successful, False otherwise """ import shutil if not os.path.exists(tmp_file): return False # 根本优化:优先尝试原子替换(最快),如果失败则直接复制(避免等待) try: # 方法1: 直接使用 os.replace (原子操作,最快) os.replace(tmp_file, out_file) return True except (OSError, PermissionError): # 如果原子替换失败(文件被占用),直接使用复制方法(避免等待) # 这样可以避免文件锁定导致的长时间等待 pass # 方法2: 直接复制文件内容(避免文件锁定问题) # 这是最可靠的方法,即使文件被占用也能成功 try: # 使用 shutil.copy2 保留元数据,但速度更快 shutil.copy2(tmp_file, out_file) # 复制成功后删除临时文件 try: os.remove(tmp_file) except Exception: pass return True except Exception: # 如果 shutil.copy2 也失败,使用手动复制(最后手段) try: with open(tmp_file, "rb") as src, open(out_file, "wb") as dst: # 使用更大的块大小提高复制速度(1MB) while True: chunk = src.read(1048576) # 1MB chunks if not chunk: break dst.write(chunk) # 复制成功后删除临时文件 try: os.remove(tmp_file) except Exception: pass return True except Exception: return False def _ensure_parent_dir(path: str) -> None: """Ensure the parent directory for `path` exists.""" try: parent = os.path.dirname(path) if parent: os.makedirs(parent, exist_ok=True) except Exception: # 不应因目录创建失败阻塞主流程,记录以便排查 logging.exception("无法创建父目录: %s", path) def _mirror_screenshot_to_external(src_file: str) -> None: """将当前项目下的 screenshot.png 同步一份到外部 axf 目录,便于完整版代码复用。 若外部目录/路径不存在或复制失败,会静默忽略,不影响主流程。 可通过环境变量 DISABLE_SCREENSHOT_MIRROR=1 禁用此功能以提升性能。 """ # 检查环境变量:是否禁用镜像功能(用于性能测试) if os.environ.get("DISABLE_SCREENSHOT_MIRROR", "0") == "1": return mirror_start = time_module.time() try: external_path = _EXTERNAL_AXF_SCREENSHOT if not external_path: return external_dir = os.path.dirname(external_path) if not external_dir: return # 检查外部路径是否是网络路径或可能较慢的路径 external_base = os.environ.get("EXTERNAL_DOUYIN_FUDAI_DIR", "") is_network_path = False if external_base: # 检查是否是网络路径(UNC路径或网络驱动器) if external_base.startswith("\\\\") or external_base.startswith("//"): is_network_path = True logging.warning( "[截图] 检测到网络路径,镜像可能较慢: %s", external_base ) # 检查是否是远程挂载点(简单启发式:包含常见网络挂载标识) elif ":" in external_base and len(external_base) > 3: # 可能是网络驱动器,但无法确定,记录路径供诊断 logging.debug("[截图] 镜像目标路径: %s", external_path) os.makedirs(external_dir, exist_ok=True) if not os.path.exists(src_file): return tmp = external_path + ".tmp" import shutil copy_start = time_module.time() shutil.copyfile(src_file, tmp) copy_dur = (time_module.time() - copy_start) * 1000.0 if copy_dur > 50.0 or is_network_path: logging.info( "[截图] 镜像复制耗时(ms)=%.1f src=%s dst=%s (网络路径=%s)", copy_dur, src_file, tmp, is_network_path, ) replace_start = time_module.time() _safe_replace_file(tmp, external_path) replace_dur = (time_module.time() - replace_start) * 1000.0 if replace_dur > 50.0 or is_network_path: logging.info( "[截图] 镜像文件替换耗时(ms)=%.1f (网络路径=%s)", replace_dur, is_network_path, ) mirror_total_dur = (time_module.time() - mirror_start) * 1000.0 if mirror_total_dur > 100.0 or is_network_path: logging.info( "[截图] 镜像总耗时(ms)=%.1f path=%s (网络路径=%s)", mirror_total_dur, external_path, is_network_path, ) except Exception: # 任何错误都不应该影响截图主流程 pass class underlying_operations: """底层操作类""" def __init__(self): # 关闭文档方向/矫正等重型前处理:我们的截图是手机UI,不是扫描文档。 # 支持延迟初始化(测试流程/只做点击时不需要 OCR)。 self.ocr: Any = None self.ocr_type = None # 'easyocr' 或 'paddleocr' # 用于存储OCR区域的坐标信息(key为picname,value为(left, top, right, bottom)) self._ocr_coords_cache: dict[str, tuple[int, int, int, int]] = {} # 默认使用CPU模式(删除GPU检测) self._gpu_available = False if not str(os.environ.get("SKIP_OCR_INIT", "")).strip(): # 优先使用EasyOCR(更快),如果失败则回退到PaddleOCR # 首先尝试EasyOCR devnull = None try: # 在Windows上,避免使用stderr重定向,可能会导致"lost sys.stderr"问题 # 改为直接静默导入和初始化 # 尝试按需导入 easyocr,避免静态检查器在缺少依赖时报错 import importlib try: easyocr = importlib.import_module("easyocr") except Exception: easyocr = None # 只加载中文和英文,加快初始化速度 # 自动检测GPU可用性,如果可用则启用GPU加速 # 临时重定向输出以减少初始化时的噪音 import io from contextlib import redirect_stdout, redirect_stderr fake_stdout = io.StringIO() fake_stderr = io.StringIO() if easyocr is not None: with redirect_stdout(fake_stdout), redirect_stderr(fake_stderr): self.ocr = easyocr.Reader( ["ch_sim", "en"], gpu=False, verbose=False ) self.ocr_type = "easyocr" gpu_status = "CPU模式" print(f"[OCR] 使用 EasyOCR({gpu_status})") else: # 强制抛出异常以触发回退逻辑 raise ImportError("easyocr not available") except Exception as e: # EasyOCR初始化失败,回退到PaddleOCR print(f"[OCR] EasyOCR初始化失败: {e},回退到PaddleOCR") try: if devnull is not None: try: devnull.close() except Exception: pass except Exception: pass try: devnull = open(os.devnull, "w", encoding="utf-8") with contextlib.redirect_stdout( devnull ), contextlib.redirect_stderr(devnull): from paddleocr import PaddleOCR # 使用 PaddleOCR(强制 CPU 模式) use_gpu = False self.ocr = PaddleOCR( lang="ch", ocr_version="PP-OCRv4", use_angle_cls=False, use_gpu=use_gpu, use_doc_orientation_classify=False, use_doc_unwarping=False, use_textline_orientation=False, ) self.ocr_type = "paddleocr" print("[OCR] EasyOCR不可用,使用 PaddleOCR(CPU模式)") print( "[OCR] 提示:安装EasyOCR可提升速度,运行: pip install easyocr" ) except Exception: # 若静默初始化失败,保留 None 并允许后续按需初始化,避免阻塞启动。 try: if devnull is not None: try: devnull.close() except Exception: pass except Exception: pass self.ocr = None self.ocr_type = None finally: try: if devnull is not None: devnull.close() except Exception: pass # 注释掉全局日志禁用,以便查看详细的计时日志 # logging.disable(logging.DEBUG) # logging.disable(logging.WARNING) # 用于短期内记录 get_screenshot 调用来源(用于精确定位高频调用) try: import collections self._screenshot_call_cache = collections.deque(maxlen=1000) except Exception: self._screenshot_call_cache = [] # 日志文件路径(JSON Lines 格式) self._screenshot_log_path = os.path.join( os.path.dirname(__file__), "logs", "screenshot_calls.jsonl" ) @staticmethod def _adb() -> str: adb = str(os.environ.get("ADB_EXECUTABLE", "adb") or "adb") # 始终加引号,避免路径带空格 / 版本混用 if adb.startswith('"') and adb.endswith('"'): return adb return f'"{adb}"' @staticmethod def _adb_path() -> str: """返回可用于 subprocess(list_args) 的 adb 路径(不带外层引号)。""" adb = str(os.environ.get("ADB_EXECUTABLE", "adb") or "adb") if adb.startswith('"') and adb.endswith('"'): adb = adb[1:-1] return adb def _ensure_ocr(self): if self.ocr is not None: return # 默认使用CPU模式(延迟初始化时) # 优先尝试EasyOCR try: # 在Windows上,避免使用stderr重定向,可能会导致"lost sys.stderr"问题 import importlib try: easyocr = importlib.import_module("easyocr") except Exception: easyocr = None # 临时重定向输出以减少初始化时的噪音 import io from contextlib import redirect_stdout, redirect_stderr fake_stdout = io.StringIO() fake_stderr = io.StringIO() if easyocr is not None: with redirect_stdout(fake_stdout), redirect_stderr(fake_stderr): self.ocr = easyocr.Reader( ["ch_sim", "en"], gpu=False, verbose=False ) self.ocr_type = "easyocr" gpu_status = "CPU模式" print(f"[OCR] 延迟初始化:使用 EasyOCR({gpu_status})") else: raise ImportError("easyocr not available") except Exception as e: # EasyOCR失败,回退到PaddleOCR print(f"[OCR] 延迟初始化EasyOCR失败: {e},回退到PaddleOCR") devnull = None try: devnull = open(os.devnull, "w", encoding="utf-8") with contextlib.redirect_stdout(devnull), contextlib.redirect_stderr( devnull ): from paddleocr import PaddleOCR # 使用 PaddleOCR(强制 CPU 模式) use_gpu = False self.ocr = PaddleOCR( lang="ch", ocr_version="PP-OCRv4", use_angle_cls=False, use_gpu=use_gpu, use_doc_orientation_classify=False, use_doc_unwarping=False, use_textline_orientation=False, ) self.ocr_type = "paddleocr" print("[OCR] 延迟初始化:使用 PaddleOCR(CPU模式)") except Exception: self.ocr = None self.ocr_type = None finally: try: if devnull is not None: devnull.close() except Exception: pass def select_device(self): """选择需要连接的设备""" string = subprocess.Popen( f"{self._adb()} devices", shell=True, stdout=subprocess.PIPE ) assert string.stdout is not None totalstring = string.stdout.read() totalstring = totalstring.decode("utf-8") # print(totalstring) pattern = r"(\b(?:[0-9]{1,3}(?:\.[0-9]{1,3}){3}(?::[0-9]+)?|[A-Za-z0-9]{8,})\b)\s*device\b" devicelist = re.findall(pattern, totalstring) # devicelist = re.compile(r'(\w*)\s*device\b').findall(totalstring) devicenum = len(devicelist) if devicenum == 0: print("当前无设备连接电脑,请检查设备连接情况!") return False elif devicenum == 1: print("当前有一台设备连接,编号:%s." % devicelist[0]) return devicelist[0] else: print("当前存在多台设备连接! 输入数字选择对应设备:") dictdevice = {} for i in range(devicenum): string = subprocess.Popen( "%s -s %s shell getprop ro.product.device" % (self._adb(), devicelist[i]), shell=True, stdout=subprocess.PIPE, ) assert string.stdout is not None modestring = string.stdout.read().strip() # 去除掉自动生成的回车 print("%s:%s---%s" % (i + 1, devicelist[i], modestring)) dictdevice[i + 1] = devicelist[i] num = input() num = int(num) while not num in dictdevice.keys(): print("输入不正确,请重新输入:") num = input() num = int(num) return dictdevice[num] def _find_pic_path(self, picname: str, for_reading: bool = True) -> Optional[str]: """查找图片路径,支持新旧位置(向后兼容),并按分类组织 Args: picname: 图片名称(不含扩展名),如 '直播广场', '福袋内容' for_reading: True=读取(支持新旧位置),False=写入(使用新位置+分类目录) Returns: 图片的完整路径,如果找不到且for_reading=True则返回None """ try: import path_config pic_base = path_config.get_pic_dir() except Exception: import path_config pic_base = path_config.get_pic_dir() # 运行时必需文件始终在根目录(不需要移动) runtime_files = {"screenshot", "cut"} if picname in runtime_files: path = os.path.join(pic_base, f"{picname}.png") return path if (os.path.exists(path) or not for_reading) else None # OCR图片文件:按分类组织到子目录 try: import importlib douyin_screenshot_manager = importlib.import_module( "douyin.handlers.douyin_screenshot_manager" ) category = douyin_screenshot_manager.get_category_for_picname(picname) except Exception: category = "others" if for_reading: # 读取:先查分类目录,再查旧位置(向后兼容) # 1. 新位置(分类目录) category_path = os.path.join(pic_base, "ocr", category, f"{picname}.png") if os.path.exists(category_path): return category_path # 2. 旧位置(ocr根目录) old_ocr_path = os.path.join(pic_base, "ocr", f"{picname}.png") if os.path.exists(old_ocr_path): return old_ocr_path # 3. 更旧的位置(pic根目录) old_root_path = os.path.join(pic_base, f"{picname}.png") if os.path.exists(old_root_path): return old_root_path return None else: # 写入:使用分类目录,确保目录存在 category_dir = os.path.join(pic_base, "ocr", category) os.makedirs(category_dir, exist_ok=True) return os.path.join(category_dir, f"{picname}.png") def get_screenshot(self, device_id, path="pic", use_service: bool = False): """截图(支持后台服务模式) 参数: device_id: 设备ID path: 路径(默认"pic") use_service: 是否使用后台截图服务(默认False,强制使用直接截图) 返回: 是否成功获取截图 """ start_ts = time_module.time() # Diagnostic: record pre-call info including caller stack (top frames) try: import inspect, json, threading frames = [] for fr in inspect.stack()[1:9]: frame_info = { "file": os.path.relpath(fr.filename, os.path.dirname(__file__)), "line": fr.lineno, "function": fr.function, } frames.append(frame_info) pre_record = { "ts": datetime.now().isoformat(), "device_id": device_id, "use_service": bool(use_service), "path": path, "phase": "start", "thread": threading.get_ident(), "stack": frames, } # cache in memory try: if hasattr(self, "_screenshot_call_cache"): try: self._screenshot_call_cache.append(pre_record) except Exception: pass except Exception: pass # append JSON line to log file try: log_dir = os.path.join(os.path.dirname(__file__), "logs") os.makedirs(log_dir, exist_ok=True) with open(self._screenshot_log_path, "a", encoding="utf-8") as _lf: _lf.write(json.dumps(pre_record, ensure_ascii=False) + "\n") except Exception: pass except Exception: pass # 如果启用服务模式,使用后台截图服务;若服务不可用或返回 False,则回退到直接截图 if use_service: try: from operations.screenshot_service import ScreenshotService service = ScreenshotService.get_instance() # 诊断:检查服务是否在运行 service_running = getattr(service, "_running", False) if not service_running: logging.debug( "[截图] 后台服务未运行,尝试启动 device=%s", device_id ) try: service.start(device_id, interval=1.5) # 等待一小段时间让服务完成第一次截图 time_module.sleep(0.2) except Exception as start_err: logging.warning( "[截图] 启动后台服务失败: %s,将使用直接截图", start_err ) try: service_result = service.get_screenshot(device_id, force=False) if service_result: dur = (time_module.time() - start_ts) * 1000.0 logging.info( "[截图] 使用后台服务耗时(ms)=%.1f device=%s", dur, device_id ) # 写结束记录 try: import json, threading end_record = { "ts": datetime.now().isoformat(), "device_id": device_id, "use_service": True, "path": path, "phase": "end", "result": True, "duration_ms": dur, "thread": threading.get_ident(), } try: if hasattr(self, "_screenshot_call_cache"): self._screenshot_call_cache.append(end_record) except Exception: pass try: with open(self._screenshot_log_path, "a", encoding="utf-8") as _lf: _lf.write(json.dumps(end_record, ensure_ascii=False) + "\n") except Exception: pass except Exception: pass # do not return here; let final return at function end result = True else: timetag = datetime.now().strftime("%H:%M:%S") logging.info( "[%s] [截图操作] 后台服务未提供有效截图,回退到直接截图", timetag, ) # 不 return,这里继续走直接截图逻辑 except Exception as e: timetag = datetime.now().strftime("%H:%M:%S") logging.warning( "[%s] [截图操作] 调用后台服务异常,回退到直接截图: %s", timetag, e, ) # ensure result exists result = False except ImportError as e: timetag = datetime.now().strftime("%H:%M:%S") logging.warning( "[%s] [截图操作] 无法导入截图服务模块,回退到直接截图: %s", timetag, e, ) result = False except Exception as e: timetag = datetime.now().strftime("%H:%M:%S") logging.warning( "[%s] [截图操作] 服务模式不可用,回退到直接截图: %s", timetag, e ) result = False # 原有的直接截图逻辑(保持向后兼容),测量其耗时 try: # If result already set by service branch, skip direct call if "result" in locals() and locals()["result"] is True: # already successful via service final_result = True else: final_result = self._get_screenshot_direct(device_id, path) except Exception: final_result = False dur = (time_module.time() - start_ts) * 1000.0 logging.info( "[截图] 总耗时(ms)=%.1f device=%s use_service=%s result=%s", dur, device_id, bool(use_service), final_result, ) # 写结束记录(包含持续时长与结果) try: import json, threading end_record = { "ts": datetime.now().isoformat(), "device_id": device_id, "use_service": bool(use_service), "path": path, "phase": "end", "result": bool(final_result), "duration_ms": dur, "thread": threading.get_ident(), } try: if hasattr(self, "_screenshot_call_cache"): self._screenshot_call_cache.append(end_record) except Exception: pass try: with open(self._screenshot_log_path, "a", encoding="utf-8") as _lf: _lf.write(json.dumps(end_record, ensure_ascii=False) + "\n") except Exception: pass except Exception: pass return bool(final_result) def _get_screenshot_direct(self, device_id, path="pic"): """直接截图(不使用服务模式)""" overall_start = time_module.time() # 初始化分阶段耗时占位,确保最终汇总时总有值 cleanup_dur = 0.0 adb_subprocess_dur = 0.0 validate_tmp_dur = 0.0 replace_dur = 0.0 mirror_dur = 0.0 validate_out_dur = 0.0 if path == "pic": try: import path_config out_dir = path_config.get_pic_dir() except Exception: import path_config out_dir = path_config.get_pic_dir() else: out_dir = os.path.join(os.path.dirname(__file__), "target_pic") if not os.path.exists(out_dir): os.makedirs(out_dir) # 统一输出到固定文件名,兼容现有 cut_pic()/后续流程 out_file = os.path.join(out_dir, "screenshot.png") tmp_file = out_file + ".tmp" timetag = datetime.now().strftime("%H:%M:%S") adb = self._adb_path() # 检查环境变量:是否跳过PNG验证(用于性能测试) skip_verify = os.environ.get("SKIP_SCREENSHOT_VERIFY", "0") == "1" def _valid_png(path0: str) -> bool: if skip_verify: # 快速路径:只检查文件存在和大小 try: if not os.path.exists(path0): return False sz = os.path.getsize(path0) return sz >= 1024 except Exception: return False verify_start = time_module.time() try: if not os.path.exists(path0): return False sz = os.path.getsize(path0) if sz < 1024: return False # 额外校验 PNG 头与文件尾(快速检测截断) header_check_start = time_module.time() try: with open(path0, "rb") as _f: header = _f.read(4) if header != b"\x89PNG": return False try: _f.seek(-256, 2) except Exception: _f.seek(0) tail = _f.read(256) if b"IEND" not in tail: return False except Exception: return False header_check_dur = (time_module.time() - header_check_start) * 1000.0 if header_check_dur > 10.0: # 只记录超过10ms的 logging.debug( "[截图] PNG头尾校验耗时(ms)=%.1f path=%s", header_check_dur, path0, ) # 优化:只在必要时使用 PIL 验证(PIL verify 可能很慢) # 对于临时文件,快速验证已足够;对于最终文件,可以跳过完整验证以提高速度 # 如果文件头尾都正确,大概率是有效的PNG文件 # PIL verify 可以移到后台或异步进行,不阻塞主流程 # 这里先跳过完整验证,只做快速检查 # pil_verify_start = time_module.time() # try: # from PIL import Image # with Image.open(path0) as im: # im.verify() # except Exception: # return False # pil_verify_dur = (time_module.time() - pil_verify_start) * 1000.0 # if pil_verify_dur > 50.0: # 只记录超过50ms的 # logging.debug("[截图] PIL verify耗时(ms)=%.1f path=%s", pil_verify_dur, path0) verify_total_dur = (time_module.time() - verify_start) * 1000.0 if verify_total_dur > 100.0: # 只记录超过100ms的 logging.info( "[截图] _valid_png总耗时(ms)=%.1f path=%s", verify_total_dur, path0, ) return True except Exception: return False # 优先走 exec-out:只需 1 次 adb 调用;不在手机端落盘,减少 I/O 与卡顿 # 为避免被并发读取到"半写入文件",写入 tmp 后再原子替换。 for attempt in range(2): try: cleanup_start = time_module.time() try: if os.path.exists(tmp_file): os.remove(tmp_file) except Exception: pass cleanup_dur = (time_module.time() - cleanup_start) * 1000.0 if cleanup_dur > 10.0: logging.debug("[截图] 清理临时文件耗时(ms)=%.1f", cleanup_dur) # 从设备获取截图(exec-out),写入临时文件 # 优化:使用 Popen + 流式读取,边接收边写入,提高性能 adb_start = time_module.time() try: process = subprocess.Popen( [adb, "-s", device_id, "exec-out", "screencap", "-p"], stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, bufsize=0, # 无缓冲,立即传输 ) # 使用更大的缓冲区(256KB)提高写入效率 if process.stdout is None: raise RuntimeError("ADB process stdout is None") with open(tmp_file, "wb", buffering=262144) as f: while True: chunk = process.stdout.read(262144) # 256KB chunks if not chunk: break f.write(chunk) # 等待进程完成 return_code = process.wait(timeout=10) if return_code != 0: raise subprocess.CalledProcessError( return_code, [adb, "-s", device_id, "exec-out", "screencap", "-p"], ) adb_subprocess_dur = (time_module.time() - adb_start) * 1000.0 logging.info( "[截图] ADB exec-out subprocess耗时(ms)=%.1f device=%s attempt=%d", adb_subprocess_dur, device_id, attempt + 1, ) # 优化:移除 fsync(现代文件系统会自动同步,减少不必要的磁盘I/O) # 文件写入已经完成,操作系统会在适当时机同步到磁盘 # 这样可以减少 10-50ms 的等待时间 except subprocess.TimeoutExpired: process.kill() raise validate_tmp_start = time_module.time() is_valid = _valid_png(tmp_file) validate_tmp_dur = (time_module.time() - validate_tmp_start) * 1000.0 if validate_tmp_dur > 50.0: logging.info( "[截图] 临时文件验证耗时(ms)=%.1f result=%s", validate_tmp_dur, is_valid, ) if is_valid: # 使用安全的文件替换函数 replace_start = time_module.time() replace_success = _safe_replace_file(tmp_file, out_file) replace_dur = (time_module.time() - replace_start) * 1000.0 if replace_dur > 50.0: logging.info( "[截图] 文件替换耗时(ms)=%.1f result=%s", replace_dur, replace_success, ) if replace_success: mirror_start = time_module.time() _mirror_screenshot_to_external(out_file) mirror_dur = (time_module.time() - mirror_start) * 1000.0 if mirror_dur > 50.0: logging.info( "[截图] 镜像到外部路径耗时(ms)=%.1f", mirror_dur ) # 优化:快速验证目标文件(只检查存在和大小,不进行完整PNG验证) # 完整验证可以在读取时进行,不阻塞截图流程 validate_out_start = time_module.time() try: # 快速检查:文件存在且大小合理 if os.path.exists(out_file): file_size = os.path.getsize(out_file) if file_size >= 1024: # 至少1KB # 快速检查PNG头(不进行完整验证) try: with open(out_file, "rb") as f: header = f.read(4) if header == b"\x89PNG": validate_out_dur = ( time_module.time() - validate_out_start ) * 1000.0 if validate_out_dur > 50.0: logging.info( "[截图] 目标文件快速验证耗时(ms)=%.1f", validate_out_dur, ) overall_dur = ( time_module.time() - overall_start ) * 1000.0 # 记录分阶段耗时汇总,便于排查瓶颈(JSON-like) logging.info( "[截图汇总] device=%s total_ms=%.1f cleanup_ms=%.1f adb_execout_ms=%.1f validate_tmp_ms=%.1f replace_ms=%.1f mirror_ms=%.1f validate_out_ms=%.1f", device_id, overall_dur, cleanup_dur, adb_subprocess_dur, validate_tmp_dur, replace_dur, mirror_dur, validate_out_dur, ) logging.info( "[截图] _get_screenshot_direct总耗时(ms)=%.1f device=%s (exec-out成功)", overall_dur, device_id, ) print( f"[{timetag}] [截图] ✓ 获取屏幕截图成功" ) return True except Exception: pass # 如果快速验证失败,记录诊断信息 try: st = os.stat(out_file) print( f"[截图诊断] 目标文件验证失败: size={st.st_size}, mtime={st.st_mtime}" ) except Exception: pass except Exception: pass else: # 如果替换失败,但临时文件有效,记录诊断并重试回退方案 try: if os.path.exists(tmp_file): st = os.stat(tmp_file) print( f"[截图诊断] 临时文件存在但替换失败: tmp={tmp_file}, size={st.st_size}" ) except Exception: pass # 继续尝试回退方案 else: # 临时文件本身无效,记录诊断信息 try: if os.path.exists(tmp_file): st = os.stat(tmp_file) print( f"[截图诊断] 临时文件无效: path={tmp_file}, size={st.st_size}" ) except Exception: pass # 继续尝试回退方案 except subprocess.TimeoutExpired: # 超时异常,记录并继续重试 print("[截图] exec-out 超时,尝试重试或回退方案") try: if os.path.exists(tmp_file): os.remove(tmp_file) except Exception: pass except Exception as e: # 继续回退/重试 print(f"[截图] exec-out 异常:{e}") try: if os.path.exists(tmp_file): os.remove(tmp_file) except Exception: pass # 回退方案:老式 shell screencap + pull(兼容极少数不支持 exec-out 的环境) fallback_start = time_module.time() try: shell_screencap_start = time_module.time() subprocess.run( [ adb, "-s", device_id, "shell", "screencap", "-p", "/sdcard/DCIM/screenshot.png", ], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, check=True, timeout=10, # 添加超时 ) shell_screencap_dur = (time_module.time() - shell_screencap_start) * 1000.0 logging.info( "[截图] 回退方案-shell screencap耗时(ms)=%.1f device=%s", shell_screencap_dur, device_id, ) pull_start = time_module.time() subprocess.run( [adb, "-s", device_id, "pull", "/sdcard/DCIM/screenshot.png", tmp_file], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, check=True, timeout=10, # 添加超时 ) pull_dur = (time_module.time() - pull_start) * 1000.0 logging.info( "[截图] 回退方案-adb pull耗时(ms)=%.1f device=%s", pull_dur, device_id ) fallback_validate_start = time_module.time() if _valid_png(tmp_file): fallback_validate_dur = ( time_module.time() - fallback_validate_start ) * 1000.0 if fallback_validate_dur > 50.0: logging.info( "[截图] 回退方案-临时文件验证耗时(ms)=%.1f", fallback_validate_dur, ) # 使用安全的文件替换函数 fallback_replace_start = time_module.time() if _safe_replace_file(tmp_file, out_file): fallback_replace_dur = ( time_module.time() - fallback_replace_start ) * 1000.0 if fallback_replace_dur > 50.0: logging.info( "[截图] 回退方案-文件替换耗时(ms)=%.1f", fallback_replace_dur, ) fallback_mirror_start = time_module.time() _mirror_screenshot_to_external(out_file) fallback_mirror_dur = ( time_module.time() - fallback_mirror_start ) * 1000.0 if fallback_mirror_dur > 50.0: logging.info( "[截图] 回退方案-镜像耗时(ms)=%.1f", fallback_mirror_dur ) try: if _valid_png(out_file): overall_dur = (time_module.time() - overall_start) * 1000.0 fallback_total_dur = ( time_module.time() - fallback_start ) * 1000.0 logging.info( "[截图] _get_screenshot_direct总耗时(ms)=%.1f device=%s (回退方案成功, 回退部分耗时=%.1fms)", overall_dur, device_id, fallback_total_dur, ) print("{} 获取屏幕截图".format(timetag)) return True except Exception: pass else: # 如果替换失败,尝试最后一次:直接复制内容 try: import shutil # 如果目标文件存在且被占用,尝试读取临时文件内容并写入 if os.path.exists(tmp_file): # 尝试直接覆盖写入(如果文件被占用,这会失败,但至少尝试了) try: with open(tmp_file, "rb") as src, open( out_file, "wb" ) as dst: dst.write(src.read()) _mirror_screenshot_to_external(out_file) try: if _valid_png(out_file): print( f"[{timetag}] [截图] ✓ 获取屏幕截图成功(通过直接写入)" ) return True except Exception: pass except (OSError, PermissionError) as e: # 如果还是失败,使用优化后的文件替换函数 replace_success = _safe_replace_file(tmp_file, out_file) if replace_success: _mirror_screenshot_to_external(out_file) try: if _valid_png(out_file): print( f"[{timetag}] [截图] ✓ 获取屏幕截图成功(通过优化替换)" ) return True except Exception: pass else: # 最后兜底:返回True,让调用者知道临时文件存在 print( f"[截图] 文件替换失败({e}),但截图已保存到临时文件 {tmp_file}" ) return True except Exception: print(f"[截图] 文件替换失败,但截图已保存到临时文件 {tmp_file}") return True # 文件异常则继续进入更深回退 except Exception: # 再次回退:尝试用 screenshot1.png 覆盖 screenshot.png try: subprocess.run( [ adb, "-s", device_id, "shell", "screencap", "-p", "/sdcard/DCIM/screenshot1.png", ], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, check=True, ) subprocess.run( [ adb, "-s", device_id, "shell", "mv", "/sdcard/DCIM/screenshot1.png", "/sdcard/DCIM/screenshot.png", ], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, check=True, ) return self.get_screenshot(device_id, path) except Exception: return False def get_current_hour(self): """获取当前的时间小时数""" time_hour = datetime.now().strftime("%H") print("当前时间{}点".format(time_hour)) return int(time_hour) def click_confirm(self, device_id): """点击确认键""" os.system("%s -s %s shell input keyevent 66" % (self._adb(), device_id)) print("点击确认键") def click_back(self, device_id): """点击返回键""" os.system("%s -s %s shell input keyevent 4" % (self._adb(), device_id)) print("点击返回键") # 电量检测方法已删除 def get_device_resolution(self, device_id): """获取设备分辨率""" resolution_info = subprocess.Popen( "%s -s %s shell wm size" % (self._adb(), device_id), shell=True, stdout=subprocess.PIPE, ) assert resolution_info.stdout is not None resolution_info_string = resolution_info.stdout.read() resolution_info_string = bytes.decode(resolution_info_string) match = re.search(r"Physical size: (\d+)x(\d+)", resolution_info_string) if match: width = int(match.group(1)) height = int(match.group(2)) print("设备分辨率为:{}*{}".format(width, height)) return width, height else: raise ValueError("Could not find resolution in adb output") def save_reward_pic(self, device_id): try: import path_config out_dir = path_config.get_pic_dir() except Exception: import path_config out_dir = path_config.get_pic_dir() if not os.path.exists(out_dir): os.makedirs(out_dir) timepic = datetime.now().strftime("%Y-%m-%d-%H-%M-%S") out_file = os.path.join(out_dir, f"{timepic}.png") adb = self._adb_path() # 同样优先使用 exec-out,减少手机端 I/O try: with open(out_file, "wb") as f: subprocess.run( [adb, "-s", device_id, "exec-out", "screencap", "-p"], stdout=f, stderr=subprocess.DEVNULL, check=True, ) print("中奖了,点击领奖,保存中奖图片{}.png".format(timepic)) return except Exception: pass # 回退:shell screencap + pull try: subprocess.run( [ adb, "-s", device_id, "shell", "screencap", "-p", "/sdcard/DCIM/screenshot.png", ], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, check=True, ) subprocess.run( [adb, "-s", device_id, "pull", "/sdcard/DCIM/screenshot.png", out_file], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, check=True, ) subprocess.run( [adb, "-s", device_id, "shell", "rm", "/sdcard/DCIM/screenshot.png"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, check=False, ) print("中奖了,点击领奖,保存中奖图片{}.png".format(timepic)) except Exception: # 保存失败不打断主流程 print("保存中奖截图失败") def cut_pic(self, left_up=(0, 63), right_down=(1080, 1620), target="", name=""): """裁剪图片,获取需要的区域小图片方便识别 添加了错误处理: - 检查文件是否存在 - 验证图片完整性 - 重试机制(如果文件损坏) - 异常处理避免程序崩溃 """ max_retries = 3 retry_delay = 0.1 for attempt in range(max_retries): try: if target == "" or target == False: path = os.path.dirname(__file__) + "/pic" pic1_path = path + "/screenshot.png" # 检查文件是否存在 if not os.path.exists(pic1_path): if attempt < max_retries - 1: time_module.sleep(retry_delay * (attempt + 1)) continue raise FileNotFoundError(f"截图文件不存在: {pic1_path}") # 验证文件大小(至少应该有一些内容) if os.path.getsize(pic1_path) < 100: if attempt < max_retries - 1: time_module.sleep(retry_delay * (attempt + 1)) continue raise ValueError(f"截图文件太小,可能损坏: {pic1_path}") # 尝试打开并验证图片 try: pic = Image.open(pic1_path) # 验证图片完整性(通过访问尺寸属性) _ = pic.size # 确保图片已加载 pic.load() except (SyntaxError, IOError, OSError) as e: # 图片损坏,重试或返回False if attempt < max_retries - 1: print( f"[警告] 图片文件可能损坏,重试中 ({attempt + 1}/{max_retries}): {e}" ) time_module.sleep(retry_delay * (attempt + 1)) continue # 最后一次尝试失败,让外层异常处理 raise if name == "": cut_pic_path = path + "/cut.png" else: # 使用新的路径查找方法(写入到新位置ocr/) cut_pic_path = self._find_pic_path(name, for_reading=False) # 验证裁剪坐标有效性 width, height = pic.size left, top = left_up right, bottom = right_down if ( left < 0 or top < 0 or right > width or bottom > height or left >= right or top >= bottom ): raise ValueError( f"裁剪坐标无效: left_up={left_up}, right_down={right_down}, image_size={pic.size}" ) cropped_img = pic.crop((left, top, right, bottom)) assert cut_pic_path is not None try: _ensure_parent_dir(cut_pic_path) cropped_img.save(cut_pic_path) except Exception: logging.exception("保存裁剪图片失败: %s", cut_pic_path) # 存储坐标信息供analyse_pic_word使用 if name: try: self._ocr_coords_cache[name] = (left, top, right, bottom) except Exception: pass # 优化:只在明确启用调试模式时保存调试图像(减少I/O操作) # 默认不保存,避免每次cut_pic都进行文件I/O操作(可节省50-200ms) try: # 检查是否启用调试图像保存 save_debug = ( os.environ.get("SAVE_CUT_PIC_DEBUG_IMAGES", "0") == "1" ) if not save_debug: # 检查全局配置 try: save_debug = ( os.environ.get("SAVE_DEBUG_IMAGES", "0") == "1" ) except Exception: pass if save_debug: from douyin.handlers import douyin_screenshot_manager douyin_screenshot_manager.save_latest_attempt( cropped_img, operation_type="cut_pic" ) # 保存调试图像(完整截图+区域标注) pic_rgb = pic.convert("RGB") debug_path = douyin_screenshot_manager.save_debug_image( pic_rgb, operation_type="cut_pic", picname=name if name else "cut", step="before_ocr", coords=(left, top, right, bottom), extra_info={ "source": "screenshot.png", "size": f"{right-left}x{bottom-top}", }, ) if debug_path: try: logging.info( "[调试图像] cut_pic保存: %s", debug_path ) except Exception: pass except Exception: pass return True else: path_target = os.path.dirname(__file__) + "/pic/" + target pic1_path = path_target + "/screenshot.png" # 检查文件是否存在 if not os.path.exists(pic1_path): if attempt < max_retries - 1: time_module.sleep(retry_delay * (attempt + 1)) continue raise FileNotFoundError(f"截图文件不存在: {pic1_path}") # 验证文件大小 if os.path.getsize(pic1_path) < 100: if attempt < max_retries - 1: time_module.sleep(retry_delay * (attempt + 1)) continue raise ValueError(f"截图文件太小,可能损坏: {pic1_path}") # 尝试打开并验证图片 try: pic = Image.open(pic1_path) _ = pic.size pic.load() except (SyntaxError, IOError, OSError) as e: # 图片损坏,重试或返回False if attempt < max_retries - 1: print( f"[警告] 图片文件可能损坏,重试中 ({attempt + 1}/{max_retries}): {e}" ) time_module.sleep(retry_delay * (attempt + 1)) continue # 最后一次尝试失败,让外层异常处理 raise if name == "": cut_pic_path = path_target + "/cut.png" else: cut_pic_path = path_target + "/" + name + ".png" # 验证裁剪坐标有效性 width, height = pic.size left, top = left_up right, bottom = right_down if ( left < 0 or top < 0 or right > width or bottom > height or left >= right or top >= bottom ): error_msg = f"裁剪坐标无效: left_up={left_up}, right_down={right_down}, image_size={pic.size}" if attempt == max_retries - 1: print(f"[错误] {error_msg}") return False continue assert cut_pic_path is not None cropped_img = pic.crop((left, top, right, bottom)) try: _ensure_parent_dir(cut_pic_path) cropped_img.save(cut_pic_path) except Exception: logging.exception("保存裁剪图片失败: %s", cut_pic_path) return False # 存储坐标信息供analyse_pic_word使用 if name: try: self._ocr_coords_cache[name] = (left, top, right, bottom) except Exception: pass # 保存调试图像(完整截图+区域标注) try: from douyin.handlers import douyin_screenshot_manager pic_rgb = pic.convert("RGB") debug_path = douyin_screenshot_manager.save_debug_image( pic_rgb, operation_type="cut_pic", picname=name if name else "cut", step="before_ocr", coords=(left, top, right, bottom), extra_info={ "source": target + "/screenshot.png", "size": f"{right-left}x{bottom-top}", }, ) if debug_path: try: logging.info("[调试图像] cut_pic保存: %s", debug_path) except Exception: pass except Exception: pass return True except (SyntaxError, IOError, OSError, FileNotFoundError, ValueError) as e: # 裁剪或读取图片时发生错误,最后一次尝试失败则记录并返回 False if attempt == max_retries - 1: error_msg = f"[错误] 裁剪图片失败 (尝试 {max_retries} 次): {e}" print(error_msg) print(f" 源文件: {locals().get('pic1_path', 'N/A')}") print(f" 目标文件: {locals().get('cut_pic_path', 'N/A')}") return False # 否则继续重试 continue def analyse_pic_word(self, picname, change_color=0, pic_path=None): """对指定图片运行 OCR 并返回拼接后的文本结果。 参数: picname: 用于分类和调试的图片名(如"福袋内容") change_color: 颜色处理模式,0=保留彩色,1/2=二值化等 pic_path: 可选的图片路径,未提供时通过 `_find_pic_path` 查找 返回: 识别到的文本(字符串),识别失败返回空字符串 """ if not pic_path: try: pic_path = self._find_pic_path(picname) except Exception: pic_path = None if not pic_path or not os.path.exists(pic_path): return "" # 优化:减少图像验证步骤,直接加载(节省50-100ms) img = None try: # 直接打开图像,不进行verify(节省时间) # verify会重新读取文件,对于已经存在的文件是多余的 img = Image.open(pic_path) img.load() # 立即加载图像数据,避免延迟加载 except (IOError, OSError, UnidentifiedImageError): return "" if img is None: return "" if change_color in (1, 2): img = img.convert("L") if change_color == 1: img = img.point(lambda x: 0 if int(x) < 128 else 255) else: img = img.point(lambda x: 0 if int(x) < 180 else 255) img = img.convert("RGB") else: img = img.convert("RGB") w, h = img.size if max(w, h) < 200: try: scale = max(2, 300 // max(w, h, 1)) img = img.resize((w * scale, h * scale), _RESAMPLE.BICUBIC) w, h = img.size except Exception: pass from PIL import ImageEnhance # 优化:简化图像预处理,减少不必要的操作 # 对于小图像(<300px),进行锐化和对比度增强;对于大图像,直接使用原图 img1 = img if max(w, h) < 300: # 小图像需要增强以提高识别率 try: img_sharpened = img.filter( ImageFilter.UnsharpMask(radius=1, percent=150, threshold=3) ) enhancer = ImageEnhance.Contrast(img_sharpened) img1 = enhancer.enhance(1.25) except Exception: try: img1 = img.filter(ImageFilter.SHARPEN) except Exception: img1 = img # Fallback to original image # 大图像直接使用,不进行额外处理(节省时间) # 优化:只在明确启用调试模式时保存调试图像(减少I/O操作) # 默认不保存,避免每次OCR都进行文件I/O操作(可节省50-200ms) try: # 检查是否启用调试图像保存 save_debug = os.environ.get("SAVE_OCR_DEBUG_IMAGES", "0") == "1" if not save_debug: # 检查analyser配置 try: save_debug = bool(getattr(self, "save_ocr_debug_images", False)) except Exception: pass if save_debug: from douyin.handlers import douyin_screenshot_manager import path_config # 尝试获取OCR区域坐标 ocr_coords = None try: ocr_coords = self._ocr_coords_cache.get(picname) except Exception: pass # 加载完整截图 full_screenshot = None try: screenshot_path = path_config.get_screenshot_path() if os.path.exists(screenshot_path): full_screenshot = Image.open(screenshot_path) full_screenshot.load() full_screenshot = full_screenshot.convert("RGB") except Exception: pass # 如果成功加载完整截图,使用完整截图;否则使用预处理图像 debug_image = full_screenshot if full_screenshot is not None else img1 debug_path = douyin_screenshot_manager.save_debug_image( debug_image, operation_type="ocr", picname=picname, step="preprocessed", coords=ocr_coords, extra_info={ "mode": change_color, "size": f"{w}x{h}", "source": pic_path if pic_path else picname, "has_full_screenshot": full_screenshot is not None, "has_coords": ocr_coords is not None, }, ) if debug_path: try: logging.info("[调试图像] OCR预处理保存: %s", debug_path) except Exception: pass except Exception: pass img_np = np.array(img1) # 用于收集置信度信息 ocr_confidence_info = None # 如果 OCR 未初始化(例如 EasyOCR/PaddleOCR 均不可用),安全降级为不识别 try: ocr_obj = getattr(self, "ocr", None) ocr_type = getattr(self, "ocr_type", None) except Exception: ocr_obj = None ocr_type = None if ocr_obj is None: try: logging.warning("OCR 未初始化,跳过识别:picname=%s", picname) except Exception: pass return "" try: if ocr_type == "easyocr": result = ocr_obj.readtext(img_np) extract_result = self.extract_ocr_content( result, return_confidence=True ) if isinstance(extract_result, tuple): text, ocr_confidence_info = extract_result else: text = extract_result else: result = ocr_obj.ocr(img_np) if result == [None]: return "" extract_result = self.extract_ocr_content( result, return_confidence=True ) if isinstance(extract_result, tuple): text, ocr_confidence_info = extract_result else: text = extract_result except Exception as e: try: logging.warning("OCR 调用失败,返回空结果:%s", e) except Exception: pass return "" # Initialize variables to avoid unbound variable issues img1 = img # img1 is used later, initialize from img img2 = None text2 = None # 禁用二次OCR识别(用户要求:没有GPU,不需要二次OCR) # 如果第一次识别失败,直接返回空结果,避免耗时3-4秒的二次识别 # if (not text) and max(w, h) <= 1200: # try: # img2 = img.resize((w * 2, h * 2), _RESAMPLE.LANCZOS) # try: # from PIL import ImageEnhance # # img2_sharpened = img2.filter( # ImageFilter.UnsharpMask(radius=1.5, percent=180, threshold=3) # ) # enhancer = ImageEnhance.Contrast(img2_sharpened) # img2 = enhancer.enhance(1.2) # except Exception: # img2 = img2.filter(ImageFilter.SHARPEN) # # if getattr(self, "ocr_type", None) == "easyocr": # result2 = self.ocr.readtext(np.array(img2)) # extract_result2 = self.extract_ocr_content(result2, return_confidence=True) # if isinstance(extract_result2, tuple): # text2, ocr_confidence_info = extract_result2 # else: # text2 = extract_result2 # if text2: # text = text2 # result = result2 # else: # result2 = self.ocr.ocr(np.array(img2)) # if result2 != [None]: # extract_result2 = self.extract_ocr_content(result2, return_confidence=True) # if isinstance(extract_result2, tuple): # text2, ocr_confidence_info = extract_result2 # else: # text2 = extract_result2 # if text2: # text = text2 # result = result2 # except Exception: # pass if picname in ("福袋内容", "福袋倒计时") and not text: try: if isinstance(result, list) and result and isinstance(result[0], dict): print( f"OCR调试({picname}): result[0] keys={list(result[0].keys())}" ) else: print(f"OCR调试({picname}): result type={type(result)}") except Exception: pass try: from douyin.handlers import douyin_screenshot_manager # Ensure img1 is defined before use if "img1" not in locals(): img1 = img img_for_attempt = ( img2 if (img2 is not None and text2 and text2.strip()) else img1 ) douyin_screenshot_manager.save_latest_attempt( img_for_attempt, operation_type="ocr" ) # 保存OCR结果后的调试图像(带OCR文本结果和置信度)- 使用完整截图 ocr_result_preview = ( (text or text2 or "").strip()[:50] if (text or text2) else "" ) extra_info_dict = { "mode": change_color, "text_len": len(text or text2 or ""), "text_preview": ocr_result_preview, "used_img2": bool(img2 is not None and text2 and text2.strip()), } # 添加置信度信息 if ocr_confidence_info: extra_info_dict["avg_confidence"] = round( ocr_confidence_info.get("avg_confidence", 0.0), 4 ) extra_info_dict["min_confidence"] = round( ocr_confidence_info.get("min_confidence", 0.0), 4 ) extra_info_dict["max_confidence"] = round( ocr_confidence_info.get("max_confidence", 0.0), 4 ) extra_info_dict["confidence_items"] = ocr_confidence_info.get( "item_count", 0 ) # 尝试获取OCR区域坐标 ocr_coords = None try: ocr_coords = self._ocr_coords_cache.get(picname) except Exception: pass # 加载完整截图 full_screenshot = None try: import path_config screenshot_path = path_config.get_screenshot_path() if os.path.exists(screenshot_path): full_screenshot = Image.open(screenshot_path) full_screenshot.load() full_screenshot = full_screenshot.convert("RGB") except Exception: pass # 如果成功加载完整截图,使用完整截图;否则使用预处理图像 debug_image = ( full_screenshot if full_screenshot is not None else img_for_attempt ) extra_info_dict["has_full_screenshot"] = full_screenshot is not None extra_info_dict["has_coords"] = ocr_coords is not None debug_path = douyin_screenshot_manager.save_debug_image( debug_image, operation_type="ocr", picname=picname, step="after_ocr", coords=ocr_coords, extra_info=extra_info_dict, ) # 记录OCR置信度到日志 if ocr_confidence_info and ocr_confidence_info.get("item_count", 0) > 0: try: logging.info( "[OCR置信度] %s: 平均=%.4f, 最小=%.4f, 最大=%.4f, 项目数=%d", picname, ocr_confidence_info.get("avg_confidence", 0.0), ocr_confidence_info.get("min_confidence", 0.0), ocr_confidence_info.get("max_confidence", 0.0), int(ocr_confidence_info.get("item_count", 0) or 0), ) except Exception: pass if debug_path: try: logging.info( "[调试图像] OCR结果保存: %s, 文本: %s", debug_path, ocr_result_preview, ) except Exception: pass except Exception: pass if text and text.strip() and picname: try: try: from douyin.handlers import douyin_screenshot_manager category = douyin_screenshot_manager.get_category_for_picname( picname ) except Exception: category = "others" base_dir = os.path.dirname(__file__) pic_base = os.path.join(base_dir, "pic") category_dir = os.path.join(pic_base, "ocr", category) os.makedirs(category_dir, exist_ok=True) success_path = os.path.join(category_dir, f"{picname}_success.png") try: # Ensure img1 is defined before use if "img1" not in locals(): img1 = img if img2 is not None and text2 and text2.strip(): img_to_save = img2 else: img_to_save = img1 try: from douyin.handlers import douyin_screenshot_manager douyin_screenshot_manager.save_last_success( img_to_save, operation_type="ocr" ) except Exception: pass img_to_save.save(success_path) except Exception: try: if img is not None: img.save(success_path) except Exception: pass except Exception: pass return text def analyse_pic_digits(self, picname, change_color=1, pic_path=None): """轻量数字OCR识别函数,专门用于识别数字(0-9)。 相比analyse_pic_word,这个函数: 1. 使用二值化预处理(change_color=1),更适合数字识别 2. 只提取数字字符,忽略其他字符 3. 使用更轻量的OCR配置(如果支持) 参数: picname: 用于分类和调试的图片名(如"福袋价值") change_color: 颜色处理模式,默认1=二值化(更适合数字) pic_path: 可选的图片路径,未提供时通过 `_find_pic_path` 查找 返回: 识别到的数字文本(字符串),识别失败返回空字符串 """ if not pic_path: try: pic_path = self._find_pic_path(picname) except Exception: pic_path = None if not pic_path or not os.path.exists(pic_path): return "" # 优化:减少图像验证步骤,直接加载(节省50-100ms) img = None try: # 直接打开图像,不进行verify(节省时间) img = Image.open(pic_path) img.load() # 立即加载图像数据,避免延迟加载 except (IOError, OSError, UnidentifiedImageError): return "" if img is None: return "" # 数字识别更适合使用二值化处理 img = img.convert("L") if change_color == 1: img = img.point(lambda x: 0 if int(x) < 128 else 255) elif change_color == 2: img = img.point(lambda x: 0 if int(x) < 180 else 255) img = img.convert("RGB") w, h = img.size # 数字识别需要更高的分辨率 if max(w, h) < 200: try: scale = max(2, 400 // max(w, h, 1)) img = img.resize((w * scale, h * scale), _RESAMPLE.BICUBIC) w, h = img.size except Exception: pass # 优化:简化数字OCR预处理,只对小图像进行增强 from PIL import ImageEnhance img1 = img # 对于已经放大过的图像(>=200px),直接使用,不进行额外处理 if max(w, h) < 200: # 小图像需要增强以提高识别率 try: img_sharpened = img.filter( ImageFilter.UnsharpMask(radius=1, percent=200, threshold=3) ) enhancer = ImageEnhance.Contrast(img_sharpened) img1 = enhancer.enhance(1.3) # 更高的对比度 except Exception: try: img1 = img.filter(ImageFilter.SHARPEN) except Exception: img1 = img # Fallback to original image # 大图像直接使用,不进行额外处理(节省时间) # 保存预处理后的调试图像(OCR前)- 使用完整截图 try: from douyin.handlers import douyin_screenshot_manager import path_config # 尝试获取OCR区域坐标 ocr_coords = None try: ocr_coords = self._ocr_coords_cache.get(picname) except Exception: pass # 加载完整截图 full_screenshot = None try: screenshot_path = path_config.get_screenshot_path() if os.path.exists(screenshot_path): full_screenshot = Image.open(screenshot_path) full_screenshot.load() full_screenshot = full_screenshot.convert("RGB") except Exception: pass # 如果成功加载完整截图,使用完整截图;否则使用预处理图像 debug_image = full_screenshot if full_screenshot is not None else img1 debug_path = douyin_screenshot_manager.save_debug_image( debug_image, operation_type="ocr", picname=picname, step="preprocessed", coords=ocr_coords, extra_info={ "mode": change_color, "size": f"{w}x{h}", "source": pic_path if pic_path else picname, "has_full_screenshot": full_screenshot is not None, "has_coords": ocr_coords is not None, "type": "digits", }, ) if debug_path: try: logging.info("[调试图像] OCR数字预处理保存: %s", debug_path) except Exception: pass except Exception: pass img_np = np.array(img1) # 使用OCR识别(与analyse_pic_word相同的OCR引擎) ocr_confidence_info = None if getattr(self, "ocr_type", None) == "easyocr": result = self.ocr.readtext(img_np) extract_result = self.extract_ocr_content(result, return_confidence=True) if isinstance(extract_result, tuple): text, ocr_confidence_info = extract_result else: text = extract_result else: result = self.ocr.ocr(img_np) if result == [None]: return "" extract_result = self.extract_ocr_content(result, return_confidence=True) if isinstance(extract_result, tuple): text, ocr_confidence_info = extract_result else: text = extract_result # 保存OCR结果后的调试图像(带OCR文本结果和置信度)- 使用完整截图 try: from douyin.handlers import douyin_screenshot_manager import path_config # 尝试获取OCR区域坐标 ocr_coords = None try: ocr_coords = self._ocr_coords_cache.get(picname) except Exception: pass # 加载完整截图 full_screenshot = None try: screenshot_path = path_config.get_screenshot_path() if os.path.exists(screenshot_path): full_screenshot = Image.open(screenshot_path) full_screenshot.load() full_screenshot = full_screenshot.convert("RGB") except Exception: pass # 如果成功加载完整截图,使用完整截图;否则使用预处理图像 debug_image = full_screenshot if full_screenshot is not None else img1 # 准备额外信息 # 保留小数点(包括全角/中文标点归一化为英文点)以便调试时看到如 24.9 这样的值 digits_result = "" if text: t_norm = ( str(text).replace("。", ".").replace(",", ".").replace(".", ".") ) digits_result = "".join( ch for ch in t_norm if ch.isdigit() or ch == "." ) extra_info_dict = { "mode": change_color, "text_len": len(text or ""), "digits": digits_result, "type": "digits", } # 添加置信度信息 if ocr_confidence_info: extra_info_dict["avg_confidence"] = round( ocr_confidence_info.get("avg_confidence", 0.0), 4 ) extra_info_dict["min_confidence"] = round( ocr_confidence_info.get("min_confidence", 0.0), 4 ) extra_info_dict["max_confidence"] = round( ocr_confidence_info.get("max_confidence", 0.0), 4 ) extra_info_dict["confidence_items"] = ocr_confidence_info.get( "item_count", 0 ) extra_info_dict["has_full_screenshot"] = full_screenshot is not None extra_info_dict["has_coords"] = ocr_coords is not None debug_path = douyin_screenshot_manager.save_debug_image( debug_image, operation_type="ocr", picname=picname, step="after_ocr", coords=ocr_coords, extra_info=extra_info_dict, ) # 记录OCR置信度到日志 if ocr_confidence_info and ocr_confidence_info.get("item_count", 0) > 0: try: logging.info( "[OCR置信度] %s: 平均=%.4f, 最小=%.4f, 最大=%.4f, 项目数=%d", picname, ocr_confidence_info.get("avg_confidence", 0.0), ocr_confidence_info.get("min_confidence", 0.0), ocr_confidence_info.get("max_confidence", 0.0), int(ocr_confidence_info.get("item_count", 0) or 0), ) except Exception: pass if debug_path: try: logging.info( "[调试图像] OCR数字结果保存: %s, 数字: %s", debug_path, digits_result, ) except Exception: pass except Exception: pass # 只提取数字字符,并进行智能过滤 if text: # 同时准备两种数字抽取结果: # 1) digits_only: 仅数字(用于倒计时/位置等只需整数的场景) # 2) digits: 允许小数点(用于金额/带小数的数值),并规范化常见全角标点 text_norm = ( str(text).replace("。", ".").replace(",", ".").replace(".", ".") ) digits_only = "".join(ch for ch in text_norm if ch.isdigit()) digits = "".join(ch for ch in text_norm if ch.isdigit() or ch == ".") # 智能过滤:如果识别到的数字太多(可能是识别到了文件名或坐标), # 尝试提取合理的数字段 if len(digits_only) > 10: # 对于价值:通常不超过6位数字(999999元以内) # 对于倒计时:通常是1-2位数字 # 尝试提取最后几个数字(通常是实际值) # 或者提取最短的连续数字段 # 提取所有连续的数字段(允许小数点的段落匹配) digit_segments = re.findall(r"\d+(?:\.\d+)?", text_norm) if digit_segments: # 对于价值,选择最大的合理数字段(不超过6位数字,不计小数点) # 对于倒计时,选择1-2位的数字段 # 这里先返回所有数字,让调用方处理 # 但优先选择较短的段(1-6位),忽略小数点长度影响 reasonable_segments = [ s for s in digit_segments if 1 <= len(s.replace(".", "")) <= 6 ] if reasonable_segments: # 如果有多段,优先选择较短的(倒计时)或较长的(价值) # 这里返回最长的合理段(通常是价值) return max(reasonable_segments, key=len) else: # 如果没有合理段,返回最短的段 return ( min(digit_segments, key=len) if digit_segments else digits[:6] ) return digits return "" def extract_ocr_content(self, content=None, return_confidence=False): """对OCR识别到的内容进行取值和拼接,变成完整的一段内容 参数: content: OCR识别结果 return_confidence: 是否返回置信度信息(默认False,保持向后兼容) 返回: 如果return_confidence=False: 返回文本字符串 如果return_confidence=True: 返回(text, confidence_info)元组,其中confidence_info是置信度信息字典 """ ocr_result = content or [] # 用于收集置信度信息 confidence_scores = [] confidence_info = { "avg_confidence": 0.0, "min_confidence": 1.0, "max_confidence": 0.0, "item_count": 0, } def _extract_text_and_confidence_from_item(item): """提取文本和置信度""" text = "" confidence = None # 兼容EasyOCR格式:[(bbox, text, confidence), ...] -> (bbox, text, confidence) if isinstance(item, (list, tuple)) and len(item) >= 2: # EasyOCR格式:item = (bbox, text, confidence),text在索引1,confidence在索引2 if len(item) >= 3: confidence = item[2] if isinstance(item[1], str): text = item[1] return text, confidence # 兼容旧格式:[box, (text, score)] second = item[1] if isinstance(second, (list, tuple)) and len(second) >= 1: if isinstance(second[0], str): return second[0] if isinstance(second, dict): for key in ("text", "rec_text", "transcription"): val = second.get(key) if isinstance(val, str) and val: return val if isinstance(second, str): return second # 兼容新格式:dict / 对象 if isinstance(item, dict): # 常见:rec_texts 为字符串列表 for list_key in ("rec_texts", "texts", "text_list"): vals = item.get(list_key) if isinstance(vals, list) and vals: vals = [v for v in vals if isinstance(v, str) and v] if vals: return "".join(vals) for key in ("text", "rec_text", "transcription"): val = item.get(key) if isinstance(val, str) and val: return val for attr in ("text", "rec_text", "transcription"): val = getattr(item, attr, None) if isinstance(val, str) and val: return val # 对象属性里也可能是列表 for list_attr in ("rec_texts", "texts", "text_list"): vals = getattr(item, list_attr, None) if isinstance(vals, list) and vals: vals = [v for v in vals if isinstance(v, str) and v] if vals: return "".join(vals) return "" # PaddleOCR 可能返回 [[...]] 或 [...] items = ocr_result if ( isinstance(ocr_result, list) and len(ocr_result) == 1 and isinstance(ocr_result[0], list) ): items = ocr_result[0] def _extract_xy_from_item(item): """尽力从 OCR item 中提取用于排序的 (y, x)。无法提取则返回 None。""" try: if isinstance(item, (list, tuple)) and len(item) >= 1: box = item[0] # EasyOCR格式:bbox = [[x1,y1], [x2,y2], [x3,y3], [x4,y4]] # PaddleOCR格式:box 为 4 点坐标列表 if isinstance(box, (list, tuple)) and box: xs = [] ys = [] for p in box: if isinstance(p, (list, tuple)) and len(p) >= 2: xs.append(float(p[0])) ys.append(float(p[1])) if xs and ys: return (min(ys), min(xs)) except Exception: return None return None def _extract_text_from_item(item): """提取文本(原始逻辑,保持向后兼容)""" # 兼容EasyOCR格式:[(bbox, text, confidence), ...] -> (bbox, text, confidence) if isinstance(item, (list, tuple)) and len(item) >= 2: # EasyOCR格式:item = (bbox, text, confidence),text在索引1 if isinstance(item[1], str): return item[1] # 兼容旧格式:[box, (text, score)] second = item[1] if isinstance(second, (list, tuple)) and len(second) >= 1: if isinstance(second[0], str): return second[0] if isinstance(second, dict): for key in ("text", "rec_text", "transcription"): val = second.get(key) if isinstance(val, str) and val: return val if isinstance(second, str): return second # 兼容新格式:dict / 对象 if isinstance(item, dict): # 常见:rec_texts 为字符串列表 for list_key in ("rec_texts", "texts", "text_list"): vals = item.get(list_key) if isinstance(vals, list) and vals: vals = [v for v in vals if isinstance(v, str) and v] if vals: return "".join(vals) for key in ("text", "rec_text", "transcription"): val = item.get(key) if isinstance(val, str) and val: return val for attr in ("text", "rec_text", "transcription"): val = getattr(item, attr, None) if isinstance(val, str) and val: return val # 对象属性里也可能是列表 for list_attr in ("rec_texts", "texts", "text_list"): vals = getattr(item, list_attr, None) if isinstance(vals, list) and vals: vals = [v for v in vals if isinstance(v, str) and v] if vals: return "".join(vals) return "" extracted_items = [] if isinstance(items, list): for idx, item in enumerate(items): text = _extract_text_from_item(item) if not text: continue xy = _extract_xy_from_item(item) if xy is None: # 无坐标信息:保持原相对顺序 extracted_items.append((1e18, float(idx), text)) else: y, x = xy extracted_items.append((float(y), float(x), text)) # 按从上到下、从左到右排序;解决同一行多个框的乱序拼接(例如倒计时两段数字) extracted_items.sort(key=lambda t: (t[0], t[1])) # 如果不需要返回置信度,直接返回文本(保持向后兼容) if not return_confidence: return "".join([t[2] for t in extracted_items]) # 如果需要返回置信度,提取置信度信息 if return_confidence: for item in items: confidence = None # EasyOCR格式:[(bbox, text, confidence), ...] if isinstance(item, (list, tuple)) and len(item) >= 3: confidence = item[2] # PaddleOCR格式:[box, (text, score)] elif isinstance(item, (list, tuple)) and len(item) >= 2: second = item[1] if isinstance(second, (list, tuple)) and len(second) >= 2: confidence = second[1] elif isinstance(second, dict): confidence = second.get("confidence") or second.get("score") if confidence is not None: try: conf_float = float(confidence) confidence_scores.append(conf_float) confidence_info["min_confidence"] = min( confidence_info["min_confidence"], conf_float ) confidence_info["max_confidence"] = max( confidence_info["max_confidence"], conf_float ) except (ValueError, TypeError): pass if confidence_scores: confidence_info["avg_confidence"] = sum(confidence_scores) / len( confidence_scores ) confidence_info["item_count"] = len(confidence_scores) else: confidence_info["min_confidence"] = 0.0 return "".join([t[2] for t in extracted_items]), confidence_info return "".join([t[2] for t in extracted_items]) def delay(self, seconds: float = 1): time_module.sleep(seconds) def random_delay(self, min_seconds=1, max_seconds=100): """等待随机的时长""" random_wait_seconds = random.randint(min_seconds, max_seconds) print("随机等待{}秒.".format(random_wait_seconds)) self.delay(random_wait_seconds) def check_countdown(self, last_time=""): """对倒计时时间进行转化,返回 (剩余秒数, 预计开奖时间戳)。 兼容 OCR 常见格式与误识别: - 11:37 / 11:37 / 11:37后开奖 - 1分23秒 / 23秒 - 1137 / 934 / 31 - 字符混淆:O->0, l/I->1, S->5 等 """ def _norm(s: str) -> str: t = str(s or "") t = re.sub(r"\s+", "", t) t = t.replace(":", ":") # 常见 OCR 混淆纠正 trans = str.maketrans( { "O": "0", "o": "0", "Q": "0", "D": "0", "I": "1", "l": "1", "|": "1", "!": "1", "S": "5", "s": "5", "B": "8", "G": "6", "Z": "2", } ) t = t.translate(trans) return t def _parse_seconds(text: str) -> Optional[int]: t = _norm(text) if not t: return None # 1) mm:ss(允许 ss 为 1 位) m = re.search(r"(\d{1,2})[:](\d{1,2})", t) if m: mm = int(m.group(1)) ss = int(m.group(2)) # 若秒数 >= 60,优先尝试交换(OCR 容易把分秒顺序颠倒) if ss >= 60 and mm < 60: mm, ss = ss, mm # 兜底:仍不合法则做进位 if ss >= 60: mm += ss // 60 ss = ss % 60 return max(0, mm * 60 + ss) # 2) 中文:x分y秒 / y秒 m = re.search(r"(\d{1,2})分(\d{1,2})秒", t) if m: mm = int(m.group(1)) ss = int(m.group(2)) if ss >= 60: mm += ss // 60 ss = ss % 60 return max(0, mm * 60 + ss) m = re.search(r"(\d{1,3})秒", t) if m: return max(0, int(m.group(1))) # 3) 纯数字:1137(=11:37) / 934(=9:34) / 31(=31秒) digits = "".join(ch for ch in t if ch.isdigit()) if not digits: return None if len(digits) == 4: a = int(digits[:2]) b = int(digits[2:]) # 两种解释:a:b 或 b:a。 # 默认按“前两位=分、后两位=秒”,只有在明显异常(>15分钟)或秒不合法时才交换。 if b < 60: total1 = a * 60 + b else: total1 = None if a < 60: total2 = b * 60 + a else: total2 = None if total1 is not None and total1 <= 900: return total1 if total2 is not None and total2 <= 900: return total2 # 若一个明显异常而另一个更合理(<=900),交换 if total1 is not None and total2 is not None: if total1 > 900 and total2 <= 900: return total2 if total2 > 900 and total1 <= 900: return total1 # 兜底:谁能解析就用谁;都不行则做进位 if total1 is not None: return total1 if total2 is not None: return total2 mm, ss = a, b mm += ss // 60 ss = ss % 60 return mm * 60 + ss if len(digits) == 3: mm = int(digits[0]) ss = int(digits[1:]) if ss >= 60: # 例如 159 可能是 15:09(OCR 丢 0) mm2 = int(digits[:2]) ss2 = int(digits[2]) if ss2 < 60: return mm2 * 60 + ss2 mm += ss // 60 ss = ss % 60 return mm * 60 + ss if len(digits) == 2: return int(digits) # 直接当秒(更符合“临近开奖”场景) if len(digits) == 1: return int(digits) # 太长:取最后4位(OCR 常把别的数字也识别进来) tail = digits[-4:] a = int(tail[:2]) b = int(tail[2:]) if b >= 60 and a < 60: a, b = b, a if b >= 60: a += b // 60 b = b % 60 return a * 60 + b try: total_seconds = _parse_seconds(last_time) if total_seconds is None: print("时间格式异常") return False print("剩余总秒数:", total_seconds) # 超过 15 分钟基本是误识别:按 14分50秒处理,避免卡死等待 if total_seconds > 900: total_seconds = 890 now = datetime.now() print("当前时间:", now.strftime("%Y-%m-%d %H:%M:%S")) future_time = now + timedelta(seconds=int(total_seconds)) future_timestamp = future_time.timestamp() future_datetime = datetime.fromtimestamp(future_timestamp) formatted_future_time = future_datetime.strftime("%Y-%m-%d %H:%M:%S") print("预计开奖时间:", formatted_future_time) return int(total_seconds), future_timestamp except Exception: print("输入的字符串不是有效的时间格式") return False def swipe( self, device_id, left_up_x=0, left_up_y=0, right_down_x=1080, right_down_y=1500, steps=300, ): """划动屏幕 参数说明: - left_up_x, left_up_y: 滑动起始位置(手指按下位置) - right_down_x, right_down_y: 滑动结束位置(手指抬起位置) - steps: 滑动持续时间(毫秒),数值越大滑动越慢,默认300ms(已优化,比之前的200ms更快) 执行命令:adb shell input swipe """ adb_cmd = self._adb() # 注意:adb input swipe 的参数顺序是:start_x start_y end_x end_y duration_ms # 这里 left_up_x/y 是起始位置,right_down_x/y 是结束位置 cmd = f"{adb_cmd} -s {device_id} shell input swipe {left_up_x} {left_up_y} {right_down_x} {right_down_y} {steps}" try: # 使用 subprocess 执行命令,捕获错误输出 result = subprocess.run( cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=5.0, text=True, check=False, ) # 检查是否有权限错误 stderr_text = result.stderr if result.stderr else "" has_permission_error = result.returncode != 0 and ( "SecurityException" in stderr_text or "INJECT_EVENTS" in stderr_text or "Injecting to another application" in stderr_text ) if has_permission_error: # 尝试使用 uiautomator(如果可用,某些设备可能支持) try: self._swipe_with_uiautomator( device_id, left_up_x, left_up_y, right_down_x, right_down_y ) return except Exception: pass # 记录错误但不抛出异常,避免中断主流程 error_msg = stderr_text.strip() if stderr_text else "权限不足" print(f"[警告] 滑动操作失败: {error_msg}") print("[提示] 设备不允许 ADB 输入注入,可能的解决方案:") print( " 1. 在设备开发者选项中启用'USB调试(安全设置)'或'允许模拟点击'" ) print(" 2. 使用已 root 的设备") print(" 3. 某些设备需要在设置中手动授权 ADB 输入权限") # 不抛出异常,让程序继续运行(可能只是滑动失败,其他功能仍可用) except subprocess.TimeoutExpired: print("[警告] 滑动操作超时") except Exception as e: # 降级到原来的 os.system 方法 try: os.system(cmd) except Exception: print(f"[警告] 滑动操作失败: {type(e).__name__}: {e}") def _swipe_with_sendevent(self, device_id, x1, y1, x2, y2, steps): """使用 sendevent 方法滑动(需要 root 权限)""" # 获取触摸设备路径 result = subprocess.run( f"{self._adb()} -s {device_id} shell getevent -p | grep -A 5 ABS_MT", shell=True, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, text=True, check=False, ) if "event" not in result.stdout.lower(): raise FileNotFoundError("无法找到触摸设备") # 简化实现:直接使用 input 命令的替代参数 # 注意:sendevent 需要 root 权限,这里只是尝试 raise PermissionError("sendevent 需要 root 权限,暂不支持") def _swipe_with_uiautomator(self, device_id, x1, y1, x2, y2): """使用 uiautomator 滑动(如果可用) 注意:uiautomator 在某些设备上可能也需要权限,但可以尝试作为备选方案。 """ # 使用 uiautomator swipe 命令 # 注意:uiautomator 命令格式可能因 Android 版本而异 # Android 5.0+: uiautomator swipe [steps] cmd = ( f"{self._adb()} -s {device_id} shell uiautomator swipe {x1} {y1} {x2} {y2}" ) result = subprocess.run( cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=5.0, check=False, ) if result.returncode != 0: raise RuntimeError( f"uiautomator swipe 失败: {result.stderr.decode('utf-8', errors='ignore')}" ) def _swipe_with_sendevent_improved(self, device_id, x1, y1, x2, y2, steps=50): """使用 sendevent 方法滑动(需要 root 权限) 这是更底层的触摸事件模拟方式,通过直接向 /dev/input/eventX 写入事件。 优点:绕过部分权限检查 缺点:需要 root 权限,实现复杂,不同设备的事件格式可能不同 """ # 获取触摸设备路径(通常是 /dev/input/eventX) result = subprocess.run( f"{self._adb()} -s {device_id} shell getevent -p 2>/dev/null | grep -A 10 ABS_MT", shell=True, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, text=True, timeout=3.0, check=False, ) # 解析设备路径(简化版,实际需要更复杂的解析) # 这里只是示例,完整实现需要: # 1. 解析 getevent -p 输出找到触摸设备 # 2. 计算触摸事件的坐标范围(ABS_MT_POSITION_X/Y 的 min/max) # 3. 将屏幕坐标转换为设备坐标 # 4. 发送 DOWN -> MOVE -> UP 事件序列 if "event" not in result.stdout.lower(): raise FileNotFoundError("无法找到触摸设备(需要 root 权限)") # 注意:完整的 sendevent 实现非常复杂,需要: # - 设备特定的坐标转换 # - 正确的事件序列(SYN_REPORT 等) # - 不同 Android 版本的事件格式可能不同 raise PermissionError("sendevent 需要 root 权限且实现复杂,暂不支持完整实现") def click(self, device_id, x=500, y=500): """点击坐标位置""" os.system("{} -s {} shell input tap {} {}".format(self._adb(), device_id, x, y)) def long_press( self, device_id: str, x: int, y: int, duration_ms: int = 900 ) -> None: """长按某个坐标(通过 swipe x y x y duration 模拟)。""" d = int(duration_ms or 900) if d < 300: d = 300 if d > 2000: d = 2000 os.system( f"{self._adb()} -s {device_id} shell input swipe {int(x)} {int(y)} {int(x)} {int(y)} {d}" ) def _run_adb_shell( self, device_id: str, shell_cmd: str, timeout_seconds: float = 3.0 ) -> bool: """运行 adb shell 命令,成功返回 True。""" try: r = subprocess.run( f"{self._adb()} -s {device_id} shell {shell_cmd}", shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, timeout=timeout_seconds, check=False, ) return r.returncode == 0 except Exception: return False def _run_adb_shell_capture( self, device_id: str, shell_cmd: str, timeout_seconds: float = 3.0 ) -> Tuple[bool, str]: """运行 adb shell 命令并捕获输出,返回 (ok, stdout)。""" try: r = subprocess.run( f"{self._adb()} -s {device_id} shell {shell_cmd}", shell=True, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, timeout=timeout_seconds, text=True, encoding="utf-8", errors="ignore", check=False, ) return (r.returncode == 0), (r.stdout or "") except Exception: return False, "" def get_top_package(self, device_id: str) -> str: """尝试返回当前处于前台的包名(如 com.ss.android.ugc.aweme)。 失败返回空字符串。实现采用 dumpsys 输出的多种字段进行解析,兼容不同 Android 版本。 """ try: ok, out = self._run_adb_shell_capture( device_id, "dumpsys window windows", timeout_seconds=4.0 ) if ok and out: for ln in (out or "").splitlines(): if ( "mCurrentFocus" in ln or "mFocusedApp" in ln or "mFocusedWindow" in ln ): m = re.search(r"([A-Za-z0-9_\.]+)\/", ln) if m: return m.group(1) # 兜底尝试:activity 栈里的 resumed activity ok2, out2 = self._run_adb_shell_capture( device_id, "dumpsys activity activities", timeout_seconds=4.0 ) if ok2 and out2: for ln in (out2 or "").splitlines(): if "mResumedActivity" in ln or "ResumedActivity" in ln: m = re.search(r"([A-Za-z0-9_\.]+)\/", ln) if m: return m.group(1) except Exception: pass return "" @staticmethod # type: ignore[misc] def _escape_shell_double_quotes(text: str) -> str: return str(text).replace("\\", "\\\\").replace('"', '\\"') def set_clipboard_text(self, device_id: str, text: str) -> bool: """设置设备剪贴板文本(尽量支持中文)。 优先:Android 10+ 的 `cmd clipboard set`。 兜底:Clipper(若设备装了 clipper app)。 """ t = str(text or "").strip() if not t: return False # 1) cmd clipboard set "..." esc = self._escape_shell_double_quotes(t) if self._run_adb_shell( device_id, f'cmd clipboard set "{esc}"', timeout_seconds=3.0 ): return True # 2) Clipper broadcast: am broadcast -a clipper.set -e text "..." if self._run_adb_shell( device_id, f'am broadcast -a clipper.set -e text "{esc}"', timeout_seconds=3.0, ): return True return False def paste_from_clipboard(self, device_id: str) -> bool: """尝试触发“粘贴”操作(需要输入框已获得焦点)。""" # KEYCODE_PASTE=279(部分机型/系统可能不支持,但无副作用) return self._run_adb_shell(device_id, "input keyevent 279", timeout_seconds=2.0) def input_text(self, device_id: str, text: str) -> bool: """使用 adb input text 输入(对中文可能不稳定,仅兜底)。""" t = str(text or "") if not t: return False # 空格用 %s,避免被 shell 分词 t = t.replace(" ", "%s") # 仅做最小转义,避免破坏中文 t = ( t.replace("&", "\\&") .replace("<", "\\<") .replace(">", "\\>") .replace("|", "\\|") ) return self._run_adb_shell( device_id, f'input text "{self._escape_shell_double_quotes(t)}"', timeout_seconds=5.0, ) def paste_text(self, device_id: str, text: str) -> bool: """优先走剪贴板粘贴,其次兜底 input text。""" if self.set_clipboard_text(device_id, text): self.paste_from_clipboard(device_id) return True return self.input_text(device_id, text) # ===== ADBKeyboard (可选) ===== def get_current_ime(self, device_id: str) -> str: ok, out = self._run_adb_shell_capture( device_id, "settings get secure default_input_method", timeout_seconds=2.0 ) if not ok: return "" return (out or "").strip() def list_imes(self, device_id: str) -> list[str]: # 部分 ROM 会限制 `ime` shell 命令(报 WRITE_SECURE_SETTINGS),这里优先用 settings 查询。 ok, out = self._run_adb_shell_capture( device_id, "settings get secure enabled_input_methods", timeout_seconds=2.5 ) if ok and out: raw = (out or "").strip() # 形如:a/.A:b/.B:c/.C parts = [p.strip() for p in raw.split(":") if p.strip()] if parts: return parts ok2, out2 = self._run_adb_shell_capture( device_id, "ime list -s", timeout_seconds=2.5 ) if not ok2: # 最后兜底:至少把当前默认输入法返回,避免上层无法判断 cur = self.get_current_ime(device_id) return [cur] if cur else [] lines = [ln.strip() for ln in (out2 or "").splitlines() if ln.strip()] return lines @staticmethod # type: ignore[misc] def _pick_adbkeyboard_ime(imes: list[str]) -> str: # 兼容不同包名/组件名写法,优先匹配常见的 AdbIME for cand in imes or []: s = str(cand).strip() ls = s.lower() if "adbkeyboard" in ls and "adbime" in ls: return s for cand in imes or []: s = str(cand).strip() ls = s.lower() if "adbkeyboard" in ls or "adbime" in ls: return s return "" def ensure_adbkeyboard_ime( self, device_id: str, preferred_ime: str = "com.android.adbkeyboard/.AdbIME" ) -> Tuple[bool, str, str]: """确保 ADBKeyboard 输入法处于激活状态。 返回 (ok, prev_ime, adb_ime)。ok=False 表示设备未安装/未启用 ADBKeyboard。 """ prev = self.get_current_ime(device_id) # 如果当前已经是 ADBKeyboard,就不用再尝试切换(很多 ROM 禁止 ime set) if "adbkeyboard" in str(prev).lower() or "adbime" in str(prev).lower(): return True, prev, prev imes = self.list_imes(device_id) adb_ime = "" pref = str(preferred_ime or "").strip() if pref and pref in imes: adb_ime = pref else: adb_ime = self._pick_adbkeyboard_ime(imes) if not adb_ime: return False, prev, "" if prev != adb_ime: if not self.try_set_ime(device_id, adb_ime): return False, prev, adb_ime return True, prev, adb_ime def restore_ime(self, device_id: str, prev_ime: str) -> bool: ime = str(prev_ime or "").strip() if not ime: return False return self.try_set_ime(device_id, ime) def try_set_ime(self, device_id: str, ime_id: str) -> bool: ime = str(ime_id or "").strip() if not ime: return False # 1) 优先走 ime(某些 ROM 会失败) self._run_adb_shell(device_id, f"ime enable {ime}", timeout_seconds=2.5) if self._run_adb_shell(device_id, f"ime set {ime}", timeout_seconds=2.5): return True # 2) 退化:尝试 settings put(通常也会被 ROM 限制,但试一下无害) if self._run_adb_shell( device_id, f"settings put secure default_input_method {shlex.quote(ime)}", timeout_seconds=2.5, ): cur = self.get_current_ime(device_id) return cur == ime return False def _adbkeyboard_broadcast(self, device_id: str, action: str, msg: str) -> bool: act = str(action or "").strip() if not act: return False esc = self._escape_shell_double_quotes(str(msg or "")) # 这里不依赖 broadcast 的返回文本,只要命令成功执行就认为 ok return self._run_adb_shell( device_id, f'am broadcast -a {act} --es msg "{esc}"', timeout_seconds=3.0 ) def _adbkeyboard_broadcast_int( self, device_id: str, action: str, key: str, value: int ) -> bool: act = str(action or "").strip() k = str(key or "").strip() if not act or not k: return False try: v = int(value) except Exception: return False return self._run_adb_shell( device_id, f"am broadcast -a {act} --ei {k} {v}", timeout_seconds=3.0 ) def keyevent(self, device_id: str, keycode: int) -> bool: try: code = int(keycode) except Exception: return False return self._run_adb_shell( device_id, f"input keyevent {code}", timeout_seconds=2.0 ) def adbkeyboard_editor_action(self, device_id: str, action_code: int = 4) -> bool: """使用 ADBKeyBoard 触发 editor action。 常用:IME_ACTION_SEND=4, IME_ACTION_DONE=6。 仅当当前输入法是 ADBKeyBoard 时更可靠。 """ return self._adbkeyboard_broadcast_int( device_id, "ADB_EDITOR_CODE", "code", int(action_code) ) def confirm_from_keyboard(self, device_id: str) -> bool: """在输入框中用“键盘确认/发送”提交。 优先:ADBKeyBoard editor action SEND(4) 兜底:KEYCODE_ENTER(66) """ cur = "" try: cur = self.get_current_ime(device_id) except Exception: cur = "" if "adbkeyboard" in str(cur).lower() or "adbime" in str(cur).lower(): if self.adbkeyboard_editor_action(device_id, 4): return True return self.keyevent(device_id, 66) def input_text_adbkeyboard( self, device_id: str, text: str, ime_id: str = "com.android.adbkeyboard/.AdbIME", restore_prev: bool = True, ) -> bool: """使用 ADBKeyboard 输入文本(更适合中文)。 要求:手机已安装并启用 ADBKeyboard。若不可用返回 False。 """ t = str(text or "") if not t: return False ok, prev, adb_ime = self.ensure_adbkeyboard_ime(device_id, preferred_ime=ime_id) if not ok: return False success = False try: # 优先 b64(兼容中文/特殊字符),其次纯文本 b64 = base64.b64encode(t.encode("utf-8")).decode("ascii") if self._adbkeyboard_broadcast(device_id, "ADB_INPUT_B64", b64): success = True elif self._adbkeyboard_broadcast(device_id, "ADB_INPUT_TEXT", t): success = True # 给 IME 一点时间把文本真正“打进”输入框 time_module.sleep(0.35) return success finally: # 尝试恢复原输入法,避免影响手动使用 if restore_prev and prev and prev != adb_ime: # 再稍等一下,避免过早切回导致输入丢失 time_module.sleep(0.25) self.restore_ime(device_id, prev) def paste_text_at(self, device_id: str, x: int, y: int, text: str) -> bool: """最简单的“粘贴到输入框”方法。 流程:点击输入框获取焦点 -> 设置剪贴板 -> 发送 KEYCODE_PASTE(279)。 若剪贴板不可用则退化为 `adb shell input text`(中文可能不稳定)。 """ t = str(text or "").strip() if not t: return False # 先确保输入框获得焦点 try: self.click(device_id, int(x), int(y)) time_module.sleep(0.2) # 有些输入框第一次点不进,补一次轻点 self.click(device_id, int(x), int(y)) time_module.sleep(0.15) except Exception: pass # 0) 优先:ADBKeyboard(如果已安装/可用) # 说明:这一步不依赖剪贴板/粘贴菜单,通常对中文更稳定 try: if self.input_text_adbkeyboard(device_id, t, restore_prev=False): return True except Exception: pass # 1) 优先:剪贴板 + 粘贴键 if self.set_clipboard_text(device_id, t): # 给系统一点时间刷新剪贴板 time_module.sleep(0.15) if self.paste_from_clipboard(device_id): return True # 2) 兜底:adb input text(中文可能不稳定) return self.input_text(device_id, t) def _uiautomator_dump(self, device_id: str, local_path: str) -> bool: """拉取当前界面 UI 树到本地 xml(失败返回 False)。""" try: remote = "/sdcard/window_dump.xml" subprocess.run( f"{self._adb()} -s {device_id} shell uiautomator dump {remote}", shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, timeout=2, check=False, ) subprocess.run( f'{self._adb()} -s {device_id} pull {remote} "{local_path}"', shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, timeout=2, check=False, ) return os.path.exists(local_path) except subprocess.TimeoutExpired: return False except Exception: return False @staticmethod # type: ignore[misc] def _parse_bounds(bounds: str): m = re.match(r"\[(\d+),(\d+)\]\[(\d+),(\d+)\]", str(bounds or "")) if not m: return None x1, y1, x2, y2 = map(int, m.groups()) return x1, y1, x2, y2 def tap_by_text( self, device_id: str, text: str, contains: bool = True, timeout_seconds: float = 2.0, ) -> bool: """通过 uiautomator dump 按 text/content-desc 点击(命中即点并返回 True)。 适用于:\"推荐\"、\"直播\"、\"点击进入直播间\" 等。 """ if not text: return False try: import path_config local_path = path_config.get_window_dump_path() except Exception: import path_config local_path = path_config.get_window_dump_path() start = time_module.time() while True: if self._uiautomator_dump(device_id, local_path): try: tree = ET.parse(local_path) root = tree.getroot() except Exception: root = None if root is not None: target = str(text) for node in root.iter(): t = node.attrib.get("text", "") d = node.attrib.get("content-desc", "") hit = False if contains: if (target in t) or (target in d): hit = True else: if (target == t) or (target == d): hit = True if not hit: continue b = node.attrib.get("bounds", "") parsed = self._parse_bounds(b) if not parsed: continue x1, y1, x2, y2 = parsed cx = int((x1 + x2) / 2) cy = int((y1 + y2) / 2) self.click(device_id, cx, cy) return True if time_module.time() - start >= timeout_seconds: return False time_module.sleep(0.2) def tap_close_icon( self, device_id: str, screen_width: int = 1080, timeout_seconds: float = 2.0 ) -> bool: """尝试点击直播间顶部的关闭(X)按钮。 逻辑:从 uiautomator dump 里找顶部(较小 y)的可点击小图标,优先命中 content-desc/text 含“关闭/退出/返回”。 如果没有文字,就在顶部区域按“最像关闭”的位置进行启发式选择。 """ try: import path_config local_path = path_config.get_window_dump_path() except Exception: import path_config local_path = path_config.get_window_dump_path() start = time_module.time() def _score(node_attrib: dict, bounds_tuple) -> int: x1, y1, x2, y2 = bounds_tuple w = x2 - x1 h = y2 - y1 if y2 > 420: return -9999 if w < 50 or h < 50 or w > 260 or h > 260: return -9999 t = (node_attrib.get("text", "") or "").strip() d = (node_attrib.get("content-desc", "") or "").strip() s = t + " " + d score = 0 # 明确关闭/退出优先 for kw in ("关闭", "退出", "返回", "close", "Close"): if kw in s: score += 200 # 排除一些常见非关闭按钮 for bad in ("分享", "更多", "菜单", "礼物", "榜", "关注", "评论"): if bad in s: score -= 120 # 顶部更靠上更像 score += max(0, 120 - y1) # 更接近正方形的小图标更像 score -= abs(w - h) # 默认更偏右上角(如果你的关闭在左边,也能被“更靠上/更像图标”拉回来) cx = (x1 + x2) / 2 if screen_width > 0: score += int((cx / screen_width) * 40) return score while True: if self._uiautomator_dump(device_id, local_path): try: root = ET.parse(local_path).getroot() except Exception: root = None best = None best_score = -999999 if root is not None: for node in root.iter(): attrib = getattr(node, "attrib", {}) or {} # clickable 属性可能是 'true'/'false' if str(attrib.get("clickable", "")).lower() != "true": continue if str(attrib.get("enabled", "true")).lower() == "false": continue b = attrib.get("bounds", "") parsed = self._parse_bounds(b) if not parsed: continue sc = _score(attrib, parsed) if sc > best_score: best_score = sc best = parsed if best and best_score > -1000: x1, y1, x2, y2 = best cx = int((x1 + x2) / 2) cy = int((y1 + y2) / 2) self.click(device_id, cx, cy) return True if time_module.time() - start >= timeout_seconds: return False time_module.sleep(0.2)