import json import filecmp from pathlib import Path from shutil import copy def read_json(json_path): with open(json_path, "r") as f: d = json.load(f) return d def write_json(json_path, dic): with open(json_path, "w") as f: json.dump(dic, f) print(f"Wrote json to {json_path}") def get_setname(json_path): json_path_ = Path(json_path) dataset_nm = json_path_.parent.parts[-2] print(f"Processing {dataset_nm} (name derived from json path)") return dataset_nm def read_coco_json(coco_json): coco_dict = read_json(coco_json) return coco_dict def assure_copy(src, dst): assert Path(src).is_file() if Path(dst).is_file() and filecmp.cmp(src, dst, shallow=True): return Path(dst).parent.mkdir(exist_ok=True, parents=True) copy(src, dst) def path(str_path, is_dir=False, mkdir=False): path_ = Path(str_path) if is_dir: if mkdir: path_.mkdir(parents=True, exist_ok=True) assert path_.is_dir(), path_ else: assert path_.is_file(), path_ return path_