-
Notifications
You must be signed in to change notification settings - Fork 10
/
parse_json.py
57 lines (50 loc) · 1.68 KB
/
parse_json.py
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
50
51
52
53
54
55
56
57
import json, os
import argparse
from tqdm import tqdm
def parse_args():
parser = argparse.ArgumentParser(description="Train FGVC Network")
parser.add_argument(
"--file",
help="json file to be converted",
required=True,
type=str,
)
parser.add_argument(
"--root",
help="root path to save image",
type=str,
required=True,
)
parser.add_argument(
"--sp",
help="save path for converted file ",
type=str,
required=False,
default="."
)
args = parser.parse_args()
return args
def convert(json_file, image_root):
all_annos = json.load(open(json_file, 'r'))
import ipdb; ipdb.set_trace()
annos = all_annos['annotations']
images = all_annos['images']
new_annos = []
print("Converting file {} ...".format(json_file))
for anno, image in tqdm(zip(annos, images)):
assert image["id"] == anno["id"]
new_annos.append({"image_id": image["id"],
"im_height": image["height"],
"im_width": image["width"],
"category_id": anno["category_id"],
"fpath": os.path.join(image_root, image["file_name"])})
num_classes = len(all_annos["categories"])
return {"annotations": new_annos,
"num_classes": num_classes}
if __name__ == "__main__":
args = parse_args()
converted_annos = convert(args.file, args.root)
save_path = os.path.join(args.sp, "converted_" + os.path.split(args.file)[-1])
print("Converted, Saveing converted file to {}".format(save_path))
with open(save_path, "w") as f:
json.dump(converted_annos, f)