当前位置:   article > 正文

rolabelimg和labelimg标注完后voc格式转dota格式,xml转txt_rolabelimg格式转dota格式

rolabelimg格式转dota格式

话不多说,直接上代码

  1. import os
  2. import os.path as osp
  3. import math
  4. BASEDIR = osp.dirname(osp.abspath(__file__))
  5. XMLDIR = osp.join(BASEDIR, 'D:\Bishe\mydata\original_image\data_xml')
  6. OUTDIR = osp.join(BASEDIR, 'D:\Bishe\mydata\original_image\data_txt3')
  7. if not osp.exists(OUTDIR):
  8. os.makedirs(OUTDIR)
  9. #pi=3.1415926
  10. xmlnames = [i for i in os.listdir(XMLDIR) if i.endswith('.xml')]
  11. # print(names)
  12. pi=3.1415926
  13. #转换成四点坐标
  14. def convert(cx,cy,w,h,a):
  15. if a>=pi:
  16. a-=pi
  17. #计算斜径半长
  18. l=math.sqrt(w**2+h**2)/2
  19. #计算初始矩形角度
  20. a0=math.atan(h/w)
  21. #旋转,计算旋转角
  22. #右上角点 ↗
  23. a1=a0+a
  24. x1=cx+l*math.cos(a1)
  25. y1=cy+l*math.sin(a1)
  26. #右下角点 ↘
  27. a2=-a0+a
  28. x2=cx+l*math.cos(a2)
  29. y2=cy+l*math.sin(a2)
  30. #左下角点 ↙
  31. a3=a1+pi
  32. x3=cx+l*math.cos(a3)
  33. y3=cy+l*math.sin(a3)
  34. #左上角点 ↖
  35. a4=a2+pi
  36. x4=cx+l*math.cos(a4)
  37. y4=cy+l*math.sin(a4)
  38. return [x1,y1,x2,y2,x3,y3,x4,y4]
  39. #点关于直线对称
  40. for xmlname in xmlnames:
  41. cx = []
  42. cy = []
  43. w = []
  44. h = []
  45. angle = []
  46. txtname = xmlname.split('.')[-2] + '.txt'
  47. with open(osp.join(OUTDIR, txtname), 'w') as fp:
  48. fp.write('')
  49. with open(osp.join(XMLDIR, xmlname), 'r') as fp:
  50. lines = fp.readlines()
  51. for line in lines:
  52. # print(line, end='')
  53. if line.strip().startswith('<width>'):
  54. img_width = eval(line.strip().strip('<width>').strip('</width>'))
  55. # print(img_width)
  56. if line.strip().startswith('<height>'):
  57. img_height = eval(line.strip().strip('<height>').strip('</height>'))
  58. # print(img_height)
  59. if line.strip().startswith('<depth>'):
  60. img_depth = eval(line.strip().strip('<depth>').strip('</depth>'))
  61. # print(img_depth)
  62. if line.strip().startswith('<cx>'):
  63. cx.append(eval(line.strip().strip('<cx>').strip('</cx>')))
  64. # print(cx)
  65. if line.strip().startswith('<cy>'):
  66. cy.append(eval(line.strip().strip('<cy>').strip('</cy>')))
  67. # print(cy)
  68. if line.strip().startswith('<w>'):
  69. w.append(eval(line.strip().strip('<w>').strip('</w>')))
  70. # print(w)
  71. if line.strip().startswith('<h>'):
  72. h.append(eval(line.strip().strip('<h>').strip('</h>')))
  73. # print(h)
  74. if line.strip().startswith('<angle>'):
  75. angle.append(eval(line.strip().strip('<angle>').strip('</angle>')))
  76. # print(angle)
  77. with open(osp.join(OUTDIR, txtname), 'a') as fp:
  78. fp.write("imagesource:GoogleEarth")
  79. fp.write('\n')
  80. fp.write("gsd:0.146343590398")
  81. fp.write('\n')
  82. for i in range(len(cx)):
  83. cls0 = 0.0
  84. cx_i = cx[i]
  85. cy_i = cy[i]
  86. w_i = w[i]
  87. h_i = h[i]
  88. a_i = angle[i]
  89. x0,y0,x1,y1,x2,y2,x3,y3=convert(cx_i,cy_i,w_i,h_i,a_i)
  90. put_str = ' '.join([str(x0), str(y0), str(x1), str(y1), str(x2), str(y2), str(x3), str(y3),str('bigship'),str(0)])
  91. #put_str = ' '.join([str(cx_i), str(cy_i), str(w_i), str(h_i), str(a_i)])
  92. with open(osp.join(OUTDIR, txtname), 'a') as fp:
  93. fp.write(put_str)
  94. fp.write('\n')
  95. print(xmlname, 'to', txtname, 'done.')

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

闽ICP备14008679号