-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreprocessing.py
57 lines (50 loc) · 1.96 KB
/
preprocessing.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
55
56
57
import argparse
import glob
from scipy import misc
from utils import dataAugmentation,createGaussianLabel
import numpy as np
def get_parser():
parser = argparse.ArgumentParser('preprocess')
parser.add_argument('--inputPath', '-i', required=True)
parser.add_argument('--outputPath', '-o', required=True)
parser.add_argument('--outputsize','-s', type=sizes,default=(256,256,3))
parser.add_argument("--augmentation", '-a', type=str2bool, nargs='?',const=True, default=False)
parser.add_argument("--GaussianSize", '-g', type=int, default=9)
return parser
def str2bool(v):
if isinstance(v, bool):
return v
if v.lower() in ('yes', 'true', 't', 'y', '1'):
return True
elif v.lower() in ('no', 'false', 'f', 'n', '0'):
return False
else:
raise argparse.ArgumentTypeError('Boolean value expected.')
def sizes(s):
try:
x, y, c = map(int, s.split(','))
return (x, y, c)
except:
raise argparse.ArgumentTypeError("size must be x,y,c")
def preprocess(args=None):
parser = get_parser()
args = parser.parse_args(args)
jpgFiles = glob.glob(args.inputPath+'/'+'*.jpg')
for f in jpgFiles:
print("processing file"+f)
image=misc.imread(f)
img=misc.imresize(image,args.outputsize)
label=createGaussianLabel(f.replace(".jpg",".json"),(image.shape[0],image.shape[1],2),image.shape,args.GaussianSize)
if args.augmentation:
images,labels=dataAugmentation([img],[label])
for i in range(len(images)):
name=args.outputPath+'/'+(f.replace(".jpg","").split('/')[-1])+"_"+str(i)
misc.imsave(name+'.jpg',images[i])
np.save(name+'.npy',labels[i].astype(np.uint8))
else:
name=args.outputPath+(f.replace(".jpg","").split('/')[-1])
print(name)
misc.imsave(name+'.jpg',img)
np.save(name+'.npy',label)
if __name__ == "__main__":
preprocess()