-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchange_coco_root.py
More file actions
30 lines (23 loc) · 1016 Bytes
/
change_coco_root.py
File metadata and controls
30 lines (23 loc) · 1016 Bytes
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
import argparse
import json
import os
def build_parser():
parser = argparse.ArgumentParser()
parser.add_argument('-json', '--json-file', required=True, type=str)
parser.add_argument('-root', '--root-folder', required=True, type=str)
parser.add_argument('-new-root', '--new-root-folder', required=True, type=str)
parser.add_argument('-out', '--out-file', required=True, type=str)
return parser
def change_coco_root(json_dict, root_folder, new_root_folder):
for image in json_dict['images']:
file_name = image['file_name']
new_file_name = os.path.relpath(os.path.join(root_folder, file_name), new_root_folder)
image['file_name'] = new_file_name
if __name__ == '__main__':
parser = build_parser()
args = parser.parse_args()
with open(args.json_file, 'r') as f:
json_dict = json.load(f)
change_coco_root(json_dict, args.root_folder, args.new_root_folder)
with open(args.out_file, 'w') as f:
json.dump(json_dict, f, indent=2)