• labelimg标注的VOC格式标签xml文件和yolo格式标签txt文件相互转换


    目录

    1 labelimg标注VOC格式和yolo格式介绍

    1.1 voc格式

    1.2 yolo数据格式介绍

    2 voc格式数据和yolo格式数据相互转换

    2.1 voc转yolo代码

    2.2 yolo转voc格式代码 


    1 labelimg标注VOC格式和yolo格式介绍

            labelimg标注工具怎么安装和使用在我的博客中已经讲解了,有需要可以看看,博客

    1.1 voc格式

            VOC格式文件保存在和图像名称一样的xml文件中,xml文件中的标注信息如下图所示:文中红色框中保存着标注图片的主要信息。第一个红色框中里面显示着图片的保存在哪个文件夹中,标签对应的图片名称,还有图片保存的绝对路径。第二个红色框中的信息为标签图片的大小尺寸和通道,正常都是3通道。第三个红色框中的信息为图中标注的对象的信息,分别为该目标对象的属于哪一类,可以看出该对象名称为boat,然后还有标注框的两个点,分别为框的起始点和终结点。第四个红色框中的信息和第三个框框一样,都是我们标注对象的信息,不过该对象为cat。

    1.2 yolo数据格式介绍

            yolo数据格式,是把每个图片的标注信息保存在一个和图片名称一样的txt文件中。txt文件中的信息如下图所示:

    1. 0 0.47416020671834624 0.4523809523809524 0.5968992248062015 0.683982683982684
    2. 1 0.874031007751938 0.4069264069264069 0.1227390180878553 0.2727272727272727

            每一行代表标注的一个目标,第一个数字代表着这个数的类别,第一类目标就是0,第二类目标就是1,以此类推。后面的四个数字是归一化后的的标注的中心点坐标和归一化标注框的长和宽。

    2 voc格式数据和yolo格式数据相互转换

    2.1 voc转yolo代码

    1. import xml.etree.ElementTree as ET
    2. import pickle
    3. import os
    4. from os import listdir, getcwd
    5. from os.path import join
    6. def convert(size, box):
    7. x_center = (box[0] + box[1]) / 2.0
    8. y_center = (box[2] + box[3]) / 2.0
    9. x = x_center / size[0]
    10. y = y_center / size[1]
    11. w = (box[1] - box[0]) / size[0]
    12. h = (box[3] - box[2]) / size[1]
    13. return (x, y, w, h)
    14. def convert_annotation(xml_files_path, save_txt_files_path, classes):
    15. xml_files = os.listdir(xml_files_path)
    16. print(xml_files)
    17. for xml_name in xml_files:
    18. print(xml_name)
    19. xml_file = os.path.join(xml_files_path, xml_name)
    20. out_txt_path = os.path.join(save_txt_files_path, xml_name.split('.')[0] + '.txt')
    21. out_txt_f = open(out_txt_path, 'w')
    22. tree = ET.parse(xml_file)
    23. root = tree.getroot()
    24. size = root.find('size')
    25. w = int(size.find('width').text)
    26. h = int(size.find('height').text)
    27. for obj in root.iter('object'):
    28. difficult = obj.find('difficult').text
    29. cls = obj.find('name').text
    30. if cls not in classes or int(difficult) == 1:
    31. continue
    32. cls_id = classes.index(cls)
    33. xmlbox = obj.find('bndbox')
    34. b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text),
    35. float(xmlbox.find('ymax').text))
    36. # b=(xmin, xmax, ymin, ymax)
    37. print(w, h, b)
    38. bb = convert((w, h), b)
    39. out_txt_f.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')
    40. if __name__ == "__main__":
    41. # 需要转换的类别,需要一一对应
    42. classes1 = ['boat', 'cat']
    43. # 2、voc格式的xml标签文件路径
    44. xml_files1 = r'C:\Users\86159\Desktop\VOC2007\Annotations'
    45. # 3、转化为yolo格式的txt标签文件存储路径
    46. save_txt_files1 = r'C:\Users\86159\Desktop\VOC2007\label'
    47. convert_annotation(xml_files1, save_txt_files1, classes1)

             需要注意的是一定要将自己的类别名字写对,对应好,否则会出错。

    2.2 yolo转voc格式代码 

    1. from xml.dom.minidom import Document
    2. import os
    3. import cv2
    4. # def makexml(txtPath, xmlPath, picPath): # txt所在文件夹路径,xml文件保存路径,图片所在文件夹路径
    5. def makexml(picPath, txtPath, xmlPath): # txt所在文件夹路径,xml文件保存路径,图片所在文件夹路径
    6. """此函数用于将yolo格式txt标注文件转换为voc格式xml标注文件
    7. 在自己的标注图片文件夹下建三个子文件夹,分别命名为picture、txt、xml
    8. """
    9. dic = {'0': "boat", # 创建字典用来对类型进行转换
    10. '1': "cat", # 此处的字典要与自己的classes.txt文件中的类对应,且顺序要一致
    11. }
    12. files = os.listdir(txtPath)
    13. for i, name in enumerate(files):
    14. xmlBuilder = Document()
    15. annotation = xmlBuilder.createElement("annotation") # 创建annotation标签
    16. xmlBuilder.appendChild(annotation)
    17. txtFile = open(txtPath + name)
    18. txtList = txtFile.readlines()
    19. img = cv2.imread(picPath + name[0:-4] + ".jpg")
    20. Pheight, Pwidth, Pdepth = img.shape
    21. folder = xmlBuilder.createElement("folder") # folder标签
    22. foldercontent = xmlBuilder.createTextNode("driving_annotation_dataset")
    23. folder.appendChild(foldercontent)
    24. annotation.appendChild(folder) # folder标签结束
    25. filename = xmlBuilder.createElement("filename") # filename标签
    26. filenamecontent = xmlBuilder.createTextNode(name[0:-4] + ".jpg")
    27. filename.appendChild(filenamecontent)
    28. annotation.appendChild(filename) # filename标签结束
    29. size = xmlBuilder.createElement("size") # size标签
    30. width = xmlBuilder.createElement("width") # size子标签width
    31. widthcontent = xmlBuilder.createTextNode(str(Pwidth))
    32. width.appendChild(widthcontent)
    33. size.appendChild(width) # size子标签width结束
    34. height = xmlBuilder.createElement("height") # size子标签height
    35. heightcontent = xmlBuilder.createTextNode(str(Pheight))
    36. height.appendChild(heightcontent)
    37. size.appendChild(height) # size子标签height结束
    38. depth = xmlBuilder.createElement("depth") # size子标签depth
    39. depthcontent = xmlBuilder.createTextNode(str(Pdepth))
    40. depth.appendChild(depthcontent)
    41. size.appendChild(depth) # size子标签depth结束
    42. annotation.appendChild(size) # size标签结束
    43. for j in txtList:
    44. oneline = j.strip().split(" ")
    45. object = xmlBuilder.createElement("object") # object 标签
    46. picname = xmlBuilder.createElement("name") # name标签
    47. namecontent = xmlBuilder.createTextNode(dic[oneline[0]])
    48. picname.appendChild(namecontent)
    49. object.appendChild(picname) # name标签结束
    50. pose = xmlBuilder.createElement("pose") # pose标签
    51. posecontent = xmlBuilder.createTextNode("Unspecified")
    52. pose.appendChild(posecontent)
    53. object.appendChild(pose) # pose标签结束
    54. truncated = xmlBuilder.createElement("truncated") # truncated标签
    55. truncatedContent = xmlBuilder.createTextNode("0")
    56. truncated.appendChild(truncatedContent)
    57. object.appendChild(truncated) # truncated标签结束
    58. difficult = xmlBuilder.createElement("difficult") # difficult标签
    59. difficultcontent = xmlBuilder.createTextNode("0")
    60. difficult.appendChild(difficultcontent)
    61. object.appendChild(difficult) # difficult标签结束
    62. bndbox = xmlBuilder.createElement("bndbox") # bndbox标签
    63. xmin = xmlBuilder.createElement("xmin") # xmin标签
    64. mathData = int(((float(oneline[1])) * Pwidth + 1) - (float(oneline[3])) * 0.5 * Pwidth)
    65. xminContent = xmlBuilder.createTextNode(str(mathData))
    66. xmin.appendChild(xminContent)
    67. bndbox.appendChild(xmin) # xmin标签结束
    68. ymin = xmlBuilder.createElement("ymin") # ymin标签
    69. mathData = int(((float(oneline[2])) * Pheight + 1) - (float(oneline[4])) * 0.5 * Pheight)
    70. yminContent = xmlBuilder.createTextNode(str(mathData))
    71. ymin.appendChild(yminContent)
    72. bndbox.appendChild(ymin) # ymin标签结束
    73. xmax = xmlBuilder.createElement("xmax") # xmax标签
    74. mathData = int(((float(oneline[1])) * Pwidth + 1) + (float(oneline[3])) * 0.5 * Pwidth)
    75. xmaxContent = xmlBuilder.createTextNode(str(mathData))
    76. xmax.appendChild(xmaxContent)
    77. bndbox.appendChild(xmax) # xmax标签结束
    78. ymax = xmlBuilder.createElement("ymax") # ymax标签
    79. mathData = int(((float(oneline[2])) * Pheight + 1) + (float(oneline[4])) * 0.5 * Pheight)
    80. ymaxContent = xmlBuilder.createTextNode(str(mathData))
    81. ymax.appendChild(ymaxContent)
    82. bndbox.appendChild(ymax) # ymax标签结束
    83. object.appendChild(bndbox) # bndbox标签结束
    84. annotation.appendChild(object) # object标签结束
    85. f = open(xmlPath + name[0:-4] + ".xml", 'w')
    86. xmlBuilder.writexml(f, indent='\t', newl='\n', addindent='\t', encoding='utf-8')
    87. f.close()
    88. if __name__ == "__main__":
    89. picPath = "C:/Users/86159/Desktop/VOC2007/JPEGImage/" # 图片所在文件夹路径,后面的/一定要带上
    90. txtPath = "C:/Users/86159/Desktop/VOC2007/yolo/" # txt所在文件夹路径,后面的/一定要带上
    91. xmlPath = "C:/Users/86159/Desktop/VOC2007/Annotations1/" # xml文件保存路径,后面的/一定要带上
    92. makexml(picPath, txtPath, xmlPath)

            需注要意的是一定要将类别和名字对应好

  • 相关阅读:
    肯尼亚市场开发攻略,收藏一篇就够了
    NLP(自然语言处理)
    深夜看代码2
    linux学习笔记
    [附源码]计算机毕业设计springboot万佳商城管理系统
    【广州华锐互动】VR虚拟现实技术助力太空探险:穿越时空,探索宇宙奥秘
    【完美云曦篇】新预告,云曦遭魔改被抓,石昊首秀九天劫光,反杀战王
    生产环境下调试的技巧
    Python中Scrapy框架搭建ip代理池教程
    在分布式事务场景下,如何设计一个高可靠的跨系统转账
  • 原文地址:https://blog.csdn.net/didiaopao/article/details/119910139