-
Notifications
You must be signed in to change notification settings - Fork 0
/
process_data_dir.py
37 lines (28 loc) · 1.11 KB
/
process_data_dir.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
from pathlib import Path
from os.path import join as opj
import json
import argparse
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--base_dir', default='/tank/qbeer/mammo_annotations_v3', type=str)
parser.add_argument('--output_json', default='data.json', type=str)
args = parser.parse_args()
base = Path(args.base_dir)
data = {}
for path in base.iterdir():
if path.is_file() and path.suffix == '.json':
try:
_id_ = path.stem
data[_id_] = {}
data[_id_]['annotations'] = str(path)
data[_id_]['images'] = []
for image_path in Path(opj(base, _id_)).iterdir():
if image_path.is_file() and image_path.suffix == '.dcm' and not image_path.stem.endswith('raw'):
data[_id_]['images'].append(str(image_path))
except Exception as e:
print(e)
continue
with open(args.output_json, 'w') as fp:
json.dump(data, fp, indent=4, sort_keys=True)
if __name__ == "__main__":
exit(main())