实现rolabelimg对于dota格式文件的直接加载和保存
在本篇博客中,我们将讲解如何修改roLabelImg.py
文件,使其能够直接加载和保存Dota格式的标注文件(txt)以替换掉复杂的xml文件。通过对源代码的修改,我们将实现支持加载并保存Dota格式标注数据,以便与roLabelImg的图形界面进行配合。
1. 修改roLabelImg.py中的函数
在原本的roLabelImg.py
中,首先,我们需要替换PascalVocReader
为DotaReader
,这样我们就可以通过自定义的DotaReader
类来解析Dota格式的标注数据。
# 修改前
from pascal_voc_io import PascalVocReader
# 修改后
from pascal_voc_io import PascalVocReader, DotaReader
接着,我们需要更新读取标注的代码。在原有的代码中,使用的是PascalVocReader
,现在我们需要将其替换为DotaReader
来解析Dota格式的标注文件。
# 修改前
tVocParseReader = PascalVocReader(xmlPath)
# 修改后
tVocParseReader = DotaReader(xmlPath)
在标注保存部分的代码中,我们将标注以Dota格式保存为txt文件,而不是Pascal VOC格式的xml文件。以下是代码的修改:
# 修改前
try:
if self.usingPascalVocFormat is True:
print('Img: ' + self.filePath + ' -> Its xml: ' + annotationFilePath)
self.labelFile.savePascalVocFormat(annotationFilePath, shapes, self.filePath, self.imageData,
self.lineColor.getRgb(), self.fillColor.getRgb())
else:
self.labelFile.save(annotationFilePath, shapes, self.filePath, self.imageData,
self.lineColor.getRgb(), self.fillColor.getRgb())
# 修改后
try:
print('Img: ' + self.filePath + ' -> Its txt: ' + annotationFilePath)
with open(annotationFilePath, 'w') as f:
for shape in shapes:
points = shape['points']
label = shape['label']
difficult = 0
# 将4个点坐标 + 标签 + 难度级别写入文件
line = " ".join([f"{p[0]} {p[1]}" for p in points]) + f" {label} {difficult}\n"
f.write(line)
return True
2. 修改pascal_voc_io.py中的代码
我们需要在pascal_voc_io.py
中新增DotaReader
类,它负责解析Dota格式的标注文件并将其转换为roLabelImg可以识别的格式。
DotaReader
类的实现
首先,我们实现一个辅助函数polygon_to_rotated_box
,该函数用于将Dota格式中的四个点坐标转换为一个旋转框,便于后续处理。
def polygon_to_rotated_box(polygon):
"""
将8参数多边形(四个点的坐标)转换为5参数旋转框。
"""
# 将多边形顶点转换为numpy数组
poly_points = np.array(polygon, dtype=np.float32).reshape(-1, 2)
# 获取最小外接矩形
rect = cv2.minAreaRect(poly_points)
(cx, cy), (w, h), theta = rect
# OpenCV返回的角度是负角度,需要转换成正角度
if w < h:
w, h = h, w
theta += 90
theta = np.deg2rad(theta) # 将角度转换为弧度
return cx, cy, w, h, theta
接着,我们实现DotaReader
类,它负责读取Dota格式的txt标注文件并将每个标注信息解析为相应的格式。
class DotaReader:
def __init__(self, filepath):
self.shapes = []
self.filepath = filepath
self.parseDotaFile()
self.verified = False
def getShapes(self):
return self.shapes
def addShape(self, label, points, difficult):
# 将每个标注转换为相应的四个角点(顺时针或逆时针)
cx, cy, w, h, theta = polygon_to_rotated_box(points)
self.shapes.append((label, points, theta, True, None, None, difficult))
def parseDotaFile(self):
assert self.filepath.endswith('.txt'), "Unsupport file format"
print(self.filepath)
with open(self.filepath, 'r') as file:
lines = file.readlines()
for line in lines:
parts = line.strip().split()
if len(parts) == 9:
# 提取四个点坐标(8个数值)
x1, y1, x2, y2, x3, y3, x4, y4 = map(float, parts[:8])
label = parts[8] # 标签
difficult = 0 # 难度标记,0 或 1
# 将四个坐标点按顺时针顺序排列
points = [(x1, y1), (x2, y2), (x3, y3), (x4, y4)]
# 添加标注信息到shapes
self.addShape(label, points, difficult)
elif len(parts) == 10:
# 提取四个点坐标(8个数值)
x1, y1, x2, y2, x3, y3, x4, y4 = map(float, parts[:8])
label = parts[8] # 标签
difficult = int(parts[9]) # 难度标记,0 或 1
# 将四个坐标点按顺时针顺序排列
points = [(x1, y1), (x2, y2), (x3, y3), (x4, y4)]
# 添加标注信息到shapes
self.addShape(label, points, difficult)
else:
continue
3. 总结
通过以上修改,我们成功实现了roLabelImg支持Dota格式文件的加载和保存。在roLabelImg.py
中,我们通过替换PascalVocReader
为DotaReader
,使得程序能够读取Dota格式的txt文件,并将标注信息以txt格式保存。通过修改pascal_voc_io.py
文件中的代码,我们新增了DotaReader
类,它能够处理Dota格式的标注数据,并将其转换为可供roLabelImg使用的格式。
这些修改为我们在使用roLabelImg进行图像标注时提供了更多灵活性,特别是对于Dota数据集的支持。
---
希望这篇博客对您有所帮助,如果您喜欢这篇文章,请点赞或关注,我会持续分享更多实用的 目标检测工具 技术内容!
---