import os import logging import subprocess from typing import Tuple class ODMProcessMonitor: """ODM进程监控器""" def __init__(self, mode: str = "快拼模式"): self.logger = logging.getLogger('UAV_Preprocess.ODMMonitor') self.mode = mode def _check_success(self, grid_dir: str) -> bool: """检查ODM是否执行成功""" success_markers = ['odm_orthophoto', 'odm_georeferencing'] if self.mode != "快拼模式": success_markers.append('odm_texturing') return all(os.path.exists(os.path.join(grid_dir, 'project', marker)) for marker in success_markers) def run_odm_with_monitor(self, grid_dir: str, grid_idx: int, fast_mode: bool = True) -> Tuple[bool, str]: """运行ODM命令""" self.logger.info(f"开始处理网格 {grid_idx + 1}") # 规范化路径 grid_dir = os.path.abspath(grid_dir).replace('\\', '/') # 设置环境变量 # env = os.environ.copy() # env["PYTHONUNBUFFERED"] = "1" # 构建命令字符串 command = ( f"docker run --rm " # 移除 -ti 参数 f"-v {grid_dir}:/datasets " f"opendronemap/odm " f"--project-path /datasets project " f"--max-concurrency 10 " f"--force-gps " ) if fast_mode: command += ( f"--feature-quality lowest " f"--orthophoto-resolution 8 " f"--fast-orthophoto " f"--skip-3dmodel " ) command += "--rerun-all" # 执行命令 result = subprocess.run( command, shell=True, # env=env, cwd=grid_dir, # 设置工作目录 stdout=subprocess.DEVNULL, # 完全禁用输出捕获 stderr=subprocess.DEVNULL, creationflags=subprocess.CREATE_NO_WINDOW if os.name == 'nt' else 0 # Windows下不创建新窗口 ) # 检查是否成功完成 if result.returncode == 0 and self._check_success(grid_dir): self.logger.info(f"网格 {grid_idx + 1} 处理成功") return True, "" return False, f"网格 {grid_idx + 1} 处理失败"