You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

convert_ic03.py 4.8 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. # Copyright 2020 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ============================================================================
  15. import os
  16. import argparse
  17. from xml.etree import ElementTree as ET
  18. from PIL import Image
  19. import numpy as np
  20. def init_args():
  21. parser = argparse.ArgumentParser('')
  22. parser.add_argument('-d', '--dataset_dir', type=str, default='./',
  23. help='path to original images')
  24. parser.add_argument('-x', '--xml_file', type=str, default='test.xml',
  25. help='Directory where character dictionaries for the dataset were stored')
  26. parser.add_argument('-o', '--output_dir', type=str, default='./processed',
  27. help='Directory where ord map dictionaries for the dataset were stored')
  28. parser.add_argument('-a', '--output_annotation', type=str, default='./annotation.txt',
  29. help='Directory where ord map dictionaries for the dataset were stored')
  30. return parser.parse_args()
  31. def xml_to_dict(xml_file, save_file=False):
  32. tree = ET.parse(xml_file)
  33. root = tree.getroot()
  34. imgs_labels = []
  35. for ch in root:
  36. im_label = {}
  37. for ch01 in ch:
  38. if ch01.tag in 'taggedRectangles':
  39. # multiple children
  40. rect_list = []
  41. for ch02 in ch01:
  42. rect = {}
  43. rect['location'] = ch02.attrib
  44. rect['label'] = ch02[0].text
  45. rect_list.append(rect)
  46. im_label['rect'] = rect_list
  47. else:
  48. im_label[ch01.tag] = ch01.text
  49. imgs_labels.append(im_label)
  50. if save_file:
  51. np.save("annotation_train.npy", imgs_labels)
  52. return imgs_labels
  53. def image_crop_save(image, location, output_dir):
  54. """
  55. crop image with location (h,w,x,y)
  56. save cropped image to output directory
  57. """
  58. # avoid negative value of coordinates in annotation
  59. start_x = np.maximum(location[2], 0)
  60. end_x = start_x + location[1]
  61. start_y = np.maximum(location[3], 0)
  62. end_y = start_y + location[0]
  63. print("image array shape :{}".format(image.shape))
  64. print("crop region ", start_x, end_x, start_y, end_y)
  65. if len(image.shape) == 3:
  66. cropped = image[start_y:end_y, start_x:end_x, :]
  67. else:
  68. cropped = image[start_y:end_y, start_x:end_x]
  69. im = Image.fromarray(np.uint8(cropped))
  70. im.save(output_dir)
  71. def convert():
  72. args = init_args()
  73. if not os.path.exists(args.dataset_dir):
  74. raise ValueError("dataset_dir :{} does not exist".format(args.dataset_dir))
  75. if not os.path.exists(args.xml_file):
  76. raise ValueError("xml_file :{} does not exist".format(args.xml_file))
  77. if not os.path.exists(args.output_dir):
  78. os.makedirs(args.output_dir)
  79. ims_labels_dict = xml_to_dict(args.xml_file, True)
  80. num_images = len(ims_labels_dict)
  81. annotation_list = []
  82. print("Converting annotation, {} images in total ".format(num_images))
  83. for i in range(num_images):
  84. img_label = ims_labels_dict[i]
  85. image_name = img_label['imageName']
  86. rects = img_label['rect']
  87. ext = image_name.split('.')[-1]
  88. name = image_name[:-(len(ext)+1)]
  89. fullpath = os.path.join(args.dataset_dir, image_name)
  90. im_array = np.asarray(Image.open(fullpath))
  91. print("processing image: {}".format(image_name))
  92. for j, rect in enumerate(rects):
  93. location = rect['location']
  94. h = int(float(location['height']))
  95. w = int(float(location['width']))
  96. x = int(float(location['x']))
  97. y = int(float(location['y']))
  98. label = rect['label']
  99. loc = [h, w, x, y]
  100. output_name = name.replace("/", "_") + "_" + str(j) + "_" + label + '.' + ext
  101. output_name = output_name.replace(",", "")
  102. output_file = os.path.join(args.output_dir, output_name)
  103. image_crop_save(im_array, loc, output_file)
  104. ann = output_name + "," + label + ','
  105. annotation_list.append(ann)
  106. ann_file = args.output_annotation
  107. with open(ann_file, 'w') as f:
  108. for line in annotation_list:
  109. txt = line + '\n'
  110. f.write(txt)
  111. if __name__ == "__main__":
  112. convert()