当前位置:   article > 正文

XML标签转yolo的txt格式标签代码_xml转yolo格式

xml转yolo格式

使用方法:

修改input_dir 和 out_dir即可

无需提前知道数据集中含有多少class

转化完成后会在out_dir建立一个classes.txt的文件可以看到labels

  1. import os
  2. import xml.etree.ElementTree as ET
  3. # xml文件存放目录(不要以\结尾)
  4. input_dir = r'F:\YOLOv5_garbage\yolov5-master\inference\annotations'
  5. # 输出txt文件目录(不要以\结尾)
  6. out_dir = r'F:\YOLOv5_garbage\yolov5-master\inference\yolo_annotations'
  7. class_list = []
  8. # 获取目录所有xml文件
  9. def file_name(input_dir):
  10. F = []
  11. for root, dirs, files in os.walk(input_dir):
  12. for file in files:
  13. # print file.decode('gbk') #文件名中有中文字符时转码
  14. if os.path.splitext(file)[1] == '.xml':
  15. t = os.path.splitext(file)[0]
  16. F.append(t) # 将所有的文件名添加到L列表中
  17. return F # 返回L列表
  18. # 获取所有分类
  19. def get_class(filelist):
  20. for i in filelist:
  21. f_dir = input_dir + "\\" + i + ".xml"
  22. in_file = open(f_dir, encoding='UTF-8')
  23. filetree = ET.parse(in_file)
  24. in_file.close()
  25. root = filetree.getroot()
  26. for obj in root.iter('object'):
  27. difficult = obj.find('difficult').text
  28. cls = obj.find('name').text
  29. if cls not in class_list or int(difficult) == 1:
  30. class_list.append(cls)
  31. def ConverCoordinate(imgshape, bbox):
  32. # 将xml像素坐标转换为txt归一化后的坐标
  33. xmin, xmax, ymin, ymax = bbox
  34. width = imgshape[0]
  35. height = imgshape[1]
  36. dw = 1. / width
  37. dh = 1. / height
  38. x = (xmin + xmax) / 2.0
  39. y = (ymin + ymax) / 2.0
  40. w = xmax - xmin
  41. h = ymax - ymin
  42. # 归一化
  43. x = x * dw
  44. y = y * dh
  45. w = w * dw
  46. h = h * dh
  47. return x, y, w, h
  48. def readxml(i):
  49. f_dir = input_dir + "\\" + i + ".xml"
  50. txtresult = ''
  51. outfile = open(f_dir, encoding='UTF-8')
  52. filetree = ET.parse(outfile)
  53. outfile.close()
  54. root = filetree.getroot()
  55. # 获取图片大小
  56. size = root.find('size')
  57. width = int(size.find('width').text)
  58. height = int(size.find('height').text)
  59. imgshape = (width, height)
  60. # 转化为yolov5的格式
  61. for obj in root.findall('object'):
  62. # 获取类别名
  63. obj_name = obj.find('name').text
  64. obj_id = class_list.index(obj_name)
  65. # 获取每个obj的bbox框的左上和右下坐标
  66. bbox = obj.find('bndbox')
  67. xmin = float(bbox.find('xmin').text)
  68. xmax = float(bbox.find('xmax').text)
  69. ymin = float(bbox.find('ymin').text)
  70. ymax = float(bbox.find('ymax').text)
  71. bbox_coor = (xmin, xmax, ymin, ymax)
  72. x, y, w, h = ConverCoordinate(imgshape, bbox_coor)
  73. txt = '{} {} {} {} {}\n'.format(obj_id, x, y, w, h)
  74. txtresult = txtresult + txt
  75. # print(txtresult)
  76. f = open(out_dir + "\\" + i + ".txt", 'a')
  77. f.write(txtresult)
  78. f.close()
  79. # 获取文件夹下的所有文件
  80. filelist = file_name(input_dir)
  81. # 获取所有分类
  82. get_class(filelist)
  83. # 打印class
  84. print(class_list)
  85. # xml转txt
  86. for i in filelist:
  87. readxml(i)
  88. # 在out_dir下生成一个class文件
  89. f = open(out_dir + "\\classes.txt", 'a')
  90. classresult = ''
  91. for i in class_list:
  92. classresult = classresult + i + "\n"
  93. f.write(classresult)
  94. f.close()

参考链接:

xml格式数据集转yolov5可用的txt_yolov5 xml to yolov-CSDN博客

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/代码探险家/article/detail/869510
推荐阅读
相关标签
  

闽ICP备14008679号