util.py 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  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. data = json.load(open(json_file))
  10. img_b64 = data["imageData"]
  11. img = image_module.img_b64_to_arr(img_b64)
  12. return img, data
  13. def get_img_and_lbl():
  14. img, data = get_img_and_data()
  15. label_name_to_value = {"__background__": 0}
  16. for shape in data["shapes"]:
  17. label_name = shape["label"]
  18. label_value = len(label_name_to_value)
  19. label_name_to_value[label_name] = label_value
  20. n_labels = max(label_name_to_value.values()) + 1
  21. label_names = [None] * n_labels
  22. for label_name, label_value in label_name_to_value.items():
  23. label_names[label_value] = label_name
  24. lbl, _ = shape_module.shapes_to_label(
  25. img.shape, data["shapes"], label_name_to_value
  26. )
  27. return img, lbl, label_names