-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
223 lines (188 loc) · 8.04 KB
/
utils.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
from io import BytesIO
import cv2
import numpy as np
import glob
from tqdm import tqdm
from IPython.display import Image
import matplotlib as mpl
from concurrent.futures import ThreadPoolExecutor, as_completed
from moviepy.editor import VideoFileClip
from features import Features
from features import ColorHistFeatures, HogImageFeatures
def load_test_images(glob_regex='test_images/*.jpg'):
images = []
files = []
for f in glob.glob(glob_regex):
img = cv2.imread(f)
# img=cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
images.append(img)
files.append(f)
# print(f,img.shape)
return images, files
def load_test_video(file_name='test_video.mp4'):
vimages = []
vframes = []
count = 0
clip = VideoFileClip(file_name)
for img in clip.iter_frames(progress_bar=True):
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
vimages.append(img)
vframes.append("%s - %d" % (file_name, count))
count += 1
return vimages, vframes
def arr2img(arr):
"""Display a 2- or 3-d numpy array as an image."""
if arr.ndim == 2:
format, cmap = 'png', mpl.cm.gray
elif arr.ndim == 3:
format, cmap = 'jpg', None
else:
raise ValueError("Only 2- or 3-d arrays can be displayed as images.")
# Don't let matplotlib autoscale the color range so we can control
# overall luminosity
vmax = 255 if arr.dtype == 'uint8' else 1.0
# vmax=1.0
with BytesIO() as buffer:
mpl.image.imsave(buffer, arr, format=format, cmap=cmap,
vmin=0, vmax=vmax)
out = buffer.getvalue()
return Image(out)
def load_images(glob_regex='var/non-vehicles/Extras/*.png'):
images = []
files = []
for f in glob.glob(glob_regex):
img = cv2.imread(f)
images.append(img)
files.append(f)
return np.array(images), np.array(files)
def load_car_not_car_images(root_path='var',
non_vehicle_sub_dirs=['Extras', 'GTI'],
vehicle_sub_dirs=['GTI_Far', 'GTI_Left',
'GTI_MiddleClose', 'GTI_Right',
'KITTI_extracted']):
non_vehicle_paths = [root_path + '/non-vehicles/' + p
for p in non_vehicle_sub_dirs]
vehicle_paths = [root_path + '/vehicles/' + p
for p in vehicle_sub_dirs]
def images_from_path(path):
images, files = load_images(glob_regex=path + '/*.png')
print(path, images.shape, len(files))
return images
non_vehicle_images = np.concatenate([images_from_path(p)
for p in non_vehicle_paths], axis=0)
vehicle_images = np.concatenate([images_from_path(p)
for p in vehicle_paths], axis=0)
return vehicle_images, non_vehicle_images
def extract_color_features(imgs, cspace='BGR', spatial_size=(32, 32),
hist_bins=32, hist_range=(0, 256)):
# Create a list to append feature vectors to
all_features = []
def feature_extract(image, cspace='BGR', spatial_size=(32, 32),
hist_bins=32, hist_range=(0, 256)):
# apply color conversion if other than 'RGB'
if cspace != 'BGR':
if cspace == 'HSV':
feature_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
elif cspace == 'LUV':
feature_image = cv2.cvtColor(image, cv2.COLOR_BGR2LUV)
elif cspace == 'HLS':
feature_image = cv2.cvtColor(image, cv2.COLOR_BGR2HLS)
elif cspace == 'YUV':
feature_image = cv2.cvtColor(image, cv2.COLOR_BGR2YUV)
elif cspace == 'YCrCb':
feature_image = cv2.cvtColor(image, cv2.COLOR_BGR2YCrCb)
else:
feature_image = np.copy(image)
# bsf = BinSpatialFeatures(feature_image, size=spatial_size)
chf = ColorHistFeatures(
feature_image, nbins=hist_bins, bins_range=hist_range)
return chf.values
pbar = tqdm(total=len(imgs))
with ThreadPoolExecutor() as executor:
extract_futures = {executor.submit(feature_extract, img,
cspace, spatial_size,
hist_bins, hist_range):
img for img in imgs}
for future in as_completed(extract_futures):
img = extract_futures[future]
try:
features = future.result()
pbar.update(1)
except Exception as exc:
print('image feature extract generated an exception: %s'
% (exc))
import sys
import traceback
exc_type, exc_value, exc_traceback = sys.exc_info()
print(repr(traceback.format_tb(exc_traceback)))
raise exc
else:
all_features.append(features)
pbar.close()
return all_features
def extract_hog_features(imgs, cspace='BGR', orient=9,
pix_per_cell=8, cell_per_block=2, hog_channel=0,
spatial_size=(32, 32),
hist_bins=32, hist_range=(0, 256)):
# Create a list to append feature vectors to
all_features = []
def feature_extract(image, cspace='BGR', orient=9,
pix_per_cell=8, cell_per_block=2, hog_channel=0,
spatial_size=(32, 32),
hist_bins=32, hist_range=(0, 256)):
# apply color conversion if other than 'RGB'
if cspace != 'BGR':
if cspace == 'HSV':
feature_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
elif cspace == 'LUV':
feature_image = cv2.cvtColor(image, cv2.COLOR_BGR2LUV)
elif cspace == 'HLS':
feature_image = cv2.cvtColor(image, cv2.COLOR_BGR2HLS)
elif cspace == 'YUV':
feature_image = cv2.cvtColor(image, cv2.COLOR_BGR2YUV)
elif cspace == 'YCrCb':
feature_image = cv2.cvtColor(image, cv2.COLOR_BGR2YCrCb)
else:
feature_image = np.copy(image)
# bsf = BinSpatialFeatures(feature_image, size=spatial_size)
# chf = ColorHistFeatures(
# feature_image, nbins=hist_bins, bins_range=hist_range)
# Call get_hog_features()
hog_features = Features([])
if hog_channel == 'ALL':
for channel in range(feature_image.shape[2]):
hog_features += HogImageFeatures(
feature_image[:, :, channel],
orient, pix_per_cell, cell_per_block)
else:
hog_features += HogImageFeatures(
feature_image[:, :, hog_channel],
orient, pix_per_cell, cell_per_block)
# return np.concatenate((hog_features.values, chf.values))
# features = hog_features + chf
return hog_features.features
# return features
pbar = tqdm(total=len(imgs))
with ThreadPoolExecutor() as executor:
extract_futures = {executor.submit(feature_extract, img, cspace,
orient,
pix_per_cell, cell_per_block,
hog_channel, spatial_size,
hist_bins, hist_range):
img for img in imgs}
for future in as_completed(extract_futures):
img = extract_futures[future]
try:
features = future.result()
pbar.update(1)
except Exception as exc:
print('feature extract generated an exception: %s' % (exc))
import sys
import traceback
exc_type, exc_value, exc_traceback = sys.exc_info()
print(repr(traceback.format_tb(exc_traceback)))
raise exc
else:
all_features.append(features.values)
pbar.close()
return all_features