当前位置:   article > 正文

1、rolableimg标注旋转框的xml文件转dota格式的txt文件;2、如何批量从指定文件夹中根据后缀名提取文件,并存到新文件夹中_rolabelimg转dota

rolabelimg转dota
  1. # *_* coding : UTF-8 *_*
  2. # 文件名称 :roxml_to_dota.py
  3. # 功能描述 :把rolabelimg标注的xml文件转换成dota能识别的xml文件
  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. import cv2
  9. import numpy as np
  10. def edit_xml(xml_file):
  11. if ".xml" not in xml_file:
  12. return
  13. tree = ET.parse(xml_file)
  14. objs = tree.findall('object')
  15. txt = xml_file.replace(".xml", ".txt")
  16. jpg = xml_file.replace(".xml", ".jpg") #图片是jpg格式
  17. src = cv2.imread(jpg, 1)
  18. with open(txt, 'w') as wf:
  19. wf.write("imagesource:GoogleEarth\n")
  20. wf.write("gsd:0.115726939386\n")
  21. for ix, obj in enumerate(objs):
  22. x0text = ""
  23. y0text = ""
  24. x1text = ""
  25. y1text = ""
  26. x2text = ""
  27. y2text = ""
  28. x3text = ""
  29. y3text = ""
  30. difficulttext = ""
  31. className = ""
  32. obj_type = obj.find('type')
  33. type = obj_type.text
  34. obj_name = obj.find('name')
  35. className = obj_name.text
  36. obj_difficult = obj.find('difficult')
  37. difficulttext = obj_difficult.text
  38. if type == 'bndbox': #若用rolabelimg标注的是水平框
  39. obj_bnd = obj.find('bndbox')
  40. obj_xmin = obj_bnd.find('xmin')
  41. obj_ymin = obj_bnd.find('ymin')
  42. obj_xmax = obj_bnd.find('xmax')
  43. obj_ymax = obj_bnd.find('ymax')
  44. xmin = float(obj_xmin.text)
  45. ymin = float(obj_ymin.text)
  46. xmax = float(obj_xmax.text)
  47. ymax = float(obj_ymax.text)
  48. x0text = str(xmin)
  49. y0text = str(ymin)
  50. x1text = str(xmax)
  51. y1text = str(ymin)
  52. x2text = str(xmin)
  53. y2text = str(ymax)
  54. x3text = str(xmax)
  55. y3text = str(ymax)
  56. points = np.array([[int(float(x0text)), int(float(y0text))], [int(float(x1text)), int(float(y1text))], [int(float(x2text)), int(float(y2text))],
  57. [int(float(x3text)), int(float(y3text))]], np.int32)
  58. cv2.polylines(src, [points], True, (255, 0, 0)) # 画任意多边
  59. elif type == 'robndbox': #若用rolabelimg标注的是旋转框
  60. obj_bnd = obj.find('robndbox')
  61. obj_bnd.tag = 'bndbox' # 修改节点名
  62. obj_cx = obj_bnd.find('cx')
  63. obj_cy = obj_bnd.find('cy')
  64. obj_w = obj_bnd.find('w')
  65. obj_h = obj_bnd.find('h')
  66. obj_angle = obj_bnd.find('angle')
  67. cx = float(obj_cx.text)
  68. cy = float(obj_cy.text)
  69. w = float(obj_w.text)
  70. h = float(obj_h.text)
  71. angle = float(obj_angle.text)
  72. x0text, y0text = rotatePoint(cx, cy, cx - w / 2, cy - h / 2, -angle)
  73. x1text, y1text = rotatePoint(cx, cy, cx + w / 2, cy - h / 2, -angle)
  74. x2text, y2text = rotatePoint(cx, cy, cx + w / 2, cy + h / 2, -angle)
  75. x3text, y3text = rotatePoint(cx, cy, cx - w / 2, cy + h / 2, -angle)
  76. points = np.array([[int(float(x0text)), int(float(y0text))], [int(float(x1text)), int(float(y1text))], [int(float(x2text)), int(float(y2text))],
  77. [int(float(x3text)), int(float(y3text))]], np.int32)
  78. cv2.polylines(src, [points], True, (255, 0, 0)) # 画任意多边形
  79. # print(x0text,y0text,x1text,y1text,x2text,y2text,x3text,y3text,className,difficulttext)
  80. wf.write(
  81. "{} {} {} {} {} {} {} {} {} {}\n".format(x0text, y0text, x1text, y1text, x2text, y2text, x3text, y3text,
  82. className, difficulttext))
  83. # 转换成四点坐标
  84. def rotatePoint(xc, yc, xp, yp, theta):
  85. xoff = xp - xc;
  86. yoff = yp - yc;
  87. cosTheta = math.cos(theta)
  88. sinTheta = math.sin(theta)
  89. pResx = cosTheta * xoff + sinTheta * yoff
  90. pResy = - sinTheta * xoff + cosTheta * yoff
  91. return str(int(xc + pResx)), str(int(yc + pResy))
  92. if __name__ == '__main__':
  93. dir = "D:/soft/Pycharm/pythonProjectss/updatepicturesname/xml1130" #此处的xml1130文件夹中必须既要放jpg图片集又要放图片对应的xml文件不然会报打不开文件的错误!!
  94. filelist = os.listdir(dir)
  95. for file in filelist:
  96. edit_xml(os.path.join(dir, file))

中途报错:ValueError: invalid literal for int() with base 10问题处理

解决::把points=......那行代码先将字符串转换为浮点float,再将浮点数转化为整数int。

示例:a=int(float(123.456))

最终的txt文件内容如下:

  1. #功能描述:从指定文件夹中根据文件名后缀提取相应文件以txt文件为例,并存储到新文件夹中
  2. import os
  3. import shutil
  4. '''
  5. 参数描述:
  6. file_dir指读的文件目录;save_dir为保存文件的目录
  7. suffix用于存放打算提取的文件的后缀名;
  8. '''
  9. def filterfile(file_dir,save_dir,suffix):
  10. if os.path.exists(save_dir):
  11. shutil.rmtree(save_dir) #如果已经存在该文件夹,移除
  12. if not os.path.exists(save_dir):
  13. os.makedirs(save_dir) #如果不存在该文件夹,则创建,用于储存后续提取出来的文件
  14. filelist = [] #存储要copy的文件全名
  15. for dirpath,dirnames,filenames in os.walk(file_dir):#根据路径执行树状的遍历,分别遍历根目录,根目录下的文件夹,文件夹下的文件
  16. for file in filenames:#遍历文件夹中的文件
  17. file_type = file.split('.')[-1]#对文件名根据.进行分隔,实现文件名,后缀名的分离
  18. if(file_type in suffix):#下面根据后缀名是否在列表中,提取文件
  19. file_fullname = os.path.join(dirpath, file) #文件全名
  20. filelist.append(file_fullname)#将符合要求的文件存放在列表中
  21. for file in filelist:
  22. shutil.copy(file, save_dir)#将列表中的文件复制到新的文件夹
  23. if __name__ == "__main__":
  24. filterfile("xml1130","txtfiles",['txt'])

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

闽ICP备14008679号