-
Notifications
You must be signed in to change notification settings - Fork 8
/
write_img_names.py
46 lines (33 loc) · 1.28 KB
/
write_img_names.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
import argparse, glob, os, numpy as np, math
parser = argparse.ArgumentParser()
parser.add_argument("-p", "--path", type=str, help="images path to write in the file")
parser.add_argument("-ptrain", "--ptrain", type=float, help="percentage for training between 0 and 1")
parser.add_argument("-v", "--verbose", action="store_true",
help="increase output verbosity")
args = parser.parse_args()
if args.path:
# files = glob.glob(os.path.join(args.path, '*.jpg'))
files = glob.glob(args.path)
np.random.shuffle(files)
ptrain = 0.95
if args.ptrain:
ptrain = args.ptrain
if files:
file_train = open("train.txt","w")
file_test = open("test.txt","w")
cont = 1
for filename in files:
filename = "data/img/{}".format(os.path.basename(filename))
if args.verbose:
type_file = ("train" if cont <= math.ceil(len(files)*ptrain) else "test")
print("writing in {}.txt '{}'".format(type_file, filename))
if cont <= math.ceil(len(files)*ptrain):
file_train.write("{}\n".format(filename))
else:
file_test.write("{}\n".format(filename))
cont += 1
file_train.close()
file_test.close()
print("Num train: \t{}\nNum test: \t{}".format(int(math.ceil(len(files)*ptrain)), int(len(files) - math.ceil(len(files)*(ptrain)))))
else:
print("No images")