延庆县专业网站制作网站建设,wordpress门户cms,wordpress相对地址,长春网站建设首选网诚传媒在本篇博客中#xff0c;我们将讲解如何修改roLabelImg.py文件#xff0c;使其能够直接加载和保存Dota格式的标注文件#xff08;txt#xff09;以替换掉复杂的xml文件。通过对源代码的修改#xff0c;我们将实现支持加载并保存Dota格式标注数据#xff0c;以便与roLabel…在本篇博客中我们将讲解如何修改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}\nf.write(line)return True2. 修改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, dtypenp.float32).reshape(-1, 2)# 获取最小外接矩形rect cv2.minAreaRect(poly_points)(cx, cy), (w, h), theta rect# OpenCV返回的角度是负角度需要转换成正角度if w h:w, h h, wtheta 90theta np.deg2rad(theta) # 将角度转换为弧度return cx, cy, w, h, theta接着我们实现DotaReader类它负责读取Dota格式的txt标注文件并将每个标注信息解析为相应的格式。
class DotaReader:def __init__(self, filepath):self.shapes []self.filepath filepathself.parseDotaFile()self.verified Falsedef getShapes(self):return self.shapesdef 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 formatprint(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)]# 添加标注信息到shapesself.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)]# 添加标注信息到shapesself.addShape(label, points, difficult)else:continue3. 总结
通过以上修改我们成功实现了roLabelImg支持Dota格式文件的加载和保存。在roLabelImg.py中我们通过替换PascalVocReader为DotaReader使得程序能够读取Dota格式的txt文件并将标注信息以txt格式保存。通过修改pascal_voc_io.py文件中的代码我们新增了DotaReader类它能够处理Dota格式的标注数据并将其转换为可供roLabelImg使用的格式。
这些修改为我们在使用roLabelImg进行图像标注时提供了更多灵活性特别是对于Dota数据集的支持。
---
希望这篇博客对您有所帮助如果您喜欢这篇文章请点赞或关注我会持续分享更多实用的 目标检测工具 技术内容
---