labelme2voc.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #!/usr/bin/env python
  2. from __future__ import print_function
  3. import argparse
  4. import glob
  5. import json
  6. import os
  7. import os.path as osp
  8. import sys
  9. try:
  10. import lxml.builder
  11. import lxml.etree
  12. except ImportError:
  13. print('Please install lxml:\n\n pip install lxml\n')
  14. sys.exit(1)
  15. import numpy as np
  16. import PIL.Image
  17. import labelme
  18. def main():
  19. parser = argparse.ArgumentParser(
  20. formatter_class=argparse.ArgumentDefaultsHelpFormatter
  21. )
  22. parser.add_argument('input_dir', help='input annotated directory')
  23. parser.add_argument('output_dir', help='output dataset directory')
  24. parser.add_argument('--labels', help='labels file', required=True)
  25. parser.add_argument(
  26. '--noviz', help='no visualization', action='store_true'
  27. )
  28. args = parser.parse_args()
  29. if osp.exists(args.output_dir):
  30. print('Output directory already exists:', args.output_dir)
  31. sys.exit(1)
  32. os.makedirs(args.output_dir)
  33. os.makedirs(osp.join(args.output_dir, 'JPEGImages'))
  34. os.makedirs(osp.join(args.output_dir, 'Annotations'))
  35. if not args.noviz:
  36. os.makedirs(osp.join(args.output_dir, 'AnnotationsVisualization'))
  37. print('Creating dataset:', args.output_dir)
  38. class_names = []
  39. class_name_to_id = {}
  40. for i, line in enumerate(open(args.labels).readlines()):
  41. class_id = i - 1 # starts with -1
  42. class_name = line.strip()
  43. class_name_to_id[class_name] = class_id
  44. if class_id == -1:
  45. assert class_name == '__ignore__'
  46. continue
  47. elif class_id == 0:
  48. assert class_name == '_background_'
  49. class_names.append(class_name)
  50. class_names = tuple(class_names)
  51. print('class_names:', class_names)
  52. out_class_names_file = osp.join(args.output_dir, 'class_names.txt')
  53. with open(out_class_names_file, 'w') as f:
  54. f.writelines('\n'.join(class_names))
  55. print('Saved class_names:', out_class_names_file)
  56. for label_file in glob.glob(osp.join(args.input_dir, '*.json')):
  57. print('Generating dataset from:', label_file)
  58. with open(label_file) as f:
  59. data = json.load(f)
  60. base = osp.splitext(osp.basename(label_file))[0]
  61. out_img_file = osp.join(
  62. args.output_dir, 'JPEGImages', base + '.jpg')
  63. out_xml_file = osp.join(
  64. args.output_dir, 'Annotations', base + '.xml')
  65. if not args.noviz:
  66. out_viz_file = osp.join(
  67. args.output_dir, 'AnnotationsVisualization', base + '.jpg')
  68. img_file = osp.join(osp.dirname(label_file), data['imagePath'])
  69. img = np.asarray(PIL.Image.open(img_file))
  70. PIL.Image.fromarray(img).save(out_img_file)
  71. maker = lxml.builder.ElementMaker()
  72. xml = maker.annotation(
  73. maker.folder(),
  74. maker.filename(base + '.jpg'),
  75. maker.database(), # e.g., The VOC2007 Database
  76. maker.annotation(), # e.g., Pascal VOC2007
  77. maker.image(), # e.g., flickr
  78. maker.size(
  79. maker.height(str(img.shape[0])),
  80. maker.width(str(img.shape[1])),
  81. maker.depth(str(img.shape[2])),
  82. ),
  83. maker.segmented(),
  84. )
  85. bboxes = []
  86. labels = []
  87. for shape in data['shapes']:
  88. if shape['shape_type'] != 'rectangle':
  89. print('Skipping shape: label={label}, shape_type={shape_type}'
  90. .format(**shape))
  91. continue
  92. class_name = shape['label']
  93. class_id = class_names.index(class_name)
  94. (xmin, ymin), (xmax, ymax) = shape['points']
  95. # swap if min is larger than max.
  96. xmin, xmax = sorted([xmin, xmax])
  97. ymin, ymax = sorted([ymin, ymax])
  98. bboxes.append([xmin, ymin, xmax, ymax])
  99. labels.append(class_id)
  100. xml.append(
  101. maker.object(
  102. maker.name(shape['label']),
  103. maker.pose(),
  104. maker.truncated(),
  105. maker.difficult(),
  106. maker.bndbox(
  107. maker.xmin(str(xmin)),
  108. maker.ymin(str(ymin)),
  109. maker.xmax(str(xmax)),
  110. maker.ymax(str(ymax)),
  111. ),
  112. )
  113. )
  114. if not args.noviz:
  115. captions = [class_names[l] for l in labels]
  116. viz = labelme.utils.draw_instances(
  117. img, bboxes, labels, captions=captions
  118. )
  119. PIL.Image.fromarray(viz).save(out_viz_file)
  120. with open(out_xml_file, 'wb') as f:
  121. f.write(lxml.etree.tostring(xml, pretty_print=True))
  122. if __name__ == '__main__':
  123. main()