当前位置:   article > 正文

Python手动修改FAIR1M数据集标签类别_fair1m是什么格式

fair1m是什么格式
  1. import cv2
  2. import os
  3. import random
  4. import xml.etree.ElementTree as ET
  5. import tkinter as tk
  6. from PIL import Image, ImageTk
  7. imgs_path = r"E:\data\FAIR1M\test\images"
  8. labels_path = r"E:\data\FAIR1M\test\test"
  9. classes = ['Boeing737', 'Boeing747', 'Boeing777', 'Boeing787', 'C919', 'A220', 'A321', 'A330', 'A350', 'ARJ21',
  10. 'other-airplane', 'Passenger Ship', 'Motorboat', 'Fishing Boat', 'Tugboat', 'Engineering Ship',
  11. 'Liquid Cargo Ship', 'Dry Cargo Ship', 'Warship', 'other-ship', 'Small Car', 'Bus', 'Cargo Truck',
  12. 'Dump Truck', 'Van', 'Trailer', 'Tractor', 'Excavator', 'Truck Tractor', 'other-vehicle', 'Basketball Court',
  13. 'Tennis Court', 'Football Field', 'Baseball Field', 'Intersection', 'Roundabout', 'Bridge']
  14. imgs_list = os.listdir(imgs_path)
  15. # random.shuffle(imgs_list)
  16. imgs_list.sort()
  17. labels_list = os.listdir(labels_path)
  18. print("请输入从第几个图片开始(上次处理处理到的图片):")
  19. start = int(input())
  20. cv2.namedWindow("FAIR1M", 0)
  21. # cv2.namedWindow("Obj", cv2.WINDOW_AUTOSIZE)
  22. def mouse(event, x, y, flags, param):
  23. if event == cv2.EVENT_LBUTTONDOWN:
  24. print(filename)
  25. cv2.waitKey(4000)
  26. def btn_click():
  27. print("11111")
  28. for i in range(start, len(imgs_list)):
  29. filename = imgs_list[i]
  30. img_num = filename.split(".")[0] # 获得图片的序号
  31. print("------------------------------------------------------------------------------")
  32. print("doing ", i, "-------", filename, "img.......")
  33. with open(os.path.join(labels_path, img_num + ".xml"), "r", encoding='UTF-8') as xml_file:
  34. tree = ET.parse(xml_file)
  35. root = tree.getroot()
  36. size = root.find('size')
  37. # w = int(size.find('width').text) # 图片的宽
  38. # h = int(size.find('height').text)
  39. # width = image.shape[1]
  40. # hight = image.shape[0]
  41. for obj in root.iter('object'):
  42. possibleresult = obj.find('possibleresult')
  43. cls_name = possibleresult.find('name').text
  44. probability = possibleresult.find('probability').text
  45. points = obj.find('points')
  46. bbox = []
  47. # if cls_name in ["Small Car", "Van", "Dump Truck", "Cargo Truck", "other-ship", "other-airplane", "other-vehicle",
  48. # 'Boeing737', 'Boeing747', 'Boeing777', 'Boeing787', 'C919', 'A220', 'A321', 'A330', 'A350', 'ARJ21']:
  49. if cls_name not in ["Bridge"]:
  50. continue
  51. _image = cv2.imread(os.path.join(imgs_path, img_num + ".tif"))
  52. # _image = image.copy()
  53. width, hight = _image.shape[1], _image.shape[0]
  54. x_l = []
  55. y_l = []
  56. for i in range(0, len(points) - 1): # 只遍历前四个点
  57. point = points[i]
  58. xy = point.text.split(",")
  59. x = int(float(xy[0]))
  60. y = int(float(xy[1]))
  61. bbox.append((x, y))
  62. x_l.append(x)
  63. y_l.append(y)
  64. x1 = min(x_l)
  65. y1 = min(y_l)
  66. x2 = max(x_l)
  67. y2 = max(y_l)
  68. # cv2.resizeWindow("Obj", (x2 - x1) * 1, (y2 - y1) * 1)
  69. if x1 == x2 or y1 == y2 or x1 < 0 or x2 < 0 or y1 < 0 or y2 < 0:
  70. continue
  71. # obj_img = _image[y1:y2, x1:x2]
  72. cv2.putText(_image, cls_name, (200, 300), cv2.FONT_HERSHEY_SIMPLEX, 2, (255, 0, 0), 2)
  73. # cv2.rectangle(_image, (x1, y1), (x2, y2), (0, 0, 255), 1, 4)
  74. cv2.putText(_image, probability[0:4], (x2, y2), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 255), 3)
  75. cv2.line(_image, bbox[0], bbox[1], (0, 0, 255), 1, 4)
  76. cv2.line(_image, bbox[1], bbox[2], (0, 0, 255), 1, 4)
  77. cv2.line(_image, bbox[2], bbox[3], (0, 0, 255), 1, 4)
  78. cv2.line(_image, bbox[3], bbox[0], (0, 0, 255), 1, 4)
  79. cv2.resizeWindow("FAIR1M", width, hight)
  80. cv2.imshow("FAIR1M", _image)
  81. # cv2.imshow("Obj", obj_img)
  82. print("当前目标的类别是:", cls_name, ",准确度是:", probability)
  83. print(x1, y1, x2, y2)
  84. print("宽:", (x2 - x1), ",高:", (y2 - y1))
  85. print("按键功能:回车->查看下一个目标;+->跳至下一个图片;")
  86. print("请输入想修改的类别(0~36):")
  87. cv2.waitKey(10)
  88. inp = input()
  89. if inp == "":
  90. print("已选择不修改...........")
  91. continue
  92. if inp == "+":
  93. break
  94. lab_num = int(inp)
  95. if lab_num < 0 or lab_num > 36:
  96. print("输入不合法,此目标修改作废.......")
  97. else:
  98. possibleresult.find('name').text = classes[lab_num]
  99. print("已经将该目标修改为:", classes[lab_num])
  100. # cv2.destroyWindow("Obj")
  101. tree.write(os.path.join(labels_path, img_num + ".xml"), encoding="UTF-8") # 更新xml文件
  102. # cv2.resizeWindow("FAIR1M", 800, 800)
  103. # cv2.imshow("FAIR1M", image)
  104. cv2.setMouseCallback("FAIR1M", mouse)
  105. cv2.waitKey(2500)
  106. cv2.destroyAllWindows()

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

闽ICP备14008679号