x2voc.py 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #!/usr/bin/env python
  2. # coding: utf-8
  3. # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. import cv2
  17. import json
  18. import os
  19. import os.path as osp
  20. import shutil
  21. import numpy as np
  22. from .base import MyEncoder, is_pic, get_encoding
  23. class X2VOC(object):
  24. def __init__(self):
  25. pass
  26. def convert(self, image_dir, json_dir, dataset_save_dir):
  27. """转换。
  28. Args:
  29. image_dir (str): 图像文件存放的路径。
  30. json_dir (str): 与每张图像对应的json文件的存放路径。
  31. dataset_save_dir (str): 转换后数据集存放路径。
  32. """
  33. assert osp.exists(image_dir), "The image folder does not exist!"
  34. assert osp.exists(json_dir), "The json folder does not exist!"
  35. assert osp.exists(dataset_save_dir), "The save folder does not exist!"
  36. # Convert the image files.
  37. new_image_dir = osp.join(dataset_save_dir, "JPEGImages")
  38. if osp.exists(new_image_dir):
  39. shutil.rmtree(new_image_dir)
  40. os.makedirs(new_image_dir)
  41. for img_name in os.listdir(image_dir):
  42. if is_pic(img_name):
  43. shutil.copyfile(
  44. osp.join(image_dir, img_name),
  45. osp.join(new_image_dir, img_name))
  46. # Convert the json files.
  47. xml_dir = osp.join(dataset_save_dir, "Annotations")
  48. if osp.exists(xml_dir):
  49. shutil.rmtree(xml_dir)
  50. os.makedirs(xml_dir)
  51. self.json2xml(new_image_dir, json_dir, xml_dir)
  52. class LabelMe2VOC(X2VOC):
  53. """将使用LabelMe标注的数据集转换为VOC数据集。
  54. """
  55. def __init__(self):
  56. pass
  57. def json2xml(self, image_dir, json_dir, xml_dir):
  58. import xml.dom.minidom as minidom
  59. for img_name in os.listdir(image_dir):
  60. img_name_part = osp.splitext(img_name)[0]
  61. json_file = osp.join(json_dir, img_name_part + ".json")
  62. if not osp.exists(json_file):
  63. os.remove(os.remove(osp.join(image_dir, img_name)))
  64. continue
  65. xml_doc = minidom.Document()
  66. root = xml_doc.createElement("annotation")
  67. xml_doc.appendChild(root)
  68. node_folder = xml_doc.createElement("folder")
  69. node_folder.appendChild(xml_doc.createTextNode("JPEGImages"))
  70. root.appendChild(node_folder)
  71. node_filename = xml_doc.createElement("filename")
  72. node_filename.appendChild(xml_doc.createTextNode(img_name))
  73. root.appendChild(node_filename)
  74. with open(json_file, mode="r", \
  75. encoding=get_encoding(json_file)) as j:
  76. json_info = json.load(j)
  77. h = json_info["imageHeight"]
  78. w = json_info["imageWidth"]
  79. node_size = xml_doc.createElement("size")
  80. node_width = xml_doc.createElement("width")
  81. node_width.appendChild(xml_doc.createTextNode(str(w)))
  82. node_size.appendChild(node_width)
  83. node_height = xml_doc.createElement("height")
  84. node_height.appendChild(xml_doc.createTextNode(str(h)))
  85. node_size.appendChild(node_height)
  86. node_depth = xml_doc.createElement("depth")
  87. node_depth.appendChild(xml_doc.createTextNode(str(3)))
  88. node_size.appendChild(node_depth)
  89. root.appendChild(node_size)
  90. for shape in json_info["shapes"]:
  91. if shape["shape_type"] != "rectangle":
  92. continue
  93. label = shape["label"]
  94. (xmin, ymin), (xmax, ymax) = shape["points"]
  95. xmin, xmax = sorted([xmin, xmax])
  96. ymin, ymax = sorted([ymin, ymax])
  97. node_obj = xml_doc.createElement("object")
  98. node_name = xml_doc.createElement("name")
  99. node_name.appendChild(xml_doc.createTextNode(label))
  100. node_obj.appendChild(node_name)
  101. node_diff = xml_doc.createElement("difficult")
  102. node_diff.appendChild(xml_doc.createTextNode(str(0)))
  103. node_obj.appendChild(node_diff)
  104. node_box = xml_doc.createElement("bndbox")
  105. node_xmin = xml_doc.createElement("xmin")
  106. node_xmin.appendChild(xml_doc.createTextNode(str(xmin)))
  107. node_box.appendChild(node_xmin)
  108. node_ymin = xml_doc.createElement("ymin")
  109. node_ymin.appendChild(xml_doc.createTextNode(str(ymin)))
  110. node_box.appendChild(node_ymin)
  111. node_xmax = xml_doc.createElement("xmax")
  112. node_xmax.appendChild(xml_doc.createTextNode(str(xmax)))
  113. node_box.appendChild(node_xmax)
  114. node_ymax = xml_doc.createElement("ymax")
  115. node_ymax.appendChild(xml_doc.createTextNode(str(ymax)))
  116. node_box.appendChild(node_ymax)
  117. node_obj.appendChild(node_box)
  118. root.appendChild(node_obj)
  119. with open(osp.join(xml_dir, img_name_part + ".xml"), 'w') as fxml:
  120. xml_doc.writexml(fxml, indent='\t', addindent='\t', newl='\n', encoding="utf-8")
  121. class EasyData2VOC(X2VOC):
  122. """将使用EasyData标注的分割数据集转换为VOC数据集。
  123. """
  124. def __init__(self):
  125. pass
  126. def json2xml(self, image_dir, json_dir, xml_dir):
  127. import xml.dom.minidom as minidom
  128. for img_name in os.listdir(image_dir):
  129. img_name_part = osp.splitext(img_name)[0]
  130. json_file = osp.join(json_dir, img_name_part + ".json")
  131. if not osp.exists(json_file):
  132. os.remove(os.remove(osp.join(image_dir, img_name)))
  133. continue
  134. xml_doc = minidom.Document()
  135. root = xml_doc.createElement("annotation")
  136. xml_doc.appendChild(root)
  137. node_folder = xml_doc.createElement("folder")
  138. node_folder.appendChild(xml_doc.createTextNode("JPEGImages"))
  139. root.appendChild(node_folder)
  140. node_filename = xml_doc.createElement("filename")
  141. node_filename.appendChild(xml_doc.createTextNode(img_name))
  142. root.appendChild(node_filename)
  143. img = cv2.imread(osp.join(image_dir, img_name))
  144. h = img.shape[0]
  145. w = img.shape[1]
  146. node_size = xml_doc.createElement("size")
  147. node_width = xml_doc.createElement("width")
  148. node_width.appendChild(xml_doc.createTextNode(str(w)))
  149. node_size.appendChild(node_width)
  150. node_height = xml_doc.createElement("height")
  151. node_height.appendChild(xml_doc.createTextNode(str(h)))
  152. node_size.appendChild(node_height)
  153. node_depth = xml_doc.createElement("depth")
  154. node_depth.appendChild(xml_doc.createTextNode(str(3)))
  155. node_size.appendChild(node_depth)
  156. root.appendChild(node_size)
  157. with open(json_file, mode="r", \
  158. encoding=get_encoding(json_file)) as j:
  159. json_info = json.load(j)
  160. for shape in json_info["labels"]:
  161. label = shape["name"]
  162. xmin = shape["x1"]
  163. ymin = shape["y1"]
  164. xmax = shape["x2"]
  165. ymax = shape["y2"]
  166. node_obj = xml_doc.createElement("object")
  167. node_name = xml_doc.createElement("name")
  168. node_name.appendChild(xml_doc.createTextNode(label))
  169. node_obj.appendChild(node_name)
  170. node_diff = xml_doc.createElement("difficult")
  171. node_diff.appendChild(xml_doc.createTextNode(str(0)))
  172. node_obj.appendChild(node_diff)
  173. node_box = xml_doc.createElement("bndbox")
  174. node_xmin = xml_doc.createElement("xmin")
  175. node_xmin.appendChild(xml_doc.createTextNode(str(xmin)))
  176. node_box.appendChild(node_xmin)
  177. node_ymin = xml_doc.createElement("ymin")
  178. node_ymin.appendChild(xml_doc.createTextNode(str(ymin)))
  179. node_box.appendChild(node_ymin)
  180. node_xmax = xml_doc.createElement("xmax")
  181. node_xmax.appendChild(xml_doc.createTextNode(str(xmax)))
  182. node_box.appendChild(node_xmax)
  183. node_ymax = xml_doc.createElement("ymax")
  184. node_ymax.appendChild(xml_doc.createTextNode(str(ymax)))
  185. node_box.appendChild(node_ymax)
  186. node_obj.appendChild(node_box)
  187. root.appendChild(node_obj)
  188. with open(osp.join(xml_dir, img_name_part + ".xml"), 'w') as fxml:
  189. xml_doc.writexml(fxml, indent='\t', addindent='\t', newl='\n', encoding="utf-8")