roLabelImg标注转YOLO格式实战:手把手教你处理旋转目标检测数据集
roLabelImg标注转YOLO格式实战手把手教你处理旋转目标检测数据集在计算机视觉领域旋转目标检测正逐渐成为研究热点。与传统水平框检测不同旋转框能更精确地定位倾斜或密集排列的物体。roLabelImg作为一款开源的旋转标注工具生成的XML格式标注需要转换为YOLO训练所需的TXT格式。本文将深入解析这一转换过程提供可直接运行的Python脚本并分享实际项目中的经验技巧。1. 环境准备与基础概念旋转目标检测在遥感图像、文本检测、工业质检等领域有广泛应用。与常规的YOLO格式不同旋转框标注需要额外存储角度信息。roLabelImg生成的XML文件中每个对象包含中心点坐标(cx,cy)、宽度(w)、高度(h)和旋转角度(angle)五个关键参数。典型的roLabelImg XML结构如下robndbox cx542.5/cx cy243.5/cy w47.0/w h20.0/h angle1.047/angle /robndbox转换前的准备工作安装Python 3.6环境准备已标注的roLabelImg XML文件创建项目目录结构project/ ├── xmllabels/ # 存放原始XML文件 ├── labels/ # 输出YOLO格式TXT文件 └── convert.py # 转换脚本2. 核心转换算法解析转换脚本的核心是将roLabelImg的旋转框参数转换为YOLO格式。YOLO旋转框格式为class_id x_center y_center width height angle_degrees其中所有坐标和尺寸都是相对于图像宽高的归一化值(0-1)角度以度为单位(0-179)。关键转换函数分析def convert(size, box): 归一化坐标转换 dw 1. / (size[0]) # 宽度归一化因子 dh 1. / (size[1]) # 高度归一化因子 x box[0] * dw # 中心点x坐标归一化 y box[1] * dh # 中心点y坐标归一化 w box[2] * dw # 宽度归一化 h box[3] * dh # 高度归一化 return x, y, w, h角度处理需要特别注意roLabelImg使用弧度制需要转换为角度制当宽度小于高度时需要交换w/h并调整角度最终角度应落在0-179度范围内3. 完整转换脚本实现以下是经过优化的完整转换脚本增加了错误处理和日志功能#!/usr/bin/env python3 # -*- coding: utf-8 -*- import xml.etree.ElementTree as ET import os import math from tqdm import tqdm # 进度条显示 # 配置参数 CLASSES [ship, airplane, storage-tank] # 修改为你的类别 XML_DIR xmllabels # XML文件目录 TXT_DIR labels # 输出TXT目录 LOG_FILE conversion.log # 日志文件 def setup_logging(): 初始化日志系统 import logging logging.basicConfig( filenameLOG_FILE, levellogging.INFO, format%(asctime)s - %(levelname)s - %(message)s ) return logging.getLogger() logger setup_logging() def convert(size, box): 坐标归一化转换 dw, dh 1./size[0], 1./size[1] x box[0] * dw y box[1] * dh w box[2] * dw h box[3] * dh return x, y, w, h def process_angle(theta_rad, w, h): 处理旋转角度 if w h: theta_deg (math.degrees(theta_rad) 90) % 180 else: theta_deg math.degrees(theta_rad) % 180 return int(round(theta_deg)) def convert_annotation(xml_file): 处理单个XML文件 try: tree ET.parse(xml_file) root tree.getroot() # 获取图像尺寸 size root.find(size) w int(size.find(width).text) h int(size.find(height).text) # 准备输出文件 basename os.path.splitext(os.path.basename(xml_file))[0] txt_file os.path.join(TXT_DIR, f{basename}.txt) with open(txt_file, w) as f: for obj in root.iter(object): cls obj.find(name).text if cls not in CLASSES: continue robndbox obj.find(robndbox) cx float(robndbox.find(cx).text) cy float(robndbox.find(cy).text) width float(robndbox.find(w).text) height float(robndbox.find(h).text) angle float(robndbox.find(angle).text) # 处理特殊情况 if width 0 or height 0: logger.warning(fInvalid box size in {xml_file}) continue # 转换坐标和角度 x, y, w_norm, h_norm convert((w,h), (cx,cy,width,height)) theta process_angle(angle, width, height) # 写入YOLO格式 cls_id CLASSES.index(cls) f.write(f{cls_id} {x:.6f} {y:.6f} {w_norm:.6f} {h_norm:.6f} {theta}\n) return True except Exception as e: logger.error(fError processing {xml_file}: {str(e)}) return False def main(): 主转换流程 # 创建输出目录 os.makedirs(TXT_DIR, exist_okTrue) # 获取所有XML文件 xml_files [f for f in os.listdir(XML_DIR) if f.endswith(.xml)] if not xml_files: logger.error(No XML files found in input directory) return # 批量转换 success_count 0 for xml_file in tqdm(xml_files, descConverting): full_path os.path.join(XML_DIR, xml_file) if convert_annotation(full_path): success_count 1 logger.info(fConversion completed. Success: {success_count}/{len(xml_files)}) print(f\n转换完成成功处理 {success_count}/{len(xml_files)} 个文件) if __name__ __main__: main()4. 常见问题与解决方案在实际项目中我们可能会遇到以下典型问题4.1 角度处理异常现象转换后的检测框方向不正确原因宽高比接近1时角度判断不稳定弧度到角度的转换存在精度损失解决方案# 在process_angle函数中添加容错处理 if abs(w - h) 1e-3: # 近似正方形 theta_deg math.degrees(theta_rad) % 90 # 限制在0-90度4.2 坐标越界问题现象归一化后的坐标超出[0,1]范围修复方法# 在convert函数中添加边界检查 x max(0, min(1, x)) y max(0, min(1, y)) w_norm max(0, min(1, w_norm)) h_norm max(0, min(1, h_norm))4.3 多类别处理当需要处理多个类别时建议使用字典提高效率CLASS_DICT {name: idx for idx, name in enumerate(CLASSES)} # 在convert_annotation中使用 cls_id CLASS_DICT.get(cls, -1) if cls_id -1: continue5. 验证转换结果转换完成后建议使用以下脚本可视化检查结果import cv2 import numpy as np def draw_rotated_box(img, x, y, w, h, angle, color(0,255,0), thickness2): 在图像上绘制旋转矩形框 cx int(x * img.shape[1]) cy int(y * img.shape[0]) width int(w * img.shape[1]) height int(h * img.shape[0]) rect ((cx, cy), (width, height), angle) box cv2.boxPoints(rect) box np.int0(box) cv2.drawContours(img, [box], 0, color, thickness) return img def visualize(image_file, label_file): 可视化标注结果 img cv2.imread(image_file) with open(label_file) as f: for line in f: parts line.strip().split() if len(parts) ! 6: continue cls_id, x, y, w, h, angle parts img draw_rotated_box(img, float(x), float(y), float(w), float(h), int(angle)) cv2.imshow(Preview, img) cv2.waitKey(0) cv2.destroyAllWindows()使用建议对转换后的样本进行随机抽查重点关注边界情况和特殊角度比较原始标注和转换后结果的一致性6. 性能优化与批量处理对于大规模数据集可以考虑以下优化措施多进程处理from multiprocessing import Pool def batch_convert(xml_files): with Pool(processes4) as pool: # 使用4个进程 results list(tqdm(pool.imap(convert_annotation, xml_files), totallen(xml_files))) return sum(results) # 返回成功计数内存优化技巧使用生成器逐步处理文件及时关闭文件句柄避免不必要的DOM解析实际项目中一个包含10,000张图像的数据集在8核CPU上转换时间通常不超过5分钟。转换过程中建议监控内存使用情况特别是处理超大尺寸图像时。7. 与其他工具的集成转换后的YOLO格式标注可以直接用于以下场景YOLOv5-OBB训练python train.py --data dataset.yaml --weights yolov5s-obb.pt --img 640其中dataset.yaml配置示例# 旋转目标检测数据集配置 train: ../train/images val: ../val/images # 类别信息 names: 0: ship 1: airplane 2: storage-tankLabelImg-YOLO格式验证使用支持旋转框的标注工具验证转换结果推荐使用LabelMe或CVAT进行二次检查在模型训练阶段建议先在小批量数据上验证标注的正确性再开始全量训练。一个实用的技巧是编写数据加载时的可视化检查代码确保训练器读取的标注与预期一致。
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2425090.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!