import os import logging import subprocess from typing import Dict, Tuple import pandas as pd class ODMProcessMonitor: """ODM处理监控器""" def __init__(self, output_dir: str, mode: str = "快拼模式"): self.output_dir = output_dir 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}") # 构建Docker命令 grid_dir = grid_dir[0].lower()+grid_dir[1:].replace('\\', '/') docker_command = ( f"docker run --gpus all -ti --rm " f"-v {grid_dir}:/datasets " f"opendronemap/odm:gpu " f"--project-path /datasets project " f"--max-concurrency 10 " f"--force-gps " f"--feature-quality lowest " f"--orthophoto-resolution 10 " ) if fast_mode: docker_command += ( f"--fast-orthophoto " f"--skip-3dmodel " ) docker_command += "--rerun-all" self.logger.info(docker_command) result = subprocess.run( docker_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = result.stdout.decode( 'utf-8'), result.stderr.decode('utf-8') # 检查执行结果 if self._check_success(grid_dir): self.logger.info(stdout, stderr) self.logger.info(f"网格 {grid_idx + 1} 处理成功") return True, "" return False, f"网格 {grid_idx + 1} 处理失败" def process_all_grids(self, grid_points: Dict[int, pd.DataFrame]): """处理所有网格""" self.logger.info("开始执行网格处理") for grid_idx in grid_points.keys(): grid_dir = os.path.join( self.output_dir, f'grid_{grid_idx + 1}' ) success, error_msg = self.run_odm_with_monitor( grid_dir=grid_dir, grid_idx=grid_idx, fast_mode=(self.mode == "快拼模式") ) if not success: raise Exception(f"网格 {grid_idx + 1} 处理失败: {error_msg}")