
本文解析在批量处理 dota 格式转 yolo 格式时,因 `coordinateslist` 全局累积未清空,导致后续输出文件写入错误数据、坐标值大于 1 的根本原因及修复方案。
在将 Dota 格式(多边形顶点坐标 + 类别)批量转换为 YOLO 格式(归一化中心点 + 宽高)的过程中,一个看似微小却极具破坏性的错误常被忽视:状态变量未在每次循环迭代中重置。
你提供的代码片段中,coordinatesList = list() 被定义在外部作用域(如 for 循环之外),而实际逻辑中它应在每次处理一个新输入文件前清空。否则,coordinatesList 会持续累积前序文件解析出的所有坐标点——当程序开始处理第二个 .txt 文件时,coordinatesList 中不仅包含当前文件的坐标,还残留着第一个文件的数据。这直接导致:
- 后续输出文件行数异常增多(远超原始有效行数);
- centerX / centerY 等计算基于混杂坐标,极大可能超出 [0, 1] 归一化范围;
- boundingWidth / boundingHeight 失真,甚至为负或远大于 1;
- 最终 YOLO 标签文件完全失效,无法被训练或推理流程正确读取。
✅ 正确做法是:将 coordinatesList = [] 显式置于每个文件处理流程的起始位置,确保其作用域严格限定于单次文件解析周期内。
以下是修正后的核心逻辑片段(已整合上下文并增强健壮性):
立即学习“Python免费学习笔记(深入)”;
import os
import cv2
textDir = "./inputData"
imageDir = "./inputImages" # 假设图像目录已知
outputDir = "./outputData"
os.makedirs(outputDir, exist_ok=True)
for textFile in os.listdir(textDir):
if not textFile.endswith(".txt"):
continue
textPath = os.path.join(textDir, textFile)
# 寻找同名图像(如 P0000.txt → P0000.jpg)
imageFile = textFile.replace(".txt", ".jpg") # 或 .png,按需调整
imagePath = os.path.join(imageDir, imageFile)
if not os.path.exists(imagePath):
print(f"⚠️ 跳过 {textFile}:对应图像 {imageFile} 不存在")
continue
img = cv2.imread(imagePath)
if img is None:
print(f"⚠️ 跳过 {textFile}:无法加载图像 {imageFile}")
continue
imageHeight, imageWidth = img.shape[:2] # 忽略 channels 更安全
# ✅ 关键修复:每次处理新文件前,重置 coordinatesList
coordinatesList = []
try:
with open(textPath, "r", encoding="utf-8") as f:
lines = f.read().splitlines()
# 跳过前两行元数据(Dota 格式约定)
data_lines = lines[2:]
for line in data_lines:
if not line.strip():
continue
# 提取所有数字部分(忽略末尾类别和难度字段)
coords_str = ' '.join(line.rsplit(' ', 2)[:-2]).strip()
if not coords_str:
continue
coordinatesList.append(coords_str.split())
except Exception as e:
print(f"❌ 解析 {textFile} 失败: {e}")
continue
# 生成 YOLO 标签并写入输出文件(覆盖模式,非追加!)
outputPath = os.path.join(outputDir, textFile)
try:
with open(outputPath, "w", encoding="utf-8") as out_f:
for coords in coordinatesList:
try:
coords_num = [float(x) for x in coords]
xs = coords_num[::2]
ys = coords_num[1::2]
if len(xs) < 3 or len(ys) < 3:
print(f"⚠️ {textFile}: 坐标点不足 3 个,跳过该行")
continue
x_min, x_max = min(xs), max(xs)
y_min, y_max = min(ys), max(ys)
# 归一化:除以图像宽/高
cx = ((x_max + x_min) / 2) / imageWidth
cy = ((y_max + y_min) / 2) / imageHeight
w = (x_max - x_min) / imageWidth
h = (y_max - y_min) / imageHeight
# 防御性检查:确保归一化值在合理范围内
if not (0 <= cx <= 1 and 0 <= cy <= 1 and 0 < w <= 1 and 0 < h <= 1):
print(f"⚠️ {textFile}: 归一化坐标越界 (cx={cx:.4f}, cy={cy:.4f}, w={w:.4f}, h={h:.4f}),跳过")
continue
out_line = f"0 {cx:.16f} {cy:.16f} {w:.16f} {h:.16f}\n"
out_f.write(out_line)
except (ValueError, ZeroDivisionError) as e:
print(f"⚠️ {textFile}: 坐标解析异常: {e},跳过该行")
continue
print(f"✅ 已生成 {outputPath} ({len(coordinatesList)} 行)")
except Exception as e:
print(f"❌ 写入 {outputPath} 失败: {e}")? 关键改进说明:
- coordinatesList = [] 移至循环内:彻底隔离各文件处理上下文,杜绝跨文件数据污染;
- 使用 "w" 模式而非 "a":避免重复运行时旧内容残留(原代码中 open(..., "a") 是严重隐患);
- 显式异常捕获与日志反馈:便于定位具体哪一行/哪个文件出错;
- 归一化值边界校验:主动过滤非法结果,提升鲁棒性;
- 编码声明与路径安全:避免中文路径或特殊字符引发的 UnicodeDecodeError;
- 图像尺寸获取优化:img.shape[:2] 更通用,兼容灰度/彩色图。
? 延伸建议:
- 若需支持多种图像后缀(.jpg, .png, .jpeg),可用 pathlib.Path 构建更灵活的匹配逻辑;
- 对大规模数据集,可引入 tqdm 进度条提升用户体验;
- 将转换逻辑封装为函数(如 dota_to_yolo_line(coords, img_shape)),利于单元测试与复用。
遵循以上实践,即可稳定、准确地完成任意数量 Dota 文件到 YOLO 格式的批量转换,彻底规避“仅首文件正确”的典型陷阱。










