-
Notifications
You must be signed in to change notification settings - Fork 9
/
prepare.py
65 lines (45 loc) · 1.73 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
62
63
64
import argparse
parser = argparse.ArgumentParser()
add_arg = parser.add_argument
add_arg('--input', default='cars/', type=str, \
help='\'OUTPUTDIR\' set in \'downloadAndExtract.sh\'.')
add_arg('--output', default='imgs/', type=str, \
help='Cropped images will be stored here.')
args = parser.parse_args()
from scipy.io import loadmat
from scipy.misc import imresize
from skimage.io import imread, imsave
from os.path import exists
from os import makedirs
from shutil import rmtree
if __name__ == '__main__':
print('Running ...')
if not exists(args.output):
makedirs(args.output)
path2train = args.input + 'cars_train/'
anns = loadmat(args.input + 'devkit/cars_train_annos.mat')['annotations']
for x1, y1, x2, y2, _, fname in anns[0]:
x1 = x1[0][0]
x2 = x2[0][0]
y1 = y1[0][0]
y2 = y2[0][0]
fname = fname[0]
img = imread(path2train + fname)
if len(img.shape) == 3 and x2 - x1 >= 128 and y2 - y1 >= 128:
img = imresize(img[y1:y2, x1:x2, :], size=(128, 128, 3))
imsave(args.output + fname, img)
path2test = args.input + 'cars_test/'
anns = loadmat(args.input + 'devkit/cars_test_annos.mat')['annotations']
for x1, y1, x2, y2, fname in anns[0]:
x1 = x1[0][0]
x2 = x2[0][0]
y1 = y1[0][0]
y2 = y2[0][0]
fname = fname[0]
img = imread(path2test + fname)
if len(img.shape) == 3 and x2 - x1 >= 128 and y2 - y1 >= 128:
img = imresize(img[y1:y2, x1:x2, :], size=(128, 128, 3))
imsave(args.output + '_' + fname, img)
print('Done.\nClearing downloaded archives and temporarily extracted files ...')
rmtree(args.input)
print('Done.')