From ff8de2c9b84a938357be0f77014394cb83ca4326 Mon Sep 17 00:00:00 2001 From: Codex Date: Wed, 20 May 2026 22:25:35 +0800 Subject: [PATCH 01/10] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D7=E4=B8=AACritic?= =?UTF-8?q?al=E5=B9=B6=E5=8F=91=E5=AE=89=E5=85=A8=E5=92=8C=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=E5=A4=84=E7=90=86=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. OCR 单例无锁保护 - 添加 _ocr_lock 和 _ocr_coords_lock 2. ADB 文本广播命令注入 - 使用 shlex.quote() 转义 3. 截图文件 Race Condition - 添加 PNG 完整性验证 4. JSON 持久化无锁 - 添加文件级锁字典 5. 双检查锁不完整 - 修复 _ensure_debug_save_worker_started 6. Daemon 线程无 Graceful Shutdown - 添加 shutdown event 和 atexit 7. 关键路径异常处理 - 添加 logging.debug 替代 pass Co-Authored-By: Claude Opus 4.5 --- Underlying_Operations.py | 178 +++++++++------ douyin/core/douyin_flow_control.py | 9 +- douyin/handlers/douyin_screenshot_manager.py | 40 +++- douyin/reservation/douyin_reservation.py | 222 ++++++++++--------- douyin/stage/douyin_stage_handler.py | 2 +- run_cli.py | 31 +-- 6 files changed, 283 insertions(+), 199 deletions(-) diff --git a/Underlying_Operations.py b/Underlying_Operations.py index 615473e..a52c9dd 100644 --- a/Underlying_Operations.py +++ b/Underlying_Operations.py @@ -1,4 +1,5 @@ import subprocess +import threading from datetime import datetime, timedelta import os import sys @@ -48,11 +49,26 @@ _EXTERNAL_AXF_SCREENSHOT = ( ) +def _verify_png_integrity(path: str) -> bool: + """验证 PNG 文件头尾完整性,防止读取半写文件""" + try: + with open(path, 'rb') as f: + header = f.read(8) + if len(header) < 8 or header[:4] != b'\x89PNG': + return False + f.seek(-12, 2) # IEND chunk 至少 12 字节 + tail = f.read(12) + return b'IEND' in tail + except Exception: + return False + + def _safe_replace_file(tmp_file: str, out_file: str, max_retries: int = 2) -> bool: """安全地替换文件,处理Windows文件锁定问题。 根本优化:优先使用直接复制而不是替换,避免文件锁定问题。 如果文件被占用,直接复制内容而不是等待释放。 + 对于 PNG 文件,会先验证完整性再替换。 Args: tmp_file: 临时文件路径 @@ -67,6 +83,12 @@ def _safe_replace_file(tmp_file: str, out_file: str, max_retries: int = 2) -> bo if not os.path.exists(tmp_file): return False + # 对于 PNG 文件,先验证完整性 + if tmp_file.lower().endswith('.png'): + if not _verify_png_integrity(tmp_file): + logging.debug("PNG 文件不完整,跳过替换: %s", tmp_file) + return False + # 根本优化:优先尝试原子替换(最快),如果失败则直接复制(避免等待) try: # 方法1: 直接使用 os.replace (原子操作,最快) @@ -205,6 +227,9 @@ class underlying_operations: 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]] = {} + # 线程安全锁 + self._ocr_lock = threading.RLock() # 保护 OCR 初始化 + self._ocr_coords_lock = threading.Lock() # 保护坐标缓存 # 默认使用CPU模式(删除GPU检测) self._gpu_available = False @@ -338,69 +363,73 @@ class underlying_operations: if self.ocr is not None: return - # 默认使用CPU模式(延迟初始化时) + with self._ocr_lock: + if self.ocr is not None: + return - # 优先尝试EasyOCR - try: - # 在Windows上,避免使用stderr重定向,可能会导致"lost sys.stderr"问题 - import importlib + # 默认使用CPU模式(延迟初始化时) + # 优先尝试EasyOCR try: - easyocr = importlib.import_module("easyocr") - except Exception: - easyocr = None + # 在Windows上,避免使用stderr重定向,可能会导致"lost sys.stderr"问题 + import importlib - # 临时重定向输出以减少初始化时的噪音 - 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() + easyocr = importlib.import_module("easyocr") except Exception: - pass + 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): """选择需要连接的设备""" @@ -1396,7 +1425,8 @@ class underlying_operations: # 存储坐标信息供analyse_pic_word使用 if name: try: - self._ocr_coords_cache[name] = (left, top, right, bottom) + with self._ocr_coords_lock: + self._ocr_coords_cache[name] = (left, top, right, bottom) except Exception: pass @@ -1517,7 +1547,8 @@ class underlying_operations: # 存储坐标信息供analyse_pic_word使用 if name: try: - self._ocr_coords_cache[name] = (left, top, right, bottom) + with self._ocr_coords_lock: + self._ocr_coords_cache[name] = (left, top, right, bottom) except Exception: pass @@ -1652,7 +1683,8 @@ class underlying_operations: # 尝试获取OCR区域坐标 ocr_coords = None try: - ocr_coords = self._ocr_coords_cache.get(picname) + with self._ocr_coords_lock: + ocr_coords = self._ocr_coords_cache.get(picname) except Exception: pass @@ -2895,11 +2927,14 @@ class underlying_operations: if not t: return False + # 使用 shlex.quote 转义文本,防止 shell 注入 + safe_t = shlex.quote(t) + # 1) cmd clipboard set try: if ( subprocess.run( - self._adb_args(device_id, "shell", "cmd", "clipboard", "set", t), + self._adb_args(device_id, "shell", "cmd", "clipboard", "set", safe_t), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, timeout=3.0, @@ -2924,7 +2959,7 @@ class underlying_operations: "clipper.set", "-e", "text", - t, + safe_t, ), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, @@ -2951,13 +2986,9 @@ class underlying_operations: return False # 空格用 %s,避免被 shell 分词 t = t.replace(" ", "%s") - # 仅做最小转义,避免破坏中文 - t = ( - t.replace("&", "\\&") - .replace("<", "\\<") - .replace(">", "\\>") - .replace("|", "\\|") - ) + # 完整转义所有 shell 特殊字符 + for ch in ['&', '<', '>', '|', ';', '$', '`', "'", '"', '\\', '(', ')', '{', '}', '[', ']', '!', '#', '*', '?']: + t = t.replace(ch, '\\' + ch) try: return ( subprocess.run( @@ -3085,12 +3116,13 @@ class underlying_operations: act = str(action or "").strip() if not act: return False - # 这里不依赖 broadcast 的返回文本,只要命令成功执行就认为 ok + # 使用 shlex.quote 转义消息,防止 shell 注入 + safe_msg = shlex.quote(str(msg or "")) try: return ( subprocess.run( self._adb_args( - device_id, "shell", "am", "broadcast", "-a", act, "--es", "msg", str(msg or "") + device_id, "shell", "am", "broadcast", "-a", act, "--es", "msg", safe_msg ), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, @@ -3113,11 +3145,13 @@ class underlying_operations: v = int(value) except Exception: return False + # 使用 shlex.quote 转义 key,防止 shell 注入 + safe_k = shlex.quote(k) try: return ( subprocess.run( self._adb_args( - device_id, "shell", "am", "broadcast", "-a", act, "--ei", k, v + device_id, "shell", "am", "broadcast", "-a", act, "--ei", safe_k, v ), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, diff --git a/douyin/core/douyin_flow_control.py b/douyin/core/douyin_flow_control.py index 17e0d06..c5968cc 100644 --- a/douyin/core/douyin_flow_control.py +++ b/douyin/core/douyin_flow_control.py @@ -12,6 +12,7 @@ Flow control main helpers (本地模块化实现,基础版) from __future__ import annotations from typing import Any, Callable, List, Optional, Tuple +import logging import time import os @@ -55,8 +56,8 @@ def find_and_open_fudai_detail( "流程", "find_and_open_fudai_detail:正在等待开奖,跳过打开福袋详情", ) - except Exception: - pass + except Exception as e: + logging.debug("日志记录失败: %s", e) elapsed = time.time() - func_start_time analyser._log_kv( "流程", @@ -64,8 +65,8 @@ def find_and_open_fudai_detail( elapsed_seconds=round(elapsed, 2), ) return False - except Exception: - pass + except Exception as e: + logging.debug("检查等待开奖状态失败: %s", e) device_id = getattr(analyser, "device_id", "") diff --git a/douyin/handlers/douyin_screenshot_manager.py b/douyin/handlers/douyin_screenshot_manager.py index bb6a348..a381163 100644 --- a/douyin/handlers/douyin_screenshot_manager.py +++ b/douyin/handlers/douyin_screenshot_manager.py @@ -28,6 +28,8 @@ import time # 背景保存队列与工作线程(用于异步保存调试图像,避免阻塞主流程) _debug_save_queue: "Queue[Tuple[Image.Image, str, bool, Optional[Tuple[int,int,int,int]], Optional[dict], str, str, str]]" = (Queue()) _debug_save_worker_started = False +_debug_save_worker_lock = threading.Lock() # 保护 worker 启动的双检查锁 +_debug_save_shutdown_event = threading.Event() # 用于优雅关闭 # 异步保存计数(用于 GUI 显示) _async_save_success_count = 0 @@ -42,12 +44,12 @@ _SKIP_DEBUG_SAVE_IN_HOTLOOPS = bool( def _debug_save_loop() -> None: """后台线程循环,消费调试图像保存任务并执行同步保存。""" - while True: + while not _debug_save_shutdown_event.is_set(): try: - task = _debug_save_queue.get() - if not task: + task = _debug_save_queue.get(timeout=1.0) + if task is None: _debug_save_queue.task_done() - continue + break ( img, file_path, @@ -318,6 +320,8 @@ def _debug_save_loop() -> None: pass finally: _debug_save_queue.task_done() + except Empty: + continue except Exception: # 防止线程因异常退出 try: @@ -328,13 +332,35 @@ def _debug_save_loop() -> None: pass +def shutdown_debug_save_worker(timeout: float = 5.0) -> None: + """优雅关闭调试保存工作线程""" + global _debug_save_worker_started + if not _debug_save_worker_started: + return + _debug_save_shutdown_event.set() + _debug_save_queue.put(None) # 毒丸信号 + try: + _debug_save_queue.join() + except Exception: + pass + _debug_save_worker_started = False + + +# 注册 atexit 清理函数 +import atexit +atexit.register(shutdown_debug_save_worker) + + def _ensure_debug_save_worker_started() -> None: global _debug_save_worker_started if _debug_save_worker_started: return - t = threading.Thread(target=_debug_save_loop, name="debug-save-worker", daemon=True) - t.start() - _debug_save_worker_started = True + with _debug_save_worker_lock: + if _debug_save_worker_started: + return + t = threading.Thread(target=_debug_save_loop, name="debug-save-worker", daemon=True) + t.start() + _debug_save_worker_started = True def get_async_save_counters() -> tuple[int, int]: diff --git a/douyin/reservation/douyin_reservation.py b/douyin/reservation/douyin_reservation.py index 2fb9406..a551d5d 100644 --- a/douyin/reservation/douyin_reservation.py +++ b/douyin/reservation/douyin_reservation.py @@ -3,9 +3,23 @@ from __future__ import annotations import json import os import time +import threading from datetime import datetime from typing import Any, Optional +# 文件级锁字典,用于保护 JSON 文件的并发读写 +_json_file_locks: dict[str, threading.Lock] = {} +_json_locks_lock = threading.Lock() + + +def _get_file_lock(path: str) -> threading.Lock: + """获取文件级别的锁""" + with _json_locks_lock: + if path not in _json_file_locks: + _json_file_locks[path] = threading.Lock() + return _json_file_locks[path] + + # 导入预约错误处理工具 try: from douyin.reservation.core.reservation_error_utils import ( @@ -25,7 +39,7 @@ except ImportError: def _write_json_atomic(path: str, data: object, *, indent: int = 2, analyser: Optional[Any] = None) -> None: - """原子写入 JSON 文件,带详细的错误处理。 + """原子写入 JSON 文件,带详细的错误处理和线程锁保护。 参数: path: 文件路径 @@ -33,83 +47,85 @@ def _write_json_atomic(path: str, data: object, *, indent: int = 2, analyser: Op indent: JSON 缩进 analyser: 分析器对象(可选,用于日志记录) """ - try: - os.makedirs(os.path.dirname(path), exist_ok=True) - except PermissionError as e: - if _RESERVATION_ERROR_UTILS_AVAILABLE and analyser: - log_reservation_error( - analyser, - e, - format_reservation_context(path=path), - level="ERROR", - stage="创建状态文件目录(权限错误)", - ) - raise - except OSError as e: - if _RESERVATION_ERROR_UTILS_AVAILABLE and analyser: - log_reservation_error( - analyser, - e, - format_reservation_context(path=path), - level="ERROR", - stage="创建状态文件目录(系统错误)", - ) - raise - - tmp = path + ".tmp" - try: - with open(tmp, "w", encoding="utf-8") as f: - json.dump(data, f, ensure_ascii=False, indent=indent) - f.write("\n") - except (ValueError, TypeError) as e: - if _RESERVATION_ERROR_UTILS_AVAILABLE and analyser: - log_reservation_error( - analyser, - e, - format_reservation_context(path=path), - level="ERROR", - stage="序列化状态数据", - ) - raise - except PermissionError as e: - if _RESERVATION_ERROR_UTILS_AVAILABLE and analyser: - log_reservation_error( - analyser, - e, - format_reservation_context(path=path), - level="ERROR", - stage="写入状态文件(权限错误)", - ) - raise - except OSError as e: - if _RESERVATION_ERROR_UTILS_AVAILABLE and analyser: - log_reservation_error( - analyser, - e, - format_reservation_context(path=path), - level="ERROR", - stage="写入状态文件(系统错误)", - ) - raise - - try: - os.replace(tmp, path) - except OSError as e: - # 清理临时文件 + lock = _get_file_lock(path) + with lock: try: - if os.path.exists(tmp): - os.remove(tmp) - except Exception: - pass - if _RESERVATION_ERROR_UTILS_AVAILABLE and analyser: - log_reservation_error( - analyser, - e, - format_reservation_context(path=path), - level="ERROR", - stage="原子替换状态文件", - ) - raise + os.makedirs(os.path.dirname(path), exist_ok=True) + except PermissionError as e: + if _RESERVATION_ERROR_UTILS_AVAILABLE and analyser: + log_reservation_error( + analyser, + e, + format_reservation_context(path=path), + level="ERROR", + stage="创建状态文件目录(权限错误)", + ) + raise + except OSError as e: + if _RESERVATION_ERROR_UTILS_AVAILABLE and analyser: + log_reservation_error( + analyser, + e, + format_reservation_context(path=path), + level="ERROR", + stage="创建状态文件目录(系统错误)", + ) + raise + + tmp = path + ".tmp" + try: + with open(tmp, "w", encoding="utf-8") as f: + json.dump(data, f, ensure_ascii=False, indent=indent) + f.write("\n") + except (ValueError, TypeError) as e: + if _RESERVATION_ERROR_UTILS_AVAILABLE and analyser: + log_reservation_error( + analyser, + e, + format_reservation_context(path=path), + level="ERROR", + stage="序列化状态数据", + ) + raise + except PermissionError as e: + if _RESERVATION_ERROR_UTILS_AVAILABLE and analyser: + log_reservation_error( + analyser, + e, + format_reservation_context(path=path), + level="ERROR", + stage="写入状态文件(权限错误)", + ) + raise + except OSError as e: + if _RESERVATION_ERROR_UTILS_AVAILABLE and analyser: + log_reservation_error( + analyser, + e, + format_reservation_context(path=path), + level="ERROR", + stage="写入状态文件(系统错误)", + ) + raise + + try: + os.replace(tmp, path) + except OSError as e: + # 清理临时文件 + try: + if os.path.exists(tmp): + os.remove(tmp) + except Exception: + pass + if _RESERVATION_ERROR_UTILS_AVAILABLE and analyser: + log_reservation_error( + analyser, + e, + format_reservation_context(path=path), + level="ERROR", + stage="原子替换状态文件", + ) + raise def write_reservations_state( @@ -190,32 +206,34 @@ def write_reservations_state( def load_high_value_rooms_map(path: str) -> dict[str, dict]: - """讀取高價值直播間映射(跨運行持久化)。""" - try: - if not os.path.exists(path): + """讀取高價值直播間映射(跨運行持久化),带线程锁保护。""" + lock = _get_file_lock(path) + with lock: + try: + if not os.path.exists(path): + return {} + with open(path, "r", encoding="utf-8") as f: + data = json.load(f) + + # 兼容兩種格式:{"rooms": [...]} 或 直接是 [...] + rooms = None + if isinstance(data, dict): + rooms = data.get("rooms") + elif isinstance(data, list): + rooms = data + + out: dict[str, dict] = {} + if isinstance(rooms, list): + for it in rooms: + if not isinstance(it, dict): + continue + key = str(it.get("room_title") or "").strip() + if not key: + continue + out[key] = dict(it) + return out + except Exception: return {} - with open(path, "r", encoding="utf-8") as f: - data = json.load(f) - - # 兼容兩種格式:{"rooms": [...]} 或 直接是 [...] - rooms = None - if isinstance(data, dict): - rooms = data.get("rooms") - elif isinstance(data, list): - rooms = data - - out: dict[str, dict] = {} - if isinstance(rooms, list): - for it in rooms: - if not isinstance(it, dict): - continue - key = str(it.get("room_title") or "").strip() - if not key: - continue - out[key] = dict(it) - return out - except Exception: - return {} def write_high_value_rooms_state(path: str, rooms_map: dict, *, reason: str = "") -> None: diff --git a/douyin/stage/douyin_stage_handler.py b/douyin/stage/douyin_stage_handler.py index b6c2cb6..60a1626 100644 --- a/douyin/stage/douyin_stage_handler.py +++ b/douyin/stage/douyin_stage_handler.py @@ -406,7 +406,7 @@ class StageHandler: best_binary_score = max_val roi_binary_fixed = roi_bin tpl_binary_fixed = tpl_bin - except: + except Exception: continue # 方法2:自适应二值化(调整参数,blockSize使用较小值以提高精度) diff --git a/run_cli.py b/run_cli.py index 9dc6927..a0ea4dd 100644 --- a/run_cli.py +++ b/run_cli.py @@ -1,5 +1,6 @@ import argparse import importlib.util +import logging import os import shutil import sys @@ -10,18 +11,21 @@ if os.environ.get("PARAM_TRACK_ALL") == "1": from douyin.utils.param_usage_tracer import start_tracing start_tracing() - except Exception: - # 启用失败不影响主流程 - pass + except Exception as e: + logging.debug("参数追踪启用失败: %s", e) # ==== 首先配置日志系统,确保日志文件能正常写入 ============================== try: from douyin.logging import douyin_logging douyin_logging.configure_logging() -except Exception: - # 日志配置失败不影响主流程,但会缺少文件日志 - pass +except Exception as e: + # 日志配置失败,使用基础配置作为兜底 + logging.basicConfig( + level=logging.INFO, + format='%(asctime)s - [%(levelname)s] - %(message)s' + ) + logging.warning("日志系统配置失败,使用基础配置: %s", e) # 尝试确保行为日志的异步 listener 在进程启动时就被启动, # 以避免某些运行环境中模块未完整初始化导致日志未落盘的问题。 @@ -33,11 +37,11 @@ try: action_logger_mod = importlib.import_module("douyin.logging.action_logger") try: action_logger_mod._start_queue_listener_if_needed() - except Exception: + except Exception as e: # 启动失败时不影响主流程,action_logger 有回退到同步 file handler 的逻辑 - pass -except Exception: - pass + logging.debug("action_logger 启动失败: %s", e) +except Exception as e: + logging.debug("action_logger 模块导入失败: %s", e) # ==== 兼容导入完整逻辑模块 douyin_fudai 及其依赖 ============================== # 如果需要从外部工作树加载兼容模块,可显式设置 EXTERNAL_DOUYIN_FUDAI_DIR。 @@ -61,8 +65,9 @@ def _load_local_shim(mod_name: str, rel_path: str) -> None: module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) sys.modules[mod_name] = module - except Exception: + except Exception as e: # shim 加载失败不影响主流程,后续仍可尝试外部模块 + logging.debug("shim 模块加载失败 %s: %s", mod_name, e) return @@ -80,9 +85,9 @@ try: and _EXTERNAL_DOUYIN_FUDAI_DIR not in sys.path ): sys.path.append(_EXTERNAL_DOUYIN_FUDAI_DIR) -except Exception: +except Exception as e: # 不影响主流程;如果目录不存在,后续导入会按默认逻辑报错 - pass + logging.debug("外部模块路径配置失败: %s", e) # 在导入 PaddleOCR 之前设置,避免每次启动都做连通性检查提示并抑制底层 glog/oneDNN 等噪音 # DISABLE_MODEL_SOURCE_CHECK: 跳过 PaddleOCR 的主机连通性检查(国内网络环境常卡) -- 2.39.5 From 84e8d0c46d65c1156675cf651246adbdb945b021 Mon Sep 17 00:00:00 2001 From: Codex Date: Wed, 20 May 2026 22:39:47 +0800 Subject: [PATCH 02/10] =?UTF-8?q?fix:=20Web=20GUI=20=E5=AE=89=E5=85=A8?= =?UTF-8?q?=E5=8A=A0=E5=9B=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. PID 操作添加专用锁保护,防止 force_stop 并发问题 2. POST 接口添加 CSRF token 验证 3. 新增 /api/csrf-token 接口供前端获取 token Co-Authored-By: Claude Opus 4.5 --- gui/web_gui.py | 61 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 40 insertions(+), 21 deletions(-) diff --git a/gui/web_gui.py b/gui/web_gui.py index 5722479..a0d41f9 100644 --- a/gui/web_gui.py +++ b/gui/web_gui.py @@ -1,6 +1,7 @@ import json import os import re +import secrets import shutil import subprocess import sys @@ -22,6 +23,9 @@ BASE_DIR = path_config.PROJECT_ROOT CONFIG_PATH = path_config.get_config_path() WEBUI_DIR = path_config.get_webui_dir() +# CSRF token for POST request protection (local-only server) +_CSRF_TOKEN = secrets.token_hex(32) + # ANSI转义序列清理正则表达式 _ANSI_ESCAPE_RE = re.compile(r"\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])") @@ -132,6 +136,7 @@ class ProcRunner: def __init__(self) -> None: self._proc: Optional[subprocess.Popen] = None self._lock = threading.Lock() + self._pid_lock = threading.Lock() # 保护 PID 相关操作 self._log: list[str] = [] self._log_capacity: int = ( 10000 # 0=不限制(可能占用大量内存),增加到10000避免日志被过早截断 @@ -1035,28 +1040,29 @@ class ProcRunner: return False def force_stop(self) -> None: - with self._lock: - pid = self._pid - p = self._proc - self._append_log(f"[停止] 强制停止请求 pid={pid or '-'}") - try: - if p is not None and p.poll() is None: - p.kill() - except Exception: - pass - if pid: + with self._pid_lock: + with self._lock: + pid = self._pid + p = self._proc + self._append_log(f"[停止] 强制停止请求 pid={pid or '-'}") try: - if os.name == "nt": - subprocess.run(["taskkill", "/PID", str(pid), "/T", "/F"], capture_output=True, text=True, check=False, timeout=10) - else: - subprocess.run(["kill", "-9", str(pid)], capture_output=True, text=True, check=False, timeout=10) - except Exception as e: - self._append_log(f"[停止] 系统强制结束失败: {e}") - with self._lock: - self._proc = None - self._pid = None - self._exit_code = self._exit_code if self._exit_code is not None else -9 - self._append_log("[停止] 强制停止完成") + if p is not None and p.poll() is None: + p.kill() + except Exception: + pass + if pid: + try: + if os.name == "nt": + subprocess.run(["taskkill", "/PID", str(pid), "/T", "/F"], capture_output=True, text=True, check=False, timeout=10) + else: + subprocess.run(["kill", "-9", str(pid)], capture_output=True, text=True, check=False, timeout=10) + except Exception as e: + self._append_log(f"[停止] 系统强制结束失败: {e}") + with self._lock: + self._proc = None + self._pid = None + self._exit_code = self._exit_code if self._exit_code is not None else -9 + self._append_log("[停止] 强制停止完成") def stop(self) -> None: with self._lock: @@ -1729,6 +1735,9 @@ class Handler(BaseHTTPRequestHandler): u = urlparse(self.path) if u.path.startswith("/api/"): api_path = u.path.rstrip("/") + # 提供 CSRF token 给前端 + if u.path == "/api/csrf-token": + return self._send_json({"token": _CSRF_TOKEN}) if u.path == "/api/devices": adb = find_adb_executable() return self._send_json({"adb": adb, "devices": list_adb_devices(adb)}) @@ -1912,8 +1921,18 @@ class Handler(BaseHTTPRequestHandler): self._serve_static(u.path) + def _verify_csrf_token(self) -> bool: + """验证 CSRF token,本地服务器的简单防护""" + token = self.headers.get("X-CSRF-Token", "") + return token == _CSRF_TOKEN + def do_POST(self): u = urlparse(self.path) + + # CSRF token 验证(对修改操作的 POST 请求) + if not self._verify_csrf_token(): + return self._send_json({"ok": False, "error": "Invalid CSRF token"}, status=403) + if u.path == "/api/config": body = self._read_json() cfg = read_config() -- 2.39.5 From 5ce314b41c640cbfd6735bb4e83a3063d37692ae Mon Sep 17 00:00:00 2001 From: Codex Date: Wed, 20 May 2026 22:46:02 +0800 Subject: [PATCH 03/10] =?UTF-8?q?chore:=20=E6=B8=85=E7=90=86=E6=AD=BB?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=92=8C=E8=A3=B8=20except?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 删除空文件 sitecustomize.py - 修复 tools/simple_region_selector.py 中的裸 except: Co-Authored-By: Claude Opus 4.5 --- sitecustomize.py | 1 - tools/simple_region_selector.py | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) delete mode 100644 sitecustomize.py diff --git a/sitecustomize.py b/sitecustomize.py deleted file mode 100644 index 8b13789..0000000 --- a/sitecustomize.py +++ /dev/null @@ -1 +0,0 @@ - diff --git a/tools/simple_region_selector.py b/tools/simple_region_selector.py index 6a1bae5..591ca93 100644 --- a/tools/simple_region_selector.py +++ b/tools/simple_region_selector.py @@ -240,7 +240,7 @@ Ctrl+F: 适应窗口""" if self.scale != 1.0: try: resample = Image.Resampling.LANCZOS if hasattr(Image, 'Resampling') else Image.LANCZOS - except: + except Exception: resample = Image.BICUBIC resized = self.img.resize((disp_w, disp_h), resample) else: -- 2.39.5 From 1dea80d5be64115ab7bf3d296ae9934b692aa195 Mon Sep 17 00:00:00 2001 From: Codex Date: Wed, 20 May 2026 22:48:43 +0800 Subject: [PATCH 04/10] =?UTF-8?q?fix:=20=E6=88=AA=E5=9B=BE=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E6=B7=BB=E5=8A=A0=20atexit=20=E4=BC=98=E9=9B=85?= =?UTF-8?q?=E5=85=B3=E9=97=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ScreenshotService 在进程退出时通过 atexit 钩子停止后台线程, 避免 daemon 线程被强杀导致的资源泄漏。 Co-Authored-By: Claude Opus 4.5 --- operations/screenshot_service.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/operations/screenshot_service.py b/operations/screenshot_service.py index b0c3272..264154a 100644 --- a/operations/screenshot_service.py +++ b/operations/screenshot_service.py @@ -8,6 +8,7 @@ from __future__ import annotations +import atexit import os import threading import time @@ -336,3 +337,15 @@ def get_optimal_workers() -> int: if cpu_count >= 2: return 3 return 2 + + +def _shutdown_screenshot_service() -> None: + """atexit 注册的清理函数,确保进程退出时优雅关闭截图服务""" + try: + if ScreenshotService._instance is not None: + ScreenshotService._instance.stop() + except Exception: + pass + + +atexit.register(_shutdown_screenshot_service) -- 2.39.5 From 61d72b2b8d9c09ed76af66a0e37cd00c263b53d0 Mon Sep 17 00:00:00 2001 From: Codex Date: Wed, 20 May 2026 22:50:33 +0800 Subject: [PATCH 05/10] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=20np.array=20?= =?UTF-8?q?=E4=B8=BA=20np.asarray=20=E5=87=8F=E5=B0=91=E5=86=85=E5=AD=98?= =?UTF-8?q?=E6=8B=B7=E8=B4=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit np.array() 默认会复制数据,而 np.asarray() 只在必要时才复制。 对于只读用途,asarray() 性能更好。 Co-Authored-By: Claude Opus 4.5 --- douyin/detection/douyin_pixel.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/douyin/detection/douyin_pixel.py b/douyin/detection/douyin_pixel.py index 7055585..6d62d46 100644 --- a/douyin/detection/douyin_pixel.py +++ b/douyin/detection/douyin_pixel.py @@ -85,7 +85,7 @@ def detect_coupon_popup_background( return False bgr = cv2.cvtColor( # type: ignore[attr-defined] - np.array(pic_rgb), cv2.COLOR_RGB2BGR # type: ignore[attr-defined] + np.asarray(pic_rgb), cv2.COLOR_RGB2BGR # type: ignore[attr-defined] ) roi = bgr[y1:y2, x1:x2] # type: ignore[index] if roi.size == 0: @@ -230,7 +230,7 @@ def detect_redpacket_popup_background( # 裁剪感兴趣区域 roi = pic_rgb.crop((x1, y1, x2, y2)) - arr = np.array(roi) + arr = np.asarray(roi) if arr.size == 0: if return_info: @@ -834,10 +834,10 @@ def detect_live_room_icons_by_pixel(pic_rgb: Image.Image) -> tuple[bool, float]: draw.polygon(polygon_points, fill=255) # 将mask转换为numpy数组 - mask_array = np.array(mask) + mask_array = np.asarray(mask) # 获取原始图像的像素数据 - img_array = np.array(pic_rgb) + img_array = np.asarray(pic_rgb) # 计算多边形内部的总像素数 total_pixels = np.sum(mask_array > 0) -- 2.39.5 From 68a2ab73bae742124f862cf322c7aed3290ac935 Mon Sep 17 00:00:00 2001 From: Codex Date: Wed, 20 May 2026 22:54:01 +0800 Subject: [PATCH 06/10] =?UTF-8?q?refactor:=20=E6=8A=BD=E5=8F=96=E5=80=92?= =?UTF-8?q?=E8=AE=A1=E6=97=B6=E8=A7=A3=E6=9E=90=E5=B7=A5=E5=85=B7=E5=88=B0?= =?UTF-8?q?=20time=5Futils=20=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将散落在三处的重复 _parse_mm_ss / _extract_2digits 实现集中到 douyin/utils/time_utils.py,便于复用与单元测试。 不修改既有调用点(保持向后兼容),后续可按需迁移。 Co-Authored-By: Claude Opus 4.5 --- douyin/utils/time_utils.py | 61 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 douyin/utils/time_utils.py diff --git a/douyin/utils/time_utils.py b/douyin/utils/time_utils.py new file mode 100644 index 0000000..90bb8fe --- /dev/null +++ b/douyin/utils/time_utils.py @@ -0,0 +1,61 @@ +"""倒计时解析与时间相关的工具函数。 + +集中化以前散落在多个文件的重复实现: +- douyin/fudai/douyin_fudai_core.py: _parse_mm_ss +- douyin/fudai/core/fudai_detail_reader.py: _parse_mm_ss +- fudai_detail_modules/fudai_detail_reader.py: _parse_mm_ss + +注意:normalize_countdown_text 仍在 douyin/utils/douyin_utils.py 中, +那里逻辑更复杂(包含 OCR 字符纠正等),不在此处重复。 +""" + +from __future__ import annotations + +import logging +import re +from typing import Optional, Tuple + + +def parse_mm_ss(text: str) -> Optional[Tuple[int, int]]: + """从文本中解析 mm:ss 格式的时间,返回 (分钟, 秒) 元组。 + + 支持中文冒号 `:` 和英文冒号 `:`。 + + Args: + text: 待解析的文本,例如 "11:37" 或 "5:30后开奖" + + Returns: + 成功返回 (mm, ss) 元组;失败返回 None + """ + if not text: + return None + m = re.search(r"(\d{1,2})[::](\d{2})", str(text)) + if not m: + return None + try: + return int(m.group(1)), int(m.group(2)) + except (ValueError, TypeError) as e: + logging.debug("解析倒计时格式失败: text=%r, error=%s", str(text)[:50], e) + return None + + +def extract_2digits(text: str) -> str: + """从文本中提取最多 2 位数字。 + + 用于 OCR 倒计时分钟段/秒段的预处理。 + + Args: + text: OCR 识别的文本 + + Returns: + 2 位数字字符串;不足 2 位时左侧补 0;无数字返回空串 + """ + digits = "".join(ch for ch in str(text or "") if ch.isdigit()) + if len(digits) >= 2: + return digits[:2] + if len(digits) == 1: + return "0" + digits + return "" + + +__all__ = ["parse_mm_ss", "extract_2digits"] -- 2.39.5 From 6b22939c7e7f0a5512b88bf082f46a4a41ccff77 Mon Sep 17 00:00:00 2001 From: Codex Date: Wed, 20 May 2026 22:54:44 +0800 Subject: [PATCH 07/10] =?UTF-8?q?fix:=20=E6=94=B9=E5=96=84=20fudai=5Fdetai?= =?UTF-8?q?l=5Fvalidator=20=E5=BC=82=E5=B8=B8=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 收紧 except 类型为 ValueError/TypeError - 添加 logging.debug 替代静默 pass,便于排查 OCR 识别错误 Co-Authored-By: Claude Opus 4.5 --- fudai_detail_modules/fudai_detail_validator.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/fudai_detail_modules/fudai_detail_validator.py b/fudai_detail_modules/fudai_detail_validator.py index e7a3b15..e26dfaf 100644 --- a/fudai_detail_modules/fudai_detail_validator.py +++ b/fudai_detail_modules/fudai_detail_validator.py @@ -8,6 +8,7 @@ from __future__ import annotations """ from typing import Any, Tuple +import logging import re import os import json @@ -28,15 +29,18 @@ def validate_fudai_detail(detail: Tuple, analyser: Any = None) -> Tuple[bool, st time_text = str(detail[1] or "").strip() try: participants = int(detail[2] or 0) - except Exception: + except (ValueError, TypeError) as e: + logging.debug("参与人数解析失败: %r, 使用默认值0: %s", detail[2], e) participants = 0 try: copies = int(detail[3] or 0) - except Exception: + except (ValueError, TypeError) as e: + logging.debug("份数解析失败: %r, 使用默认值0: %s", detail[3], e) copies = 0 try: prize_value_yuan = float(detail[4] or 0.0) - except Exception: + except (ValueError, TypeError) as e: + logging.debug("奖品金额解析失败: %r, 使用默认值0.0: %s", detail[4], e) prize_value_yuan = 0.0 countdown_tuple = detail[5] @@ -45,8 +49,8 @@ def validate_fudai_detail(detail: Tuple, analyser: Any = None) -> Tuple[bool, st try: countdown_start = float(countdown_tuple[0] or 0.0) countdown_end = float(countdown_tuple[1] or 0.0) - except Exception: - pass + except (ValueError, TypeError) as e: + logging.debug("倒计时元组解析失败: %r: %s", countdown_tuple, e) # 检查1:内容不能为空(至少要有一些文字) # 如果运行时显式禁用了内容识别,则跳过此检查(允许内容为空) -- 2.39.5 From 92753c51a5585d03166b96a160064c852e61047c Mon Sep 17 00:00:00 2001 From: Codex Date: Wed, 20 May 2026 22:56:51 +0800 Subject: [PATCH 08/10] =?UTF-8?q?chore:=20=E5=88=A0=E9=99=A4=E5=B7=B2?= =?UTF-8?q?=E5=BA=9F=E5=BC=83=E7=9A=84=20deleted=5Ftests=5Fbackup=20?= =?UTF-8?q?=E7=9B=AE=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 22 个备份测试文件已废弃多月,从代码库中移除以减少干扰。 如需历史,仍可从 git 历史中恢复。 Co-Authored-By: Claude Opus 4.5 --- .../test_adb_screenshot_timing.py | 222 ---------- ...t_completely_disable_screenshot_service.py | 106 ----- .../test_direct_screenshot.py | 287 ------------- .../test_disable_streaming_screenshots.py | 118 ------ .../test_loop_screenshot_stop.py | 95 ----- deleted_tests_backup/test_ocr_multithread.py | 100 ----- .../test_ocr_multithread_performance.py | 325 --------------- deleted_tests_backup/test_ocr_speed.py | 196 --------- .../test_ocr_speed_optimized.py | 212 ---------- .../test_parallel_ocr_performance.py | 383 ------------------ .../test_parallel_ocr_quick.py | 141 ------- .../test_parallel_ocr_simple.py | 206 ---------- .../test_real_parallel_ocr.py | 376 ----------------- .../test_screenshot_error_handling.py | 109 ----- .../test_screenshot_file_wait.py | 92 ----- .../test_screenshot_performance.py | 127 ------ .../test_screenshot_reuse_optimization.py | 129 ------ .../test_screenshot_service_fix.py | 59 --- ...est_screenshot_service_ocr_optimization.py | 91 ----- .../test_screenshot_service_ocr_simple.py | 84 ---- .../test_screenshot_service_ocr_stop.py | 113 ------ deleted_tests_backup/test_time_import_fix.py | 44 -- 22 files changed, 3615 deletions(-) delete mode 100644 deleted_tests_backup/test_adb_screenshot_timing.py delete mode 100644 deleted_tests_backup/test_completely_disable_screenshot_service.py delete mode 100644 deleted_tests_backup/test_direct_screenshot.py delete mode 100644 deleted_tests_backup/test_disable_streaming_screenshots.py delete mode 100644 deleted_tests_backup/test_loop_screenshot_stop.py delete mode 100644 deleted_tests_backup/test_ocr_multithread.py delete mode 100644 deleted_tests_backup/test_ocr_multithread_performance.py delete mode 100644 deleted_tests_backup/test_ocr_speed.py delete mode 100644 deleted_tests_backup/test_ocr_speed_optimized.py delete mode 100644 deleted_tests_backup/test_parallel_ocr_performance.py delete mode 100644 deleted_tests_backup/test_parallel_ocr_quick.py delete mode 100644 deleted_tests_backup/test_parallel_ocr_simple.py delete mode 100644 deleted_tests_backup/test_real_parallel_ocr.py delete mode 100644 deleted_tests_backup/test_screenshot_error_handling.py delete mode 100644 deleted_tests_backup/test_screenshot_file_wait.py delete mode 100644 deleted_tests_backup/test_screenshot_performance.py delete mode 100644 deleted_tests_backup/test_screenshot_reuse_optimization.py delete mode 100644 deleted_tests_backup/test_screenshot_service_fix.py delete mode 100644 deleted_tests_backup/test_screenshot_service_ocr_optimization.py delete mode 100644 deleted_tests_backup/test_screenshot_service_ocr_simple.py delete mode 100644 deleted_tests_backup/test_screenshot_service_ocr_stop.py delete mode 100644 deleted_tests_backup/test_time_import_fix.py diff --git a/deleted_tests_backup/test_adb_screenshot_timing.py b/deleted_tests_backup/test_adb_screenshot_timing.py deleted file mode 100644 index 3b8604a..0000000 --- a/deleted_tests_backup/test_adb_screenshot_timing.py +++ /dev/null @@ -1,222 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- -""" -ADB 截图基准测试脚本 - -用于测试 ADB exec-out screencap 的实际耗时,帮助诊断截图慢的问题。 - -使用方法: -1. 确保设备已连接: adb devices -2. 运行: python test_adb_screenshot_timing.py [device_id] - 如果不提供 device_id,将使用第一个设备 -""" - -import subprocess -import sys -import time -import os -from typing import Optional - - -def get_adb_path() -> str: - """获取 ADB 路径""" - # 尝试从环境变量获取 - adb = os.environ.get("ADB_PATH", "adb") - # 尝试常见路径 - if not os.path.exists(adb): - common_paths = [ - "adb", - r"C:\Users\{}\AppData\Local\Android\Sdk\platform-tools\adb.exe".format(os.environ.get("USERNAME", "")), - r"C:\Android\platform-tools\adb.exe", - ] - for path in common_paths: - try: - result = subprocess.run([path, "version"], capture_output=True, timeout=5) - if result.returncode == 0: - return path - except Exception: - continue - return adb - - -def get_device_id() -> Optional[str]: - """获取第一个连接的设备ID""" - adb = get_adb_path() - try: - result = subprocess.run([adb, "devices"], capture_output=True, text=True, timeout=10) - if result.returncode != 0: - print(f"[错误] adb devices 失败: {result.stderr}") - return None - - lines = result.stdout.strip().split("\n")[1:] # 跳过第一行 "List of devices attached" - devices = [] - for line in lines: - if line.strip() and "\t" in line: - device_id = line.split("\t")[0].strip() - if device_id: - devices.append(device_id) - - if not devices: - print("[错误] 未找到连接的设备") - return None - - if len(devices) > 1: - print(f"[提示] 找到多个设备: {devices},使用第一个: {devices[0]}") - - return devices[0] - except Exception as e: - print(f"[错误] 获取设备列表失败: {e}") - return None - - -def test_exec_out_screencap(device_id: str, iterations: int = 5) -> None: - """测试 exec-out screencap 耗时""" - adb = get_adb_path() - print(f"\n{'='*60}") - print(f"测试 ADB exec-out screencap") - print(f"设备: {device_id}") - print(f"迭代次数: {iterations}") - print(f"{'='*60}\n") - - times = [] - sizes = [] - - for i in range(iterations): - try: - start = time.time() - result = subprocess.run( - [adb, "-s", device_id, "exec-out", "screencap", "-p"], - capture_output=True, - timeout=15, - ) - elapsed = (time.time() - start) * 1000.0 # 转换为毫秒 - - if result.returncode == 0: - size = len(result.stdout) - times.append(elapsed) - sizes.append(size) - print(f" 迭代 {i+1}: {elapsed:.1f}ms, 大小: {size/1024:.1f}KB") - else: - print(f" 迭代 {i+1}: 失败 (返回码: {result.returncode})") - except subprocess.TimeoutExpired: - print(f" 迭代 {i+1}: 超时 (>15秒)") - except Exception as e: - print(f" 迭代 {i+1}: 异常: {e}") - - if times: - avg_time = sum(times) / len(times) - min_time = min(times) - max_time = max(times) - avg_size = sum(sizes) / len(sizes) if sizes else 0 - - print(f"\n{'='*60}") - print(f"统计结果:") - print(f" 平均耗时: {avg_time:.1f}ms") - print(f" 最小耗时: {min_time:.1f}ms") - print(f" 最大耗时: {max_time:.1f}ms") - print(f" 平均大小: {avg_size/1024:.1f}KB") - print(f"{'='*60}\n") - - if avg_time > 3000: - print("[警告] 平均耗时超过3秒,可能存在问题:") - print(" - 检查 USB 连接是否稳定") - print(" - 检查是否使用 Wi-Fi ADB(通常较慢)") - print(" - 检查设备性能是否正常") - elif avg_time > 2000: - print("[提示] 平均耗时超过2秒,建议:") - print(" - 考虑使用 USB 连接而非 Wi-Fi") - print(" - 检查设备性能") - - -def test_shell_screencap_pull(device_id: str, iterations: int = 3) -> None: - """测试 shell screencap + pull 耗时(回退方案)""" - adb = get_adb_path() - print(f"\n{'='*60}") - print(f"测试 ADB shell screencap + pull (回退方案)") - print(f"设备: {device_id}") - print(f"迭代次数: {iterations}") - print(f"{'='*60}\n") - - times = [] - - for i in range(iterations): - try: - # 清理旧文件 - subprocess.run([adb, "-s", device_id, "shell", "rm", "/sdcard/DCIM/screenshot.png"], - capture_output=True, timeout=5) - - start = time.time() - - # Step 1: shell screencap - step1_start = time.time() - result1 = subprocess.run( - [adb, "-s", device_id, "shell", "screencap", "-p", "/sdcard/DCIM/screenshot.png"], - capture_output=True, - timeout=10, - ) - step1_dur = (time.time() - step1_start) * 1000.0 - - if result1.returncode != 0: - print(f" 迭代 {i+1}: shell screencap 失败") - continue - - # Step 2: pull - step2_start = time.time() - result2 = subprocess.run( - [adb, "-s", device_id, "pull", "/sdcard/DCIM/screenshot.png", "test_screenshot.png"], - capture_output=True, - timeout=10, - ) - step2_dur = (time.time() - step2_start) * 1000.0 - - elapsed = (time.time() - start) * 1000.0 - - if result2.returncode == 0 and os.path.exists("test_screenshot.png"): - size = os.path.getsize("test_screenshot.png") - times.append(elapsed) - print(f" 迭代 {i+1}: 总耗时 {elapsed:.1f}ms (screencap: {step1_dur:.1f}ms, pull: {step2_dur:.1f}ms), 大小: {size/1024:.1f}KB") - try: - os.remove("test_screenshot.png") - except Exception: - pass - else: - print(f" 迭代 {i+1}: pull 失败") - except Exception as e: - print(f" 迭代 {i+1}: 异常: {e}") - - if times: - avg_time = sum(times) / len(times) - min_time = min(times) - max_time = max(times) - - print(f"\n{'='*60}") - print(f"统计结果 (回退方案):") - print(f" 平均耗时: {avg_time:.1f}ms") - print(f" 最小耗时: {min_time:.1f}ms") - print(f" 最大耗时: {max_time:.1f}ms") - print(f"{'='*60}\n") - - -def main(): - device_id = None - if len(sys.argv) > 1: - device_id = sys.argv[1] - else: - device_id = get_device_id() - - if not device_id: - print("[错误] 无法获取设备ID") - sys.exit(1) - - print(f"使用设备: {device_id}") - print(f"ADB 路径: {get_adb_path()}") - - # 测试 exec-out(主要方案) - test_exec_out_screencap(device_id, iterations=5) - - # 测试 shell + pull(回退方案,较少迭代) - test_shell_screencap_pull(device_id, iterations=3) - - -if __name__ == "__main__": - main() diff --git a/deleted_tests_backup/test_completely_disable_screenshot_service.py b/deleted_tests_backup/test_completely_disable_screenshot_service.py deleted file mode 100644 index c830402..0000000 --- a/deleted_tests_backup/test_completely_disable_screenshot_service.py +++ /dev/null @@ -1,106 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- -""" -测试完全禁用截图服务功能 -""" - -import time -import sys -import os - -# 添加项目根目录到路径 -sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) - -def test_completely_disable_screenshot_service(): - """测试完全禁用截图服务""" - - print("测试完全禁用截图服务功能...") - print("=" * 50) - - # 1. 测试截图服务的get_screenshot方法 - print("\n1. 测试截图服务的get_screenshot方法") - - from operations.screenshot_service import ScreenshotService - service = ScreenshotService.get_instance() - - # 调用get_screenshot,应该直接返回False - result = service.get_screenshot("test_device") - print(f"service.get_screenshot() 返回: {result}") - - if result == False: - print("[OK] 截图服务正确返回False,不启动后台服务") - else: - print("[ERROR] 截图服务返回了非False值") - return False - - # 2. 测试服务状态 - print("\n2. 检查服务状态") - - if service.is_running(): - print("[WARN] 服务正在运行,但应该已被禁用") - else: - print("[OK] 服务未运行") - - print(f"当前设备ID: {service._current_device_id}") - - # 3. 测试直接截图调用 - print("\n3. 测试直接截图调用") - - from operations.screenshot_operations import ScreenshotOperations - ops = ScreenshotOperations() - - # 检查默认参数 - import inspect - sig = inspect.signature(ops.get_screenshot) - params = sig.parameters - use_service_default = params['use_service'].default - - print(f"直接截图方法use_service默认值: {use_service_default}") - - if use_service_default == False: - print("[OK] 直接截图方法默认不使用服务") - else: - print("[WARN] 直接截图方法仍可能使用服务") - - # 4. 验证所有修改 - print("\n4. 验证所有修改") - - modified_files = [ - "operations/screenshot_operations.py", - "operations/screenshot_service.py", - "douyin/core/douyin_fudai.py", - "douyin/core/douyin_main_flow.py", - "fudai_detail_modules/fudai_detail_reader.py" - ] - - for file_path in modified_files: - if os.path.exists(file_path): - print(f"[OK] {file_path} 存在") - else: - print(f"[ERROR] {file_path} 不存在") - return False - - # 5. 总结禁用状态 - print("\n5. 截图服务禁用总结") - print("-" * 40) - print("已完成的禁用:") - print("1. [DONE] 修改 get_screenshot() 默认 use_service=False") - print("2. [DONE] 让 ScreenshotService.get_screenshot() 直接返回False") - print("3. [DONE] 移除所有后台服务启动代码") - print("4. [DONE] 移除设备ID变更时的自动服务启动") - print("5. [DONE] 移除OCR操作后的服务重启") - - print("\n预期效果:") - print("- 所有截图调用都会使用直接ADB命令") - print("- 后台截图服务完全不会启动") - print("- 每次截图都是独立的,不会复用缓存") - print("- 系统更加稳定,但响应稍慢") - - return True - -if __name__ == "__main__": - success = test_completely_disable_screenshot_service() - print(f"\n{'='*50}") - print(f"测试结果: {'PASS' if success else 'FAIL'}") - print("后台截图服务已完全禁用,使用原始直接截图模式") - print(f"{'='*50}") diff --git a/deleted_tests_backup/test_direct_screenshot.py b/deleted_tests_backup/test_direct_screenshot.py deleted file mode 100644 index 85058b0..0000000 --- a/deleted_tests_backup/test_direct_screenshot.py +++ /dev/null @@ -1,287 +0,0 @@ -#!/usr/bin/env python3 -""" -测试直接截图功能 -验证所有截图调用都使用直接模式(use_service=False) -""" - -import os -import sys -import time -from datetime import datetime - -# 添加项目根目录到路径 -sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) - -def test_screenshot_operations(): - """测试 operations.screenshot_operations 模块""" - print("=" * 80) - print("测试 1: operations.screenshot_operations.get_screenshot()") - print("=" * 80) - - try: - from operations.screenshot_operations import get_screenshot, ScreenshotOperations - - # 获取设备ID - device_id = os.environ.get("DEVICE_ID", "") - if not device_id: - print("[WARN] 未设置 DEVICE_ID 环境变量,尝试从配置读取...") - try: - import json - import path_config - config_path = path_config.get_config_path() - # 尝试从常见位置读取 - device_id = "MB75DEFQZHZTGMLJ" # 从日志中看到的设备ID - except Exception: - print("[ERROR] 无法获取设备ID,请设置环境变量 DEVICE_ID") - return False - - print(f"设备ID: {device_id}") - print(f"测试模式: 直接截图 (use_service=False)") - print() - - # 测试1: 使用默认参数(应该是 False) - print("测试 1.1: 使用默认参数调用 get_screenshot()") - start = time.time() - try: - result = get_screenshot(device_id) - elapsed = (time.time() - start) * 1000 - status = "[OK] 成功" if result else "[FAIL] 失败" - print(f" 结果: {status}, 耗时: {elapsed:.1f}ms") - except Exception as e: - elapsed = (time.time() - start) * 1000 - print(f" [ERROR] 异常: {e}, 耗时: {elapsed:.1f}ms") - - print() - - # 测试2: 显式传递 use_service=False - print("测试 1.2: 显式传递 use_service=False") - start = time.time() - try: - ops = ScreenshotOperations() - result = ops.get_screenshot(device_id, use_service=False) - elapsed = (time.time() - start) * 1000 - status = "[OK] 成功" if result else "[FAIL] 失败" - print(f" 结果: {status}, 耗时: {elapsed:.1f}ms") - except Exception as e: - elapsed = (time.time() - start) * 1000 - print(f" [ERROR] 异常: {e}, 耗时: {elapsed:.1f}ms") - - print() - - # 测试3: 验证不会使用服务模式 - print("测试 1.3: 验证不会调用后台服务") - try: - from operations.screenshot_service import ScreenshotService - service = ScreenshotService.get_instance() - service_running_before = service.is_running() - print(f" 服务运行状态(测试前): {service_running_before}") - - # 调用截图(应该不会启动服务) - get_screenshot(device_id) - - service_running_after = service.is_running() - print(f" 服务运行状态(测试后): {service_running_after}") - - if not service_running_after: - print(" [OK] 确认:服务未被启动(符合预期)") - else: - print(" [WARN] 警告:服务仍在运行(可能之前已启动)") - except Exception as e: - print(f" [WARN] 无法检查服务状态: {e}") - - return True - - except ImportError as e: - print(f"[ERROR] 导入错误: {e}") - return False - except Exception as e: - print(f"[ERROR] 测试失败: {e}") - import traceback - traceback.print_exc() - return False - - -def test_underlying_operations(): - """测试 Underlying_Operations 模块""" - print() - print("=" * 80) - print("测试 2: Underlying_Operations.get_screenshot()") - print("=" * 80) - - try: - from Underlying_Operations import underlying_operations - - device_id = os.environ.get("DEVICE_ID", "MB75DEFQZHZTGMLJ") - print(f"设备ID: {device_id}") - print() - - ops = underlying_operations() - - # 测试默认参数 - print("测试 2.1: 使用默认参数(应该是 False)") - start = time.time() - try: - result = ops.get_screenshot(device_id) - elapsed = (time.time() - start) * 1000 - status = "[OK] 成功" if result else "[FAIL] 失败" - print(f" 结果: {status}, 耗时: {elapsed:.1f}ms") - - if elapsed > 2000: - print(f" [WARN] 警告:耗时较长 ({elapsed:.1f}ms),但应该 < 2秒") - elif elapsed > 6000: - print(f" [ERROR] 错误:耗时过长 ({elapsed:.1f}ms),可能仍在使用服务模式") - else: - print(f" [OK] 正常:耗时在预期范围内") - except Exception as e: - elapsed = (time.time() - start) * 1000 - print(f" [ERROR] 异常: {e}, 耗时: {elapsed:.1f}ms") - import traceback - traceback.print_exc() - - return True - - except Exception as e: - print(f"[ERROR] 测试失败: {e}") - import traceback - traceback.print_exc() - return False - - -def test_stage_diagnosis_screenshot(): - """测试阶段诊断中的截图调用""" - print() - print("=" * 80) - print("测试 3: 阶段诊断截图调用(模拟)") - print("=" * 80) - - try: - # 检查关键文件中的调用 - import inspect - from douyin.core import douyin_fudai - - # 查找 get_screenshot 调用 - source_file = inspect.getfile(douyin_fudai.fudai_analyse) - with open(source_file, 'r', encoding='utf-8') as f: - content = f.read() - - # 检查是否包含 use_service=False - if 'get_screenshot(self.device_id, use_service=False)' in content: - print(" [OK] 确认:douyin_fudai.py 中已使用 use_service=False") - elif 'get_screenshot(self.device_id)' in content: - print(" [WARN] 警告:douyin_fudai.py 中未显式传递 use_service=False") - print(" (但默认值应该是 False)") - else: - print(" [WARN] 未找到 get_screenshot 调用") - - return True - - except Exception as e: - print(f" [WARN] 无法检查: {e}") - return False - - -def check_logs_for_timeouts(): - """检查日志中是否有超时记录""" - print() - print("=" * 80) - print("测试 4: 检查日志中的超时记录") - print("=" * 80) - - try: - import glob - log_files = glob.glob("logs/unified_*.log") - if not log_files: - print(" [WARN] 未找到日志文件") - return True - - # 读取最新的日志文件 - latest_log = max(log_files, key=os.path.getmtime) - print(f" 检查日志文件: {latest_log}") - - timeout_count = 0 - screenshot_count = 0 - - with open(latest_log, 'r', encoding='utf-8', errors='ignore') as f: - # 只读取最后 1000 行 - lines = f.readlines()[-1000:] - - for line in lines: - if '[TIMEOUT] 等待超时' in line or '等待超时' in line: - timeout_count += 1 - if 'screenshot_elapsed_seconds' in line: - screenshot_count += 1 - # 提取耗时 - try: - import re - match = re.search(r'screenshot_elapsed_seconds=([\d.]+)', line) - if match: - elapsed = float(match.group(1)) - if elapsed > 5.0: - print(f" [WARN] 发现耗时过长的截图: {elapsed:.2f}秒") - except Exception: - pass - - print(f" 超时记录数(最近1000行): {timeout_count}") - print(f" 截图耗时记录数: {screenshot_count}") - - if timeout_count == 0: - print(" [OK] 未发现超时记录(符合预期)") - else: - print(f" [WARN] 发现 {timeout_count} 条超时记录") - - return True - - except Exception as e: - print(f" [WARN] 无法检查日志: {e}") - return False - - -def main(): - """主测试函数""" - print() - print("=" * 80) - print("直接截图功能测试") - print("=" * 80) - print(f"测试时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}") - print() - - results = [] - - # 测试1: screenshot_operations - results.append(("screenshot_operations", test_screenshot_operations())) - - # 测试2: underlying_operations - results.append(("underlying_operations", test_underlying_operations())) - - # 测试3: 代码检查 - results.append(("stage_diagnosis", test_stage_diagnosis_screenshot())) - - # 测试4: 日志检查 - results.append(("log_check", check_logs_for_timeouts())) - - # 总结 - print() - print("=" * 80) - print("测试总结") - print("=" * 80) - - passed = sum(1 for _, result in results if result) - total = len(results) - - for name, result in results: - status = "[PASS] 通过" if result else "[FAIL] 失败" - print(f" {name}: {status}") - - print() - print(f"总计: {passed}/{total} 测试通过") - - if passed == total: - print("[SUCCESS] 所有测试通过!") - return 0 - else: - print("[WARN] 部分测试未通过,请检查上述输出") - return 1 - - -if __name__ == "__main__": - exit(main()) diff --git a/deleted_tests_backup/test_disable_streaming_screenshots.py b/deleted_tests_backup/test_disable_streaming_screenshots.py deleted file mode 100644 index 6e6ec60..0000000 --- a/deleted_tests_backup/test_disable_streaming_screenshots.py +++ /dev/null @@ -1,118 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- -""" -测试禁用流式截图功能 -""" - -import time -import sys -import os - -# 添加项目根目录到路径 -sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) - -def test_disable_streaming_screenshots(): - """测试禁用流式截图功能""" - - print("测试禁用流式截图功能...") - print("=" * 50) - - # 1. 检查截图服务的默认行为 - print("\n1. 检查截图服务默认行为") - - from operations.screenshot_operations import ScreenshotOperations - ops = ScreenshotOperations() - - # 检查get_screenshot方法的默认参数 - import inspect - sig = inspect.signature(ops.get_screenshot) - params = sig.parameters - - use_service_default = params['use_service'].default - print(f"get_screenshot use_service 默认值: {use_service_default}") - - if use_service_default == False: - print("[OK] 默认禁用后台服务,使用直接截图") - else: - print("[FAIL] 仍然使用后台服务") - - # 2. 模拟截图调用 - print("\n2. 模拟截图调用测试") - - # 创建模拟的analyser - class MockAnalyser: - def __init__(self): - self.device_id = "test_device_123" - self.operation = ops - - def _log_kv(self, key, value, **kwargs): - print(f"[{key}] {value}") - - analyser = MockAnalyser() - - # 测试调用(这里不会实际执行截图,因为没有真实设备) - try: - # 这里只是检查方法调用路径,不会实际截图 - print("截图方法调用路径:") - print("- get_screenshot(use_service=False) -> 直接截图") - print("- 跳过后台服务检查") - print("[OK] 确认使用直接截图模式") - except Exception as e: - print(f"[ERROR] 测试失败: {e}") - - # 3. 检查后台服务状态 - print("\n3. 检查后台服务状态") - - try: - from operations.screenshot_service import ScreenshotService - service = ScreenshotService.get_instance() - - if service.is_running(): - print("[WARN] 后台服务正在运行(可能有其他地方启动)") - else: - print("[OK] 后台服务未运行") - - print(f"服务状态: running={service.is_running()}") - print(f"当前设备ID: {service._current_device_id}") - - except Exception as e: - print(f"检查服务状态失败: {e}") - - # 4. 验证修改的文件 - print("\n4. 验证修改的文件") - - modified_files = [ - "operations/screenshot_operations.py", - "douyin/core/douyin_fudai.py", - "douyin/core/douyin_main_flow.py" - ] - - for file_path in modified_files: - if os.path.exists(file_path): - print(f"[OK] {file_path} 存在") - else: - print(f"[ERROR] {file_path} 不存在") - - # 5. 总结 - print("\n5. 修改总结") - print("-" * 30) - print("已完成的修改:") - print("1. [DONE] 修改 get_screenshot() 默认 use_service=False") - print("2. [DONE] 移除 douyin_fudai.py 中的后台服务启动") - print("3. [DONE] 移除 douyin_main_flow.py 中的后台服务启动") - print("4. [DONE] 所有截图调用将使用直接截图模式") - - print("\n预期效果:") - print("- 每次截图都会直接调用ADB命令") - print("- 不再有后台截图服务持续运行") - print("- 截图响应时间会变慢,但更稳定") - print("- 消除了后台服务的复杂性") - - return True - -if __name__ == "__main__": - success = test_disable_streaming_screenshots() - print(f"\n{'='*50}") - print(f"测试结果: {'PASS' if success else 'FAIL'}") - print("流式截图已暂时禁用,使用直接截图模式") - print(f"{'='*50}") diff --git a/deleted_tests_backup/test_loop_screenshot_stop.py b/deleted_tests_backup/test_loop_screenshot_stop.py deleted file mode 100644 index ece2c48..0000000 --- a/deleted_tests_backup/test_loop_screenshot_stop.py +++ /dev/null @@ -1,95 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- -""" -测试循环期间停止后台截图服务的功能 -""" - -import time -import sys -import os - -# 添加项目根目录到路径 -sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) - -def test_loop_screenshot_service_stop(): - """测试循环期间后台截图服务是否被停止""" - - print("测试循环期间停止后台截图服务功能...") - - # 1. 启动截图服务(模拟正常启动) - print("\n1. 启动后台截图服务...") - from operations.screenshot_service import ScreenshotService - service = ScreenshotService.get_instance() - - device_id = "emulator-5554" - - if not service.is_running(): - service.start(device_id, interval=1.5) - time.sleep(0.5) - - if service.is_running(): - print("[OK] 后台截图服务已启动") - else: - print("[FAIL] 后台截图服务启动失败") - return False - - # 2. 模拟fudai_choujiang的逻辑 - print("\n2. 模拟循环开始前的截图服务停止...") - - # 这部分代码模拟fudai_choujiang中的逻辑 - screenshot_service_stopped = False - try: - service_inner = ScreenshotService.get_instance() - if service_inner.is_running(): - service_inner.stop() - screenshot_service_stopped = True - print("[OK] 循环开始前停止后台截图服务") - except Exception as e: - print(f"[FAIL] 停止后台截图服务失败: {e}") - return False - - # 检查服务是否已停止 - if service.is_running(): - print("[FAIL] 后台截图服务仍然在运行") - return False - else: - print("[OK] 后台截图服务已停止") - - # 3. 模拟循环中的操作(这里不会触发截图服务) - print("\n3. 模拟循环中的阶段诊断...") - time.sleep(2) # 模拟一些操作 - - # 4. 模拟程序结束(atexit会重新启动服务) - print("\n4. 模拟程序结束,检查服务重新启动...") - - # 手动调用atexit函数(在实际运行中会自动调用) - if screenshot_service_stopped: - try: - service_restart = ScreenshotService.get_instance() - if device_id: - service_restart.start(device_id, interval=1.5) - print("[OK] 循环结束后重新启动后台截图服务") - except Exception as e: - print(f"[FAIL] 重新启动后台截图服务失败: {e}") - return False - - # 检查服务是否重新启动 - time.sleep(0.5) - if service.is_running(): - print("[OK] 后台截图服务已重新启动") - success = True - else: - print("[FAIL] 后台截图服务未重新启动") - success = False - - # 5. 清理 - if service.is_running(): - service.stop() - print("\n5. 测试完成,停止后台截图服务") - - print(f"\n测试结果: {'PASS' if success else 'FAIL'}") - return success - -if __name__ == "__main__": - success = test_loop_screenshot_service_stop() - sys.exit(0 if success else 1) diff --git a/deleted_tests_backup/test_ocr_multithread.py b/deleted_tests_backup/test_ocr_multithread.py deleted file mode 100644 index 3e347dd..0000000 --- a/deleted_tests_backup/test_ocr_multithread.py +++ /dev/null @@ -1,100 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- -""" -测试OCR多线程优化效果 -""" - -import os -import sys -import time -from concurrent.futures import ThreadPoolExecutor -from operations.screenshot_service import get_optimal_workers - -def test_ocr_multithread_config(): - """测试OCR多线程配置""" - print("=" * 70) - print("OCR多线程配置检查") - print("=" * 70) - print() - - # 1. CPU信息 - cpu_count = os.cpu_count() or 4 - print(f"1. CPU信息") - print("-" * 70) - print(f" CPU核心数: {cpu_count}") - print() - - # 2. OMP线程数配置 - print("2. OMP线程数配置(底层库多线程)") - print("-" * 70) - omp_threads = os.environ.get("OMP_NUM_THREADS", "未设置") - print(f" OMP_NUM_THREADS: {omp_threads}") - - # 计算推荐的OMP线程数 - optimal_omp_threads = min(4, max(2, cpu_count // 3)) - print(f" 推荐值: {optimal_omp_threads} (根据CPU核心数动态计算)") - print() - - # 3. 线程池大小配置 - print("3. 线程池大小配置(任务级多线程)") - print("-" * 70) - optimal_workers = get_optimal_workers() - print(f" 推荐线程池大小: {optimal_workers} (根据CPU核心数动态计算)") - print(f" 说明: 用于并行执行多个OCR任务") - print() - - # 4. 优化说明 - print("4. 优化说明") - print("-" * 70) - print(" 优化前:") - print(" - OMP_NUM_THREADS=1 (限制底层库只能使用1个线程)") - print(" - 线程池大小固定为6 (未充分利用多核CPU)") - print() - print(" 优化后:") - print(f" - OMP_NUM_THREADS={optimal_omp_threads} (允许底层库使用多线程)") - print(f" - 线程池大小={optimal_workers} (根据CPU核心数动态调整)") - print() - print(" 预期效果:") - print(" - 底层库(PaddleOCR/EasyOCR)可以并行处理图像") - print(" - 多个OCR任务可以并行执行") - print(" - 充分利用多核CPU资源") - print(" - 预计性能提升: 30-50% (取决于任务数量和CPU核心数)") - print() - - # 5. 验证OCR引擎是否已初始化 - print("5. OCR引擎状态") - print("-" * 70) - try: - from operations.ocr_operations import _ocr_processor - if _ocr_processor.ocr is not None: - print(f" OCR引擎: {_ocr_processor.ocr_type}") - print(" 状态: 已初始化") - else: - print(" 状态: 未初始化(延迟初始化)") - except Exception as e: - print(f" 状态: 检查失败 - {e}") - print() - - print("=" * 70) - print("检查完成") - print("=" * 70) - -def test_ocr_performance(): - """测试OCR性能(需要实际运行OCR)""" - print() - print("=" * 70) - print("OCR性能测试") - print("=" * 70) - print() - print("提示: 此测试需要实际运行OCR任务") - print(" 可以在实际使用中观察OCR耗时变化") - print() - print("测试方法:") - print("1. 运行程序,观察OCR任务的耗时") - print("2. 对比优化前后的性能差异") - print("3. 查看日志中的OCR耗时信息") - print() - -if __name__ == "__main__": - test_ocr_multithread_config() - test_ocr_performance() diff --git a/deleted_tests_backup/test_ocr_multithread_performance.py b/deleted_tests_backup/test_ocr_multithread_performance.py deleted file mode 100644 index 87fbd71..0000000 --- a/deleted_tests_backup/test_ocr_multithread_performance.py +++ /dev/null @@ -1,325 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- -""" -OCR多线程性能测试 -测试优化前后的性能差异 -""" - -import os -import sys -import time -from concurrent.futures import ThreadPoolExecutor -from PIL import Image -import numpy as np -import tempfile - -def test_ocr_single(image_path, ocr_processor, omp_threads): - """测试单个OCR任务的性能""" - old_omp = os.environ.get('OMP_NUM_THREADS', '4') - os.environ['OMP_NUM_THREADS'] = str(omp_threads) - - try: - img = Image.open(image_path) - if img.mode != 'RGB': - img = img.convert('RGB') - img_np = np.array(img) - - start_time = time.time() - - if ocr_processor.ocr_type == "easyocr": - result = ocr_processor.ocr.readtext(img_np) - else: - result = ocr_processor.ocr.ocr(img_np) - - elapsed = (time.time() - start_time) * 1000 - - # 提取文本 - from Underlying_Operations import underlying_operations - ops = underlying_operations() - text = ops.extract_ocr_content(result) - - return { - 'time': elapsed, - 'text': text, - 'success': True - } - except Exception as e: - return { - 'time': 0, - 'text': '', - 'success': False, - 'error': str(e) - } - finally: - os.environ['OMP_NUM_THREADS'] = old_omp - -def test_ocr_config(image_path, config_name, omp_threads, num_iterations=5): - """测试特定配置下的OCR性能""" - print(f"\n{'='*70}") - print(f"测试配置: {config_name}") - print(f"{'='*70}") - print(f"OMP_NUM_THREADS: {omp_threads}") - print(f"迭代次数: {num_iterations}") - print() - - # 初始化OCR - try: - from operations.ocr_operations import OCRProcessor - ocr_processor = OCRProcessor() - ensure_ocr = getattr(ocr_processor, '_ensure_ocr') - ensure_ocr() - - if ocr_processor.ocr is None: - print("[ERROR] OCR引擎未初始化") - return None - - print(f"OCR引擎: {ocr_processor.ocr_type}") - print() - except Exception as e: - print(f"[ERROR] OCR引擎初始化失败: {e}") - return None - - # 执行多次测试 - times = [] - for i in range(num_iterations): - result = test_ocr_single(image_path, ocr_processor, omp_threads) - if result['success']: - times.append(result['time']) - print(f" 第{i+1}次: {result['time']:.0f}ms - {result['text'][:30] if result['text'] else '(空)'}") - else: - print(f" 第{i+1}次: 失败 - {result.get('error', 'Unknown')}") - - if not times: - print("[ERROR] 所有测试都失败") - return None - - avg_time = sum(times) / len(times) - min_time = min(times) - max_time = max(times) - - print() - print(f"平均耗时: {avg_time:.0f}ms") - print(f"最快: {min_time:.0f}ms") - print(f"最慢: {max_time:.0f}ms") - print() - - return { - 'avg_time': avg_time, - 'min_time': min_time, - 'max_time': max_time, - 'times': times - } - -def test_parallel_ocr(image_path, config_name, omp_threads, max_workers, num_tasks=6): - """测试并行OCR性能""" - print(f"\n{'='*70}") - print(f"测试配置: {config_name}") - print(f"{'='*70}") - print(f"OMP_NUM_THREADS: {omp_threads}") - print(f"线程池大小: {max_workers}") - print(f"任务数量: {num_tasks}") - print() - - # 设置OMP线程数 - old_omp = os.environ.get('OMP_NUM_THREADS', '4') - os.environ['OMP_NUM_THREADS'] = str(omp_threads) - - # 初始化OCR - try: - from operations.ocr_operations import OCRProcessor - ocr_processor = OCRProcessor() - ensure_ocr = getattr(ocr_processor, '_ensure_ocr') - ensure_ocr() - - if ocr_processor.ocr is None: - print("[ERROR] OCR引擎未初始化") - os.environ['OMP_NUM_THREADS'] = old_omp - return None - - print(f"OCR引擎: {ocr_processor.ocr_type}") - print() - except Exception as e: - print(f"[ERROR] OCR引擎初始化失败: {e}") - os.environ['OMP_NUM_THREADS'] = old_omp - return None - - # 创建多个相同的任务(使用同一张图片) - def ocr_task(): - return test_ocr_single(image_path, ocr_processor, omp_threads) - - # 并行执行 - print("开始并行OCR识别...") - start_time = time.time() - results = [] - - with ThreadPoolExecutor(max_workers=max_workers) as executor: - futures = [executor.submit(ocr_task) for _ in range(num_tasks)] - - for i, future in enumerate(futures): - try: - result = future.result(timeout=60) - results.append(result) - if result['success']: - print(f" 任务{i+1}: {result['time']:.0f}ms - {result['text'][:20] if result['text'] else '(空)'}") - else: - print(f" 任务{i+1}: 失败 - {result.get('error', 'Unknown')}") - except Exception as e: - print(f" 任务{i+1}: 异常 - {e}") - results.append({'success': False, 'error': str(e)}) - - total_elapsed = (time.time() - start_time) * 1000 - - # 恢复OMP设置 - os.environ['OMP_NUM_THREADS'] = old_omp - - # 统计结果 - successful_results = [r for r in results if r.get('success', False)] - efficiency = 0 # 初始化效率变量 - if successful_results: - task_times = [r['time'] for r in successful_results] - total_task_time = sum(task_times) - avg_task_time = total_task_time / len(task_times) - - print() - print(f"总耗时: {total_elapsed:.0f}ms ({total_elapsed/1000:.2f}秒)") - print(f"任务总耗时: {total_task_time:.0f}ms") - print(f"平均每个任务: {avg_task_time:.0f}ms") - if total_task_time > 0: - efficiency = total_task_time / total_elapsed - print(f"并行效率: {efficiency:.2f}x (理想值: {num_tasks:.1f}x)") - print() - - return { - 'total_time': total_elapsed, - 'avg_task_time': avg_task_time, - 'task_times': task_times, - 'efficiency': efficiency if total_task_time > 0 else 0 - } - else: - print("[ERROR] 所有任务都失败") - return None - -def main(): - """主函数""" - # 默认图片路径 - default_image = "pic/debug_captures/2025-12-31/20251231_152118_ocr_福袋倒计时_分钟_识别后_(390,1110)-(480,1230)_模式_1_文本长度_2_数字_14_类型_digits_平均置信度_0.9991_最小置信度_0.9991_最大置信度_0.9991_置信度项数_1_有完整截图_是_有坐标_是.png" - - if len(sys.argv) > 1: - image_path = sys.argv[1] - else: - image_path = default_image - - if not os.path.isabs(image_path): - project_root = os.path.dirname(os.path.abspath(__file__)) - image_path = os.path.join(project_root, image_path) - - if not os.path.exists(image_path): - print(f"[ERROR] 图片文件不存在: {image_path}") - return - - print("=" * 70) - print("OCR多线程性能测试") - print("=" * 70) - print() - print(f"测试图片: {os.path.basename(image_path)}") - - # 获取系统信息 - cpu_count = os.cpu_count() or 4 - print(f"CPU核心数: {cpu_count}") - try: - from operations.screenshot_service import get_optimal_workers - optimal_workers = get_optimal_workers() - print(f"推荐线程池大小: {optimal_workers}") - except (ImportError, AttributeError): - optimal_workers = 6 - print() - - # 测试1: 单次OCR - 优化前 - print("\n" + "="*70) - print("测试1: 单次OCR性能对比") - print("="*70) - result_single_old = test_ocr_config( - image_path, - "优化前 (OMP_NUM_THREADS=1)", - omp_threads=1, - num_iterations=5 - ) - - time.sleep(1) - - # 测试2: 单次OCR - 优化后 - result_single_new = test_ocr_config( - image_path, - "优化后 (OMP_NUM_THREADS=6)", - omp_threads=6, - num_iterations=5 - ) - - # 测试3: 并行OCR - 优化前 - print("\n" + "="*70) - print("测试2: 并行OCR性能对比") - print("="*70) - result_parallel_old = test_parallel_ocr( - image_path, - "优化前 (OMP=1, workers=6)", - omp_threads=1, - max_workers=6, - num_tasks=6 - ) - - time.sleep(1) - - # 测试4: 并行OCR - 优化后 - result_parallel_new = test_parallel_ocr( - image_path, - "优化后 (OMP=6, workers=10)", - omp_threads=6, - max_workers=optimal_workers, - num_tasks=6 - ) - - # 性能对比总结 - print() - print("=" * 70) - print("性能对比总结") - print("=" * 70) - print() - - if result_single_old and result_single_new: - old_avg = result_single_old['avg_time'] - new_avg = result_single_new['avg_time'] - speedup = old_avg / new_avg if new_avg > 0 else 0 - - print("单次OCR性能:") - print(f" 优化前平均: {old_avg:.0f}ms") - print(f" 优化后平均: {new_avg:.0f}ms") - print(f" 性能提升: {speedup:.2f}x") - print(f" 时间减少: {(1 - new_avg/old_avg)*100:.1f}%") - print() - - if result_parallel_old and result_parallel_new: - old_total = result_parallel_old['total_time'] - new_total = result_parallel_new['total_time'] - speedup = old_total / new_total if new_total > 0 else 0 - - print("并行OCR性能:") - print(f" 优化前总耗时: {old_total:.0f}ms ({old_total/1000:.2f}秒)") - print(f" 优化后总耗时: {new_total:.0f}ms ({new_total/1000:.2f}秒)") - print(f" 性能提升: {speedup:.2f}x") - print(f" 时间减少: {(1 - new_total/old_total)*100:.1f}%") - print() - - old_avg = result_parallel_old['avg_task_time'] - new_avg = result_parallel_new['avg_task_time'] - print("单任务性能:") - print(f" 优化前平均任务: {old_avg:.0f}ms") - print(f" 优化后平均任务: {new_avg:.0f}ms") - print(f" 单任务性能提升: {old_avg/new_avg:.2f}x" if new_avg > 0 else " 单任务性能提升: N/A") - print() - - print("=" * 70) - print("测试完成") - print("=" * 70) - -if __name__ == "__main__": - main() diff --git a/deleted_tests_backup/test_ocr_speed.py b/deleted_tests_backup/test_ocr_speed.py deleted file mode 100644 index ba01984..0000000 --- a/deleted_tests_backup/test_ocr_speed.py +++ /dev/null @@ -1,196 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- -""" -测试OCR识别速度 -""" - -import os -import sys -import time -from pathlib import Path - -def test_ocr_speed(image_path: str, iterations: int = 5): - """测试OCR识别速度""" - print("=" * 70) - print("OCR速度测试") - print("=" * 70) - print() - - # 检查图片文件是否存在 - if not os.path.exists(image_path): - print(f"[ERROR] 图片文件不存在: {image_path}") - return - - file_size = os.path.getsize(image_path) / 1024 # KB - print(f"测试图片: {image_path}") - print(f"文件大小: {file_size:.2f} KB") - print(f"测试次数: {iterations} 次") - print() - - # 获取图片信息 - try: - from PIL import Image - img = Image.open(image_path) - width, height = img.size - print(f"图片尺寸: {width}x{height}") - print(f"图片模式: {img.mode}") - - # 计算图片像素数 - total_pixels = width * height - print(f"总像素数: {total_pixels:,}") - - # 判断图片大小 - if total_pixels > 2000000: # 大于200万像素 - print(f" [提示] 图片较大,OCR处理可能需要较长时间") - print(f" 建议: 先裁剪到目标区域(如200x200)再OCR,可以显著提升速度") - print() - except Exception as e: - print(f"[WARNING] 无法读取图片信息: {e}") - print() - - # 检查OCR引擎 - print("1. OCR引擎信息") - print("-" * 70) - try: - from operations.ocr_operations import _ocr_processor - _ocr_processor._ensure_ocr() - - if _ocr_processor.ocr is None: - print("[ERROR] OCR引擎未初始化") - return - - print(f" OCR引擎: {_ocr_processor.ocr_type}") - print(f" OMP线程数: {os.environ.get('OMP_NUM_THREADS', '未设置')}") - print() - except Exception as e: - print(f"[ERROR] OCR引擎检查失败: {e}") - return - - # 测试OCR速度 - print("2. OCR速度测试") - print("-" * 70) - - times = [] - results = [] - - for i in range(iterations): - try: - # 导入OCR操作 - from Underlying_Operations import underlying_operations - ops = underlying_operations() - - # 确保OCR已初始化 - ops._ensure_ocr() - - # 执行OCR(使用analyse_pic_digits,因为这是数字识别) - start_time = time.time() - - # 直接使用图片路径进行OCR - # 注意:如果图片很大,OCR会处理整个图片,这可能会很慢 - # 建议使用预处理后的小图片进行测试 - result = ops.analyse_pic_digits( - "测试图片", - change_color=1, # 数字识别模式 - pic_path=image_path - ) - - # 记录各个阶段的耗时(如果可能) - # 注意:analyse_pic_digits 内部会进行图像预处理和OCR识别 - - elapsed = (time.time() - start_time) * 1000 # 转换为毫秒 - times.append(elapsed) - results.append(result) - - print(f" 第 {i+1} 次: {elapsed:.0f}ms, 识别结果: {result[:50] if result else '(空)'}") - - except Exception as e: - print(f" 第 {i+1} 次: 失败 - {e}") - import traceback - traceback.print_exc() - - print() - - # 统计结果 - if times: - print("3. 性能统计") - print("-" * 70) - avg_time = sum(times) / len(times) - min_time = min(times) - max_time = max(times) - - print(f" 平均耗时: {avg_time:.0f}ms") - print(f" 最快耗时: {min_time:.0f}ms") - print(f" 最慢耗时: {max_time:.0f}ms") - print(f" 耗时范围: {max_time - min_time:.0f}ms") - print() - - # 识别结果统计 - print("4. 识别结果") - print("-" * 70) - unique_results = list(set(results)) - for i, result in enumerate(unique_results, 1): - count = results.count(result) - print(f" 结果 {i}: {result[:100] if result else '(空)'} (出现 {count} 次)") - print() - - # 性能评估 - print("5. 性能评估") - print("-" * 70) - if avg_time < 500: - print(" [优秀] OCR速度很快 (< 500ms)") - elif avg_time < 1000: - print(" [良好] OCR速度良好 (500-1000ms)") - elif avg_time < 2000: - print(" [一般] OCR速度一般 (1000-2000ms)") - elif avg_time < 5000: - print(" [较慢] OCR速度较慢 (2000-5000ms)") - else: - print(" [很慢] OCR速度很慢 (> 5000ms)") - print(" 提示: 如果图片很大,建议先裁剪到目标区域再OCR") - - # 多线程效果评估 - cpu_count = os.cpu_count() or 4 - omp_threads = int(os.environ.get('OMP_NUM_THREADS', '1')) - print(f" CPU核心数: {cpu_count}") - print(f" OMP线程数: {omp_threads}") - if omp_threads > 1: - print(f" [优化] 已启用底层多线程 (OMP_NUM_THREADS={omp_threads})") - else: - print(f" [提示] 未启用底层多线程,可以优化") - print() - else: - print("[ERROR] 所有测试都失败了") - - print("=" * 70) - print("测试完成") - print("=" * 70) - -def main(): - """主函数""" - # 默认图片路径 - default_image = "pic/debug_captures/2025-12-31/20251231_152116_ocr_福袋倒计时_秒_预处理后_(480,1110)-(570,1230)_模式_1_尺寸_270x360_来源_福袋倒计时_秒.png_有完整截图_是_有坐标_是_类型_digits.png" - - # 从命令行参数获取图片路径 - if len(sys.argv) > 1: - image_path = sys.argv[1] - else: - image_path = default_image - - # 获取测试次数 - iterations = 5 - if len(sys.argv) > 2: - try: - iterations = int(sys.argv[2]) - except: - pass - - # 转换为绝对路径 - if not os.path.isabs(image_path): - # 尝试相对于项目根目录 - project_root = os.path.dirname(os.path.abspath(__file__)) - image_path = os.path.join(project_root, image_path) - - test_ocr_speed(image_path, iterations) - -if __name__ == "__main__": - main() diff --git a/deleted_tests_backup/test_ocr_speed_optimized.py b/deleted_tests_backup/test_ocr_speed_optimized.py deleted file mode 100644 index b64c5e5..0000000 --- a/deleted_tests_backup/test_ocr_speed_optimized.py +++ /dev/null @@ -1,212 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- -""" -测试OCR识别速度(优化版 - 支持裁剪区域测试) -""" - -import os -import sys -import time -from typing import Optional -from PIL import Image - -def test_ocr_on_region(image_path: str, region: Optional[tuple] = None, iterations: int = 5): - """在指定区域测试OCR速度""" - print("=" * 70) - print("OCR速度测试(区域优化版)") - print("=" * 70) - print() - - # 检查图片文件是否存在 - if not os.path.exists(image_path): - print(f"[ERROR] 图片文件不存在: {image_path}") - return - - # 加载图片 - try: - img = Image.open(image_path) - original_size = img.size - print(f"原始图片: {image_path}") - print(f"原始尺寸: {original_size[0]}x{original_size[1]}") - print(f"原始像素数: {original_size[0] * original_size[1]:,}") - print() - except Exception as e: - print(f"[ERROR] 无法加载图片: {e}") - return - - # 如果指定了区域,裁剪图片 - if region: - x1, y1, x2, y2 = region - img = img.crop((x1, y1, x2, y2)) - print(f"裁剪区域: ({x1}, {y1}) - ({x2}, {y2})") - print(f"裁剪后尺寸: {img.size[0]}x{img.size[1]}") - print(f"裁剪后像素数: {img.size[0] * img.size[1]:,}") - print() - - # 保存裁剪后的临时图片 - import tempfile - temp_dir = tempfile.gettempdir() - temp_image = os.path.join(temp_dir, "ocr_test_crop.png") - img.save(temp_image) - test_image_path = temp_image - else: - test_image_path = image_path - - # 测试OCR速度 - print("开始OCR速度测试...") - print("-" * 70) - - try: - from operations.ocr_operations import _ocr_processor - _ocr_processor._ensure_ocr() - - if _ocr_processor.ocr is None: - print("[ERROR] OCR引擎未初始化") - return - - print(f"OCR引擎: {_ocr_processor.ocr_type}") - print(f"OMP线程数: {os.environ.get('OMP_NUM_THREADS', '未设置')}") - print() - except Exception as e: - print(f"[ERROR] OCR引擎检查失败: {e}") - return - - # 执行多次测试 - times = [] - results = [] - - for i in range(iterations): - try: - import numpy as np - - # 准备图像 - if img.mode != 'RGB': - img_rgb = img.convert('RGB') - else: - img_rgb = img - - # 转换为numpy数组 - img_np = np.array(img_rgb) - - # 执行OCR - start_time = time.time() - - if _ocr_processor.ocr_type == "easyocr": - result = _ocr_processor.ocr.readtext(img_np) - else: - result = _ocr_processor.ocr.ocr(img_np) - - elapsed = (time.time() - start_time) * 1000 # 转换为毫秒 - - # 提取文本 - from Underlying_Operations import underlying_operations - ops = underlying_operations() - text = ops.extract_ocr_content(result) - - times.append(elapsed) - results.append(text) - - print(f"第 {i+1} 次: {elapsed:.0f}ms, 识别结果: {text[:50] if text else '(空)'}") - - except Exception as e: - print(f"第 {i+1} 次: 失败 - {e}") - import traceback - traceback.print_exc() - - print() - - # 统计结果 - if times: - print("性能统计") - print("-" * 70) - avg_time = sum(times) / len(times) - min_time = min(times) - max_time = max(times) - - print(f"平均耗时: {avg_time:.0f}ms ({avg_time/1000:.2f}秒)") - print(f"最快耗时: {min_time:.0f}ms ({min_time/1000:.2f}秒)") - print(f"最慢耗时: {max_time:.0f}ms ({max_time/1000:.2f}秒)") - print(f"耗时范围: {max_time - min_time:.0f}ms") - print() - - # 识别结果 - print("识别结果") - print("-" * 70) - unique_results = list(set(results)) - for i, result in enumerate(unique_results, 1): - count = results.count(result) - print(f"结果 {i}: {result[:100] if result else '(空)'} (出现 {count} 次)") - print() - - # 性能对比 - if region: - original_pixels = original_size[0] * original_size[1] - crop_pixels = img.size[0] * img.size[1] - reduction = (1 - crop_pixels / original_pixels) * 100 - print("性能对比") - print("-" * 70) - print(f"原始图片像素: {original_pixels:,}") - print(f"裁剪后像素: {crop_pixels:,}") - print(f"像素减少: {reduction:.1f}%") - print(f"预计原始图片OCR耗时: {avg_time * (original_pixels / crop_pixels):.0f}ms") - print() - - # 清理临时文件 - if region and os.path.exists(test_image_path): - try: - os.remove(test_image_path) - except: - pass - - print("=" * 70) - print("测试完成") - print("=" * 70) - -def main(): - """主函数""" - # 默认图片路径 - default_image = "pic/debug_captures/2025-12-31/20251231_152116_ocr_福袋倒计时_秒_预处理后_(480,1110)-(570,1230)_模式_1_尺寸_270x360_来源_福袋倒计时_秒.png_有完整截图_是_有坐标_是_类型_digits.png" - - # 从命令行参数获取图片路径 - if len(sys.argv) > 1: - image_path = sys.argv[1] - else: - image_path = default_image - - # 转换为绝对路径 - if not os.path.isabs(image_path): - project_root = os.path.dirname(os.path.abspath(__file__)) - image_path = os.path.join(project_root, image_path) - - # 从文件名提取区域信息(如果文件名包含坐标) - region = None - if "(480,1110)-(570,1230)" in image_path: - # 提取坐标 - region = (480, 1110, 570, 1230) - print("[INFO] 检测到文件名中的坐标信息,将使用裁剪区域进行测试") - print() - - # 获取测试次数 - iterations = 5 - if len(sys.argv) > 2: - try: - iterations = int(sys.argv[2]) - except: - pass - - # 先测试完整图片 - print("=" * 70) - print("测试1: 完整图片OCR") - print("=" * 70) - test_ocr_on_region(image_path, region=None, iterations=iterations) - - # 如果检测到区域信息,再测试裁剪区域 - if region: - print() - print("=" * 70) - print("测试2: 裁剪区域OCR(推荐)") - print("=" * 70) - test_ocr_on_region(image_path, region=region, iterations=iterations) - -if __name__ == "__main__": - main() diff --git a/deleted_tests_backup/test_parallel_ocr_performance.py b/deleted_tests_backup/test_parallel_ocr_performance.py deleted file mode 100644 index 724962d..0000000 --- a/deleted_tests_backup/test_parallel_ocr_performance.py +++ /dev/null @@ -1,383 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- -""" -测试并行OCR性能,对比优化前后的提升 -""" - -import os -import sys -import time -from concurrent.futures import ThreadPoolExecutor -from PIL import Image -import numpy as np - -def test_serial_ocr(image_path: str, num_tasks: int = 3): - """测试串行OCR(优化前的方式)""" - print("=" * 70) - print("测试1: 串行OCR(优化前)") - print("=" * 70) - print(f"任务数量: {num_tasks}") - print(f"OMP_NUM_THREADS: {os.environ.get('OMP_NUM_THREADS', '未设置')}") - print() - - # 加载图片 - try: - img = Image.open(image_path) - if img.mode != 'RGB': - img = img.convert('RGB') - img_np = np.array(img) - print(f"图片尺寸: {img.size[0]}x{img.size[1]}") - print() - except Exception as e: - print(f"[ERROR] 无法加载图片: {e}") - return None - - # 初始化OCR - try: - from operations.ocr_operations import _ocr_processor - _ocr_processor._ensure_ocr() - - if _ocr_processor.ocr is None: - print("[ERROR] OCR引擎未初始化") - return None - - print(f"OCR引擎: {_ocr_processor.ocr_type}") - print() - except Exception as e: - print(f"[ERROR] OCR引擎初始化失败: {e}") - return None - - # 串行执行OCR任务 - start_time = time.time() - results = [] - - print("开始串行OCR测试...") - for i in range(num_tasks): - task_start = time.time() - - try: - if _ocr_processor.ocr_type == "easyocr": - result = _ocr_processor.ocr.readtext(img_np) - else: - result = _ocr_processor.ocr.ocr(img_np) - - # 提取文本 - from Underlying_Operations import underlying_operations - ops = underlying_operations() - text = ops.extract_ocr_content(result) - - task_elapsed = (time.time() - task_start) * 1000 - results.append(text) - print(f" 任务 {i+1}/{num_tasks}: {task_elapsed:.0f}ms, 结果: {text[:30] if text else '(空)'}") - except Exception as e: - print(f" 任务 {i+1}/{num_tasks}: 失败 - {e}") - results.append(None) - - total_elapsed = (time.time() - start_time) * 1000 - - print() - print(f"总耗时: {total_elapsed:.0f}ms ({total_elapsed/1000:.2f}秒)") - print(f"平均每个任务: {total_elapsed/num_tasks:.0f}ms") - print() - - return total_elapsed - -def test_parallel_ocr(image_path: str, num_tasks: int = 3): - """测试并行OCR(优化后的方式)""" - print("=" * 70) - print("测试2: 并行OCR(优化后)") - print("=" * 70) - print(f"任务数量: {num_tasks}") - print(f"OMP_NUM_THREADS: {os.environ.get('OMP_NUM_THREADS', '未设置')}") - - # 获取最优线程数 - try: - from operations.screenshot_service import get_optimal_workers - optimal_workers = get_optimal_workers() - print(f"线程池大小: {optimal_workers}") - except: - optimal_workers = 6 - print(f"线程池大小: {optimal_workers} (默认)") - print() - - # 加载图片 - try: - img = Image.open(image_path) - if img.mode != 'RGB': - img = img.convert('RGB') - img_np = np.array(img) - print(f"图片尺寸: {img.size[0]}x{img.size[1]}") - print() - except Exception as e: - print(f"[ERROR] 无法加载图片: {e}") - return None - - # 初始化OCR - try: - from operations.ocr_operations import _ocr_processor - _ocr_processor._ensure_ocr() - - if _ocr_processor.ocr is None: - print("[ERROR] OCR引擎未初始化") - return None - - print(f"OCR引擎: {_ocr_processor.ocr_type}") - print() - except Exception as e: - print(f"[ERROR] OCR引擎初始化失败: {e}") - return None - - # 定义OCR任务函数 - def ocr_task(task_id): - """单个OCR任务""" - try: - if _ocr_processor.ocr_type == "easyocr": - result = _ocr_processor.ocr.readtext(img_np) - else: - result = _ocr_processor.ocr.ocr(img_np) - - # 提取文本 - from Underlying_Operations import underlying_operations - ops = underlying_operations() - text = ops.extract_ocr_content(result) - - return text - except Exception as e: - print(f" 任务 {task_id} 失败: {e}") - return None - - # 并行执行OCR任务 - start_time = time.time() - results = [] - - print("开始并行OCR测试...") - with ThreadPoolExecutor(max_workers=optimal_workers) as executor: - # 提交所有任务 - futures = {executor.submit(ocr_task, i+1): i+1 for i in range(num_tasks)} - - # 收集结果 - task_times = {} - for future in futures: - task_id = futures[future] - task_start = time.time() - try: - result = future.result() - task_elapsed = (time.time() - task_start) * 1000 - task_times[task_id] = task_elapsed - results.append(result) - print(f" 任务 {task_id}/{num_tasks}: {task_elapsed:.0f}ms, 结果: {result[:30] if result else '(空)'}") - except Exception as e: - print(f" 任务 {task_id}/{num_tasks}: 失败 - {e}") - results.append(None) - - total_elapsed = (time.time() - start_time) * 1000 - - print() - print(f"总耗时: {total_elapsed:.0f}ms ({total_elapsed/1000:.2f}秒)") - if task_times: - avg_task_time = sum(task_times.values()) / len(task_times) - print(f"平均每个任务: {avg_task_time:.0f}ms") - print(f"并行效率: {avg_task_time * num_tasks / total_elapsed:.1f}x (理想情况下应该接近{num_tasks}x)") - print() - - return total_elapsed - -def test_parallel_ocr_old_config(image_path: str, num_tasks: int = 3): - """测试并行OCR(使用旧的配置:OMP_NUM_THREADS=1, max_workers=6)""" - print("=" * 70) - print("测试3: 并行OCR(旧配置:OMP=1, 线程池=6)") - print("=" * 70) - print(f"任务数量: {num_tasks}") - - # 临时设置旧配置 - old_omp = os.environ.get('OMP_NUM_THREADS', '4') - os.environ['OMP_NUM_THREADS'] = '1' - print(f"OMP_NUM_THREADS: 1 (临时设置)") - print(f"线程池大小: 6 (固定)") - print() - - # 加载图片 - try: - img = Image.open(image_path) - if img.mode != 'RGB': - img = img.convert('RGB') - img_np = np.array(img) - print(f"图片尺寸: {img.size[0]}x{img.size[1]}") - print() - except Exception as e: - print(f"[ERROR] 无法加载图片: {e}") - os.environ['OMP_NUM_THREADS'] = old_omp - return None - - # 重新初始化OCR(使用新的OMP设置) - try: - from operations.ocr_operations import _ocr_processor - # 注意:OCR引擎已经初始化,OMP设置可能不会立即生效 - # 但我们可以测试线程池的效果 - print(f"OCR引擎: {_ocr_processor.ocr_type}") - print("注意: OMP设置可能需要重启程序才能完全生效") - print() - except Exception as e: - print(f"[ERROR] OCR引擎检查失败: {e}") - os.environ['OMP_NUM_THREADS'] = old_omp - return None - - # 定义OCR任务函数 - def ocr_task(task_id): - """单个OCR任务""" - try: - if _ocr_processor.ocr_type == "easyocr": - result = _ocr_processor.ocr.readtext(img_np) - else: - result = _ocr_processor.ocr.ocr(img_np) - - # 提取文本 - from Underlying_Operations import underlying_operations - ops = underlying_operations() - text = ops.extract_ocr_content(result) - - return text - except Exception as e: - print(f" 任务 {task_id} 失败: {e}") - return None - - # 并行执行OCR任务(使用旧的线程池大小) - start_time = time.time() - results = [] - - print("开始并行OCR测试(旧配置)...") - with ThreadPoolExecutor(max_workers=6) as executor: - # 提交所有任务 - futures = {executor.submit(ocr_task, i+1): i+1 for i in range(num_tasks)} - - # 收集结果 - task_times = {} - for future in futures: - task_id = futures[future] - task_start = time.time() - try: - result = future.result() - task_elapsed = (time.time() - task_start) * 1000 - task_times[task_id] = task_elapsed - results.append(result) - print(f" 任务 {task_id}/{num_tasks}: {task_elapsed:.0f}ms, 结果: {result[:30] if result else '(空)'}") - except Exception as e: - print(f" 任务 {task_id}/{num_tasks}: 失败 - {e}") - results.append(None) - - total_elapsed = (time.time() - start_time) * 1000 - - # 恢复OMP设置 - os.environ['OMP_NUM_THREADS'] = old_omp - - print() - print(f"总耗时: {total_elapsed:.0f}ms ({total_elapsed/1000:.2f}秒)") - if task_times: - avg_task_time = sum(task_times.values()) / len(task_times) - print(f"平均每个任务: {avg_task_time:.0f}ms") - print() - - return total_elapsed - -def main(): - """主函数""" - # 默认图片路径 - default_image = "pic/debug_captures/2025-12-31/20251231_152116_ocr_福袋倒计时_秒_预处理后_(480,1110)-(570,1230)_模式_1_尺寸_270x360_来源_福袋倒计时_秒.png_有完整截图_是_有坐标_是_类型_digits.png" - - # 从命令行参数获取图片路径 - if len(sys.argv) > 1: - image_path = sys.argv[1] - else: - image_path = default_image - - # 转换为绝对路径 - if not os.path.isabs(image_path): - project_root = os.path.dirname(os.path.abspath(__file__)) - image_path = os.path.join(project_root, image_path) - - # 检查文件是否存在 - if not os.path.exists(image_path): - print(f"[ERROR] 图片文件不存在: {image_path}") - return - - # 获取任务数量(默认3个,减少测试时间) - num_tasks = 3 - if len(sys.argv) > 2: - try: - num_tasks = int(sys.argv[2]) - except: - pass - - print("=" * 70) - print("并行OCR性能对比测试") - print("=" * 70) - print() - print(f"测试图片: {os.path.basename(image_path)}") - print(f"任务数量: {num_tasks}") - print() - - # 获取当前配置信息 - cpu_count = os.cpu_count() or 4 - omp_threads = os.environ.get('OMP_NUM_THREADS', '未设置') - print(f"系统信息:") - print(f" CPU核心数: {cpu_count}") - print(f" OMP_NUM_THREADS: {omp_threads}") - try: - from operations.screenshot_service import get_optimal_workers - optimal_workers = get_optimal_workers() - print(f" 推荐线程池大小: {optimal_workers}") - except: - pass - print() - print() - - # 测试1: 串行OCR - serial_time = test_serial_ocr(image_path, num_tasks) - - print() - print() - - # 测试2: 并行OCR(优化后) - parallel_time = test_parallel_ocr(image_path, num_tasks) - - print() - print() - - # 测试3: 并行OCR(旧配置) - parallel_old_time = test_parallel_ocr_old_config(image_path, num_tasks) - - # 性能对比总结 - print() - print("=" * 70) - print("性能对比总结") - print("=" * 70) - print() - - if serial_time and parallel_time: - speedup = serial_time / parallel_time - print(f"串行OCR: {serial_time:.0f}ms ({serial_time/1000:.2f}秒)") - print(f"并行OCR: {parallel_time:.0f}ms ({parallel_time/1000:.2f}秒)") - print(f"性能提升: {speedup:.2f}x ({(1 - parallel_time/serial_time)*100:.1f}% 时间减少)") - print() - - if parallel_old_time and parallel_time: - speedup_vs_old = parallel_old_time / parallel_time - print(f"旧配置并行: {parallel_old_time:.0f}ms ({parallel_old_time/1000:.2f}秒)") - print(f"新配置并行: {parallel_time:.0f}ms ({parallel_time/1000:.2f}秒)") - print(f"配置优化提升: {speedup_vs_old:.2f}x ({(1 - parallel_time/parallel_old_time)*100:.1f}% 时间减少)") - print() - - if serial_time and parallel_time and parallel_old_time: - total_speedup = serial_time / parallel_time - print(f"总体优化效果:") - print(f" 串行 → 并行: {serial_time/parallel_time:.2f}x") - print(f" 旧配置 → 新配置: {parallel_old_time/parallel_time:.2f}x") - print(f" 串行 → 新配置: {total_speedup:.2f}x") - print() - - print("=" * 70) - print("测试完成") - print("=" * 70) - -if __name__ == "__main__": - main() diff --git a/deleted_tests_backup/test_parallel_ocr_quick.py b/deleted_tests_backup/test_parallel_ocr_quick.py deleted file mode 100644 index 28f6e8d..0000000 --- a/deleted_tests_backup/test_parallel_ocr_quick.py +++ /dev/null @@ -1,141 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- -""" -快速测试并行OCR性能(只测试并行,不测试串行) -""" - -import os -import sys -import time -from concurrent.futures import ThreadPoolExecutor -from PIL import Image -import numpy as np - -def test_parallel_ocr_quick(image_path: str, num_tasks: int = 3): - """快速测试并行OCR""" - print("=" * 70) - print("并行OCR性能测试(快速版)") - print("=" * 70) - print() - - # 检查文件 - if not os.path.exists(image_path): - print(f"[ERROR] 图片文件不存在: {image_path}") - return None - - # 加载图片 - try: - img = Image.open(image_path) - if img.mode != 'RGB': - img = img.convert('RGB') - img_np = np.array(img) - print(f"图片: {os.path.basename(image_path)}") - print(f"尺寸: {img.size[0]}x{img.size[1]}") - print(f"任务数: {num_tasks}") - print() - except Exception as e: - print(f"[ERROR] 无法加载图片: {e}") - return None - - # 初始化OCR - try: - from operations.ocr_operations import _ocr_processor - _ocr_processor._ensure_ocr() - - if _ocr_processor.ocr is None: - print("[ERROR] OCR引擎未初始化") - return None - - print(f"OCR引擎: {_ocr_processor.ocr_type}") - print(f"OMP_NUM_THREADS: {os.environ.get('OMP_NUM_THREADS', '未设置')}") - - from operations.screenshot_service import get_optimal_workers - optimal_workers = get_optimal_workers() - print(f"线程池大小: {optimal_workers}") - print() - except Exception as e: - print(f"[ERROR] OCR引擎初始化失败: {e}") - return None - - # 定义OCR任务 - def ocr_task(task_id): - try: - if _ocr_processor.ocr_type == "easyocr": - result = _ocr_processor.ocr.readtext(img_np) - else: - result = _ocr_processor.ocr.ocr(img_np) - - from Underlying_Operations import underlying_operations - ops = underlying_operations() - text = ops.extract_ocr_content(result) - return text - except Exception as e: - return f"错误: {e}" - - # 执行并行OCR - print("开始并行OCR测试...") - start_time = time.time() - - with ThreadPoolExecutor(max_workers=optimal_workers) as executor: - futures = {executor.submit(ocr_task, i+1): i+1 for i in range(num_tasks)} - results = [] - - for future in futures: - task_id = futures[future] - try: - result = future.result() - results.append(result) - print(f" 任务 {task_id}: 完成, 结果: {result[:30] if result else '(空)'}") - except Exception as e: - print(f" 任务 {task_id}: 失败 - {e}") - results.append(None) - - total_elapsed = (time.time() - start_time) * 1000 - - print() - print(f"总耗时: {total_elapsed:.0f}ms ({total_elapsed/1000:.2f}秒)") - print(f"平均每个任务: {total_elapsed/num_tasks:.0f}ms") - print() - - # 估算串行时间(基于单次OCR耗时) - print("性能估算:") - print("-" * 70) - single_task_time = total_elapsed / num_tasks - estimated_serial_time = single_task_time * num_tasks - speedup = estimated_serial_time / total_elapsed if total_elapsed > 0 else 0 - - print(f"估算串行时间: {estimated_serial_time:.0f}ms ({estimated_serial_time/1000:.2f}秒)") - print(f"实际并行时间: {total_elapsed:.0f}ms ({total_elapsed/1000:.2f}秒)") - print(f"性能提升: {speedup:.2f}x (节省 {(1 - total_elapsed/estimated_serial_time)*100:.1f}% 时间)") - print() - - return total_elapsed - -def main(): - """主函数""" - default_image = "pic/debug_captures/2025-12-31/20251231_152116_ocr_福袋倒计时_秒_预处理后_(480,1110)-(570,1230)_模式_1_尺寸_270x360_来源_福袋倒计时_秒.png_有完整截图_是_有坐标_是_类型_digits.png" - - if len(sys.argv) > 1: - image_path = sys.argv[1] - else: - image_path = default_image - - if not os.path.isabs(image_path): - project_root = os.path.dirname(os.path.abspath(__file__)) - image_path = os.path.join(project_root, image_path) - - num_tasks = 3 - if len(sys.argv) > 2: - try: - num_tasks = int(sys.argv[2]) - except: - pass - - test_parallel_ocr_quick(image_path, num_tasks) - - print("=" * 70) - print("测试完成") - print("=" * 70) - -if __name__ == "__main__": - main() diff --git a/deleted_tests_backup/test_parallel_ocr_simple.py b/deleted_tests_backup/test_parallel_ocr_simple.py deleted file mode 100644 index d4d0aeb..0000000 --- a/deleted_tests_backup/test_parallel_ocr_simple.py +++ /dev/null @@ -1,206 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- -""" -简化版并行OCR性能对比测试 -直接使用提供的图片进行测试 -""" - -import os -import sys -import time -from concurrent.futures import ThreadPoolExecutor -from PIL import Image -import numpy as np - -def test_ocr_with_config(image_path, config_name, omp_threads, max_workers, num_tasks=6): - """使用指定配置测试OCR""" - print(f"\n{'='*70}") - print(f"{config_name}") - print(f"{'='*70}") - print(f"OMP_NUM_THREADS={omp_threads}, max_workers={max_workers}, 任务数={num_tasks}") - print() - - # 设置OMP - old_omp = os.environ.get('OMP_NUM_THREADS', '4') - os.environ['OMP_NUM_THREADS'] = str(omp_threads) - - # 加载图片 - try: - img = Image.open(image_path) - if img.mode != 'RGB': - img = img.convert('RGB') - img_np = np.array(img) - print(f"图片: {os.path.basename(image_path)}") - print(f"尺寸: {img.size[0]}x{img.size[1]} 像素") - print() - except Exception as e: - print(f"[ERROR] 无法加载图片: {e}") - os.environ['OMP_NUM_THREADS'] = old_omp - return None - - # 初始化OCR - try: - from operations.ocr_operations import OCRProcessor - ocr_processor = OCRProcessor() - ocr_processor._ensure_ocr() - - if ocr_processor.ocr is None: - print("[ERROR] OCR引擎未初始化") - os.environ['OMP_NUM_THREADS'] = old_omp - return None - - print(f"OCR引擎: {ocr_processor.ocr_type}") - print() - except Exception as e: - print(f"[ERROR] OCR引擎初始化失败: {e}") - os.environ['OMP_NUM_THREADS'] = old_omp - return None - - # OCR任务函数 - def ocr_task(task_id): - try: - start = time.time() - if ocr_processor.ocr_type == "easyocr": - result = ocr_processor.ocr.readtext(img_np) - else: - result = ocr_processor.ocr.ocr(img_np) - - from Underlying_Operations import underlying_operations - ops = underlying_operations() - text = ops.extract_ocr_content(result) - elapsed = (time.time() - start) * 1000 - return {'task_id': task_id, 'text': text, 'time': elapsed, 'success': True} - except Exception as e: - return {'task_id': task_id, 'text': '', 'time': 0, 'success': False, 'error': str(e)} - - # 执行并行OCR - print("开始并行OCR测试...") - start_time = time.time() - results = [] - - with ThreadPoolExecutor(max_workers=max_workers) as executor: - futures = {executor.submit(ocr_task, i+1): i+1 for i in range(num_tasks)} - - for future in futures: - try: - result = future.result() - results.append(result) - status = "✓" if result['success'] else "✗" - print(f" {status} 任务{result['task_id']}: {result['time']:.0f}ms, 结果: {result['text'][:20] if result['text'] else '(空)'}") - except Exception as e: - print(f" ✗ 任务失败: {e}") - - total_elapsed = (time.time() - start_time) * 1000 - - # 恢复OMP设置 - os.environ['OMP_NUM_THREADS'] = old_omp - - # 统计 - successful = [r for r in results if r['success']] - total_task_time = sum(r['time'] for r in successful) - avg_task_time = total_task_time / len(successful) if successful else 0 - - print() - print(f"总耗时: {total_elapsed:.0f}ms ({total_elapsed/1000:.2f}秒)") - print(f"任务总耗时: {total_task_time:.0f}ms") - print(f"平均每个任务: {avg_task_time:.0f}ms") - if total_task_time > 0: - efficiency = total_task_time / total_elapsed - print(f"并行效率: {efficiency:.2f}x (理想值: {num_tasks:.1f}x)") - print() - - return { - 'total_time': total_elapsed, - 'avg_task_time': avg_task_time, - 'config': config_name - } - -def main(): - """主函数""" - default_image = "pic/debug_captures/2025-12-31/20251231_152116_ocr_福袋倒计时_秒_预处理后_(480,1110)-(570,1230)_模式_1_尺寸_270x360_来源_福袋倒计时_秒.png_有完整截图_是_有坐标_是_类型_digits.png" - - if len(sys.argv) > 1: - image_path = sys.argv[1] - else: - image_path = default_image - - if not os.path.isabs(image_path): - project_root = os.path.dirname(os.path.abspath(__file__)) - image_path = os.path.join(project_root, image_path) - - if not os.path.exists(image_path): - print(f"[ERROR] 图片文件不存在: {image_path}") - return - - print("=" * 70) - print("并行OCR性能对比测试(简化版)") - print("=" * 70) - print() - print(f"测试图片: {os.path.basename(image_path)}") - - # 获取系统信息 - cpu_count = os.cpu_count() or 4 - print(f"CPU核心数: {cpu_count}") - try: - from operations.screenshot_service import get_optimal_workers - optimal_workers = get_optimal_workers() - print(f"推荐线程池大小: {optimal_workers}") - except: - optimal_workers = 6 - - num_tasks = 6 - print(f"测试任务数: {num_tasks}") - print() - - # 测试1: 优化前 - result_old = test_ocr_with_config( - image_path, - "优化前配置 (OMP=1, workers=6)", - omp_threads=1, - max_workers=6, - num_tasks=num_tasks - ) - - time.sleep(1) # 等待系统稳定 - - # 测试2: 优化后 - result_new = test_ocr_with_config( - image_path, - "优化后配置 (OMP=4, workers=10)", - omp_threads=4, - max_workers=optimal_workers, - num_tasks=num_tasks - ) - - # 对比结果 - print() - print("=" * 70) - print("性能对比总结") - print("=" * 70) - print() - - if result_old and result_new: - old_time = result_old['total_time'] - new_time = result_new['total_time'] - speedup = old_time / new_time if new_time > 0 else 0 - - print(f"优化前总耗时: {old_time:.0f}ms ({old_time/1000:.2f}秒)") - print(f"优化后总耗时: {new_time:.0f}ms ({new_time/1000:.2f}秒)") - print(f"性能提升: {speedup:.2f}x") - print(f"时间减少: {(1 - new_time/old_time)*100:.1f}%") - print() - - old_avg = result_old['avg_task_time'] - new_avg = result_new['avg_task_time'] - print(f"优化前平均任务耗时: {old_avg:.0f}ms") - print(f"优化后平均任务耗时: {new_avg:.0f}ms") - if new_avg > 0: - print(f"单任务性能提升: {old_avg/new_avg:.2f}x") - - print() - print("=" * 70) - print("测试完成") - print("=" * 70) - -if __name__ == "__main__": - main() diff --git a/deleted_tests_backup/test_real_parallel_ocr.py b/deleted_tests_backup/test_real_parallel_ocr.py deleted file mode 100644 index 381b34e..0000000 --- a/deleted_tests_backup/test_real_parallel_ocr.py +++ /dev/null @@ -1,376 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- -""" -真实场景并行OCR性能对比测试 -模拟实际使用中的多区域并行OCR识别 -""" - -import os -import sys -import time -from concurrent.futures import ThreadPoolExecutor -from PIL import Image -import numpy as np -import tempfile - -def create_test_regions(full_image_path): - """从完整截图中创建多个测试区域(模拟真实OCR场景)""" - try: - img = Image.open(full_image_path) - if img.mode != 'RGB': - img = img.convert('RGB') - - width, height = img.size - - # 判断是否是完整截图(1080x2400)还是裁剪区域 - is_full_screenshot = width >= 1000 and height >= 2000 - - if is_full_screenshot: - # 完整截图:创建多个测试区域 - # 模拟真实的OCR区域(基于福袋详情页的常见区域) - regions = { - "福袋倒计时_分钟": (400, 1050, 500, 1150), # 倒计时分钟区域 - "福袋倒计时_秒": (480, 1110, 570, 1230), # 倒计时秒区域(用户提供的区域) - "福袋内容": (50, 800, 500, 1000), # 福袋内容区域 - "福袋价值": (200, 1300, 400, 1400), # 价值区域 - "任务列表": (50, 600, 500, 750), # 任务列表区域 - "参与按钮": (300, 1500, 800, 1600), # 按钮状态区域 - } - else: - # 裁剪区域:使用当前图片作为测试区域之一,创建其他相似大小的区域 - # 从文件名提取坐标信息(如果存在) - coords_from_filename = None - if "(480,1110)-(570,1230)" in full_image_path: - coords_from_filename = (480, 1110, 570, 1230) - - # 创建多个相似大小的测试区域 - # 如果图片较小,创建多个相同大小的区域用于测试 - # 这些区域会在完整截图的合理位置 - regions = {} - - # 使用当前图片作为第一个区域 - if coords_from_filename: - x1, y1, x2, y2 = coords_from_filename - regions["福袋倒计时_秒"] = (x1, y1, x2, y2) - # 创建其他相邻区域 - offset_x = x2 - x1 - offset_y = y2 - y1 - regions["福袋倒计时_分钟"] = (x1 - offset_x - 20, y1, x1 - 20, y2) - regions["福袋内容"] = (x1, y1 - offset_y - 20, x2, y1 - 20) - regions["福袋价值"] = (x1, y2 + 20, x2, y2 + offset_y + 20) - else: - # 如果无法从文件名提取坐标,使用图片本身作为唯一测试区域 - # 创建多个相同大小的区域(通过复制当前图片) - regions["区域1"] = (0, 0, width, height) - regions["区域2"] = (0, 0, width, height) # 相同区域,用于测试并行 - regions["区域3"] = (0, 0, width, height) # 相同区域,用于测试并行 - regions["区域4"] = (0, 0, width, height) # 相同区域,用于测试并行 - regions["区域5"] = (0, 0, width, height) # 相同区域,用于测试并行 - regions["区域6"] = (0, 0, width, height) # 相同区域,用于测试并行 - - # 裁剪并保存区域图片 - region_images = {} - temp_dir = tempfile.gettempdir() - - for name, (x1, y1, x2, y2) in regions.items(): - try: - # 确保坐标在图片范围内 - x1 = max(0, min(x1, img.width)) - y1 = max(0, min(y1, img.height)) - x2 = max(x1, min(x2, img.width)) - y2 = max(y1, min(y2, img.height)) - - if x2 > x1 and y2 > y1: - crop_img = img.crop((x1, y1, x2, y2)) - temp_path = os.path.join(temp_dir, f"ocr_test_{name}.png") - crop_img.save(temp_path) - region_images[name] = { - 'path': temp_path, - 'size': crop_img.size, - 'coords': (x1, y1, x2, y2) - } - except (ValueError, OSError, IOError) as e: - print(f"[WARNING] 无法裁剪区域 {name}: {e}") - - return region_images, img.size - except (OSError, IOError, ValueError) as e: - print(f"[ERROR] 无法处理图片: {e}") - return {}, None - -def ocr_single_region(image_path, region_name, ocr_processor): - """对单个区域进行OCR识别""" - try: - img = Image.open(image_path) - if img.mode != 'RGB': - img = img.convert('RGB') - img_np = np.array(img) - - start_time = time.time() - - if ocr_processor.ocr_type == "easyocr": - result = ocr_processor.ocr.readtext(img_np) - else: - result = ocr_processor.ocr.ocr(img_np) - - elapsed = (time.time() - start_time) * 1000 - - # 提取文本 - from Underlying_Operations import underlying_operations - ops = underlying_operations() - text = ops.extract_ocr_content(result) - - return { - 'region': region_name, - 'text': text, - 'time': elapsed, - 'success': True - } - except Exception as e: - import traceback - error_msg = f"{type(e).__name__}: {str(e)}" - print(f" [DEBUG] {region_name} OCR错误: {error_msg}") - return { - 'region': region_name, - 'text': '', - 'time': 0, - 'success': False, - 'error': error_msg - } - -def test_parallel_ocr_real(region_images, config_name, omp_threads, max_workers): - """测试真实场景的并行OCR""" - print(f"\n{'='*70}") - print(f"测试: {config_name}") - print(f"{'='*70}") - print(f"配置: OMP_NUM_THREADS={omp_threads}, max_workers={max_workers}") - print(f"区域数量: {len(region_images)}") - print() - - # 设置OMP线程数 - old_omp = os.environ.get('OMP_NUM_THREADS', '4') - os.environ['OMP_NUM_THREADS'] = str(omp_threads) - - # 初始化OCR(使用新的OMP设置) - try: - from operations.ocr_operations import OCRProcessor - ocr_processor = OCRProcessor() - # 访问受保护方法以初始化OCR引擎(测试代码中需要) - # 使用 getattr 来避免 pylance 警告 - ensure_ocr = getattr(ocr_processor, '_ensure_ocr') - ensure_ocr() - - if ocr_processor.ocr is None: - print("[ERROR] OCR引擎未初始化") - os.environ['OMP_NUM_THREADS'] = old_omp - return None - - print(f"OCR引擎: {ocr_processor.ocr_type}") - print() - except (ImportError, AttributeError, RuntimeError) as e: - print(f"[ERROR] OCR引擎初始化失败: {e}") - os.environ['OMP_NUM_THREADS'] = old_omp - return None - - # 显示区域信息 - print("OCR区域信息:") - for name, info in region_images.items(): - print(f" {name}: {info['size'][0]}x{info['size'][1]} 像素, 坐标: {info['coords']}") - print() - - # 并行执行OCR - print("开始并行OCR识别...") - start_time = time.time() - results = [] - - def ocr_task(name, path): - return ocr_single_region(path, name, ocr_processor) - - with ThreadPoolExecutor(max_workers=max_workers) as executor: - # 提交所有任务 - futures = { - executor.submit(ocr_task, name, info['path']): name - for name, info in region_images.items() - } - - # 收集结果 - for future in futures: - region_name = futures[future] - try: - result = future.result(timeout=60) # 每个任务最多60秒 - results.append(result) - status = "✓" if result['success'] else "✗" - print(f" {status} {region_name}: {result['time']:.0f}ms, 结果: {result['text'][:30] if result['text'] else '(空)'}") - except TimeoutError: - print(f" ✗ {region_name}: 超时(>60秒)") - results.append({ - 'region': region_name, - 'text': '', - 'time': 0, - 'success': False, - 'error': 'Timeout' - }) - except (RuntimeError, AttributeError, KeyError) as e: - print(f" ✗ {region_name}: 失败 - {e}") - results.append({ - 'region': region_name, - 'text': '', - 'time': 0, - 'success': False, - 'error': str(e) - }) - - total_elapsed = (time.time() - start_time) * 1000 - - # 恢复OMP设置 - os.environ['OMP_NUM_THREADS'] = old_omp - - # 统计结果 - successful_results = [r for r in results if r['success']] - total_task_time = sum(r['time'] for r in successful_results) - avg_task_time = total_task_time / len(successful_results) if successful_results else 0 - - print() - print(f"总耗时: {total_elapsed:.0f}ms ({total_elapsed/1000:.2f}秒)") - print(f"任务总耗时: {total_task_time:.0f}ms") - print(f"平均每个任务: {avg_task_time:.0f}ms") - if total_task_time > 0: - efficiency = total_task_time / total_elapsed - print(f"并行效率: {efficiency:.2f}x (理想值: {len(region_images):.1f}x)") - print() - - return { - 'total_time': total_elapsed, - 'task_times': [r['time'] for r in successful_results], - 'results': results, - 'config': config_name - } - -def main(): - """主函数""" - # 默认图片路径(完整截图) - default_image = "pic/debug_captures/2025-12-31/20251231_152116_ocr_福袋倒计时_秒_预处理后_(480,1110)-(570,1230)_模式_1_尺寸_270x360_来源_福袋倒计时_秒.png_有完整截图_是_有坐标_是_类型_digits.png" - - if len(sys.argv) > 1: - image_path = sys.argv[1] - else: - image_path = default_image - - if not os.path.isabs(image_path): - project_root = os.path.dirname(os.path.abspath(__file__)) - image_path = os.path.join(project_root, image_path) - - if not os.path.exists(image_path): - print(f"[ERROR] 图片文件不存在: {image_path}") - return - - print("=" * 70) - print("真实场景并行OCR性能对比测试") - print("=" * 70) - print() - print(f"测试图片: {os.path.basename(image_path)}") - print() - - # 创建测试区域 - print("准备测试区域...") - region_images, full_size = create_test_regions(image_path) - - if not region_images: - print("[ERROR] 无法创建测试区域") - return - - if full_size is not None: - print(f"完整截图尺寸: {full_size[0]}x{full_size[1]}") - else: - print("完整截图尺寸: 未知") - print(f"成功创建 {len(region_images)} 个测试区域") - print() - - # 获取系统信息 - cpu_count = os.cpu_count() or 4 - print("系统信息:") - print(f" CPU核心数: {cpu_count}") - try: - from operations.screenshot_service import get_optimal_workers - optimal_workers = get_optimal_workers() - print(f" 推荐线程池大小: {optimal_workers}") - except (ImportError, AttributeError): - optimal_workers = 6 - print() - - # 测试1: 优化前配置(OMP=1, workers=6) - result_old = test_parallel_ocr_real( - region_images, - "优化前配置", - omp_threads=1, - max_workers=6 - ) - - # 等待一下,让系统稳定 - time.sleep(1) - - # 测试2: 优化后配置(OMP=4, workers=10) - result_new = test_parallel_ocr_real( - region_images, - "优化后配置", - omp_threads=4, - max_workers=optimal_workers - ) - - # 性能对比总结 - print() - print("=" * 70) - print("性能对比总结") - print("=" * 70) - print() - - if result_old and result_new: - old_time = result_old['total_time'] - new_time = result_new['total_time'] - speedup = old_time / new_time if new_time > 0 else 0 - - print(f"优化前总耗时: {old_time:.0f}ms ({old_time/1000:.2f}秒)") - print(f"优化后总耗时: {new_time:.0f}ms ({new_time/1000:.2f}秒)") - print(f"性能提升: {speedup:.2f}x") - print(f"时间减少: {(1 - new_time/old_time)*100:.1f}%") - print() - - # 详细对比 - if result_old['task_times'] and result_new['task_times']: - old_avg = sum(result_old['task_times']) / len(result_old['task_times']) - new_avg = sum(result_new['task_times']) / len(result_new['task_times']) - - print("任务级性能对比:") - print(f" 优化前平均任务耗时: {old_avg:.0f}ms") - print(f" 优化后平均任务耗时: {new_avg:.0f}ms") - print(f" 单任务性能提升: {old_avg/new_avg:.2f}x") - print() - - # 识别结果对比 - print("识别结果对比:") - for old_result in result_old['results']: - region_name = old_result['region'] - new_result = next((r for r in result_new['results'] if r['region'] == region_name), None) - - if old_result['success'] and new_result and new_result['success']: - old_text = old_result['text'][:20] if old_result['text'] else '(空)' - new_text = new_result['text'][:20] if new_result['text'] else '(空)' - match = "✓" if old_text == new_text else "✗" - print(f" {match} {region_name}: 结果一致") - - # 清理临时文件 - print() - print("清理临时文件...") - for info in region_images.values(): - try: - if os.path.exists(info['path']): - os.remove(info['path']) - except (OSError, PermissionError): - pass - - print() - print("=" * 70) - print("测试完成") - print("=" * 70) - -if __name__ == "__main__": - main() diff --git a/deleted_tests_backup/test_screenshot_error_handling.py b/deleted_tests_backup/test_screenshot_error_handling.py deleted file mode 100644 index 4dec6fe..0000000 --- a/deleted_tests_backup/test_screenshot_error_handling.py +++ /dev/null @@ -1,109 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- -""" -测试截图错误处理和重试机制 -""" - -import time -import sys -import os -import subprocess -from unittest.mock import patch, MagicMock - -# 添加项目根目录到路径 -sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) - -def test_adb_error_codes(): - """测试ADB错误码处理""" - - print("测试ADB错误码处理...") - print("=" * 50) - - from operations.screenshot_operations import ScreenshotOperations - ops = ScreenshotOperations() - - # 测试错误码4294967295的处理 - print("\n1. 测试错误码4294967295 (设备连接失败)") - - try: - # 模拟adb命令返回4294967295 - with patch('subprocess.Popen') as mock_popen: - mock_process = MagicMock() - mock_process.wait.return_value = 4294967295 - mock_process.stdout.read.side_effect = [b'', b''] # 空数据 - mock_popen.return_value = mock_process - - result = ops._get_screenshot_direct("test_device") - print(f"截图结果: {result}") - print("预期: False (截图失败)") - - except Exception as e: - print(f"异常: {e}") - - print("\n2. 测试正常截图流程") - # 这里不实际测试,因为需要真实的adb环境 - print("跳过实际截图测试(需要真实设备)") - -def test_retry_mechanism(): - """测试重试机制""" - - print("\n3. 测试重试机制模拟") - - # 模拟3次失败后成功的情况 - retry_count = 0 - max_retries = 3 - success = False - - for retry in range(max_retries): - retry_count = retry + 1 - print(f"重试 {retry_count}/{max_retries}") - - # 模拟前两次失败,第3次成功 - if retry < 2: - print(" 截图失败") - else: - print(" 截图成功") - success = True - break - - if retry < max_retries - 1: - print(" 等待1秒后重试...") - time.sleep(0.1) # 实际测试中用较短时间 - - print(f"\n最终结果: {'成功' if success else '失败'}") - print(f"重试次数: {retry_count}") - -def test_error_messages(): - """测试错误消息""" - - print("\n4. 测试错误消息") - - # 模拟不同的错误情况 - error_scenarios = [ - (4294967295, "设备连接失败"), - (1, "ADB命令错误"), - ("timeout", "操作超时"), - ("unknown", "未知错误") - ] - - for error_code, expected_desc in error_scenarios: - print(f"错误码 {error_code}: {expected_desc}") - - print("\n错误处理改进:") - print("- 添加了设备连接状态检查") - print("- 区分不同类型的ADB错误") - print("- 提供更详细的错误诊断信息") - print("- 实现截图失败时的重试机制") - -if __name__ == "__main__": - test_adb_error_codes() - test_retry_mechanism() - test_error_messages() - - print("\n" + "=" * 50) - print("截图错误处理测试完成") - print("修复内容:") - print("1. 添加ADB错误码4294967295的特殊处理") - print("2. 改进错误消息,提供更详细的诊断信息") - print("3. 在douyin_fudai.py中添加截图重试机制") - print("4. 当截图失败时会重试3次,而不是直接放弃") diff --git a/deleted_tests_backup/test_screenshot_file_wait.py b/deleted_tests_backup/test_screenshot_file_wait.py deleted file mode 100644 index a6385d5..0000000 --- a/deleted_tests_backup/test_screenshot_file_wait.py +++ /dev/null @@ -1,92 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- -""" -测试OCR期间截图文件等待逻辑 -""" - -import time -import sys -import os - -# 添加项目根目录到路径 -sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) - -def test_screenshot_file_wait(): - """测试截图文件等待逻辑""" - - print("测试OCR期间截图文件等待逻辑...") - - # 1. 模拟截图文件不存在的情况 - print("\n1. 模拟截图文件等待...") - - try: - import path_config - pic_path = path_config.get_screenshot_path() - except Exception: - pic_path = os.path.join(os.path.dirname(__file__), "pic", "screenshot.png") - - print(f"截图文件路径: {pic_path}") - - # 检查文件是否存在 - if os.path.exists(pic_path): - print("截图文件已存在") - file_size = os.path.getsize(pic_path) - print(f"文件大小: {file_size} bytes") - else: - print("截图文件不存在") - - # 2. 模拟等待逻辑 - print("\n2. 测试等待逻辑...") - - wait_start = time.time() - max_wait = 3.0 - found = False - - while time.time() - wait_start < max_wait: - if os.path.exists(pic_path): - file_size = os.path.getsize(pic_path) - if file_size >= 1024: # 至少1KB - found = True - break - time.sleep(0.1) - - wait_time = time.time() - wait_start - - if found: - print(".2f") - else: - print(f"等待{max_wait}秒后仍未找到有效截图文件") - - # 3. 分析问题 - print("\n3. 问题分析:") - - issues = [] - - if not found: - issues.append("[WARN] 等待期间未找到截图文件") - else: - issues.append("[OK] 成功等到截图文件") - - # 检查文件完整性 - if os.path.exists(pic_path): - try: - with open(pic_path, 'rb') as f: - header = f.read(4) - if header == b'\x89PNG': - issues.append("[OK] 截图文件格式正确 (PNG)") - else: - issues.append("[WARN] 截图文件格式异常") - except Exception as e: - issues.append(f"[ERROR] 读取截图文件失败: {e}") - else: - issues.append("[ERROR] 截图文件不存在") - - for issue in issues: - print(issue) - - print(f"\n测试结果: {'PASS' if found else 'FAIL'}") - return found - -if __name__ == "__main__": - success = test_screenshot_file_wait() - sys.exit(0 if success else 1) diff --git a/deleted_tests_backup/test_screenshot_performance.py b/deleted_tests_backup/test_screenshot_performance.py deleted file mode 100644 index 7dd849b..0000000 --- a/deleted_tests_backup/test_screenshot_performance.py +++ /dev/null @@ -1,127 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- -""" -截图性能测试脚本 - -测试截图性能并查看细粒度计时日志,验证优化效果。 -""" - -import os -import sys -import time -import logging - -# 配置日志 - 使用 DEBUG 级别以查看所有详细计时信息 -logging.basicConfig( - level=logging.DEBUG, - format='[%(levelname)s] %(message)s', - handlers=[ - logging.StreamHandler(sys.stdout) - ] -) - -def test_screenshot_with_timing(): - """测试截图并显示计时信息""" - try: - from Underlying_Operations import underlying_operations - except ImportError: - print("[错误] 无法导入 Underlying_Operations") - return - - # 获取设备ID - import subprocess - try: - result = subprocess.run(["adb", "devices"], capture_output=True, text=True, timeout=10) - if result.returncode != 0: - print("[错误] adb devices 失败") - return - - lines = result.stdout.strip().split("\n")[1:] - devices = [] - for line in lines: - if line.strip() and "\t" in line: - device_id = line.split("\t")[0].strip() - if device_id: - devices.append(device_id) - - if not devices: - print("[错误] 未找到连接的设备") - return - - device_id = devices[0] - print(f"[信息] 使用设备: {device_id}") - except Exception as e: - print(f"[错误] 获取设备ID失败: {e}") - return - - # 创建操作对象 - ops = underlying_operations() - - print("\n" + "="*60) - print("测试 1: 正常截图(启用所有验证和镜像)") - print("="*60) - - # 测试1: 正常截图 - start = time.time() - result1 = ops.get_screenshot(device_id, use_service=False) # 禁用服务,使用直接截图以便看到详细日志 - elapsed1 = (time.time() - start) * 1000.0 - print(f"\n[结果] 测试1完成: 成功={result1}, 总耗时={elapsed1:.1f}ms") - - time.sleep(1) - - print("\n" + "="*60) - print("测试 2: 禁用 PNG 验证 (SKIP_SCREENSHOT_VERIFY=1)") - print("="*60) - - # 测试2: 禁用验证 - os.environ["SKIP_SCREENSHOT_VERIFY"] = "1" - start = time.time() - result2 = ops.get_screenshot(device_id, use_service=False) - elapsed2 = (time.time() - start) * 1000.0 - print(f"\n[结果] 测试2完成: 成功={result2}, 总耗时={elapsed2:.1f}ms") - if elapsed1 > 0: - improvement = ((elapsed1 - elapsed2) / elapsed1) * 100 - print(f"[对比] 相比测试1: {improvement:+.1f}% ({elapsed1 - elapsed2:+.1f}ms)") - - time.sleep(1) - - print("\n" + "="*60) - print("测试 3: 禁用 PNG 验证 + 禁用镜像 (SKIP_SCREENSHOT_VERIFY=1, DISABLE_SCREENSHOT_MIRROR=1)") - print("="*60) - - # 测试3: 禁用验证和镜像 - os.environ["DISABLE_SCREENSHOT_MIRROR"] = "1" - start = time.time() - result3 = ops.get_screenshot(device_id, use_service=False) - elapsed3 = (time.time() - start) * 1000.0 - print(f"\n[结果] 测试3完成: 成功={result3}, 总耗时={elapsed3:.1f}ms") - if elapsed1 > 0: - improvement = ((elapsed1 - elapsed3) / elapsed1) * 100 - print(f"[对比] 相比测试1: {improvement:+.1f}% ({elapsed1 - elapsed3:+.1f}ms)") - - # 恢复环境变量 - if "SKIP_SCREENSHOT_VERIFY" in os.environ: - del os.environ["SKIP_SCREENSHOT_VERIFY"] - if "DISABLE_SCREENSHOT_MIRROR" in os.environ: - del os.environ["DISABLE_SCREENSHOT_MIRROR"] - - print("\n" + "="*60) - print("测试总结") - print("="*60) - print(f"测试1 (正常): {elapsed1:.1f}ms") - print(f"测试2 (禁用验证): {elapsed2:.1f}ms") - print(f"测试3 (禁用全部): {elapsed3:.1f}ms") - - if elapsed1 > 0 and elapsed3 > 0: - total_improvement = ((elapsed1 - elapsed3) / elapsed1) * 100 - print(f"\n最大优化效果: {total_improvement:+.1f}% ({elapsed1 - elapsed3:+.1f}ms)") - - if total_improvement > 10: - print("\n[建议] 如果优化效果明显,可以考虑:") - print(" 1. 在生产环境谨慎使用 SKIP_SCREENSHOT_VERIFY(可能影响数据完整性)") - print(" 2. 如果不需要外部镜像,可以设置 DISABLE_SCREENSHOT_MIRROR=1") - print(" 3. 检查外部镜像路径是否是网络路径(可能很慢)") - - -if __name__ == "__main__": - test_screenshot_with_timing() diff --git a/deleted_tests_backup/test_screenshot_reuse_optimization.py b/deleted_tests_backup/test_screenshot_reuse_optimization.py deleted file mode 100644 index a024004..0000000 --- a/deleted_tests_backup/test_screenshot_reuse_optimization.py +++ /dev/null @@ -1,129 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- -""" -测试点击福袋后阶段识别与OCR共用截图的优化 -""" - -import time -import sys -import os - -# 添加项目根目录到路径 -sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) - -def test_screenshot_reuse_optimization(): - """测试截图复用优化""" - - print("测试点击福袋后阶段识别与OCR共用截图优化...") - print("=" * 60) - - # 1. 模拟阶段诊断检测到任务数量 > 0 - print("\n1. 模拟阶段诊断检测到任务数量 > 0") - - class MockAnalyser: - def __init__(self): - self._fudai_detail_task_count_from_stage = 2 - self._screenshot_ready_for_ocr = False - - def _log_kv(self, key, value, **kwargs): - timestamp = time.strftime("%H:%M:%S", time.localtime()) - print(f"[{timestamp}] [{key}] {value}") - - analyser = MockAnalyser() - - # 模拟阶段诊断设置标志 - analyser._fudai_detail_task_count_from_stage = 2 - analyser._screenshot_ready_for_ocr = True - - print(f"任务数量: {analyser._fudai_detail_task_count_from_stage}") - print(f"截图复用标志: {analyser._screenshot_ready_for_ocr}") - - # 2. 模拟OCR开始时的逻辑 - print("\n2. 模拟OCR开始时的截图复用检查") - - # 检查是否可以复用阶段诊断时的截图 - screenshot_reused = False - try: - if getattr(analyser, '_screenshot_ready_for_ocr', False): - analyser._screenshot_ready_for_ocr = False # 清除标志,避免重复使用 - screenshot_reused = True - try: - analyser._log_kv( - "福袋详情", - "read_fudai_detail_once:复用阶段诊断截图", - reason="stage_diagnosed_task_count_gt_zero" - ) - except Exception: - pass - except Exception: - pass - - print(f"截图是否复用: {screenshot_reused}") - print(f"清除后的复用标志: {analyser._screenshot_ready_for_ocr}") - - # 3. 验证优化效果 - print("\n3. 验证优化效果") - - if screenshot_reused: - print("[OK] 成功复用阶段诊断时的截图,避免重复截图") - print("[OK] OCR跳过了get_screenshot调用") - print("[OK] 减少了一次不必要的截图操作") - success = True - else: - print("[FAIL] 截图复用失败,仍会进行新的截图") - success = False - - # 4. 模拟任务数量为0的情况 - print("\n4. 模拟任务数量为0的情况") - - analyser2 = MockAnalyser() - analyser2._fudai_detail_task_count_from_stage = 0 - analyser2._screenshot_ready_for_ocr = False # 任务数量为0时不应该设置复用标志 - - screenshot_reused_2 = False - try: - if getattr(analyser2, '_screenshot_ready_for_ocr', False): - analyser2._screenshot_ready_for_ocr = False - screenshot_reused_2 = True - except Exception: - pass - - print(f"任务数量: {analyser2._fudai_detail_task_count_from_stage}") - print(f"截图复用标志: {analyser2._screenshot_ready_for_ocr}") - print(f"截图是否复用: {screenshot_reused_2}") - - if not screenshot_reused_2: - print("[OK] 任务数量为0时正确不复用截图") - else: - print("[FAIL] 任务数量为0时错误复用截图") - success = False - - # 5. 分析性能提升 - print("\n5. 性能提升分析") - print("-" * 40) - - print("优化前:") - print("- 阶段诊断: 1次截图") - print("- OCR识别: 1次截图") - print("- 总计: 2次截图") - - print("\n优化后:") - print("- 阶段诊断: 1次截图") - print("- OCR识别: 复用阶段诊断截图") - print("- 总计: 1次截图") - - print("\n性能提升:") - print("- 减少50%的截图操作") - print("- 减少截图等待时间") - print("- 降低系统负载") - - print(f"\n{'='*60}") - print(f"测试结果: {'PASS' if success else 'FAIL'}") - print("优化目标: 当阶段诊断发现任务数量>0时,OCR复用同一张截图") - print(f"{'='*60}") - - return success - -if __name__ == "__main__": - success = test_screenshot_reuse_optimization() - sys.exit(0 if success else 1) diff --git a/deleted_tests_backup/test_screenshot_service_fix.py b/deleted_tests_backup/test_screenshot_service_fix.py deleted file mode 100644 index e0e5bbc..0000000 --- a/deleted_tests_backup/test_screenshot_service_fix.py +++ /dev/null @@ -1,59 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- -""" -测试截图服务修复效果 -""" - -import time -import sys -import os - -# 添加项目根目录到路径 -sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) - -def test_service_startup(): - """测试截图服务启动""" - print("测试截图服务启动修复...") - - # 模拟启动过程 - from douyin.core.douyin_fudai import fudai_analyse - - # 创建分析器实例 - analyser_instance = fudai_analyse() - - print("启动fudai_choujiang(只运行几秒钟测试服务启动)...") - - try: - # 设置设备ID(需要根据实际情况修改) - device_id = "emulator-5554" # 或者其他设备ID - - # 启动服务(只运行5秒) - analyser_instance.fudai_choujiang( - device_id=device_id, - wait_minutes=0, # 不等待,直接测试启动 - ) - - except KeyboardInterrupt: - print("测试被中断") - except Exception as e: - print(f"测试过程中出现异常: {e}") - import traceback - traceback.print_exc() - - # 检查服务状态 - print("\n检查服务状态:") - from operations.screenshot_service import ScreenshotService - service = ScreenshotService.get_instance() - info = service.get_cache_info() - - print(f"服务运行中: {info['running']}") - print(f"设备ID: {info['device_id']}") - print(f"后台线程: {'运行中' if hasattr(service, '_thread') and service._thread and service._thread.is_alive() else '未运行'}") - - # 停止服务 - if service.is_running(): - service.stop() - print("服务已停止") - -if __name__ == "__main__": - test_service_startup() diff --git a/deleted_tests_backup/test_screenshot_service_ocr_optimization.py b/deleted_tests_backup/test_screenshot_service_ocr_optimization.py deleted file mode 100644 index 844efcc..0000000 --- a/deleted_tests_backup/test_screenshot_service_ocr_optimization.py +++ /dev/null @@ -1,91 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- -""" -测试OCR时停止后台截图服务的优化效果 -""" - -import time -import sys -import os - -# 添加项目根目录到路径 -sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) - -def test_screenshot_service_ocr_integration(): - """测试截图服务与OCR集成的优化""" - print("=" * 80) - print("测试OCR时停止后台截图服务的优化") - print("=" * 80) - - # 1. 启动截图服务 - print("\n1. 启动后台截图服务...") - from operations.screenshot_service import ScreenshotService - service = ScreenshotService.get_instance() - - # 模拟设备ID(实际测试时需要替换为真实设备ID) - device_id = "emulator-5554" - - if service.is_running(): - print("服务已在运行") - else: - service.start(device_id, interval=1.5) - time.sleep(0.5) # 等待服务启动 - - if service.is_running(): - print("[OK] 后台截图服务启动成功") - else: - print("[FAIL] 后台截图服务启动失败") - return - - # 2. 模拟OCR密集操作 - print("\n2. 模拟OCR密集操作(调用read_fudai_detail_once)...") - - # 创建一个模拟的analyser对象 - class MockAnalyser: - def __init__(self): - self.device_id = device_id - self._fudai_detail_task_count_from_stage = 2 - - def _log_kv(self, key, value, **kwargs): - timestamp = time.strftime("%H:%M:%S", time.localtime()) - print(f"[{timestamp}] [{key}] {value}") - - analyser = MockAnalyser() - - # 调用read_fudai_detail_once,这应该会停止后台服务 - start_time = time.time() - try: - from fudai_detail_modules.fudai_detail_reader import read_fudai_detail_once - result = read_fudai_detail_once(analyser) - elapsed = time.time() - start_time - print(".2f") - print(f" 返回状态: {result[0]}") - except Exception as e: - elapsed = time.time() - start_time - print(".2f") - print(f" 错误: {e}") - - # 3. 检查服务是否重新启动 - print("\n3. 检查后台截图服务是否重新启动...") - time.sleep(0.5) # 等待服务重新启动 - - if service.is_running(): - print("[OK] 后台截图服务已自动重新启动") - info = service.get_cache_info() - print(f" 设备ID: {info['device_id']}") - print(f" 截图间隔: {info['interval']}秒") - print(f" 缓存有效期: {info['cache_valid_duration']}秒") - else: - print("[FAIL] 后台截图服务未重新启动") - - # 4. 清理 - if service.is_running(): - service.stop() - print("\n4. 测试完成,停止后台截图服务") - - print("\n" + "=" * 80) - print("测试完成") - print("=" * 80) - -if __name__ == "__main__": - test_screenshot_service_ocr_integration() diff --git a/deleted_tests_backup/test_screenshot_service_ocr_simple.py b/deleted_tests_backup/test_screenshot_service_ocr_simple.py deleted file mode 100644 index 49fb5a2..0000000 --- a/deleted_tests_backup/test_screenshot_service_ocr_simple.py +++ /dev/null @@ -1,84 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- -""" -简单测试:验证OCR时停止后台截图服务的代码修改 -""" - -import sys -import os - -# 添加项目根目录到路径 -sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) - -def test_code_modification(): - """测试代码修改是否正确""" - print("测试OCR时停止后台截图服务的代码修改...") - - # 1. 检查文件是否存在 - file_path = "fudai_detail_modules/fudai_detail_reader.py" - if not os.path.exists(file_path): - print(f"[FAIL] 文件不存在: {file_path}") - return False - - # 2. 检查代码中是否包含停止服务的逻辑 - with open(file_path, 'r', encoding='utf-8') as f: - content = f.read() - - # 检查关键代码片段 - checks = [ - ("停止服务代码", "service.stop()" in content), - ("启动服务代码", "service.start(" in content), - ("screenshot_service_stopped变量", "screenshot_service_stopped" in content), - ("finally块", "finally:" in content), - ("ScreenshotService导入", "from operations.screenshot_service import ScreenshotService" in content), - ] - - all_passed = True - for check_name, result in checks: - if result: - print(f"[OK] {check_name}") - else: - print(f"[FAIL] {check_name}") - all_passed = False - - # 3. 检查语法是否正确 - try: - compile(content, file_path, 'exec') - print("[OK] 语法检查通过") - except SyntaxError as e: - print(f"[FAIL] 语法错误: {e}") - all_passed = False - - # 4. 检查导入是否工作 - read_fudai_detail_once = None # 初始化变量 - try: - from fudai_detail_modules.fudai_detail_reader import read_fudai_detail_once - print("[OK] 模块导入成功") - except ImportError as e: - print(f"[FAIL] 模块导入失败: {e}") - all_passed = False - - # 5. 检查函数签名 - import inspect - try: - if read_fudai_detail_once is not None: - sig = inspect.signature(read_fudai_detail_once) - print(f"[OK] 函数签名: {sig}") - else: - print("[SKIP] 函数未导入,跳过签名检查") - except Exception as e: - print(f"[FAIL] 函数签名检查失败: {e}") - all_passed = False - - print() - if all_passed: - print("[SUCCESS] 代码修改验证通过!") - print("OCR密集操作时会自动停止后台截图服务,避免资源冲突。") - else: - print("[FAIL] 代码修改存在问题,请检查!") - - return all_passed - -if __name__ == "__main__": - success = test_code_modification() - sys.exit(0 if success else 1) diff --git a/deleted_tests_backup/test_screenshot_service_ocr_stop.py b/deleted_tests_backup/test_screenshot_service_ocr_stop.py deleted file mode 100644 index fe8e6f0..0000000 --- a/deleted_tests_backup/test_screenshot_service_ocr_stop.py +++ /dev/null @@ -1,113 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- -""" -测试OCR时停止后台截图服务的功能 -""" - -import time -import sys -import os - -# 添加项目根目录到路径 -sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) - -def test_screenshot_service_stop_during_ocr(): - """测试OCR过程中后台截图服务是否被停止""" - - print("测试OCR时停止后台截图服务功能...") - - # 1. 启动截图服务 - print("\n1. 启动后台截图服务...") - from operations.screenshot_service import ScreenshotService - service = ScreenshotService.get_instance() - - # 模拟设备ID - device_id = "emulator-5554" - - if not service.is_running(): - service.start(device_id, interval=1.5) - time.sleep(0.5) - - if service.is_running(): - print("[OK] 后台截图服务已启动") - else: - print("[FAIL] 后台截图服务启动失败") - return False - - # 记录初始状态 - initial_cache_duration = service._cache_valid_duration - print(f"初始缓存有效期: {initial_cache_duration}秒") - - # 2. 模拟OCR操作 - print("\n2. 模拟OCR操作...") - - # 创建模拟的analyser - class MockAnalyser: - def __init__(self): - self.device_id = device_id - self._fudai_detail_task_count_from_stage = 2 - - def _log_kv(self, key, value, **kwargs): - timestamp = time.strftime("%H:%M:%S", time.localtime()) - print(f"[{timestamp}] [{key}] {value}") - - analyser = MockAnalyser() - - # 记录开始时的服务状态 - service_running_before = service.is_running() - print(f"OCR开始前服务运行状态: {service_running_before}") - - start_time = time.time() # 初始化开始时间 - try: - # 调用read_fudai_detail_once - 这应该会停止后台服务 - from fudai_detail_modules.fudai_detail_reader import read_fudai_detail_once - - start_time = time.time() - result = read_fudai_detail_once(analyser) - elapsed = time.time() - start_time - - print(".2f") - print(f" 返回状态: {result[0]}") - - except Exception as e: - elapsed = time.time() - start_time - print(".2f") - print(f" 错误: {e}") - - # 3. 检查OCR后的服务状态 - print("\n3. 检查OCR后的服务状态...") - time.sleep(0.5) # 等待服务状态稳定 - - service_running_after = service.is_running() - final_cache_duration = service._cache_valid_duration - - print(f"OCR结束后服务运行状态: {service_running_after}") - print(f"最终缓存有效期: {final_cache_duration}秒") - - # 4. 验证结果 - success = True - - if service_running_before and not service_running_after: - print("[OK] 后台截图服务在OCR期间被正确停止") - elif not service_running_before: - print("[INFO] 后台截图服务在OCR开始前就已经停止") - else: - print("[FAIL] 后台截图服务在OCR期间没有被停止") - success = False - - if final_cache_duration == initial_cache_duration: - print("[OK] 缓存有效期在OCR结束后被正确恢复") - else: - print(f"[WARNING] 缓存有效期未恢复: 期望{initial_cache_duration}, 实际{final_cache_duration}") - - # 5. 清理 - if service.is_running(): - service.stop() - print("\n5. 测试完成,停止后台截图服务") - - print(f"\n测试结果: {'PASS' if success else 'FAIL'}") - return success - -if __name__ == "__main__": - success = test_screenshot_service_stop_during_ocr() - sys.exit(0 if success else 1) diff --git a/deleted_tests_backup/test_time_import_fix.py b/deleted_tests_backup/test_time_import_fix.py deleted file mode 100644 index 409f357..0000000 --- a/deleted_tests_backup/test_time_import_fix.py +++ /dev/null @@ -1,44 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- -""" -测试time导入修复 -""" - -import time - -def test_time_import(): - """测试time模块导入和使用""" - - print("测试time模块导入和使用...") - - # 模拟read_fudai_detail_once函数的开始部分 - try: - start_time = time.time() - print(".3f") - except Exception as e: - print(f"[ERROR] time.time()调用失败: {e}") - return False - - # 模拟函数内部的逻辑 - try: - task_count = 0 - # 清除标志 - # 模拟等待逻辑(之前有问题的部分) - wait_start = time.time() - print(".3f") - # 模拟循环 - while time.time() - wait_start < 0.1: # 很短的时间 - pass - - elapsed = time.time() - start_time - print(".3f") - except Exception as e: - print(f"[ERROR] 函数内部time使用失败: {e}") - return False - - print("[OK] time模块导入和使用正常") - return True - -if __name__ == "__main__": - success = test_time_import() - print(f"\n测试结果: {'PASS' if success else 'FAIL'}") -- 2.39.5 From 9bd10277b21b928b83209991f7dbedc0b506652c Mon Sep 17 00:00:00 2001 From: Codex Date: Wed, 20 May 2026 23:01:31 +0800 Subject: [PATCH 09/10] =?UTF-8?q?fix:=20=E5=89=8D=E7=AB=AF=20apiPost=20?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20CSRF=20token?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 配合后端 /api/csrf-token 接口获取并自动附加 X-CSRF-Token 请求头, 避免 CSRF 防护启用后所有 POST 请求被 403 拒绝。 Co-Authored-By: Claude Opus 4.5 --- webui/app.js | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/webui/app.js b/webui/app.js index c698720..938e120 100644 --- a/webui/app.js +++ b/webui/app.js @@ -688,10 +688,30 @@ async function apiGet(path) { return await res.json(); } +// CSRF token 缓存 +let _csrfToken = null; +async function getCsrfToken() { + if (_csrfToken) return _csrfToken; + try { + const res = await fetch("/api/csrf-token", { cache: "no-store" }); + if (res.ok) { + const data = await res.json(); + _csrfToken = data.token || ""; + } + } catch (e) { + console.warn("[CSRF] 获取 token 失败:", e); + } + return _csrfToken || ""; +} + async function apiPost(path, body) { + const token = await getCsrfToken(); const res = await fetch(path, { method: "POST", - headers: { "Content-Type": "application/json" }, + headers: { + "Content-Type": "application/json", + "X-CSRF-Token": token, + }, body: JSON.stringify(body || {}), }); const data = await res.json(); -- 2.39.5 From 92718a7cb4577c80dc411c79474fe4c843b57286 Mon Sep 17 00:00:00 2001 From: Codex Date: Wed, 20 May 2026 23:03:27 +0800 Subject: [PATCH 10/10] =?UTF-8?q?fix:=20douyin=5Ffudai=20=E5=85=B3?= =?UTF-8?q?=E9=94=AE=E8=B7=AF=E5=BE=84=E5=BC=82=E5=B8=B8=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - run_id 生成失败时记录 logging.debug - 日志系统初始化失败时记录 logging.warning,便于排查 Co-Authored-By: Claude Opus 4.5 --- douyin/core/douyin_fudai.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/douyin/core/douyin_fudai.py b/douyin/core/douyin_fudai.py index d90a4ab..3548b9d 100644 --- a/douyin/core/douyin_fudai.py +++ b/douyin/core/douyin_fudai.py @@ -71,7 +71,8 @@ class fudai_analyse: from datetime import datetime self._run_id = datetime.now().strftime("%Y%m%d%H%M%S") - except Exception: + except Exception as e: + logging.debug("生成 run_id 失败,回退到时间戳: %s", e) self._run_id = str(int(time.time())) self._log_seq = 0 self._log_kv_orig = None @@ -94,8 +95,8 @@ class fudai_analyse: # 检查 _logging_configured 标志是否存在 if not getattr(douyin_logging, "_logging_configured", False): douyin_logging.configure_logging() - except Exception: - pass + except Exception as e: + logging.warning("日志系统初始化失败: %s", e) try: from unified_logger import MDC, get_unified_logger -- 2.39.5