File size: 1,088 Bytes
463b952
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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_