json_to_dataset.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import argparse
  2. import base64
  3. import json
  4. import os
  5. import os.path as osp
  6. import warnings
  7. import PIL.Image
  8. import yaml
  9. from ..logger import logger
  10. from .. import utils
  11. def main():
  12. logger.warning('This script is aimed to demonstrate how to convert the'
  13. 'JSON file to a single image dataset, and not to handle'
  14. 'multiple JSON files to generate a real-use dataset.')
  15. parser = argparse.ArgumentParser()
  16. parser.add_argument('json_file')
  17. parser.add_argument('-o', '--out', default=None)
  18. args = parser.parse_args()
  19. json_file = args.json_file
  20. if args.out is None:
  21. out_dir = osp.basename(json_file).replace('.', '_')
  22. out_dir = osp.join(osp.dirname(json_file), out_dir)
  23. else:
  24. out_dir = args.out
  25. if not osp.exists(out_dir):
  26. os.mkdir(out_dir)
  27. data = json.load(open(json_file))
  28. if data['imageData']:
  29. imageData = data['imageData']
  30. else:
  31. imagePath = os.path.join(os.path.dirname(json_file), data['imagePath'])
  32. with open(imagePath, 'rb') as f:
  33. imageData = f.read()
  34. imageData = base64.b64encode(imageData).decode('utf-8')
  35. img = utils.img_b64_to_arr(imageData)
  36. label_name_to_value = {'_background_': 0}
  37. for shape in sorted(data['shapes'], key=lambda x: x['label']):
  38. label_name = shape['label']
  39. if label_name in label_name_to_value:
  40. label_value = label_name_to_value[label_name]
  41. else:
  42. label_value = len(label_name_to_value)
  43. label_name_to_value[label_name] = label_value
  44. lbl = utils.shapes_to_label(img.shape, data['shapes'], label_name_to_value)
  45. label_names = [None] * (max(label_name_to_value.values()) + 1)
  46. for name, value in label_name_to_value.items():
  47. label_names[value] = name
  48. lbl_viz = utils.draw_label(lbl, img, label_names)
  49. PIL.Image.fromarray(img).save(osp.join(out_dir, 'img.png'))
  50. utils.lblsave(osp.join(out_dir, 'label.png'), lbl)
  51. PIL.Image.fromarray(lbl_viz).save(osp.join(out_dir, 'label_viz.png'))
  52. with open(osp.join(out_dir, 'label_names.txt'), 'w') as f:
  53. for lbl_name in label_names:
  54. f.write(lbl_name + '\n')
  55. logger.warning('info.yaml is being replaced by label_names.txt')
  56. info = dict(label_names=label_names)
  57. with open(osp.join(out_dir, 'info.yaml'), 'w') as f:
  58. yaml.safe_dump(info, f, default_flow_style=False)
  59. logger.info('Saved to: {}'.format(out_dir))
  60. if __name__ == '__main__':
  61. main()