testing.py 849 B

123456789101112131415161718192021222324252627282930313233
  1. import json
  2. import os.path as osp
  3. import imgviz
  4. import labelme.utils
  5. def assert_labelfile_sanity(filename):
  6. assert osp.exists(filename)
  7. data = json.load(open(filename))
  8. assert 'imagePath' in data
  9. imageData = data.get('imageData', None)
  10. if imageData is None:
  11. parent_dir = osp.dirname(filename)
  12. img_file = osp.join(parent_dir, data['imagePath'])
  13. assert osp.exists(img_file)
  14. img = imgviz.io.imread(img_file)
  15. else:
  16. img = labelme.utils.img_b64_to_arr(imageData)
  17. H, W = img.shape[:2]
  18. assert H == data['imageHeight']
  19. assert W == data['imageWidth']
  20. assert 'shapes' in data
  21. for shape in data['shapes']:
  22. assert 'label' in shape
  23. assert 'points' in shape
  24. for x, y in shape['points']:
  25. assert 0 <= x <= W
  26. assert 0 <= y <= H