diff --git a/odm_preprocess.py b/odm_preprocess.py index 37faaf6..7d6ee2f 100644 --- a/odm_preprocess.py +++ b/odm_preprocess.py @@ -46,7 +46,7 @@ class ImagePreprocessor: self.gps_points = extractor.extract_all_gps() self.logger.info(f"成功提取 {len(self.gps_points)} 个GPS点") return self.gps_points - + def time_filter(self) -> pd.DataFrame: """时间过滤""" self.logger.info("开始时间过滤") @@ -64,8 +64,8 @@ class ImagePreprocessor: self.logger.info("开始过滤GPS点") filter = GPSFilter(self.config.output_dir) - self.logger.info(f"开始过滤孤立点 (距离阈值: {self.config.filter_distance_threshold}, 最小邻居数: { - self.config.filter_min_neighbors})") + self.logger.info( + f"开始过滤孤立点(距离阈值: {self.config.filter_distance_threshold}, 最小邻居数: {self.config.filter_min_neighbors})") self.gps_points = filter.filter_isolated_points( self.gps_points, self.config.filter_distance_threshold, @@ -73,8 +73,8 @@ class ImagePreprocessor: ) self.logger.info(f"孤立点过滤后剩余 {len(self.gps_points)} 个GPS点") - self.logger.info(f"开始过滤密集点 (网格大小: {self.config.filter_grid_size}, 距离阈值: { - self.config.filter_dense_distance_threshold})") + self.logger.info( + f"开始过滤密集点(网格大小: {self.config.filter_grid_size}, 距离阈值: {self.config.filter_dense_distance_threshold})") self.gps_points = filter.filter_dense_points( self.gps_points, grid_size=self.config.filter_grid_size, @@ -90,7 +90,8 @@ class ImagePreprocessor: self.logger.info(f"开始划分网格 (重叠率: {self.config.grid_overlap})") grid_divider = GridDivider(overlap=self.config.grid_overlap) - grids = grid_divider.divide_grids(self.gps_points, grid_size=self.config.grid_size) + grids = grid_divider.divide_grids( + self.gps_points, grid_size=self.config.grid_size) grid_points = grid_divider.assign_to_grids(self.gps_points, grids) self.logger.info(f"成功划分为 {len(grid_points)} 个网格") return grid_points @@ -104,9 +105,11 @@ class ImagePreprocessor: for grid_idx, points in grid_points.items(): if self.config.enable_grid_division: - output_dir = os.path.join(self.config.output_dir, f'grid_{grid_idx + 1}', 'project', 'images') + output_dir = os.path.join( + self.config.output_dir, f'grid_{grid_idx + 1}', 'project', 'images') else: - output_dir = os.path.join(self.config.output_dir, 'project', 'images') + output_dir = os.path.join( + self.config.output_dir, 'project', 'images') os.makedirs(output_dir, exist_ok=True) @@ -131,21 +134,22 @@ class ImagePreprocessor: # 创建一个新的图形 plt.figure(figsize=(20, 16)) - + # 绘制所有原始点 - plt.scatter(original_points_df['lon'], - original_points_df['lat'], - color='blue', - label="Original Points", - alpha=0.6) - + plt.scatter(original_points_df['lon'], + original_points_df['lat'], + color='blue', + label="Original Points", + alpha=0.6) + # 绘制被过滤的点 - filtered_points_df = original_points_df[original_points_df['file'].isin(filtered_files)] - plt.scatter(filtered_points_df['lon'], - filtered_points_df['lat'], - color="red", - label="Filtered Points", - alpha=0.6) + filtered_points_df = original_points_df[original_points_df['file'].isin( + filtered_files)] + plt.scatter(filtered_points_df['lon'], + filtered_points_df['lat'], + color="red", + label="Filtered Points", + alpha=0.6) # 设置图形属性 plt.title("GPS Coordinates of Images", fontsize=14) @@ -153,7 +157,7 @@ class ImagePreprocessor: plt.ylabel("Latitude", fontsize=12) plt.grid(True) plt.legend() - + # 保存图形 plt.savefig(os.path.join(self.config.output_dir, 'filter_GPS.png')) plt.close() @@ -161,7 +165,7 @@ class ImagePreprocessor: def process(self): """执行完整的预处理流程""" - try: + try: self.extract_gps() self.time_filter() self.filter_points() @@ -170,7 +174,7 @@ class ImagePreprocessor: self.visualize_results() self.logger.info("预处理任务完成") self.command_runner.run_grid_commands( - grid_points, + grid_points, self.config.enable_grid_division ) except Exception as e: @@ -181,8 +185,8 @@ class ImagePreprocessor: if __name__ == '__main__': # 创建配置 config = PreprocessConfig( - image_dir=r'C:\datasets\1815\images', - output_dir=r'C:\datasets\1815\output', + image_dir=r'E:\datasets\UAV\1815', + output_dir=r'E:\datasets\UAV\1815\output', filter_grid_size=0.001, filter_dense_distance_threshold=10, filter_distance_threshold=0.001, diff --git a/preprocess/__pycache__/gps_extractor.cpython-39.pyc b/preprocess/__pycache__/gps_extractor.cpython-39.pyc index b4369dc..e6cb333 100644 Binary files a/preprocess/__pycache__/gps_extractor.cpython-39.pyc and b/preprocess/__pycache__/gps_extractor.cpython-39.pyc differ diff --git a/preprocess/__pycache__/gps_filter.cpython-39.pyc b/preprocess/__pycache__/gps_filter.cpython-39.pyc index 2350bc0..eb00f2b 100644 Binary files a/preprocess/__pycache__/gps_filter.cpython-39.pyc and b/preprocess/__pycache__/gps_filter.cpython-39.pyc differ diff --git a/preprocess/__pycache__/grid_divider.cpython-39.pyc b/preprocess/__pycache__/grid_divider.cpython-39.pyc index 0111ed2..91d3fe4 100644 Binary files a/preprocess/__pycache__/grid_divider.cpython-39.pyc and b/preprocess/__pycache__/grid_divider.cpython-39.pyc differ diff --git a/preprocess/__pycache__/time_filter.cpython-39.pyc b/preprocess/__pycache__/time_filter.cpython-39.pyc new file mode 100644 index 0000000..99e81a5 Binary files /dev/null and b/preprocess/__pycache__/time_filter.cpython-39.pyc differ diff --git a/preprocess/command_runner.py b/preprocess/command_runner.py index 2b8c9b1..207d888 100644 --- a/preprocess/command_runner.py +++ b/preprocess/command_runner.py @@ -4,13 +4,14 @@ import subprocess from typing import Dict import pandas as pd + class CommandRunner: """执行网格处理命令的类""" def __init__(self, output_dir: str): """ 初始化命令执行器 - + Args: output_dir: 输出目录路径 """ @@ -20,7 +21,7 @@ class CommandRunner: def run_grid_commands(self, grid_points: Dict[int, pd.DataFrame], enable_grid_division: bool = True): """ 为每个网格顺序运行指定命令 - + Args: grid_points: 网格点数据字典,键为网格索引,值为该网格的点数据 enable_grid_division: 是否启用网格划分 @@ -30,7 +31,7 @@ class CommandRunner: return self.logger.info("开始执行网格处理命令") - + # 顺序执行每个网格的命令 for grid_idx in grid_points.keys(): try: @@ -42,10 +43,10 @@ class CommandRunner: def _run_command(self, grid_idx: int): """ 执行单个网格的命令 - + Args: grid_idx: 网格索引 - + Raises: Exception: 当命令执行失败时抛出异常 """ @@ -55,7 +56,7 @@ class CommandRunner: command = f"docker run -ti --rm -v {grid_dir}:/datasets opendronemap/odm --project-path /datasets project --feature-quality lowest --force-gps" self.logger.info(f"执行命令: {command} 在目录: {grid_dir}") - + # 在指定目录下执行命令 process = subprocess.Popen( command, @@ -65,10 +66,10 @@ class CommandRunner: stderr=subprocess.PIPE, text=True ) - + # 获取命令输出 stdout, stderr = process.communicate() - + # 检查命令执行结果 if process.returncode == 0: self.logger.info(f"网格 {grid_idx + 1} 命令执行成功") @@ -83,7 +84,7 @@ class CommandRunner: f.write(f"\n错误日志:\n") f.write(f"{stderr}") raise Exception(f"命令执行失败: {stderr}") - + except Exception as e: self.logger.error(f"网格 {grid_idx + 1} 命令执行出错: {str(e)}") - raise \ No newline at end of file + raise