-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlane_cv_detection.py
340 lines (238 loc) · 11.4 KB
/
lane_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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from settings import INPUTDATA_DIR
import cv2
import numpy as np
# https://github.com/MehdiSv
#%%
class Polyfitter:
def __init__(self):
self.left_fit = None
self.right_fit = None
self.leftx = None
self.rightx = None
def polyfit(self, img):
#if self.left_fit is None:
return self.polyfit_sliding(img)
nonzero = img.nonzero()
nonzeroy = np.array(nonzero[0])
nonzerox = np.array(nonzero[1])
margin = 100
left_lane_inds = (
(nonzerox > (self.left_fit[0] * (nonzeroy ** 2) + self.left_fit[1] * nonzeroy + self.left_fit[2] - margin)) & (
nonzerox < (self.left_fit[0] * (nonzeroy ** 2) + self.left_fit[1] * nonzeroy + self.left_fit[2] + margin)))
right_lane_inds = (
(nonzerox > (self.right_fit[0] * (nonzeroy ** 2) + self.right_fit[1] * nonzeroy + self.right_fit[2] - margin)) & (
nonzerox < (self.right_fit[0] * (nonzeroy ** 2) + self.right_fit[1] * nonzeroy + self.right_fit[2] + margin)))
self.leftx = nonzerox[left_lane_inds]
lefty = nonzeroy[left_lane_inds]
self.rightx = nonzerox[right_lane_inds]
righty = nonzeroy[right_lane_inds]
self.left_fit = np.polyfit(lefty, self.leftx, 2)
self.right_fit = np.polyfit(righty, self.rightx, 2)
return self.left_fit, self.right_fit
def polyfit_sliding(self, img):
histogram = np.sum(img[int(img.shape[0] / 2):, :], axis=0)
out_img = np.dstack((img, img, img)) * 255
midpoint = np.int(histogram.shape[0] / 2)
leftx_base = np.argmax(histogram[:midpoint])
rightx_base = np.argmax(histogram[midpoint:]) + midpoint
nwindows = 9
window_height = np.int(img.shape[0] / nwindows)
nonzero = img.nonzero()
nonzeroy = np.array(nonzero[0])
nonzerox = np.array(nonzero[1])
leftx_current = leftx_base
rightx_current = rightx_base
margin = 100
minpix = 50
left_lane_inds = []
right_lane_inds = []
for window in range(nwindows):
win_y_low = img.shape[0] - (window + 1) * window_height
win_y_high = img.shape[0] - window * window_height
win_xleft_low = leftx_current - margin
win_xleft_high = leftx_current + margin
win_xright_low = rightx_current - margin
win_xright_high = rightx_current + margin
cv2.rectangle(out_img, (win_xleft_low, win_y_low), (win_xleft_high, win_y_high), (0, 255, 0), 2)
cv2.rectangle(out_img, (win_xright_low, win_y_low), (win_xright_high, win_y_high), (0, 255, 0), 2)
good_left_inds = ((nonzeroy >= win_y_low) & (nonzeroy < win_y_high) & (nonzerox >= win_xleft_low) & (
nonzerox < win_xleft_high)).nonzero()[0]
good_right_inds = ((nonzeroy >= win_y_low) & (nonzeroy < win_y_high) & (nonzerox >= win_xright_low) & (
nonzerox < win_xright_high)).nonzero()[0]
left_lane_inds.append(good_left_inds)
right_lane_inds.append(good_right_inds)
if len(good_left_inds) > minpix:
leftx_current = np.int(np.mean(nonzerox[good_left_inds]))
if len(good_right_inds) > minpix:
rightx_current = np.int(np.mean(nonzerox[good_right_inds]))
left_lane_inds = np.concatenate(left_lane_inds)
right_lane_inds = np.concatenate(right_lane_inds)
self.leftx = nonzerox[left_lane_inds]
lefty = nonzeroy[left_lane_inds]
self.rightx = nonzerox[right_lane_inds]
righty = nonzeroy[right_lane_inds]
self.left_fit = np.polyfit(lefty, self.leftx, 2)
self.right_fit = np.polyfit(righty, self.rightx, 2)
return self.left_fit, self.right_fit
def measure_curvature(self, img):
ploty = np.linspace(0, 719, num=720) # to cover same y-range as image
quadratic_coeff = 3e-4 # arbitrary quadratic coefficient
leftx = np.array([200 + (y ** 2) * quadratic_coeff + np.random.randint(-50, high=51)
for y in ploty])
rightx = np.array([900 + (y ** 2) * quadratic_coeff + np.random.randint(-50, high=51)
for y in ploty])
leftx = leftx[::-1] # Reverse to match top-to-bottom in y
rightx = rightx[::-1] # Reverse to match top-to-bottom in y
# left_fit = np.polyfit(ploty, leftx, 2)
# right_fit = np.polyfit(ploty, rightx, 2)
y_eval = np.max(ploty)
# left_curverad = ((1 + (2 * left_fit[0] * y_eval + left_fit[1]) ** 2) ** 1.5) / np.absolute(2 * left_fit[0])
# right_curverad = ((1 + (2 * right_fit[0] * y_eval + right_fit[1]) ** 2) ** 1.5) / np.absolute(2 * right_fit[0])
# print(left_curverad, right_curverad)
ym_per_pix = 30 / 720 # meters per pixel in y dimension
xm_per_pix = 3.7 / 700 # meters per pixel in x dimension
left_fit_cr = np.polyfit(ploty * ym_per_pix, leftx * xm_per_pix, 2)
right_fit_cr = np.polyfit(ploty * ym_per_pix, rightx * xm_per_pix, 2)
left_curverad = ((1 + (2 * left_fit_cr[0] * y_eval * ym_per_pix + left_fit_cr[1]) ** 2) ** 1.5) / np.absolute(
2 * left_fit_cr[0])
right_curverad = (
(1 + (
2 * right_fit_cr[0] * y_eval * ym_per_pix + right_fit_cr[
1]) ** 2) ** 1.5) / np.absolute(
2 * right_fit_cr[0])
# print(left_curverad, 'm', right_curverad, 'm')
ratio = left_curverad / right_curverad
if ratio < 0.66 or ratio > 1.5:
print('Warning: shitty ratio {}'.format(ratio))
lane_leftx = self.left_fit[0] * (img.shape[0] - 1) ** 2 + self.left_fit[1] * (img.shape[0] - 1) + self.left_fit[2]
lane_rightx = self.right_fit[0] * (img.shape[0] - 1) ** 2 + self.right_fit[1] * (img.shape[0] - 1) + self.right_fit[2]
car_pos = ((img.shape[1] / 2) - ((lane_leftx + lane_rightx) / 2)) * xm_per_pix
return (left_curverad + right_curverad) / 2, car_pos.round(2)
#%%
class Polydrawer:
def draw(self, img, left_fit, right_fit, Minv):
color_warp = np.zeros_like(img).astype(np.uint8)
fity = np.linspace(0, img.shape[0] - 1, img.shape[0])
left_fitx = left_fit[0] * fity ** 2 + left_fit[1] * fity + left_fit[2]
right_fitx = right_fit[0] * fity ** 2 + right_fit[1] * fity + right_fit[2]
# Recast the x and y points into usable format for cv2.fillPoly()
pts_left = np.array([np.transpose(np.vstack([left_fitx, fity]))])
pts_right = np.array([np.flipud(np.transpose(np.vstack([right_fitx, fity])))])
pts = np.hstack((pts_left, pts_right))
pts = np.array(pts, dtype=np.int32)
cv2.fillPoly(color_warp, pts, (0, 255, 0))
# Warp the blank back to original image space using inverse perspective matrix (Minv)
newwarp = cv2.warpPerspective(color_warp, Minv, (img.shape[1], img.shape[0]))
# Combine the result with the original image
result = cv2.addWeighted(img, 1, newwarp, 0.3, 0)
return result
#%%
class Thresholder:
def __init__(self):
self.sobel_kernel = 15
self.thresh_dir_min = 0.7
self.thresh_dir_max = 1.2
self.thresh_mag_min = 50
self.thresh_mag_max = 255
def dir_thresh(self, sobelx, sobely):
abs_sobelx = np.abs(sobelx)
abs_sobely = np.abs(sobely)
scaled_sobel = np.arctan2(abs_sobely, abs_sobelx)
sxbinary = np.zeros_like(scaled_sobel)
sxbinary[(scaled_sobel >= self.thresh_dir_min) & (scaled_sobel <= self.thresh_dir_max)] = 1
return sxbinary
def mag_thresh(self, sobelx, sobely):
gradmag = np.sqrt(sobelx ** 2 + sobely ** 2)
scale_factor = np.max(gradmag) / 255
gradmag = (gradmag / scale_factor).astype(np.uint8)
binary_output = np.zeros_like(gradmag)
binary_output[(gradmag >= self.thresh_mag_min) & (gradmag <= self.thresh_mag_max)] = 1
return binary_output
def color_thresh(self, img):
img = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
yellow_min = np.array([15, 100, 120], np.uint8)
yellow_max = np.array([80, 255, 255], np.uint8)
yellow_mask = cv2.inRange(img, yellow_min, yellow_max)
white_min = np.array([0, 0, 200], np.uint8)
white_max = np.array([255, 30, 255], np.uint8)
white_mask = cv2.inRange(img, white_min, white_max)
binary_output = np.zeros_like(img[:, :, 0])
binary_output[((yellow_mask != 0) | (white_mask != 0))] = 1
filtered = img
filtered[((yellow_mask == 0) & (white_mask == 0))] = 0
return binary_output
def threshold(self, img):
sobelx = cv2.Sobel(img[:, :, 2], cv2.CV_64F, 1, 0, ksize=self.sobel_kernel)
sobely = cv2.Sobel(img[:, :, 2], cv2.CV_64F, 0, 1, ksize=self.sobel_kernel)
direc = self.dir_thresh(sobelx, sobely)
mag = self.mag_thresh(sobelx, sobely)
color = self.color_thresh(img)
combined = np.zeros_like(direc)
combined[((color == 1) & ((mag == 1) | (direc == 1)))] = 1
return combined
#%%
class Warper:
def __init__(self):
src = np.float32([
[580, 460],
[700, 460],
[1040, 680],
[260, 680],
])
dst = np.float32([
[260, 0],
[1040, 0],
[1040, 720],
[260, 720],
])
self.M = cv2.getPerspectiveTransform(src, dst)
self.Minv = cv2.getPerspectiveTransform(dst, src)
def warp(self, img):
return cv2.warpPerspective(
img,
self.M,
(img.shape[1], img.shape[0]),
flags=cv2.INTER_LINEAR
)
def unwarp(self, img):
return cv2.warpPersective(
img,
self.Minv,
(img.shape[1], img.shape[0]),
flags=cv2.INTER_LINEAR
)
#%%
def lane_cv_detector(img):
try:
objpoints = np.load(INPUTDATA_DIR+'lane/objpoints.npy')
imgpoints = np.load(INPUTDATA_DIR+'lane/imgpoints.npy')
shape = tuple(np.load(INPUTDATA_DIR+'lane/shape.npy'))
except:
objpoints = None
imgpoints = None
shape = None
if objpoints is None or imgpoints is None or shape is None:
print("Error: No Claibration data, Please RUN cam_cali.py")
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, shape,None, None)
thresholder = Thresholder()
warper = Warper()
polyfitter = Polyfitter()
polydrawer = Polydrawer()
undistorted = cv2.undistort(img, mtx, dist, None, mtx)
img = thresholder.threshold(undistorted)
img = warper.warp(img)
left_fit, right_fit = polyfitter.polyfit(img)
img = polydrawer.draw(undistorted, left_fit, right_fit, warper.Minv)
lane_curve, car_pos = polyfitter.measure_curvature(img)
if car_pos > 0:
car_pos_text = '{}m right of center'.format(car_pos)
else:
car_pos_text = '{}m left of center'.format(abs(car_pos))
cv2.putText(img, "Lane curve: {}m".format(lane_curve.round()), (10, 50), cv2.FONT_HERSHEY_SIMPLEX, 1,
color=(255, 255, 255), thickness=2)
cv2.putText(img, "Car is {}".format(car_pos_text), (10, 100), cv2.FONT_HERSHEY_SIMPLEX, 1, color=(255, 255, 255),
thickness=2)
return img
#%%