forked from chsasank/Traffic-Sign-Classification.keras
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GTSRB_h5.py
80 lines (61 loc) · 2.18 KB
/
GTSRB_h5.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# Script to make hdf5 files from training and test set
import numpy as np
from skimage import io, color, exposure, transform
import pandas as pd
import os
import glob
import h5py
NUM_CLASSES = 43
IMG_SIZE = 48
def preprocess_img(img):
# Histogram normalization in y
hsv = color.rgb2hsv(img)
hsv[:,:,2] = exposure.equalize_hist(hsv[:,:,2])
img = color.hsv2rgb(hsv)
# central scrop
min_side = min(img.shape[:-1])
centre = img.shape[0]//2, img.shape[1]//2
img = img[centre[0]-min_side//2:centre[0]+min_side//2,
centre[1]-min_side//2:centre[1]+min_side//2,
:]
# rescale to standard size
img = transform.resize(img, (IMG_SIZE, IMG_SIZE))
# roll color axis to axis 0
img = np.rollaxis(img,-1)
return img
def get_class(img_path):
return int(img_path.split('/')[-2])
if __name__ == '__main__':
root_dir = 'GTSRB/Final_Training/Images/'
imgs = []
labels = []
all_img_paths = glob.glob(os.path.join(root_dir, '*/*.ppm'))
np.random.shuffle(all_img_paths)
for img_path in all_img_paths:
try:
img = preprocess_img(io.imread(img_path))
label = get_class(img_path)
imgs.append(img)
labels.append(label)
if len(imgs)%1000 == 0: print("Processed {}/{}".format(len(imgs), len(all_img_paths)))
except (IOError, OSError):
print('missed', img_path)
pass
X = np.array(imgs, dtype='float32')
Y = np.array(labels, dtype='uint8')
with h5py.File('X.h5','w') as hf:
hf.create_dataset('imgs', data=X)
hf.create_dataset('labels', data=Y+1)
test = pd.read_csv('GT-final_test.csv',sep=';')
X_test = []
y_test = []
i = 0
for file_name, class_id in zip(list(test['Filename']), list(test['ClassId'])):
img_path = os.path.join('GTSRB/Final_Test/Images/',file_name)
X_test.append(preprocess_img(io.imread(img_path)))
y_test.append(class_id+1)
X_test = np.array(X_test, dtype='float32')
y_test = np.array(y_test, dtype='uint8')
with h5py.File('X_test.h5','w') as hf:
hf.create_dataset('imgs', data=X)
hf.create_dataset('labels', data=Y)