From 7f3036a003f408e134f66f9833464861132d80c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BE=99=E6=BE=B3?= Date: Sun, 22 Dec 2024 14:28:07 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=AE=88=E6=8A=A4=E8=BF=9B?= =?UTF-8?q?=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- odm_preprocess.py | 5 ++- preprocess/command_runner.py | 5 ++- preprocess/odm_monitor.py | 72 +++++++++++++++++++++--------------- 3 files changed, 49 insertions(+), 33 deletions(-) diff --git a/odm_preprocess.py b/odm_preprocess.py index 65d39f8..45066e9 100644 --- a/odm_preprocess.py +++ b/odm_preprocess.py @@ -74,6 +74,7 @@ class ImagePreprocessor: f"噪声点 {stats['noise_points']} 个" ) + # TODO 过滤算法还需要更新 def filter_points(self) -> pd.DataFrame: """过滤GPS点""" if not self.config.enable_filter: @@ -217,7 +218,7 @@ class ImagePreprocessor: if __name__ == "__main__": # 创建配置 config = PreprocessConfig( - image_dir=r"E:\datasets\UAV\283\project\images", + image_dir=r"E:\datasets\UAV\502", output_dir=r"E:\studio2\ODM_pro\test", cluster_eps=0.01, @@ -238,7 +239,7 @@ if __name__ == "__main__": enable_visualization=True, enable_copy_images=True, - mode="快拼模式", + mode="sadf模式", ) # 创建处理器并执行 diff --git a/preprocess/command_runner.py b/preprocess/command_runner.py index 6c12576..8db367a 100644 --- a/preprocess/command_runner.py +++ b/preprocess/command_runner.py @@ -36,10 +36,11 @@ i """ try: grid_dir = os.path.join(self.output_dir, f'grid_{grid_idx + 1}') + grid_dir = grid_dir[0].lower() + grid_dir[1:].replace('\\', '/') if self.mode == "快拼模式": - command = f"docker run -ti --rm -v {grid_dir}:/datasets opendronemap/odm --project-path /datasets project --feature-quality lowest --force-gps --use-3dmesh --fast-orthophoto --skip-3dmodel" + command = f"docker run -ti --rm -v {grid_dir}:/datasets opendronemap/odm --project-path /datasets project --feature-quality lowest --force-gps --fast-orthophoto --skip-3dmodel" else: - command = f"docker run -ti --rm -v {grid_dir}:/datasets opendronemap/odm --project-path /datasets project --feature-quality lowest --force-gps --use-3dmesh" + command = f"docker run -ti --rm -v {grid_dir}:/datasets opendronemap/odm --project-path /datasets project --feature-quality lowest --force-gps" self.logger.info(f"开始执行命令: {command}") success, error_msg = self.monitor.run_odm_with_monitor( diff --git a/preprocess/odm_monitor.py b/preprocess/odm_monitor.py index 33cdb0b..4b2cb76 100644 --- a/preprocess/odm_monitor.py +++ b/preprocess/odm_monitor.py @@ -5,13 +5,14 @@ import logging import subprocess from typing import Optional, Tuple + class ODMProcessMonitor: """ODM进程监控器""" - - def __init__(self, max_retries: int = 3, check_interval: int = 300, mode: str = "快拼模式"): + + def __init__(self, max_retries: int = 3, check_interval: int = 10, mode: str = "快拼模式"): """ 初始化监控器 - + Args: max_retries: 最大重试次数 check_interval: 检查间隔(秒) @@ -22,27 +23,33 @@ class ODMProcessMonitor: self.logger = logging.getLogger('UAV_Preprocess.ODMMonitor') self.mode = mode - def _is_process_running(self, pid: int) -> bool: - """检查进程是否在运行""" + def _check_docker_container(self, process_name: str = "opendronemap/odm") -> bool: + """检查是否有指定的Docker容器在运行""" try: - process = psutil.Process(pid) - return process.is_running() - except psutil.NoSuchProcess: + result = subprocess.run( + ["docker", "ps", "--filter", + f"ancestor={process_name}", "--format", "{{.ID}}"], + capture_output=True, + text=True + ) + return bool(result.stdout.strip()) + except Exception as e: + self.logger.error(f"检查Docker容器状态时发生错误: {str(e)}") return False def _check_success(self, grid_dir: str) -> bool: """检查ODM是否执行成功""" - # ODM成功完成时会生成这些文件夹 if self.mode == "快拼模式": success_markers = ['odm_orthophoto', 'odm_georeferencing'] else: - success_markers = ['odm_orthophoto', 'odm_georeferencing', 'odm_texturing'] - return all(os.path.exists(os.path.join(grid_dir, marker)) for marker in success_markers) + success_markers = ['odm_orthophoto', + 'odm_georeferencing', '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, command: str, grid_dir: str, grid_idx: int) -> Tuple[bool, str]: """ 运行ODM命令并监控进程 - + Args: command: ODM命令 grid_dir: 网格目录 @@ -55,7 +62,7 @@ class ODMProcessMonitor: while attempt < self.max_retries: try: self.logger.info(f"网格 {grid_idx + 1} 第 {attempt + 1} 次尝试执行ODM") - + # 启动ODM进程 process = subprocess.Popen( command, @@ -65,28 +72,35 @@ class ODMProcessMonitor: stderr=subprocess.PIPE, text=True ) - - pid = process.pid - self.logger.info(f"ODM进程启动,PID: {pid}") - # 监控进程 + self.logger.info("ODM进程已启动,开始监控Docker容器") + + # 等待进程启动 + time.sleep(10) + + # 监控Docker容器 while True: - if not self._is_process_running(pid): - # 进程结束,检查是否成功 + if not self._check_docker_container(): + # Docker容器已结束,获取进程输出 stdout, stderr = process.communicate() - - # 保存日志 - log_file = os.path.join(grid_dir, f'odm_attempt_{attempt + 1}.log') - with open(log_file, 'w', encoding='utf-8') as f: - f.write(f"=== 标准输出 ===\n{stdout}\n\n=== 错误输出 ===\n{stderr}") + time.sleep(10) + # 保存日志 + log_file = os.path.join( + grid_dir, f'odm_attempt_{attempt + 1}.log') + with open(log_file, 'w', encoding='utf-8') as f: + f.write( + f"=== 标准输出 ===\n{stdout}\n\n=== 错误输出 ===\n{stderr}") + + # 检查是否成功完成 if self._check_success(grid_dir): self.logger.info(f"网格 {grid_idx + 1} ODM处理成功") return True, "" else: - self.logger.warning(f"网格 {grid_idx + 1} 第 {attempt + 1} 次尝试失败") + self.logger.warning( + f"网格 {grid_idx + 1} 第 {attempt + 1} 次尝试失败") break - + time.sleep(self.check_interval) # 如果不是最后一次尝试,等待后重试 @@ -94,9 +108,9 @@ class ODMProcessMonitor: wait_time = (attempt + 1) * 30 self.logger.info(f"等待 {wait_time} 秒后重试...") time.sleep(wait_time) - + attempt += 1 - + except Exception as e: error_msg = f"监控进程发生异常: {str(e)}" self.logger.error(error_msg) @@ -104,4 +118,4 @@ class ODMProcessMonitor: error_msg = f"网格 {grid_idx + 1} 在 {self.max_retries} 次尝试后仍然失败,需要人工查看" self.logger.error(error_msg) - return False, error_msg \ No newline at end of file + return False, error_msg