test_utils.py 729 B

12345678910111213141516171819202122232425262728
  1. import json
  2. import os.path as osp
  3. import numpy as np
  4. import PIL.Image
  5. import labelme
  6. here = osp.dirname(osp.abspath(__file__))
  7. data_dir = osp.join(here, 'data')
  8. def test_img_b64_to_arr():
  9. json_file = osp.join(data_dir, 'apc2016_obj3.json')
  10. data = json.load(open(json_file))
  11. img_b64 = data['imageData']
  12. img = labelme.utils.img_b64_to_arr(img_b64)
  13. assert img.dtype == np.uint8
  14. assert img.shape == (907, 1210, 3)
  15. def test_img_arr_to_b64():
  16. img_file = osp.join(data_dir, 'apc2016_obj3.jpg')
  17. img_arr = np.asarray(PIL.Image.open(img_file))
  18. img_b64 = labelme.utils.img_arr_to_b64(img_arr)
  19. img_arr2 = labelme.utils.img_b64_to_arr(img_b64)
  20. np.testing.assert_allclose(img_arr, img_arr2)