ODM_pro/utils/command_runner.py

76 lines
2.6 KiB
Python
Raw Permalink Normal View History

import os
import logging
import subprocess
2024-12-20 21:30:44 +08:00
import time
from typing import Dict
import pandas as pd
2024-12-20 21:30:44 +08:00
from preprocess.odm_monitor import ODMProcessMonitor
2024-12-18 21:07:47 +08:00
class CommandRunner:
"""执行网格处理命令的类"""
2024-12-21 12:36:14 +08:00
def __init__(self, output_dir: str, max_retries: int = 3, mode: str = "快拼模式"):
"""
初始化命令执行器
2024-12-20 21:30:44 +08:00
i
Args:
output_dir: 输出目录路径
2024-12-20 21:30:44 +08:00
max_retries: 最大重试次数
"""
self.output_dir = output_dir
2024-12-20 21:30:44 +08:00
self.max_retries = max_retries
self.logger = logging.getLogger('UAV_Preprocess.CommandRunner')
2024-12-21 12:36:14 +08:00
self.monitor = ODMProcessMonitor(max_retries=max_retries, mode=mode)
self.mode = mode
def _run_command(self, grid_idx: int):
"""
执行单个网格的命令
2024-12-18 21:07:47 +08:00
Args:
grid_idx: 网格索引
2024-12-18 21:07:47 +08:00
Raises:
Exception: 当命令执行失败时抛出异常
"""
try:
grid_dir = os.path.join(self.output_dir, f'grid_{grid_idx + 1}')
2024-12-22 14:28:07 +08:00
grid_dir = grid_dir[0].lower() + grid_dir[1:].replace('\\', '/')
2024-12-21 12:03:54 +08:00
if self.mode == "快拼模式":
2024-12-22 14:28:07 +08:00
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"
2024-12-21 12:03:54 +08:00
else:
2024-12-22 14:28:07 +08:00
command = f"docker run -ti --rm -v {grid_dir}:/datasets opendronemap/odm --project-path /datasets project --feature-quality lowest --force-gps"
2024-12-20 21:30:44 +08:00
self.logger.info(f"开始执行命令: {command}")
2024-12-21 12:03:54 +08:00
success, error_msg = self.monitor.run_odm_with_monitor(
command, grid_dir, grid_idx)
2024-12-20 21:30:44 +08:00
if not success:
raise Exception(error_msg)
2024-12-18 21:07:47 +08:00
2024-12-20 21:30:44 +08:00
except Exception as e:
self.logger.error(f"网格 {grid_idx + 1} 处理失败: {str(e)}")
raise
def run_grid_commands(self, grid_points: Dict[int, pd.DataFrame], enable_grid_division: bool = True):
"""
为每个网格顺序运行指定命令
2024-12-18 21:07:47 +08:00
2024-12-20 21:30:44 +08:00
Args:
grid_points: 网格点数据字典键为网格索引值为该网格的点数据
enable_grid_division: 是否启用网格划分
"""
if not enable_grid_division:
self._run_command(0)
return
2024-12-18 21:07:47 +08:00
2024-12-20 21:30:44 +08:00
self.logger.info("开始执行网格处理命令")
2024-12-18 21:07:47 +08:00
2024-12-20 21:30:44 +08:00
for grid_idx in grid_points.keys():
try:
self._run_command(grid_idx)
except Exception as e:
self.logger.error(f"网格 {grid_idx + 1} 处理失败,停止后续执行: {str(e)}")
raise