UAV/utils/odm_monitor.py

90 lines
3.3 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
from typing import Dict, Tuple
import pandas as pd
2024-12-23 11:31:20 +08:00
class ODMProcessMonitor:
"""ODM处理监控器"""
2024-12-23 11:31:20 +08:00
def __init__(self, output_dir: str, mode: str = "快拼模式"):
self.output_dir = output_dir
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)
2025-01-02 20:11:47 +08:00
def run_odm_with_monitor(self, grid_dir: str, grid_id: tuple, fast_mode: bool = True, produce_dem: bool = False) -> Tuple[bool, str]:
2024-12-26 15:30:50 +08:00
"""运行ODM命令"""
2025-01-02 20:11:47 +08:00
if produce_dem and fast_mode:
self.logger.error("快拼模式下无法生成DEM请调整生产参数")
return False, "快拼模式下无法生成DEM请调整生产参数"
2024-12-31 21:37:44 +08:00
self.logger.info(f"开始处理网格 ({grid_id[0]},{grid_id[1]})")
2024-12-26 11:32:46 +08:00
# 构建Docker命令
grid_dir = grid_dir[0].lower()+grid_dir[1:].replace('\\', '/')
docker_command = (
2024-12-29 16:00:47 +08:00
f"docker run --gpus all -ti --rm "
2024-12-27 19:40:33 +08:00
f"-v {grid_dir}:/datasets "
2024-12-29 23:10:37 +08:00
f"opendronemap/odm:gpu "
2024-12-27 19:40:33 +08:00
f"--project-path /datasets project "
2025-01-02 10:55:02 +08:00
f"--max-concurrency 15 "
2024-12-27 19:40:33 +08:00
f"--force-gps "
f"--feature-quality lowest "
f"--orthophoto-resolution 10 "
2024-12-27 19:40:33 +08:00
)
2024-12-26 11:20:55 +08:00
2025-01-02 20:11:47 +08:00
if produce_dem:
docker_command += (
f"--dsm "
f"--dtm "
)
2024-12-27 19:40:33 +08:00
if fast_mode:
docker_command += (
2024-12-27 19:40:33 +08:00
f"--fast-orthophoto "
f"--skip-3dmodel "
2024-12-26 15:30:50 +08:00
)
2024-12-26 11:20:55 +08:00
docker_command += "--rerun-all"
self.logger.info(docker_command)
2024-12-27 19:40:33 +08:00
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')
2024-12-25 10:04:08 +08:00
2024-12-31 14:23:45 +08:00
self.logger.info(f"==========stdout==========: {stdout}")
self.logger.error(f"==========stderr==========: {stderr}")
# 检查执行结果
if self._check_success(grid_dir):
2024-12-31 21:37:44 +08:00
self.logger.info(f"网格 ({grid_id[0]},{grid_id[1]}) 处理成功")
2024-12-27 19:40:33 +08:00
return True, ""
2024-12-31 14:23:45 +08:00
else:
2024-12-31 21:37:44 +08:00
self.logger.error(f"网格 ({grid_id[0]},{grid_id[1]}) 处理失败")
return False, f"网格 ({grid_id[0]},{grid_id[1]}) 处理失败"
2025-01-02 20:11:47 +08:00
def process_all_grids(self, grid_points: Dict[tuple, pd.DataFrame], produce_dem: bool):
"""处理所有网格"""
self.logger.info("开始执行网格处理")
2024-12-31 21:37:44 +08:00
for grid_id in grid_points.keys():
grid_dir = os.path.join(
2024-12-31 21:37:44 +08:00
self.output_dir, f'grid_{grid_id[0]}_{grid_id[1]}'
)
success, error_msg = self.run_odm_with_monitor(
grid_dir=grid_dir,
2024-12-31 21:37:44 +08:00
grid_id=grid_id,
2025-01-02 20:11:47 +08:00
fast_mode=(self.mode == "快拼模式"),
produce_dem=produce_dem
)
if not success:
2024-12-31 21:37:44 +08:00
raise Exception(f"网格 ({grid_id[0]},{grid_id[1]}) 处理失败: {error_msg}")