util.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import json
  2. import os.path as osp
  3. from labelme.utils import image as image_module
  4. from labelme.utils import shape as shape_module
  5. here = osp.dirname(osp.abspath(__file__))
  6. data_dir = osp.join(here, "../data")
  7. def get_img_and_data():
  8. json_file = osp.join(data_dir, "annotated_with_data/apc2016_obj3.json")
  9. with open(json_file) as f:
  10. data = json.load(f)
  11. img_b64 = data["imageData"]
  12. img = image_module.img_b64_to_arr(img_b64)
  13. return img, data
  14. def get_img_and_lbl():
  15. img, data = get_img_and_data()
  16. label_name_to_value = {"__background__": 0}
  17. for shape in data["shapes"]:
  18. label_name = shape["label"]
  19. label_value = len(label_name_to_value)
  20. label_name_to_value[label_name] = label_value
  21. n_labels = max(label_name_to_value.values()) + 1
  22. label_names = [None] * n_labels
  23. for label_name, label_value in label_name_to_value.items():
  24. label_names[label_value] = label_name
  25. lbl, _ = shape_module.shapes_to_label(
  26. img.shape, data["shapes"], label_name_to_value
  27. )
  28. return img, lbl, label_names