-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreformatter.py
54 lines (39 loc) · 1.46 KB
/
reformatter.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
import os
import shutil
from glob import glob
from tqdm import tqdm
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument('--input_path', default='./dataset')
parser.add_argument('--output_path', default='./reformatted_dataset')
parser.add_argument('--method', default='move')
args = parser.parse_args()
trans_method = None
if args.method == "move":
trans_method = shutil.move
elif args.method == "copy":
trans_method = shutil.copy
else:
raise NotImplementedError
os.makedirs(args.output_path, exist_ok=True)
output_imgs_path = os.path.join(args.output_path, 'train_imgs')
output_text_path = os.path.join(args.output_path, 'train_gts')
os.makedirs(output_imgs_path, exist_ok=True)
os.makedirs(output_text_path, exist_ok=True)
train_list_file = open(os.path.join(args.output_path, 'train_list.txt'), 'w')
imgs_glob = args.input_path + '/**/*warped*.png'
print(imgs_glob)
for img in tqdm(glob(imgs_glob, recursive=True), desc="Iterating over images and texts ... "):
orig_text_path = img + '.txt'
img_name = ""
for part in img.split('/'):
if part not in ['.']:
img_name += '_' + part
img_name = img_name[1:]
txt_name = img_name + '.txt'
train_list_file.write(img_name + '\n')
dst_img = os.path.join(output_imgs_path, img_name)
dst_text = os.path.join(output_text_path, txt_name)
trans_method(img, dst_img)
trans_method(orig_text_path, dst_text)
train_list_file.close()