当前位置:   article > 正文

xml to DOTA_hrsc2016 xml转dota

hrsc2016 xml转dota
  1. '''
  2. 2020/6/15,标注文件转换xml转txt(vol to yolo)转完后需添加labels文件,即数字序号对应的标签名。
  3. '''
  4. import xml.etree.ElementTree as ET
  5. import numpy as np
  6. import pickle
  7. import os
  8. import cv2
  9. from os import listdir, getcwd
  10. from os.path import join
  11. classes = ['Boeing737', 'Boeing747', 'Boeing777', 'Boeing787', 'C919', 'A220', 'A321', 'A330', 'A350', 'ARJ21',
  12. 'other-airplane', 'Passenger Ship', 'Motorboat', 'Fishing Boat', 'Tugboat', 'Engineering Ship',
  13. 'Liquid Cargo Ship', 'Dry Cargo Ship', 'Warship', 'other-ship', 'Small Car', 'Bus', 'Cargo Truck',
  14. 'Dump Truck', 'Van', 'Trailer', 'Tractor', 'Excavator', 'Truck Tractor', 'other-vehicle', 'Basketball Court',
  15. 'Tennis Court', 'Football Field', 'Baseball Field', 'Intersection', 'Roundabout', 'Bridge']
  16. def convert(size, box):
  17. dw = 1. / (size[0])
  18. dh = 1. / (size[1])
  19. x = (box[0] + box[1]) / 2.0 - 1
  20. y = (box[2] + box[3]) / 2.0 - 1
  21. w = box[1] - box[0]
  22. h = box[3] - box[2]
  23. x = x * dw
  24. w = w * dw
  25. y = y * dh
  26. h = h * dh
  27. if w >= 1:
  28. w = 0.99
  29. if h >= 1:
  30. h = 0.99
  31. return (x, y, w, h)
  32. def convert_annotation(rootpath, xmlpath, xmlname):
  33. xmlfile = os.path.join(xmlpath, xmlname)
  34. with open(xmlfile, "r", encoding='UTF-8') as in_file:
  35. txtname = xmlname[:-4] + '.txt'
  36. txtpath = rootpath + '/labelTxt' # 生成的.txt文件会被保存在labelTxt目录下
  37. if not os.path.exists(txtpath):
  38. os.makedirs(txtpath)
  39. txtfile = os.path.join(txtpath, txtname)
  40. with open(txtfile, "w+", encoding='UTF-8') as out_file:
  41. tree = ET.parse(in_file)
  42. root = tree.getroot()
  43. size = root.find('size')
  44. w = int(size.find('width').text) # 图片的宽
  45. h = int(size.find('height').text)
  46. # print(w, h)
  47. out_file.truncate()
  48. for obj in root.iter('object'):
  49. possibleresult = obj.find('possibleresult')
  50. cls_name = possibleresult.find('name').text
  51. if cls_name not in classes:
  52. print("Appear not clear class name!!!!")
  53. exit()
  54. cls_name = cls_name.replace(" ", "-")
  55. points = obj.find('points')
  56. data = ""
  57. for i in range(0, len(points) - 1): # 只遍历前四个点
  58. point = points[i]
  59. xy = point.text.split(",")
  60. x = int(float(xy[0]))
  61. y = int(float(xy[1]))
  62. data = data + str(x) + " "
  63. data = data + str(y) + " "
  64. data = data + cls_name + " " + str(0)
  65. print(data)
  66. out_file.write(data + '\n')
  67. if __name__ == "__main__":
  68. rootpath = 'train'
  69. xmlpath = rootpath + '/labelXml' # 标签文件所在的路径
  70. list = os.listdir(xmlpath)
  71. for i in range(0, len(list)):
  72. path = os.path.join(xmlpath, list[i])
  73. if ('.xml' in path) or ('.XML' in path):
  74. convert_annotation(rootpath, xmlpath, list[i])
  75. print('---------------done--------------', i, '----------------------------------------')
  76. else:
  77. print('not xml file', i)

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

闽ICP备14008679号