UAV/utils/odm_monitor.py

64 lines
2.1 KiB
Python
Raw Normal View History

2024-12-23 11:31:20 +08:00
import os
import logging
2024-12-26 15:30:50 +08:00
import subprocess
2024-12-25 14:28:01 +08:00
from typing import Tuple
2024-12-23 11:31:20 +08:00
class ODMProcessMonitor:
"""ODM进程监控器"""
2024-12-26 15:30:50 +08:00
def __init__(self, mode: str = "快拼模式"):
2024-12-23 11:31:20 +08:00
self.logger = logging.getLogger('UAV_Preprocess.ODMMonitor')
self.mode = mode
def _check_success(self, grid_dir: str) -> bool:
"""检查ODM是否执行成功"""
2024-12-25 10:04:08 +08:00
success_markers = ['odm_orthophoto', 'odm_georeferencing']
if self.mode != "快拼模式":
success_markers.append('odm_texturing')
2024-12-23 11:31:20 +08:00
return all(os.path.exists(os.path.join(grid_dir, 'project', marker)) for marker in success_markers)
2024-12-25 14:28:01 +08:00
def run_odm_with_monitor(self, grid_dir: str, grid_idx: int, fast_mode: bool = True) -> Tuple[bool, str]:
2024-12-26 15:30:50 +08:00
"""运行ODM命令"""
2024-12-26 11:20:55 +08:00
try:
self.logger.info(f"开始处理网格 {grid_idx + 1}")
2024-12-26 11:32:46 +08:00
2024-12-26 15:30:50 +08:00
# 构建命令字符串
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 "
)
2024-12-25 10:04:08 +08:00
2024-12-26 11:32:46 +08:00
if fast_mode:
2024-12-26 15:30:50 +08:00
command += (
f"--feature-quality lowest "
f"--orthophoto-resolution 8 "
f"--fast-orthophoto "
f"--skip-3dmodel "
)
2024-12-25 10:04:08 +08:00
2024-12-26 15:30:50 +08:00
command += "--rerun-all"
2024-12-26 11:20:55 +08:00
2024-12-26 15:30:50 +08:00
# 执行命令
result = subprocess.run(
command,
shell=True,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL
)
2024-12-26 11:20:55 +08:00
2024-12-26 11:32:46 +08:00
# 检查是否成功完成
2024-12-26 15:30:50 +08:00
if result.returncode == 0 and self._check_success(grid_dir):
2024-12-26 11:32:46 +08:00
self.logger.info(f"网格 {grid_idx + 1} 处理成功")
return True, ""
2024-12-25 10:04:08 +08:00
2024-12-26 11:32:46 +08:00
return False, f"网格 {grid_idx + 1} 处理失败"
2024-12-25 10:04:08 +08:00
2024-12-26 11:20:55 +08:00
except Exception as e:
error_msg = f"执行异常: {str(e)}"
self.logger.error(error_msg)
2024-12-26 11:32:46 +08:00
return False, error_msg