-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvehicle_cv_detection.py
511 lines (370 loc) · 16.1 KB
/
vehicle_cv_detection.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from settings import VEHICLE_CV_CLF_DUMP, VEHICLE_LIST_DUMP
import pandas as pd
import numpy as np
import cv2
import matplotlib.image as mpimg
import pickle
import os
#from sklearn.externals import joblib
from tqdm import tqdm #pregress bar
from skimage.feature import hog
from sklearn import svm
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
#from helper import draw_boxes
#%%
def draw_pred_boxes(img, box, color=(0, 0, 255), thick=6):
imcopy = np.copy(img)
cv2.rectangle(imcopy, (box[0], box[1]), (box[2], box[3]), color, thick)
return imcopy
#%%
# Return x and y coordinates for all computed window search positions.
def slide_window(img, xy_overlap = 0.5, xy_window = 64, y_start_stop=[380, None]):
# WuStangDan's CODE
xcenter = np.int(img.shape[1]/2)
# If y start/stop positions not defined, set to image size.
if y_start_stop[0] == None:
y_start_stop[0] = 0
if y_start_stop[1] == None:
y_start_stop[1] = img.shape[0]
# Initialize a list to append window positions to.
window_list = []
starty = y_start_stop[0]
ny_window = 3
growth = 1.104
for yi in range(0,ny_window):
step = np.int(xy_window *(1 - xy_overlap))
endy = starty + xy_window
nx_window = np.int(img.shape[1]/(step*2))
if yi == 0:
nx_window = nx_window - 1*np.int(xy_window/step)
# How many xy_window tall pixels should be covered in the y direction.
ystack = 2
xy_overlap = 0.8
if yi == 1:
nx_window = nx_window - 0*np.int(xy_window/step)
ystack = 2
# Used for largest window, not current (yi == 2).
xy_overlap = 0.8
if yi == 2:
nx_window = nx_window - 0*np.int(xy_window/step)
ystack = 2
# Ensures that both overlap and non-overlap cases cover the same number of pixels
# in the y direction. Ex smallest boxes want to cover 2 * 64 pixels. With non-overlap
# thats two sets of 64 pixel tall windows stacked on top of eachother. For 50% overlap
# that requires 3 sets of 64 pixels tall windows with 50% overlap.
if xy_overlap != 0:
ystack = np.int(ystack*(xy_window/step)) - 1
for yii in range(ystack):
for xi in range(nx_window):
startx = xcenter + xi*step
endx = startx + xy_window
# Only add to list if within image bounds.
if endx < img.shape[1]:
if startx > 0:
if endy < img.shape[0]:
window_list.append( ((startx, starty), (endx, endy)) )
endx = xcenter - xi*step
startx = endx - xy_window
# Only add to list if within image bounds.
if startx > 0:
if endx < img.shape[1]:
if endy < img.shape[0]:
window_list.append( ((startx, starty), (endx, endy)) )
starty = starty + step
endy = starty + xy_window
starty = y_start_stop[0]
xy_window = np.int(xy_window **growth)
# Return the list of window positions.
return window_list
#%%
def combine_pred(img, pred_windows):
# Find overlaping or touching windows and group them.
# If no prediction windows return original image.
if pred_windows == None:
return img, []
# If only one prediction window return image with one low confidence box.
if pred_windows.size == 4:
#img = draw_pred_boxes(img, pred_windows, (255,0,255))
return img, []
prednum = np.zeros( (len(pred_windows),len(pred_windows)) )
# Calculate centroids of each window.
cent = []
for window in pred_windows:
xcent = np.int((window[0] + window[2])/2)
ycent = np.int((window[1] + window[3])/2)
cent.append((xcent, ycent))
# Classify windows as a group if distance to another centroid is within set number
# of pixels in x or y direction.
for i in range(len(cent)):
group = i+1
# If this window already has an existing match
# set its group number to that number.
temppred = prednum[:,i]
if len(temppred[temppred > 0]) > 0:
group = temppred[temppred >0][0]
for j in range(len(cent)):
if i == j:
continue
# Compare x distance.
if (abs(cent[i][0] - cent[j][0]) <= 100): # was 110
# Compare y distance.
if (abs(cent[i][1] - cent[j][1]) <= 64):
# If a match is found, check if the match is already assigned
# a group number.
temppred = prednum[:,j]
if len(temppred[temppred > 0]) > 0:
group = temppred[temppred > 0][0]
# Assign the group number to that location.
# i represents the window being compared and j represents
# the window that is found to match.
prednum[i,j] = group
# Represent grouping of windows in 1D array.
predgroup = np.zeros(len(cent))
for i in range(len(cent)):
predgroup[i] = max(prednum[i,:])
#print('predgroup:',predgroup)
# Draw pink boxes around all low confidence (single window prediction) detections.
# singleloc = np.where(predgroup == 0)
# if len(singleloc[0]) > 0:
# for i in range(len(singleloc[0])):
# img = draw_pred_boxes(img, pred_windows[singleloc[0][i]], (255,0,255))
# Set group window to largest rectangle containing all the windows that group.
groupwindoutput = []
for i in range(1, len(cent)+1):
grouploc = np.where(predgroup == i)
grouplen = len(grouploc[0])
if grouplen > 1:
groupwind = np.array([img.shape[1], img.shape[0], 0, 0])
for j in range(grouplen):
windloc = grouploc[0][j]
# Replace group window location with windows to create largest
# encasing rectangle.
if pred_windows[windloc][0] < groupwind[0]:
groupwind[0] = pred_windows[windloc][0]
if pred_windows[windloc][1] < groupwind[1]:
groupwind[1] = pred_windows[windloc][1]
if pred_windows[windloc][2] > groupwind[2]:
groupwind[2] = pred_windows[windloc][2]
if pred_windows[windloc][3] > groupwind[3]:
groupwind[3] = pred_windows[windloc][3]
# Draw box containing all prediction windows.
if grouplen > 3:
img = draw_pred_boxes(img, groupwind)
groupwindoutput.append( ((groupwind[0], groupwind[1], groupwind[2], groupwind[3])) )
#else:
#img = draw_pred_boxes(img, groupwind, (255,0,255))
#groupwindoutput.append( ((groupwind[0], groupwind[1], groupwind[2], groupwind[3])) )
#return predgroup
return img, groupwindoutput
# %%
# Need another function that when a car might have been detected, another focused sliding window search
# is performed on that specific area to have a better chance at detection.
# Return x and y coordinates for all computed window search positions.
def slide_window_focus(img, center, xy_overlap = 0.5):
xcenter = center[0]
# If y start/stop positions not defined, set to image size.
xy_window = 32
# Initialize a list to append window positions to.
window_list = []
ny_window = 5
growth = 1.104
for yi in range(0,ny_window):
step = np.int(xy_window *(1 - xy_overlap))
# Individual settings for each size of window.
if yi == 0:
nx_window = 0
ystack = 0#3
if yi == 1:
nx_window = 1
ystack = 2#2
if yi == 2:
nx_window = 1#3
ystack = 2#2
if yi == 3:
nx_window = 1#2
ystack = 0#2
if yi == 4:
nx_window = 1
ystack = 0
# Ensures that y distance is consistent with and without overlap.
if xy_overlap != 0:
ystack = np.int(ystack*(xy_window/step)) - 1
if ystack > 0:
starty = np.int(center[1] - (xy_window))
else:
starty = np.int(center[1] - xy_window/2)
endy = starty + xy_window
# If only one window is meant to be created, window centered around
# x and y centers.
if nx_window == 1:
startx = xcenter - np.int(xy_window/2)
endx = startx + xy_window
if startx > 0:
if endx < img.shape[1]:
if starty > 0:
if endy < img.shape[0]:
window_list.append( ((startx, starty), (endx, endy)) )
for yii in range(ystack):
for xi in range(nx_window):
startx = xcenter + xi*step
endx = startx + xy_window
# Only add to list if within image bounds.
if endx < img.shape[1]:
if startx > 0:
if starty > 0:
if endy < img.shape[0]:
window_list.append( ((startx, starty), (endx, endy)) )
endx = xcenter - xi*step
startx = endx - xy_window
# Only add to list if within image bounds.
if startx > 0:
if endx < img.shape[1]:
if starty > 0:
if endy < img.shape[0]:
window_list.append( ((startx, starty), (endx, endy)) )
starty = starty + step
endy = starty + xy_window
xy_window = np.int(xy_window **growth)
# Return the list of window positions.
return window_list
# %%
# Crop window area of image
def crop_resize(img, window):
crop = img[window[0][1]:window[1][1], window[0][0]:window[1][0]]
resize = cv2.resize(crop, (64, 64))
return resize
#%%
#%%
#class Car():
# def __init__(self):
# self.loc = []
# self.allwind = []
# self.count = -1
#
#
def vehicle_cv_detector(image):
classifier = Classifier()
# Cars.count = Cars.count + 1
# if ((Cars.count % 12) != 0):
# for groups in Cars.loc:
# image = draw_pred_boxes(image, groups)
# return image
# Use previous frame information about car detection to add more detection windows
# in area where car was detected to increase chances of detecting it in this frame.
# windgroup = []
# for wind in Cars.loc:
# xcenter = np.int((wind[0] + wind[2])/2)
# ycenter = np.int((wind[1] + wind[3])/2)
# windgroup.extend(slide_window_focus(img, [xcenter, ycenter]))
## image = draw_boxes(image,windgroup)
#
windows = slide_window(image,0.75)
# windows.extend(windgroup)
#
pred_windows = None
featdata = np.zeros((len(windows),1764)).astype('float32')
linsvc = classifier.get_classifier() # reomve
# Loop and gather all features data for each cropped window.
for i in range(1,len(windows)):
cropimg = crop_resize(image, windows[i])
#feat = np.append(hog_features(cropimg, 'yuv', 0), hog_features(cropimg, 'hsv', 2))
#featdata[i] = np.append(hog_features(cropimg, 'yuv', 0), hog_features(cropimg, 'hsv', 2))
featdata[i] = classifier.hog_features(cropimg, 'hsv', 0)
del i
del cropimg
# Run classifier on all of the feature data.
X_scaler = StandardScaler().fit(featdata) #Origin xdata, change to featdata
featscaled = X_scaler.transform(featdata)
croppred = linsvc.predict(featdata) #Predict confidence scores for samples.
pred_windows = None
for i in range(len(windows)):
# Decision function is used with a cut off 0.3 to filter out predictions that
# the SVC isn't confident about.
if croppred[i] >= .5:
if pred_windows == None:
pred_windows = np.hstack(windows[i])
#image = draw_boxes(image, windows[i:i+1])
else:
temp = np.hstack(windows[i])
pred_windows = np.vstack((pred_windows, temp)).astype(int)
#image = draw_boxes(image, windows[i:i+1])
imgpred, groupwind = combine_pred(image,pred_windows)
# Cars.loc = groupwind
# Cars.allwind = pred_windows
return imgpred
#%%
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# from get_classfier.py
# test
#%%
#%%
class Classifier:
def get_classifier(self):
if os.path.isfile(VEHICLE_CV_CLF_DUMP) and os.access(VEHICLE_CV_CLF_DUMP, os.R_OK):
print ('CLF-File exists and is readable')
with open(VEHICLE_CV_CLF_DUMP, 'rb') as f:
clf = pickle.load(f)
return clf
else:
df_vehicles = pd.read_csv(VEHICLE_LIST_DUMP) # Generated by vehicle_dataProcessing.py
xdata = np.zeros((len(df_vehicles),1764)).astype('float32') #1764 = feature length
ydata =df_vehicles['Label'].as_matrix() #pandas to numpy
for i in tqdm(range(0,len(df_vehicles))):
img = df_vehicles.ix[i,'File_Path']
img = mpimg.imread(img)
feat1 = self.hog_features(img, 'hsv', 0)
#feat2 = np.append(feat1, hog_features(imgd, 'hsv', 2)) # add another feature
xdata[i] = feat1
#특징값을 Normalize
X_scaler = StandardScaler().fit(xdata)
scaledx = X_scaler.transform(xdata)
# 데이터 분리 xdata = x_train, x_test, y_train, y_test
x_train, x_test, y_train, y_test = train_test_split(xdata, ydata, test_size=0.2, random_state=1337)
#train_test_split: Split arrays or matrices into random train and test subsets
# use of the rbf kernel improves the accuracy by about another percent,
# but increases the prediction time up to 1.7s(!) for 100 labels. Too slow.
#svc = svm.SVC(kernel='rbf')
linsvc = svm.LinearSVC(loss='squared_hinge',tol=1e-4, max_iter=1000) # SVC설정
self.cls_val_clf = linsvc # added
clf = linsvc.fit(x_train, y_train) # fit이용 학습
with open(VEHICLE_CV_CLF_DUMP,'wb') as f:
pickle.dump(clf,f)
print ("CLF-File generated")
return clf
def hog_features(self, img, cspace='gry', chan=0, orientbin=9, cellpix=8, cellb=2, visual=False, vector=True):
# Pick feature and channel to perform HOG on.
if cspace == 'rgb':
newimg = img[:,:,chan]
if cspace == 'gry':
newimg = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
if cspace == 'hsv':
tmpimg = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
newimg = tmpimg[:,:,chan]
if cspace == 'luv':
tmpimg = cv2.cvtColor(img, cv2.COLOR_RGB2LUV)
newimg = tmpimg[:,:,chan]
if cspace == 'hls':
tmpimg = cv2.cvtColor(img, cv2.COLOR_RGB2HLS)
newimg = tmpimg[:,:,chan]
if cspace == 'yuv':
tmpimg = cv2.cvtColor(img, cv2.COLOR_RGB2YUV)
newimg = tmpimg[:,:,chan]
if visual == True:
features, visual_image = hog(newimg, orientations=orientbin,
pixels_per_cell=(cellpix, cellpix),
cells_per_block=(cellb, cellb),
transform_sqrt=True,
visualise=True, feature_vector=True)
return features, visual_image
else:
features = hog(newimg, orientations=orientbin,
pixels_per_cell=(cellpix, cellpix),
cells_per_block=(cellb, cellb),
transform_sqrt=True,
visualise=False, feature_vector=True)
return features
#%%