File size: 1,108 Bytes
76e628e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# read all txt files and save them in a metadata.jsonl file

import os
import glob
import argparse
import json

def readTxt(dir_path):
    # read all txt files
    for file in glob.glob(dir_path + '/*.json'):
        # read jsonl file
        with open(file, 'r') as f:
            # print the name of the file
            print(file)
            # read the lines
            lines = f.readlines()
            # create a dictionary
            metadata = {}
            # add the name of the file
            metadata["file_name"] = file.split("/")[-1].split('json')[0][:-1].split("\\")[-1] + '.jpg'
            # add the text
            metadata['ground_truth'] = lines[0].strip()

            print(metadata)

            # append the metadata to the metadata.jsonl file on the directory
            with open(os.path.join(dir_path, 'metadata.jsonl'), 'a') as f:
                f.write(json.dumps(metadata) + '\n')

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--dir_path', type=str, default='data')
    args = parser.parse_args()
    readTxt(args.dir_path)