当前位置:   article > 正文

rolabelImg XML to Dota_rolabel转dota格式

rolabel转dota格式
  1. # 文件名称 :roxml_to_dota.py
  2. # 功能描述 :把rolabelimg标注的xml文件转换成dota能识别的xml文件,
  3. # 再转换成dota格式的txt文件
  4. # 把旋转框 cx,cy,w,h,angle,转换成四点坐标x1,y1,x2,y2,x3,y3,x4,y4
  5. import os
  6. import xml.etree.ElementTree as ET
  7. import math
  8. def edit_xml(xml_file,dotaxml_file):
  9. """
  10. 修改xml文件
  11. :param xml_file:xml文件的路径
  12. :return:
  13. """
  14. tree = ET.parse(xml_file)
  15. objs = tree.findall('object')
  16. for ix, obj in enumerate(objs):
  17. x0 = ET.Element("x0") # 创建节点
  18. y0 = ET.Element("y0")
  19. x1 = ET.Element("x1")
  20. y1 = ET.Element("y1")
  21. x2 = ET.Element("x2")
  22. y2 = ET.Element("y2")
  23. x3 = ET.Element("x3")
  24. y3 = ET.Element("y3")
  25. # obj_type = obj.find('bndbox')
  26. # type = obj_type.text
  27. # print(xml_file)
  28. if (obj.find('robndbox') == None):
  29. obj_bnd = obj.find('bndbox')
  30. obj_xmin = obj_bnd.find('xmin')
  31. obj_ymin = obj_bnd.find('ymin')
  32. obj_xmax = obj_bnd.find('xmax')
  33. obj_ymax = obj_bnd.find('ymax')
  34. xmin = float(obj_xmin.text)
  35. ymin = float(obj_ymin.text)
  36. xmax = float(obj_xmax.text)
  37. ymax = float(obj_ymax.text)
  38. obj_bnd.remove(obj_xmin) # 删除节点
  39. obj_bnd.remove(obj_ymin)
  40. obj_bnd.remove(obj_xmax)
  41. obj_bnd.remove(obj_ymax)
  42. x0.text = str(xmin)
  43. y0.text = str(ymax)
  44. x1.text = str(xmax)
  45. y1.text = str(ymax)
  46. x2.text = str(xmax)
  47. y2.text = str(ymin)
  48. x3.text = str(xmin)
  49. y3.text = str(ymin)
  50. else:
  51. obj_bnd = obj.find('robndbox')
  52. obj_bnd.tag = 'bndbox' # 修改节点名
  53. obj_cx = obj_bnd.find('cx')
  54. obj_cy = obj_bnd.find('cy')
  55. obj_w = obj_bnd.find('w')
  56. obj_h = obj_bnd.find('h')
  57. obj_angle = obj_bnd.find('angle')
  58. cx = float(obj_cx.text)
  59. cy = float(obj_cy.text)
  60. w = float(obj_w.text)
  61. h = float(obj_h.text)
  62. angle = float(obj_angle.text)
  63. obj_bnd.remove(obj_cx) # 删除节点
  64. obj_bnd.remove(obj_cy)
  65. obj_bnd.remove(obj_w)
  66. obj_bnd.remove(obj_h)
  67. obj_bnd.remove(obj_angle)
  68. x0.text, y0.text = rotatePoint(cx, cy, cx - w / 2, cy - h / 2, -angle)
  69. x1.text, y1.text = rotatePoint(cx, cy, cx + w / 2, cy - h / 2, -angle)
  70. x2.text, y2.text = rotatePoint(cx, cy, cx + w / 2, cy + h / 2, -angle)
  71. x3.text, y3.text = rotatePoint(cx, cy, cx - w / 2, cy + h / 2, -angle)
  72. # obj.remove(obj_type) # 删除节点
  73. obj_bnd.append(x0) # 新增节点
  74. obj_bnd.append(y0)
  75. obj_bnd.append(x1)
  76. obj_bnd.append(y1)
  77. obj_bnd.append(x2)
  78. obj_bnd.append(y2)
  79. obj_bnd.append(x3)
  80. obj_bnd.append(y3)
  81. tree.write(dotaxml_file, method='xml', encoding='utf-8') # 更新xml文件
  82. # 转换成四点坐标
  83. def rotatePoint(xc, yc, xp, yp, theta):
  84. xoff = xp - xc;
  85. yoff = yp - yc;
  86. cosTheta = math.cos(theta)
  87. sinTheta = math.sin(theta)
  88. pResx = cosTheta * xoff + sinTheta * yoff
  89. pResy = - sinTheta * xoff + cosTheta * yoff
  90. return str(int(xc + pResx)), str(int(yc + pResy))
  91. def totxt(xml_path,out_path):
  92. # 想要生成的txt文件保存的路径,这里可以自己修改
  93. files = os.listdir(xml_path)
  94. for file in files:
  95. tree = ET.parse(xml_path + os.sep + file)
  96. root = tree.getroot()
  97. name = file.strip('.xml')
  98. output = out_path + name + '.txt'
  99. file = open(output, 'w')
  100. objs = tree.findall('object')
  101. for obj in objs:
  102. cls = obj.find('name').text
  103. box = obj.find('bndbox')
  104. x0 = int(float(box.find('x0').text))
  105. y0 = int(float(box.find('y0').text))
  106. x1 = int(float(box.find('x1').text))
  107. y1 = int(float(box.find('y1').text))
  108. x2 = int(float(box.find('x2').text))
  109. y2 = int(float(box.find('y2').text))
  110. x3 = int(float(box.find('x3').text))
  111. y3 = int(float(box.find('y3').text))
  112. file.write("{} {} {} {} {} {} {} {} {} 0\n".format(x0, y0, x1, y1, x2, y2, x3, y3, cls))
  113. file.close()
  114. print(output)
  115. if __name__ == '__main__':
  116. # -----**** 第一步:把xml文件统一转换成旋转框的xml文件 ****-----
  117. roxml_path = "./roxml" # 目录下保存的是需要转换的xml文件
  118. dotaxml_path = './dotaxml'
  119. out_path = './txt/'
  120. filelist = os.listdir(roxml_path)
  121. for file in filelist:
  122. edit_xml(os.path.join(roxml_path, file), os.path.join(dotaxml_path, file))
  123. # -----**** 第二步:把旋转框xml文件转换成txt格式 ****-----
  124. totxt(dotaxml_path, out_path)

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

闽ICP备14008679号