-
Notifications
You must be signed in to change notification settings - Fork 4
/
prepare.py
61 lines (53 loc) · 2.03 KB
/
prepare.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
58
59
60
61
import argparse
from glob import glob
from random import shuffle
import os, sys
from PIL import Image
import numpy as np
parser = argparse.ArgumentParser()
add_arg = parser.add_argument
add_arg('--train', default='./train/', type=str, help='Path to training images')
add_arg('--test', default='./test/SR_testing_datasets/', type=str, help='Path to testing images')
add_arg('--output', default='./data/', type=str, help='npy\'s will be stored here.')
args = parser.parse_args()
if __name__ == '__main__':
train_path = args.output + 'train.npy'
test_path = args.output + 'test.npy'
train_files = glob(args.train + '/*.jpeg')
test_files = glob(args.test+ '/**/*.*', recursive=True)
training_set_size = len(train_files)
testing_set_size = len(test_files)
print(testing_set_size)
shuffle(train_files)
shuffle(test_files)
if not os.path.exists(args.output):
os.makedirs(args.output)
train_imgs = np.empty(shape=(training_set_size, 128, 128, 3), dtype=np.uint8)
print('Preparing Training Data...')
for i,infile in enumerate(train_files):
if i % 1000 == 0:
print(str(i)+ '/' +str(training_set_size))
try:
im = Image.open(infile)
rgbimg = Image.new('RGB', im.size)
rgbimg.paste(im)
img = rgbimg.resize((128,128))
train_imgs[i] = np.asarray(img)
except IOError:
print("cannot prepare '%s'" % infile)
np.save(train_path,train_imgs)
print('Training Data Done.')
test_imgs = np.empty(shape=(testing_set_size, 128, 128, 3), dtype=np.uint8)
print('Preparing Testing Data...')
for i,infile in enumerate(test_files):
try:
im = Image.open(infile)
rgbimg = Image.new('RGB', im.size)
rgbimg.paste(im)
img = rgbimg.resize((128,128))
test_imgs[i] = np.asarray(img)
except IOError:
print("cannot prepare '%s'" % infile)
print('Testing Data Done.')
print(test_imgs.shape)
np.save(test_path,test_imgs)