当前位置:   article > 正文

将DOTA标签格式转为VOC格式形成xml文件_将dota数据集格式转换成voc格式

将dota数据集格式转换成voc格式

DOTA数据集的格式为:

前8个数字分别为gt的4个点坐标(x、y)

转换为VOC标注的代码为:

这里有两种选择

1. 将原有的8坐标转换为4坐标的最小外界矩形,即hbb形式 xmin,ymin,xmax,ymax

2. 保留原有/坐标,即obb形式x0,y0,x1,y1,x2,y2,x3,y3

  1. import os
  2. import cv2
  3. from xml.dom.minidom import Document
  4. #windows下无需
  5. import sys
  6. stdi, stdo, stde = sys.stdin, sys.stdout, sys.stderr
  7. reload(sys)
  8. sys.setdefaultencoding('utf-8')
  9. sys.stdin, sys.stdout, sys.stderr = stdi, stdo, stde
  10. category_set = ['ship']
  11. def custombasename(fullname):
  12. return os.path.basename(os.path.splitext(fullname)[0])
  13. def limit_value(a,b):
  14. if a<1:
  15. a = 1
  16. if a>=b:
  17. a = b-1
  18. return a
  19. def readlabeltxt(txtpath, height, width, hbb = True):
  20. print(txtpath)
  21. with open(txtpath, 'r') as f_in: #打开txt文件
  22. lines = f_in.readlines()
  23. splitlines = [x.strip().split(' ') for x in lines] #根据空格分割
  24. boxes = []
  25. for i, splitline in enumerate(splitlines):
  26. if i in [0,1]: #DOTA数据集前两行对于我们来说是无用的
  27. continue
  28. label = splitline[8]
  29. if label not in category_set:#只书写制定的类别
  30. continue
  31. x1 = int(float(splitline[0]))
  32. y1 = int(float(splitline[1]))
  33. x2 = int(float(splitline[2]))
  34. y2 = int(float(splitline[3]))
  35. x3 = int(float(splitline[4]))
  36. y3 = int(float(splitline[5]))
  37. x4 = int(float(splitline[6]))
  38. y4 = int(float(splitline[7]))
  39. #如果是hbb
  40. if hbb:
  41. xx1 = min(x1,x2,x3,x4)
  42. xx2 = max(x1,x2,x3,x4)
  43. yy1 = min(y1,y2,y3,y4)
  44. yy2 = max(y1,y2,y3,y4)
  45. xx1 = limit_value(xx1, width)
  46. xx2 = limit_value(xx2, width)
  47. yy1 = limit_value(yy1, height)
  48. yy2 = limit_value(yy2, height)
  49. box = [xx1,yy1,xx2,yy2,label]
  50. boxes.append(box)
  51. else: #否则是obb
  52. x1 = limit_value(x1, width)
  53. y1 = limit_value(y1, height)
  54. x2 = limit_value(x2, width)
  55. y2 = limit_value(y2, height)
  56. x3 = limit_value(x3, width)
  57. y3 = limit_value(y3, height)
  58. x4 = limit_value(x4, width)
  59. y4 = limit_value(y4, height)
  60. box = [x1,y1,x2,y2,x3,y3,x4,y4,label]
  61. boxes.append(box)
  62. return boxes
  63. def writeXml(tmp, imgname, w, h, d, bboxes, hbb = True):
  64. doc = Document()
  65. #owner
  66. annotation = doc.createElement('annotation')
  67. doc.appendChild(annotation)
  68. #owner
  69. folder = doc.createElement('folder')
  70. annotation.appendChild(folder)
  71. folder_txt = doc.createTextNode("VOC2007")
  72. folder.appendChild(folder_txt)
  73. filename = doc.createElement('filename')
  74. annotation.appendChild(filename)
  75. filename_txt = doc.createTextNode(imgname)
  76. filename.appendChild(filename_txt)
  77. #ones#
  78. source = doc.createElement('source')
  79. annotation.appendChild(source)
  80. database = doc.createElement('database')
  81. source.appendChild(database)
  82. database_txt = doc.createTextNode("My Database")
  83. database.appendChild(database_txt)
  84. annotation_new = doc.createElement('annotation')
  85. source.appendChild(annotation_new)
  86. annotation_new_txt = doc.createTextNode("VOC2007")
  87. annotation_new.appendChild(annotation_new_txt)
  88. image = doc.createElement('image')
  89. source.appendChild(image)
  90. image_txt = doc.createTextNode("flickr")
  91. image.appendChild(image_txt)
  92. #owner
  93. owner = doc.createElement('owner')
  94. annotation.appendChild(owner)
  95. flickrid = doc.createElement('flickrid')
  96. owner.appendChild(flickrid)
  97. flickrid_txt = doc.createTextNode("NULL")
  98. flickrid.appendChild(flickrid_txt)
  99. ow_name = doc.createElement('name')
  100. owner.appendChild(ow_name)
  101. ow_name_txt = doc.createTextNode("idannel")
  102. ow_name.appendChild(ow_name_txt)
  103. #onee#
  104. #twos#
  105. size = doc.createElement('size')
  106. annotation.appendChild(size)
  107. width = doc.createElement('width')
  108. size.appendChild(width)
  109. width_txt = doc.createTextNode(str(w))
  110. width.appendChild(width_txt)
  111. height = doc.createElement('height')
  112. size.appendChild(height)
  113. height_txt = doc.createTextNode(str(h))
  114. height.appendChild(height_txt)
  115. depth = doc.createElement('depth')
  116. size.appendChild(depth)
  117. depth_txt = doc.createTextNode(str(d))
  118. depth.appendChild(depth_txt)
  119. #twoe#
  120. segmented = doc.createElement('segmented')
  121. annotation.appendChild(segmented)
  122. segmented_txt = doc.createTextNode("0")
  123. segmented.appendChild(segmented_txt)
  124. for bbox in bboxes:
  125. #threes#
  126. object_new = doc.createElement("object")
  127. annotation.appendChild(object_new)
  128. name = doc.createElement('name')
  129. object_new.appendChild(name)
  130. name_txt = doc.createTextNode(str(bbox[-1]))
  131. name.appendChild(name_txt)
  132. pose = doc.createElement('pose')
  133. object_new.appendChild(pose)
  134. pose_txt = doc.createTextNode("Unspecified")
  135. pose.appendChild(pose_txt)
  136. truncated = doc.createElement('truncated')
  137. object_new.appendChild(truncated)
  138. truncated_txt = doc.createTextNode("0")
  139. truncated.appendChild(truncated_txt)
  140. difficult = doc.createElement('difficult')
  141. object_new.appendChild(difficult)
  142. difficult_txt = doc.createTextNode("0")
  143. difficult.appendChild(difficult_txt)
  144. #threes-1#
  145. bndbox = doc.createElement('bndbox')
  146. object_new.appendChild(bndbox)
  147. if hbb:
  148. xmin = doc.createElement('xmin')
  149. bndbox.appendChild(xmin)
  150. xmin_txt = doc.createTextNode(str(bbox[0]))
  151. xmin.appendChild(xmin_txt)
  152. ymin = doc.createElement('ymin')
  153. bndbox.appendChild(ymin)
  154. ymin_txt = doc.createTextNode(str(bbox[1]))
  155. ymin.appendChild(ymin_txt)
  156. xmax = doc.createElement('xmax')
  157. bndbox.appendChild(xmax)
  158. xmax_txt = doc.createTextNode(str(bbox[2]))
  159. xmax.appendChild(xmax_txt)
  160. ymax = doc.createElement('ymax')
  161. bndbox.appendChild(ymax)
  162. ymax_txt = doc.createTextNode(str(bbox[3]))
  163. ymax.appendChild(ymax_txt)
  164. else:
  165. x0 = doc.createElement('x0')
  166. bndbox.appendChild(x0)
  167. x0_txt = doc.createTextNode(str(bbox[0]))
  168. x0.appendChild(x0_txt)
  169. y0 = doc.createElement('y0')
  170. bndbox.appendChild(y0)
  171. y0_txt = doc.createTextNode(str(bbox[1]))
  172. y0.appendChild(y0_txt)
  173. x1 = doc.createElement('x1')
  174. bndbox.appendChild(x1)
  175. x1_txt = doc.createTextNode(str(bbox[2]))
  176. x1.appendChild(x1_txt)
  177. y1 = doc.createElement('y1')
  178. bndbox.appendChild(y1)
  179. y1_txt = doc.createTextNode(str(bbox[3]))
  180. y1.appendChild(y1_txt)
  181. x2 = doc.createElement('x2')
  182. bndbox.appendChild(x2)
  183. x2_txt = doc.createTextNode(str(bbox[4]))
  184. x2.appendChild(x2_txt)
  185. y2 = doc.createElement('y2')
  186. bndbox.appendChild(y2)
  187. y2_txt = doc.createTextNode(str(bbox[5]))
  188. y2.appendChild(y2_txt)
  189. x3 = doc.createElement('x3')
  190. bndbox.appendChild(x3)
  191. x3_txt = doc.createTextNode(str(bbox[6]))
  192. x3.appendChild(x3_txt)
  193. y3 = doc.createElement('y3')
  194. bndbox.appendChild(y3)
  195. y3_txt = doc.createTextNode(str(bbox[7]))
  196. y3.appendChild(y3_txt)
  197. xmlname = os.path.splitext(imgname)[0]
  198. tempfile = os.path.join(tmp ,xmlname+'.xml')
  199. with open(tempfile, 'wb') as f:
  200. f.write(doc.toprettyxml(indent='\t', encoding='utf-8'))
  201. return
  202. if __name__ == '__main__':
  203. data_path = '/home/yantianwang/lala/ship/train/examplesplit'
  204. images_path = os.path.join(data_path, 'images') #样本图片路径
  205. labeltxt_path = os.path.join(data_path, 'labelTxt') #DOTA标签的所在路径
  206. anno_new_path = os.path.join(data_path, 'obbxml') #新的voc格式存储位置(hbb形式)
  207. ext = '.tif' #样本图片的后缀
  208. filenames=os.listdir(labeltxt_path) #获取每一个txt的名称
  209. for filename in filenames:
  210. filepath=labeltxt_path + '/'+filename #每一个DOTA标签的具体路径
  211. picname = os.path.splitext(filename)[0] + ext
  212. pic_path = os.path.join(images_path, picname)
  213. im= cv2.imread(pic_path) #读取相应的图片
  214. (H,W,D) = im.shape #返回样本的大小
  215. boxes = readlabeltxt(filepath, H, W, hbb = True) #默认是矩形(hbb)得到gt
  216. if len(boxes)==0:
  217. print('文件为空',filepath)
  218. #读取对应的样本图片,得到H,W,D用于书写xml
  219. #书写xml
  220. writeXml(anno_new_path, picname, W, H, D, boxes, hbb = True)
  221. print('正在处理%s'%filename)

 

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

闽ICP备14008679号