UAV_odm_merge/utils/odm_monitor.py
2024-12-30 20:41:22 +08:00

58 lines
2.0 KiB
Python

import os
import logging
import subprocess
from typing import Dict, Tuple
import pandas as pd
class ODMProcessMonitor:
"""ODM处理监控器"""
def __init__(self, output_dir: str, fast_mode: bool):
self.output_dir = output_dir
self.logger = logging.getLogger('UAV_Preprocess.ODMMonitor')
self.fast_mode = fast_mode
def _check_success(self, grid_dir: str) -> bool:
"""检查ODM是否执行成功"""
success_markers = ['odm_orthophoto', 'odm_georeferencing']
if not self.fast_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, project_dir: str, fast_mode: bool = True) -> Tuple[bool, str]:
"""运行ODM命令"""
# 构建Docker命令
docker_command = (
f"docker run --gpus all -ti --rm "
f"-v {project_dir}:/datasets "
f"opendronemap/odm:gpu "
f"--project-path /datasets project "
f"--max-concurrency 15 "
f"--force-gps "
f"--feature-quality lowest "
f"--orthophoto-resolution 10 "
f"--split-overlap 0 "
)
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')
self.logger.info(f"==========stdout==========: {stdout}")
self.logger.error(f"==========stderr==========: {stderr}")
# 检查执行结果
if self._check_success(image_dir):
self.logger.info(f"处理成功")
else:
self.logger.error(f"处理失败")