File size: 953 Bytes
4ee152a | 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 | import os
import json
from argparse import ArgumentParser
def process(
in_file,
out_file,
):
d = os.path.dirname(out_file)
os.makedirs(d, exist_ok=True)
try:
with open(in_file, 'r') as f:
data = json.load(f)
except:
with open(in_file, 'r') as f:
data = [json.loads(f) for l in f.readlines()]
for i, sample in enumerate(data):
if isinstance(sample, list):
assert len(sample) == 1
data[i] = sample[0]
if "image" in data[i]:
data[i]['images'] = [data[i].pop('image')]
with open(out_file, 'w') as f:
json.dump(data, f)
if __name__ == '__main__':
argparser = ArgumentParser()
argparser.add_argument("--input-file", type=str, required=True)
argparser.add_argument("--output-file", type=str, default='dataset.json')
args = argparser.parse_args()
process(args.input_file, args.output_file) |