当前位置:   article > 正文

Python FAIR1M数据集 计算框的重叠 并去重 美化_polygon.intersection).area

polygon.intersection).area
  1. from shapely.geometry import Polygon
  2. import numpy as np
  3. from xml.etree import ElementTree as ET
  4. from xml.etree.ElementTree import ElementTree, Element
  5. import os
  6. iou_thr = 0.8
  7. xmls_path = r"E:\data\FAIR1M\test\labelXmlPre"
  8. xml_list = os.listdir(xmls_path)
  9. save_dir = r"E:\data\FAIR1M\test\test"
  10. # 美化xml
  11. def prettyXml(element, indent, newline, level = 0): # elemnt为传进来的Elment类,参数indent用于缩进,newline用于换行
  12. if element: # 判断element是否有子元素
  13. if element.text == None or element.text.isspace(): # 如果element的text没有内容
  14. element.text = newline + indent * (level + 1)
  15. else:
  16. element.text = newline + indent * (level + 1) + element.text.strip() + newline + indent * (level + 1)
  17. #else: # 此处两行如果把注释去掉,Element的text也会另起一行
  18. #element.text = newline + indent * (level + 1) + element.text.strip() + newline + indent * level
  19. temp = list(element) # 将elemnt转成list
  20. for subelement in temp:
  21. if temp.index(subelement) < (len(temp) - 1): # 如果不是list的最后一个元素,说明下一个行是同级别元素的起始,缩进应一致
  22. subelement.tail = newline + indent * (level + 1)
  23. else: # 如果是list的最后一个元素, 说明下一行是母元素的结束,缩进应该少一个
  24. subelement.tail = newline + indent * level
  25. prettyXml(subelement, indent, newline, level = level + 1) # 对子元素进行递归操作
  26. # 创建一个xml文件,并带有基本信息
  27. def create_xml(bbox_list, remove_bbox, save_dir, xml_name):
  28. annotation = Element("annotation")
  29. # 创建source及其子元素, 并将内容追加到annotation
  30. source = Element("source")
  31. file_name = Element("filename")
  32. file_name.text = xml_name[0:-4] + ".tif"
  33. origin = Element("origin")
  34. origin.text = "GF2/GF3"
  35. source.append(file_name)
  36. source.append(origin)
  37. annotation.append(source)
  38. # 创建research及其子元素
  39. research = Element("research")
  40. version = Element("version")
  41. version.text = '1.0'
  42. provider = Element("provider")
  43. provider.text = "AIRCAS"
  44. author = Element("author")
  45. author.text = "QLKT-UPUP"
  46. pluginname = Element("pluginname")
  47. pluginname.text = "FAIR1M"
  48. pluginclass = Element("pluginclass")
  49. pluginclass.text = "object detection"
  50. time = Element("time")
  51. time.text = "2021-09-02"
  52. research.append(version)
  53. research.append(provider)
  54. research.append(author)
  55. research.append(pluginname)
  56. research.append(pluginclass)
  57. research.append(time)
  58. annotation.append(research)
  59. # 创建objects
  60. objects = Element("objects")
  61. for i in range(len(bbox_list)):
  62. if i not in remove_bbox:
  63. # -------------------将剩余的object加入------------------
  64. obj = Element("object")
  65. coordinate = Element("coordinate")
  66. coordinate.text = "pixel"
  67. typ = Element("type")
  68. typ.text = "rectangle"
  69. description = Element("description")
  70. description.text = "None"
  71. possibleresult = Element("possibleresult")
  72. name = Element("name")
  73. name.text = bbox_list[i][0]
  74. probability = Element("probability")
  75. probability.text = str(bbox_list[i][1])
  76. possibleresult.append(name)
  77. possibleresult.append(probability)
  78. points = Element("points")
  79. point1 = Element("point")
  80. point1.text = str(bbox_list[i][2]) + ", " + str(bbox_list[i][3])
  81. point2 = Element("point")
  82. point2.text = str(bbox_list[i][4]) + ", " + str(bbox_list[i][5])
  83. point3 = Element("point")
  84. point3.text = str(bbox_list[i][6]) + ", " + str(bbox_list[i][7])
  85. point4 = Element("point")
  86. point4.text = str(bbox_list[i][8]) + ", " + str(bbox_list[i][9])
  87. point5 = Element("point")
  88. point5.text = str(bbox_list[i][2]) + ", " + str(bbox_list[i][3])
  89. points.append(point1)
  90. points.append(point2)
  91. points.append(point3)
  92. points.append(point4)
  93. points.append(point5)
  94. obj.append(coordinate)
  95. obj.append(typ)
  96. obj.append(description)
  97. obj.append(possibleresult)
  98. obj.append(points)
  99. objects.append(obj)
  100. # print(1111111111111111)
  101. # -----------------------------------------------------
  102. annotation.append(objects)
  103. # 美化xml文件的结果
  104. prettyXml(annotation, '\t', '\n')
  105. tree = ElementTree(annotation)
  106. # 将结果保存到文件
  107. tree.write(os.path.join(save_dir, xml_name), encoding='utf-8', xml_declaration=True)
  108. def intersection(g, p):
  109. g = np.asarray(g)
  110. p = np.asarray(p)
  111. g = Polygon(g[0:8].reshape((4, 2)))
  112. p = Polygon(p[0:8].reshape((4, 2)))
  113. if not g.is_valid or not p.is_valid:
  114. return 0
  115. inter = Polygon(g).intersection(Polygon(p)).area
  116. union = g.area + p.area - inter
  117. if union == 0:
  118. return 0
  119. else:
  120. return inter / union
  121. for xml_name in xml_list:
  122. print("-----------------------", xml_name, "----------------------------")
  123. file_path = os.path.join(xmls_path, xml_name)
  124. tree = ET.parse(file_path)
  125. annotation = tree.getroot()
  126. objects = annotation.find("objects")
  127. bbox_list = []
  128. for obj in objects:
  129. bbox = []
  130. possibleresult = obj.find('possibleresult')
  131. cls_name = possibleresult.find('name').text
  132. probability = float(possibleresult.find('probability').text)
  133. bbox.append(cls_name)
  134. bbox.append(probability)
  135. points = obj.find('points')
  136. for i in range(0, len(points) - 1): # 只遍历前四个点
  137. point = points[i]
  138. xy = point.text.split(",")
  139. x = float(xy[0])
  140. y = float(xy[1])
  141. bbox.append(float('%.2f' % x))
  142. bbox.append(float('%.2f' % y))
  143. bbox_list.append(bbox)
  144. remove_bbox = []
  145. for i in range(len(bbox_list)):
  146. # print("____________________________")
  147. bbox1 = bbox_list[i]
  148. for j in range(i + 1, len(bbox_list)):
  149. bbox2 = bbox_list[j]
  150. iou = intersection(bbox1[2:10], bbox2[2:10])
  151. if iou > iou_thr:
  152. if bbox1[1] < bbox2[1] and bbox1[1] < 0.11: # 比较两个目标的score
  153. if i not in remove_bbox:
  154. remove_bbox.append(i)
  155. elif bbox2[1] < 0.11:
  156. if j not in remove_bbox:
  157. remove_bbox.append(j)
  158. create_xml(bbox_list, remove_bbox, save_dir, xml_name)
  159. # print(iou)

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

闽ICP备14008679号