-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.py
144 lines (117 loc) · 4.15 KB
/
data.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
'''
Author: Ambareesh Ravi
Date: Jul 31, 2021
Title: data.py
Description:
Contains the data handling functionalities
'''
from utils import *
from tensorflow.keras.utils import to_categorical
class TrafficSign_Dataset:
# Creates the Traffic sign dataset
def __init__(self, data_path = "TrafficSign_Images/", image_size = (32, 32), isTrain = True, isNormalized = True, isDerived = False):
'''
Initializes the class
Args:
data_path - Parent path to the dataset as <str>
image_size - size of the inputs as W,H as <tuple>
isTrain - If it is training model as <bool>
isNormalized - if the data is to be normalized as <bool>
isDerived - To use like an abstract class as <bool>
Returns:
-
Exception:
-
'''
self.image_size = image_size
self.isTrain = isTrain
self.path_tag = "train" if self.isTrain else "test"
self.data_path = os.path.join(data_path, self.path_tag)
self.isNormalized = isNormalized
self.test_csv = os.path.join(data_path, "Test.csv")
signs_df = pd.read_csv(os.path.join(data_path, "sign_names.csv"))
self.idx_class = dict([(s_id, name) for s_id, name in zip(np.array(signs_df['ClassId']), np.array(signs_df['SignName']))])
self.isDerived = isDerived
if not self.isDerived:
self.data, self.labels = self.create_train_data() if self.isTrain else self.create_test_data()
def resize_im_array(self, im):
'''
Resizes the image array
Args:
im - image array as <nparray>
Returns:
resized image array as <np.array>
Exception:
-
'''
return cv2.resize(im, self.image_size)
def read_image(self, path):
'''
Reads image into the memory
Args:
path - image path as <str>
Returns:
image array as <np.array>
Exception:
-
'''
im = cv2.imread(path)
im = self.resize_im_array(im)
return im
def create_train_data(self,):
'''
Creates and stores the train data into memory
Args:
-
Returns:
data as <np.array>, labels as <np.array>
Exception:
-
'''
data, labels = list(), list()
for d in tqdm(read_directory_content(self.data_path)):
class_name = os.path.split(d)[-1]
for fl in read_directory_content(d):
try:
im_array = self.read_image(fl)
data.append(im_array)
labels.append(int(class_name))
except Exception as e:
print(e)
pass
data, labels = np.array(data).astype(np.float32), np.array(labels).astype(np.uint8)
if self.isNormalized: data /= 255.
indices = np.arange(len(labels))
np.random.shuffle(indices)
return data[indices], to_categorical(labels[indices], len(self.idx_class))
def create_test_data(self,):
'''
Creates and stores the test data into memory
Args:
-
Returns:
data as <np.array>, labels as <np.array>
Exception:
-
'''
data, labels = list(), list()
test_df = pd.read_csv(self.test_csv)
files = np.array(test_df['Path'])
labels = np.array(test_df['ClassId'])
for fl in tqdm(files):
im_array = self.read_image(os.path.join(self.data_path, fl.replace("Test/", "")))
data.append(im_array)
data = np.array(data).astype(np.float32)
if self.isNormalized: data /= 255.
return data, labels
def __call__(self):
'''
Returns (data, labels) when called
Args:
-
Returns:
(data as <np.array>, labels as <np.array>)
Exception:
-
'''
return self.data, self.labels