基于Linus Torvalds代码哲学重构的遥感影像处理管道:简单、直接、可靠。
- ✅ 配置文件管理所有参数
- ✅ 智能模型文件搜索
- ✅ 灵活的目录结构
- ✅ 统一文件管理,避免重复遍历
- ✅ 流式处理,减少内存使用
- ✅ 简化坐标系统处理
- ✅ 增强错误处理和恢复
- ✅ 支持并行处理
- ✅ 断点续传功能
# 创建虚拟环境(推荐)
conda create -n remote_sensing python=3.9
conda activate remote_sensing
# 安装依赖
pip install -r requirements.txt
将YOLO分割模型文件放在以下任一目录:
./models/best.pt
../models/best.pt
C:/remote/ZHDN_FarmlandSeg_v2.1/models/best.pt
# 完整管道
python main.py input.tif output_directory
# 使用自定义配置
python main.py input.tif output_directory --config custom_settings.yaml
python main.py /path/to/input.tif /path/to/output_dir
这将执行完整的4步流程:
- TIFF切片 - 将大图切成1004x1004瓦片
- 掩膜生成 - 使用YOLO模型生成分割掩膜
- 矢量化 - 将掩膜转换为shapefile
- 合并处理 - 合并、融合、拆分shapefile
# 从掩膜生成开始(如果瓦片已存在)
python main.py --stage mask /path/to/output_dir
# 从矢量化开始(如果掩膜已存在)
python main.py --stage vectorize /path/to/output_dir
# 仅处理shapefile(如果shapefile已存在)
python main.py --stage shape /path/to/output_dir
python main.py --status /path/to/output_dir
编辑 config/settings.yaml
以自定义参数:
# 模型配置
model:
search_paths:
- "./models"
- "../models"
confidence_threshold: 0.5
# 切片配置
tiling:
tile_size: 1004
overlap_meters: 4
resolution_meters_per_pixel: 1.0
# 处理配置
processing:
max_workers: 4 # 并行线程数
memory_limit_gb: 8.0
output_directory/
├── tiles/ # 切片文件(TIFF + JPG)
├── masks/ # 掩膜文件
├── shapes/ # 单个shapefile
├── final/ # 最终输出
│ ├── dissolved_*.shp # 融合后的shapefile
│ └── split_*.shp # 拆分后的最终结果
└── logs/ # 处理日志
from remote_sensing_pipeline import RemoteSensingPipeline
# 创建管道实例
pipeline = RemoteSensingPipeline(output_dir="./output")
# 运行完整管道
results = pipeline.run_full_pipeline("input.tif")
# 检查结果
if results['success']:
print(f"处理完成: {results['final_outputs']['split']}")
else:
print(f"处理失败: {results['error']}")
processing:
chunk_size: 50 # 减少批处理大小
memory_limit_gb: 4.0 # 降低内存限制
processing:
max_workers: 8 # 增加线程数(适合CPU密集型)
max_workers: 1 # 单线程(适合GPU推理)
model:
device: "cuda" # 使用GPU
device: "cpu" # 使用CPU
device: "auto" # 自动选择
FileNotFoundError: 未找到模型文件
解决方案: 确保模型文件在搜索路径中,或修改配置文件中的 model.search_paths
MemoryError: 内存不足
解决方案:
- 减少
processing.max_workers
- 降低
processing.memory_limit_gb
- 使用更小的
tiling.tile_size
RuntimeError: CUDA out of memory
解决方案:
- 设置
model.device: "cpu"
- 减少并行处理线程数
特性 | 原版 | 重构版 |
---|---|---|
配置管理 | 硬编码路径 | ✅ YAML配置文件 |
文件管理 | 重复遍历 | ✅ 统一文件管理器 |
错误处理 | 基础try-catch | ✅ 增强错误恢复 |
并行处理 | 无 | ✅ 可配置并行 |
断点续传 | 无 | ✅ 部分管道支持 |
内存管理 | 全载入内存 | ✅ 流式处理 |
代码复杂度 | 复杂间隙调整 | ✅ 简化算法 |
- Python 3.8+
- 内存: 8GB+ (取决于图像大小)
- 存储: 输入文件大小的3-5倍
- GPU: 可选,用于加速推理
# 运行测试(如果有)
pytest tests/
# 代码格式化
black remote_sensing_pipeline/
# 类型检查
mypy remote_sensing_pipeline/
设计哲学: "Bad programmers worry about the code. Good programmers worry about data structures and their relationships." - Linus Torvalds