From 6ad875d59ab121183ae7a3a1217063cbed90638b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BE=99=E6=BE=B3?= Date: Fri, 27 Dec 2024 19:40:33 +0800 Subject: [PATCH] subprocess.run --- odm_preprocess.py | 2 +- .../test_docker_run.py | 0 utils/odm_monitor.py | 78 ++++++++++--------- 3 files changed, 42 insertions(+), 38 deletions(-) rename test_docker_run.py => tools/test_docker_run.py (100%) diff --git a/odm_preprocess.py b/odm_preprocess.py index a605208..c20f76b 100644 --- a/odm_preprocess.py +++ b/odm_preprocess.py @@ -307,7 +307,7 @@ class ImagePreprocessor: if __name__ == "__main__": # 创建配置 config = PreprocessConfig( - image_dir=r"F:\error_data\20241024100834\project\images", + image_dir=r"G:\error_data\20241024100834\project\images", output_dir=r"G:\ODM_output\20241024100834", cluster_eps=0.01, diff --git a/test_docker_run.py b/tools/test_docker_run.py similarity index 100% rename from test_docker_run.py rename to tools/test_docker_run.py diff --git a/utils/odm_monitor.py b/utils/odm_monitor.py index 3c4c464..1f73f86 100644 --- a/utils/odm_monitor.py +++ b/utils/odm_monitor.py @@ -20,45 +20,49 @@ class ODMProcessMonitor: def run_odm_with_monitor(self, grid_dir: str, grid_idx: int, fast_mode: bool = True) -> Tuple[bool, str]: """运行ODM命令""" - try: - self.logger.info(f"开始处理网格 {grid_idx + 1}") + self.logger.info(f"开始处理网格 {grid_idx + 1}") - # 构建命令字符串 - command = ( - f"docker run -ti --rm " - f"-v {grid_dir}:/datasets " - f"opendronemap/odm " - f"--project-path /datasets project " - f"--max-concurrency 10 " - f"--force-gps " + # 规范化路径 + 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 " ) - if fast_mode: - command += ( - f"--feature-quality lowest " - f"--orthophoto-resolution 8 " - f"--fast-orthophoto " - f"--skip-3dmodel " - ) + command += "--rerun-all" - 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下不创建新窗口 + ) - # 执行命令 - result = subprocess.run( - command, - shell=True, - stdout=subprocess.DEVNULL, - stderr=subprocess.DEVNULL - ) - - # 检查是否成功完成 - 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} 处理失败" - - except Exception as e: - error_msg = f"执行异常: {str(e)}" - self.logger.error(error_msg) - return False, error_msg \ No newline at end of file + # 检查是否成功完成 + 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} 处理失败" \ No newline at end of file