规范代码格式

This commit is contained in:
龙澳 2024-12-18 21:07:47 +08:00
parent 9e30a58fa9
commit e9cd12b2f6
6 changed files with 41 additions and 36 deletions

View File

@ -46,7 +46,7 @@ class ImagePreprocessor:
self.gps_points = extractor.extract_all_gps() self.gps_points = extractor.extract_all_gps()
self.logger.info(f"成功提取 {len(self.gps_points)} 个GPS点") self.logger.info(f"成功提取 {len(self.gps_points)} 个GPS点")
return self.gps_points return self.gps_points
def time_filter(self) -> pd.DataFrame: def time_filter(self) -> pd.DataFrame:
"""时间过滤""" """时间过滤"""
self.logger.info("开始时间过滤") self.logger.info("开始时间过滤")
@ -64,8 +64,8 @@ class ImagePreprocessor:
self.logger.info("开始过滤GPS点") self.logger.info("开始过滤GPS点")
filter = GPSFilter(self.config.output_dir) filter = GPSFilter(self.config.output_dir)
self.logger.info(f"开始过滤孤立点 (距离阈值: {self.config.filter_distance_threshold}, 最小邻居数: { self.logger.info(
self.config.filter_min_neighbors})") f"开始过滤孤立点(距离阈值: {self.config.filter_distance_threshold}, 最小邻居数: {self.config.filter_min_neighbors})")
self.gps_points = filter.filter_isolated_points( self.gps_points = filter.filter_isolated_points(
self.gps_points, self.gps_points,
self.config.filter_distance_threshold, self.config.filter_distance_threshold,
@ -73,8 +73,8 @@ class ImagePreprocessor:
) )
self.logger.info(f"孤立点过滤后剩余 {len(self.gps_points)} 个GPS点") self.logger.info(f"孤立点过滤后剩余 {len(self.gps_points)} 个GPS点")
self.logger.info(f"开始过滤密集点 (网格大小: {self.config.filter_grid_size}, 距离阈值: { self.logger.info(
self.config.filter_dense_distance_threshold})") f"开始过滤密集点(网格大小: {self.config.filter_grid_size}, 距离阈值: {self.config.filter_dense_distance_threshold})")
self.gps_points = filter.filter_dense_points( self.gps_points = filter.filter_dense_points(
self.gps_points, self.gps_points,
grid_size=self.config.filter_grid_size, grid_size=self.config.filter_grid_size,
@ -90,7 +90,8 @@ class ImagePreprocessor:
self.logger.info(f"开始划分网格 (重叠率: {self.config.grid_overlap})") self.logger.info(f"开始划分网格 (重叠率: {self.config.grid_overlap})")
grid_divider = GridDivider(overlap=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) grid_points = grid_divider.assign_to_grids(self.gps_points, grids)
self.logger.info(f"成功划分为 {len(grid_points)} 个网格") self.logger.info(f"成功划分为 {len(grid_points)} 个网格")
return grid_points return grid_points
@ -104,9 +105,11 @@ class ImagePreprocessor:
for grid_idx, points in grid_points.items(): for grid_idx, points in grid_points.items():
if self.config.enable_grid_division: 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: 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) os.makedirs(output_dir, exist_ok=True)
@ -131,21 +134,22 @@ class ImagePreprocessor:
# 创建一个新的图形 # 创建一个新的图形
plt.figure(figsize=(20, 16)) plt.figure(figsize=(20, 16))
# 绘制所有原始点 # 绘制所有原始点
plt.scatter(original_points_df['lon'], plt.scatter(original_points_df['lon'],
original_points_df['lat'], original_points_df['lat'],
color='blue', color='blue',
label="Original Points", label="Original Points",
alpha=0.6) alpha=0.6)
# 绘制被过滤的点 # 绘制被过滤的点
filtered_points_df = original_points_df[original_points_df['file'].isin(filtered_files)] filtered_points_df = original_points_df[original_points_df['file'].isin(
plt.scatter(filtered_points_df['lon'], filtered_files)]
filtered_points_df['lat'], plt.scatter(filtered_points_df['lon'],
color="red", filtered_points_df['lat'],
label="Filtered Points", color="red",
alpha=0.6) label="Filtered Points",
alpha=0.6)
# 设置图形属性 # 设置图形属性
plt.title("GPS Coordinates of Images", fontsize=14) plt.title("GPS Coordinates of Images", fontsize=14)
@ -153,7 +157,7 @@ class ImagePreprocessor:
plt.ylabel("Latitude", fontsize=12) plt.ylabel("Latitude", fontsize=12)
plt.grid(True) plt.grid(True)
plt.legend() plt.legend()
# 保存图形 # 保存图形
plt.savefig(os.path.join(self.config.output_dir, 'filter_GPS.png')) plt.savefig(os.path.join(self.config.output_dir, 'filter_GPS.png'))
plt.close() plt.close()
@ -161,7 +165,7 @@ class ImagePreprocessor:
def process(self): def process(self):
"""执行完整的预处理流程""" """执行完整的预处理流程"""
try: try:
self.extract_gps() self.extract_gps()
self.time_filter() self.time_filter()
self.filter_points() self.filter_points()
@ -170,7 +174,7 @@ class ImagePreprocessor:
self.visualize_results() self.visualize_results()
self.logger.info("预处理任务完成") self.logger.info("预处理任务完成")
self.command_runner.run_grid_commands( self.command_runner.run_grid_commands(
grid_points, grid_points,
self.config.enable_grid_division self.config.enable_grid_division
) )
except Exception as e: except Exception as e:
@ -181,8 +185,8 @@ class ImagePreprocessor:
if __name__ == '__main__': if __name__ == '__main__':
# 创建配置 # 创建配置
config = PreprocessConfig( config = PreprocessConfig(
image_dir=r'C:\datasets\1815\images', image_dir=r'E:\datasets\UAV\1815',
output_dir=r'C:\datasets\1815\output', output_dir=r'E:\datasets\UAV\1815\output',
filter_grid_size=0.001, filter_grid_size=0.001,
filter_dense_distance_threshold=10, filter_dense_distance_threshold=10,
filter_distance_threshold=0.001, filter_distance_threshold=0.001,

Binary file not shown.

View File

@ -4,13 +4,14 @@ import subprocess
from typing import Dict from typing import Dict
import pandas as pd import pandas as pd
class CommandRunner: class CommandRunner:
"""执行网格处理命令的类""" """执行网格处理命令的类"""
def __init__(self, output_dir: str): def __init__(self, output_dir: str):
""" """
初始化命令执行器 初始化命令执行器
Args: Args:
output_dir: 输出目录路径 output_dir: 输出目录路径
""" """
@ -20,7 +21,7 @@ class CommandRunner:
def run_grid_commands(self, grid_points: Dict[int, pd.DataFrame], enable_grid_division: bool = True): def run_grid_commands(self, grid_points: Dict[int, pd.DataFrame], enable_grid_division: bool = True):
""" """
为每个网格顺序运行指定命令 为每个网格顺序运行指定命令
Args: Args:
grid_points: 网格点数据字典键为网格索引值为该网格的点数据 grid_points: 网格点数据字典键为网格索引值为该网格的点数据
enable_grid_division: 是否启用网格划分 enable_grid_division: 是否启用网格划分
@ -30,7 +31,7 @@ class CommandRunner:
return return
self.logger.info("开始执行网格处理命令") self.logger.info("开始执行网格处理命令")
# 顺序执行每个网格的命令 # 顺序执行每个网格的命令
for grid_idx in grid_points.keys(): for grid_idx in grid_points.keys():
try: try:
@ -42,10 +43,10 @@ class CommandRunner:
def _run_command(self, grid_idx: int): def _run_command(self, grid_idx: int):
""" """
执行单个网格的命令 执行单个网格的命令
Args: Args:
grid_idx: 网格索引 grid_idx: 网格索引
Raises: Raises:
Exception: 当命令执行失败时抛出异常 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" 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}") self.logger.info(f"执行命令: {command} 在目录: {grid_dir}")
# 在指定目录下执行命令 # 在指定目录下执行命令
process = subprocess.Popen( process = subprocess.Popen(
command, command,
@ -65,10 +66,10 @@ class CommandRunner:
stderr=subprocess.PIPE, stderr=subprocess.PIPE,
text=True text=True
) )
# 获取命令输出 # 获取命令输出
stdout, stderr = process.communicate() stdout, stderr = process.communicate()
# 检查命令执行结果 # 检查命令执行结果
if process.returncode == 0: if process.returncode == 0:
self.logger.info(f"网格 {grid_idx + 1} 命令执行成功") self.logger.info(f"网格 {grid_idx + 1} 命令执行成功")
@ -83,7 +84,7 @@ class CommandRunner:
f.write(f"\n错误日志:\n") f.write(f"\n错误日志:\n")
f.write(f"{stderr}") f.write(f"{stderr}")
raise Exception(f"命令执行失败: {stderr}") raise Exception(f"命令执行失败: {stderr}")
except Exception as e: except Exception as e:
self.logger.error(f"网格 {grid_idx + 1} 命令执行出错: {str(e)}") self.logger.error(f"网格 {grid_idx + 1} 命令执行出错: {str(e)}")
raise raise