-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeaturegenerator.py
208 lines (181 loc) · 9.03 KB
/
featuregenerator.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import cv2
import numpy as np
from skimage.feature import hog
import matplotlib.image as mpimg
from settings import Settings, Constants
class FeatureGenerator:
def __init__(self, settingsDict):
self.settingsDict = settingsDict
self.constants = Constants()
def generate_hog_features(self, feature_image, orient,
pix_per_cell, cell_per_block,
hog_channel=0,
vis=False,
feature_vec=True):
if hog_channel == 'ALL':
hog_features = []
for channel in range(feature_image.shape[2]):
img_channel = feature_image[:, :, channel]
features = hog(img_channel, orientations=orient,
pixels_per_cell=(pix_per_cell, pix_per_cell),
cells_per_block=(cell_per_block,
cell_per_block),
transform_sqrt=True,
visualise=vis, feature_vector=feature_vec)
hog_features.extend(features)
else:
img_channel = feature_image[:, :, hog_channel]
hog_features = hog(img_channel, orientations=orient,
pixels_per_cell=(pix_per_cell, pix_per_cell),
cells_per_block=(cell_per_block,
cell_per_block),
transform_sqrt=True,
visualise=vis, feature_vector=feature_vec)
return hog_features
def getAllFeatures(self, img, settingsDict):
allFeatures = []
orient = self.settingsDict[self.constants.ORIENTATION]
pix_per_cell = self.settingsDict[self.constants.PIXEL_PER_CELL]
cell_per_block = self.settingsDict[self.constants.CELL_PER_BLOCK]
size = self.settingsDict[self.constants.SPATIAL_SIZE]
nbins = self.settingsDict[self.constants.COLOR_HIST_BINS]
bins_range = self.settingsDict[self.constants.COLOR_HIST_BINS_RANGE]
colorspace = self.settingsDict[self.constants.COLOR_SPACE]
img = self.convert_color(img, colorspace)
hgFeatures = self.generate_hog_features(img, orient,
pix_per_cell=pix_per_cell,
cell_per_block=cell_per_block,
vis=False,
feature_vec=True)
spFeatures = self.generate_spatial_features(img, size=size)
histFeatures = self.generate_color_histogram_features(
img,
nbins=nbins,
bins_range=bins_range)
allFeatures.append(spFeatures)
allFeatures.append(histFeatures)
allFeatures.append(hgFeatures)
return np.concatenate(allFeatures)
def computeFeatures(self, img_paths):
features = []
for path in img_paths:
# get the features for each image
img = mpimg.imread(path)
f = self.getAllFeatures(img, self.settingsDict)
features.append(f)
return features
def generate_spatial_features(self, img, size=(32, 32)):
# Use cv2.resize().ravel() to create the feature vector
features = cv2.resize(img, size).ravel()
# Return the feature vector
return features
def generate_color_histogram_features(self, img, nbins=32,
bins_range=(0, 256)):
# Compute the histogram of the color channels separately
channel1_hist = np.histogram(img[:, :, 0], bins=nbins,
range=bins_range)
channel2_hist = np.histogram(img[:, :, 1], bins=nbins,
range=bins_range)
channel3_hist = np.histogram(img[:, :, 2], bins=nbins,
range=bins_range)
# Concatenate the histograms into a single feature vector
hist_features = np.concatenate((channel1_hist[0],
channel2_hist[0],
channel3_hist[0]))
# Return the individual histograms, bin_centers and feature vector
return hist_features
def convert_color(self, image, colorspace):
feature_image = np.copy(image)
if colorspace != 'RGB':
if colorspace == 'HSV':
feature_image = cv2.cvtColor(image, cv2.COLOR_RGB2HSV)
elif colorspace == 'LUV':
feature_image = cv2.cvtColor(image, cv2.COLOR_RGB2LUV)
elif colorspace == 'HLS':
feature_image = cv2.cvtColor(image, cv2.COLOR_RGB2HLS)
elif colorspace == 'YUV':
feature_image = cv2.cvtColor(image, cv2.COLOR_RGB2YUV)
elif colorspace == 'YCrCb':
feature_image = cv2.cvtColor(image, cv2.COLOR_RGB2YCrCb)
return feature_image
def bin_spatial(img, size=(32, 32)):
# Use cv2.resize().ravel() to create the feature vector
features = cv2.resize(img, size).ravel()
# Return the feature vector
return features
def color_hist(img, nbins=32, bins_range=(0, 256)):
# Compute the histogram of the color channels separately
channel1_hist = np.histogram(img[:, :, 0], bins=nbins,
range=bins_range)
channel2_hist = np.histogram(img[:, :, 1], bins=nbins,
range=bins_range)
channel3_hist = np.histogram(img[:, :, 2], bins=nbins,
range=bins_range)
# Concatenate the histograms into a single feature vector
hist_features = np.concatenate((channel1_hist[0],
channel2_hist[0],
channel3_hist[0]))
# Return the individual histograms, bin_centers and feature vector
return hist_features
def get_hog_features(img, orient, pix_per_cell, cell_per_block,
vis=False, feature_vec=True):
# Call with two outputs if vis==True
if vis == True:
features, hog_image = hog(img, orientations=orient,
pixels_per_cell=(pix_per_cell, pix_per_cell),
cells_per_block=(cell_per_block, cell_per_block),
transform_sqrt=True,
visualise=vis, feature_vector=feature_vec)
return features, hog_image
# Otherwise call with one output
else:
features = hog(img, orientations=orient,
pixels_per_cell=(pix_per_cell, pix_per_cell),
cells_per_block=(cell_per_block, cell_per_block),
transform_sqrt=True,
visualise=vis, feature_vector=feature_vec)
return features
def single_img_features(img, color_space='RGB', spatial_size=(32, 32),
hist_bins=32, orient=9,
pix_per_cell=8, cell_per_block=2, hog_channel=0,
spatial_feat=True, hist_feat=True, hog_feat=True):
#1) Define an empty list to receive features
img_features = []
#2) Apply color conversion if other than 'RGB'
if color_space != 'RGB':
if color_space == 'HSV':
feature_image = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
elif color_space == 'LUV':
feature_image = cv2.cvtColor(img, cv2.COLOR_RGB2LUV)
elif color_space == 'HLS':
feature_image = cv2.cvtColor(img, cv2.COLOR_RGB2HLS)
elif color_space == 'YUV':
feature_image = cv2.cvtColor(img, cv2.COLOR_RGB2YUV)
elif color_space == 'YCrCb':
feature_image = cv2.cvtColor(img, cv2.COLOR_RGB2YCrCb)
else: feature_image = np.copy(img)
#3) Compute spatial features if flag is set
if spatial_feat == True:
spatial_features = bin_spatial(feature_image, size=spatial_size)
#4) Append features to list
img_features.append(spatial_features)
#5) Compute histogram features if flag is set
if hist_feat == True:
hist_features = color_hist(feature_image, nbins=hist_bins)
#6) Append features to list
img_features.append(hist_features)
#7) Compute HOG features if flag is set
if hog_feat == True:
if hog_channel == 'ALL':
hog_features = []
for channel in range(feature_image.shape[2]):
hog_features.extend(get_hog_features(feature_image[:,:,channel],
orient, pix_per_cell, cell_per_block,
vis=False, feature_vec=True))
else:
hog_features = get_hog_features(feature_image[:,:,hog_channel], orient,
pix_per_cell, cell_per_block, vis=False, feature_vec=True)
#8) Append features to list
img_features.append(hog_features)
#9) Return concatenated array of features
return np.concatenate(img_features)