draw_json.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/usr/bin/env python
  2. import argparse
  3. import base64
  4. import json
  5. import os
  6. import sys
  7. import matplotlib.pyplot as plt
  8. from labelme import utils
  9. PY2 = sys.version_info[0] == 2
  10. def main():
  11. parser = argparse.ArgumentParser()
  12. parser.add_argument('json_file')
  13. args = parser.parse_args()
  14. json_file = args.json_file
  15. data = json.load(open(json_file))
  16. if data['imageData']:
  17. imageData = data['imageData']
  18. else:
  19. imagePath = os.path.join(os.path.dirname(json_file), data['imagePath'])
  20. with open(imagePath, 'rb') as f:
  21. imageData = f.read()
  22. imageData = base64.b64encode(imageData).decode('utf-8')
  23. img = utils.img_b64_to_arr(imageData)
  24. label_name_to_value = {'_background_': 0}
  25. for shape in data['shapes']:
  26. label_name = shape['label']
  27. if label_name in label_name_to_value:
  28. label_value = label_name_to_value[label_name]
  29. else:
  30. label_value = len(label_name_to_value)
  31. label_name_to_value[label_name] = label_value
  32. lbl = utils.shapes_to_label(
  33. img.shape, data['shapes'], label_name_to_value)
  34. captions = ['{}: {}'.format(lv, ln)
  35. for ln, lv in label_name_to_value.items()]
  36. lbl_viz = utils.draw_label(lbl, img, captions)
  37. plt.subplot(121)
  38. plt.imshow(img)
  39. plt.subplot(122)
  40. plt.imshow(lbl_viz)
  41. plt.show()
  42. if __name__ == '__main__':
  43. main()