2024-12-23 11:31:20 +08:00
|
|
|
import os
|
|
|
|
import time
|
|
|
|
import logging
|
2024-12-25 23:19:14 +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-25 14:28:01 +08:00
|
|
|
def __init__(self, max_retries: int = 3, mode: str = "快拼模式"):
|
2024-12-23 11:31:20 +08:00
|
|
|
self.max_retries = max_retries
|
|
|
|
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-25 23:19:14 +08:00
|
|
|
"""运行ODM命令"""
|
2024-12-23 11:31:20 +08:00
|
|
|
attempt = 0
|
|
|
|
while attempt < self.max_retries:
|
|
|
|
try:
|
2024-12-25 14:28:01 +08:00
|
|
|
self.logger.info(f"网格 {grid_idx + 1} 第 {attempt + 1} 次尝试")
|
2024-12-25 23:19:14 +08:00
|
|
|
|
|
|
|
# 构建基础命令
|
|
|
|
cmd = [
|
|
|
|
"docker", "run", "--rm",
|
|
|
|
"-v", f"{grid_dir}:/datasets",
|
|
|
|
"opendronemap/odm",
|
2024-12-25 14:28:01 +08:00
|
|
|
"--project-path", "/datasets", "project",
|
|
|
|
"--max-concurrency", "10",
|
|
|
|
"--force-gps",
|
|
|
|
"--rerun-all"
|
|
|
|
]
|
2024-12-25 10:04:08 +08:00
|
|
|
|
2024-12-25 23:19:14 +08:00
|
|
|
# 快速模式额外参数
|
2024-12-25 14:28:01 +08:00
|
|
|
if fast_mode:
|
2024-12-25 23:19:14 +08:00
|
|
|
cmd.extend([
|
2024-12-25 14:28:01 +08:00
|
|
|
"--feature-quality", "lowest",
|
|
|
|
"--orthophoto-resolution", "8",
|
|
|
|
"--fast-orthophoto",
|
|
|
|
"--skip-3dmodel"
|
|
|
|
])
|
|
|
|
|
2024-12-25 23:19:14 +08:00
|
|
|
# 执行命令,不捕获输出
|
|
|
|
result = subprocess.run(
|
|
|
|
cmd,
|
|
|
|
stdout=subprocess.DEVNULL,
|
|
|
|
stderr=subprocess.PIPE,
|
|
|
|
text=True
|
2024-12-25 14:28:01 +08:00
|
|
|
)
|
2024-12-25 10:04:08 +08:00
|
|
|
|
2024-12-25 23:19:14 +08:00
|
|
|
# 检查执行结果
|
|
|
|
if result.returncode == 0 and self._check_success(grid_dir):
|
2024-12-24 23:00:33 +08:00
|
|
|
self.logger.info(f"网格 {grid_idx + 1} ODM处理成功")
|
|
|
|
return True, ""
|
2024-12-25 23:19:14 +08:00
|
|
|
|
|
|
|
# 只在失败时记录错误信息
|
|
|
|
if result.returncode != 0:
|
|
|
|
self.logger.error(f"网格 {grid_idx + 1} 执行失败,错误信息:{result.stderr[-500:]}")
|
2024-12-25 10:04:08 +08:00
|
|
|
|
2024-12-23 11:31:20 +08:00
|
|
|
except Exception as e:
|
2024-12-25 14:28:01 +08:00
|
|
|
error_msg = f"执行异常: {str(e)}"
|
2024-12-23 11:31:20 +08:00
|
|
|
self.logger.error(error_msg)
|
|
|
|
return False, error_msg
|
2024-12-25 10:04:08 +08:00
|
|
|
|
2024-12-24 23:00:33 +08:00
|
|
|
attempt += 1
|
|
|
|
if attempt < self.max_retries:
|
2024-12-25 14:28:01 +08:00
|
|
|
time.sleep(30)
|
2024-12-25 10:04:08 +08:00
|
|
|
|
2024-12-24 23:00:33 +08:00
|
|
|
error_msg = f"网格 {grid_idx + 1} 在 {self.max_retries} 次尝试后仍然失败"
|
2024-12-23 11:31:20 +08:00
|
|
|
self.logger.error(error_msg)
|
|
|
|
return False, error_msg
|