-
Notifications
You must be signed in to change notification settings - Fork 0
/
advanced-lane-finding.py
571 lines (464 loc) · 21.6 KB
/
advanced-lane-finding.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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
# %% [markdown]
# # Advanced Lane Line Finding
# %% [markdown]
# ## Pipeline Description
# 1. Calibrate images using chessboard images, get camera matrix and distortion coefficients.
# 1. Undistort each frame in video using computed camera matrix and distortion coefficients.
# 1. Define trapezoid which fits a lane, apply perspective transform to warp image into bird-eye view.
# 1. Filter out unnecessary noise in the image, focus on detecting lines:
# 1. Apply color thresholding on S channel in HLS color space, separates yellow color well.
# 1. Apply mask to filter out white color, separates white color well.
# 1. (Gradient filtering was also tried, but found 2 methods above to work well in my case).
# 1. Find the start of the lines using histogram peaks.
# 1. Fit the polynomial by applying sliding window.
# 1. Once polynomial exists from a previous frame, search line pixels from prior polynomial within a margin.
# 1. If detected line is not good (outlier), fallback to histogram peak & sliding window search again.
# %% [markdown]
# %%
import numpy as np
import cv2
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import glob
import math
from moviepy.editor import VideoFileClip
from IPython.display import HTML
# %%
# Helper function to plot all images at once
def show_images(images, img_names, save=False, save_prefix=''):
cols = 2
rows = math.ceil(len(images)/cols)
plt.figure(figsize=(15, 15))
for i in range(0, len(images)):
img_name = img_names[i]
plt.subplot(rows, cols, i+1)
img = images[i]
cmap = None
if len(img.shape) < 3:
cmap = 'gray'
plt.title(img_names[i])
plt.imshow(img, cmap=cmap)
if save:
img_to_save = img
if len(img.shape) is 3:
img_to_save = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
cv2.imwrite('output_images/' + save_prefix + img_name.split('/')[1], img_to_save)
plt.tight_layout()
plt.show()
# %% [markdown]
# ## Camera Calibration: Prepare Object and Image Points
# %%
objpoints = [] # 3D points in real world space
imgpoints = [] # 2D points in image plane
# Prepare object points like (0,0,0),(1,0,0),(2,0,0),...(nx-1,ny-1,0)
nx = 9
ny = 6
patternSize = (nx, ny)
objp = np.zeros((nx*ny, 3), np.float32)
objp[:,:2] = np.mgrid[0:nx,0:ny].T.reshape(-1, 2)
# %%
calibFileNames = glob.glob('camera_cal/calibration*.jpg')
cornersNotFoundCount = 0
lastImgWithCorners = None
for fname in calibFileNames:
img = mpimg.imread(fname)
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
patternWasFound, corners = cv2.findChessboardCorners(gray, patternSize, None)
if patternWasFound == True:
objpoints.append(objp)
imgpoints.append(corners)
img = cv2.drawChessboardCorners(img, patternSize, corners, patternWasFound)
lastImgWithCorners = img
# save images locally
# imgToSave = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
# cv2.imwrite('output_images/' + fname.split('/')[1], imgToSave)
else:
cornersNotFoundCount += 1
plt.imshow(lastImgWithCorners)
print("total images ", len(calibFileNames))
print("corners found ", len(objpoints))
print("corners not found ", cornersNotFoundCount)
# %% [markdown]
# ## Camera Calibration: Calibrate, Undistort
# %%
# Calibrate camera -> get camera matrix and distortion coefficients
imgSizeXY = (lastImgWithCorners.shape[1], lastImgWithCorners.shape[0])
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, imgSizeXY, None, None)
# %% [markdown]
# ### Camera Calibration: Test image undistortion
# %%
distortedImg = mpimg.imread('camera_cal/calibration1.jpg')
undist = cv2.undistort(distortedImg, mtx, dist, None, mtx)
show_images([distortedImg, undist], ['Distorted', 'Undistorted'])
# %% [markdown]
# ## Test Road Images - Original
# %%
testRoadImgFnames = glob.glob('test_images/test*.jpg')
testRoadImages = list(map(lambda fname: mpimg.imread(fname), testRoadImgFnames))
show_images(testRoadImages, testRoadImgFnames)
# %% [markdown]
# ## Test Road Images - Undistorted
#%%
testRoadImagesUndist = list(map(lambda img: cv2.undistort(img, mtx, dist, None, mtx), testRoadImages))
show_images(testRoadImagesUndist, testRoadImgFnames, save=False, save_prefix='undistorted_')
# %% [markdown]
# ## Perspective Transform: Define Trapezoid
# %%
# Draw trapezoid, to see which params fit lanes best
def getTrapezoid():
yBottom = 681
yTop = 465
xLeftBottom = 258
xRightBottom = 1050
xLeftUp = 575
xRightUp = 710
return np.array([[xRightBottom,yBottom],[xLeftBottom,yBottom],[xLeftUp,yTop],[xRightUp,yTop]], np.int32)
def drawTrapezoid(img):
return cv2.polylines(np.copy(img), [getTrapezoid()], True, (255,0,0), thickness=2)
imgsWithTrapezoid = list(map(lambda img: drawTrapezoid(img), testRoadImagesUndist))
show_images(imgsWithTrapezoid, testRoadImgFnames, save=False, save_prefix='trapezoid')
# %% [markdown]
# ## Perspective Transform: Warp Undistorted Images
# %%
s = testRoadImages[0].shape
X = s[1]
Y = s[0]
srcPerspective = getTrapezoid().astype(np.float32)
warpXOffset = 320
dstPerspective = np.float32([(X-warpXOffset, Y), (warpXOffset, Y), (warpXOffset, 0), (X-warpXOffset, 0)])
M = cv2.getPerspectiveTransform(srcPerspective, dstPerspective)
MInv = cv2.getPerspectiveTransform(dstPerspective, srcPerspective)
warpedOriginal = list(map(lambda img: cv2.warpPerspective(img, M, (X, Y), flags=cv2.INTER_LINEAR), testRoadImagesUndist))
show_images(warpedOriginal, testRoadImgFnames, save=False, save_prefix='warpedOriginal_')
# %% [markdown]
# ## Perspective Transform: Apply HLS S Channel Filtering, White Mask, Gradients Thresholds
# %%
def absoluteSobelThresh(img, orient='x', sobelKernel=3, thresh=(0,255)):
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
xflag = yflag = 0
if orient == 'x':
xflag = 1
elif orient == 'y':
yflag = 1
sobel = cv2.Sobel(gray, cv2.CV_64F, xflag, yflag, ksize=sobelKernel)
sobelAbs = np.absolute(sobel)
sobelScaled = (255 * (sobelAbs / np.max(sobelAbs))).astype(np.uint8)
binaryInThresh = np.zeros_like(sobelScaled)
binaryInThresh[(sobelScaled >= thresh[0]) & (sobelScaled < thresh[1])] = 1
return binaryInThresh
def magnitudeThresh(img, sobelKernel=3, thresh=(0, 255)):
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
sobelX = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=sobelKernel)
sobelY = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=sobelKernel)
sobelMag = np.sqrt(sobelX**2 + sobelY**2)
sobelScaled = (255 * (sobelMag/ np.max(sobelMag))).astype(np.uint8)
binaryInThresh = np.zeros_like(sobelScaled)
binaryInThresh[(sobelScaled >= thresh[0]) & (sobelScaled < thresh[1])] = 1
return binaryInThresh
def directionThresh(img, sobelKernel=3, thresh=(0, np.pi/2)):
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
sobelX = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=sobelKernel)
sobelY = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=sobelKernel)
absGradDir = np.arctan2(np.absolute(sobelY), np.absolute(sobelX))
binaryInThresh = np.zeros_like(absGradDir)
binaryInThresh[(binaryInThresh >= thresh[0]) & (binaryInThresh < thresh[1])] = 1
return binaryInThresh
def combinedGradientThresh(img):
ksize = 15
gradX = absoluteSobelThresh(img, orient='x', sobelKernel=ksize, thresh=(20,100))
gradY = absoluteSobelThresh(img, orient='y', sobelKernel=ksize, thresh=(20,100))
gradMag = magnitudeThresh(img, sobelKernel=ksize, thresh=(30, 100))
gradDir = directionThresh(img, sobelKernel=ksize, thresh=(0.7, 1.3))
combined = np.zeros_like(gradX)
combined[((gradX == 1) & (gradY == 1)) | (gradMag == 1) & (gradDir == 1)] = 1
return combined
def hlsSChannelThresh(img, sThresh=(170, 255)):
hls = cv2.cvtColor(img, cv2.COLOR_RGB2HLS)
sChan = hls[:,:,2]
sBinary = np.zeros_like(sChan)
sBinary[(sChan >= sThresh[0]) & (sChan < sThresh[1])] = 1
return sBinary
def combinedFiltering(img, debug=False):
sChanThresh = hlsSChannelThresh(img)
combinedGradient = combinedGradientThresh(img)
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
whiteMask = cv2.inRange(gray, 220, 255) / 255
# s channel - red; white mask - green; gradient - blue, for debugging
if debug == True:
combinedImg = (np.dstack((sChanThresh, whiteMask, combinedGradient)) * 255).astype(np.uint8)
return np.vstack((img, combinedImg))
binary = np.zeros_like(sChanThresh).astype(np.uint8)
binary[(sChanThresh == 1) | (whiteMask == 1)] = 1
return binary
# %%
threshImgsDebug = list(map(lambda img: combinedFiltering(img, debug=True), warpedOriginal))
show_images(threshImgsDebug, testRoadImgFnames, save=False, save_prefix='combinedThreshDebug_')
# %%
threshImgs = list(map(lambda img: combinedFiltering(img), warpedOriginal))
show_images(threshImgs, testRoadImgFnames)
# %% [markdown]
# ## Detect Left & Right Lines, Fit Polynomial
# %%
def hist(img):
bottomHalf = img[img.shape[0]//2:, :]
histogram = np.sum(bottomHalf, axis=0)
return histogram
def findLanePixels(warpedImg, debug=False):
histogram = hist(warpedImg)
midpoint = histogram.shape[0]//2
leftBase = np.argmax(histogram[:midpoint])
rightBase = midpoint + np.argmax(histogram[midpoint:])
ySize = warpedImg.shape[0]
xSize = warpedImg.shape[1]
margin = 90
minpix = 50
nwindows = 9
height = ySize // nwindows
nonzero = warpedImg.nonzero()
nonzeroy = np.array(nonzero[0])
nonzerox = np.array(nonzero[1])
allLeftLaneIndices = []
allRightLaneIndices = []
currentBaseLeft = leftBase
currentBaseRight = rightBase
outImg = None
if debug:
outImg = np.dstack((warpedImg,)*3) * 255
for window in range(nwindows):
leftYBottom = rightYBottom = ySize - window * height
leftYTop = rightYTop = ySize - (window+1) * height
leftXLeft = currentBaseLeft - margin
leftXRight = currentBaseLeft + margin
rightXLeft = currentBaseRight - margin
rightXRight = currentBaseRight + margin
if debug:
cv2.rectangle(outImg,(leftXLeft,leftYBottom),(leftXRight,leftYTop),(0,255,0), 2)
cv2.rectangle(outImg,(rightXLeft,rightYBottom),(rightXRight,rightYTop),(0,0,255), 2)
leftIndices = ((nonzerox >= leftXLeft) & (nonzerox < leftXRight) & (nonzeroy >= leftYTop) & (nonzeroy < leftYBottom)).nonzero()[0]
rightIndices = ((nonzerox >= rightXLeft) & (nonzerox < rightXRight) & (nonzeroy >= rightYTop) & (nonzeroy < rightYBottom)).nonzero()[0]
if len(leftIndices) > minpix:
currentBaseLeft = np.int(np.mean(nonzerox[leftIndices]))
if len(rightIndices) > minpix:
currentBaseRight = np.int(np.mean(nonzerox[rightIndices]))
allLeftLaneIndices.append(leftIndices)
allRightLaneIndices.append(rightIndices)
allLeftLaneIndices = np.concatenate(allLeftLaneIndices)
allRightLaneIndices = np.concatenate(allRightLaneIndices)
leftX = nonzerox[allLeftLaneIndices]
leftY = nonzeroy[allLeftLaneIndices]
rightX = nonzerox[allRightLaneIndices]
rightY = nonzeroy[allRightLaneIndices]
return leftX, leftY, rightX, rightY, outImg
def fitPoly(imgShape, leftx, lefty, rightx, righty):
# Fit a second order polynomial
leftFit = np.polyfit(lefty, leftx, 2)
rightFit = np.polyfit(righty, rightx, 2)
# Generate x and y values for plotting
plotY = np.linspace(0, imgShape[0]-1, imgShape[0])
leftFitX = leftFit[0]*plotY**2 + leftFit[1]*plotY + leftFit[2]
rightFitX = rightFit[0]*plotY**2 + rightFit[1]*plotY + rightFit[2]
return leftFitX, rightFitX, plotY, leftFit, rightFit
def fitPolynomialFromScratch(warpedImg, debug=False):
leftX, leftY, rightX, rightY, outImg = findLanePixels(warpedImg, debug)
leftFitX, rightFitX, plotY, leftFit, rightFit = fitPoly(warpedImg.shape, leftX, leftY, rightX, rightY)
if debug:
outImg[leftY, leftX] = [0, 255, 0]
outImg[rightY, rightX] = [0, 0, 255]
polyline1 = np.array(list(zip(leftFitX, plotY)))
polyline2 = np.array(list(zip(rightFitX, plotY)))
cv2.polylines(outImg, np.int32([polyline1, polyline2]), False, (255,255,0), thickness=4)
return leftFitX, rightFitX, plotY, leftFit, rightFit, outImg
def searchAroundPoly(warpedImg, leftFit, rightFit):
margin = 100
nonzero = warpedImg.nonzero()
nonzeroy = np.array(nonzero[0])
nonzerox = np.array(nonzero[1])
leftLaneIndices = ((nonzerox >= leftFit[0]*nonzeroy**2 + leftFit[1]*nonzeroy + leftFit[2] - margin) &
(nonzerox <= leftFit[0]*nonzeroy**2 + leftFit[1]*nonzeroy + leftFit[2] + margin)).nonzero()[0]
rightLaneIndices = ((nonzerox >= rightFit[0]*nonzeroy**2 + rightFit[1]*nonzeroy + rightFit[2] - margin) &
(nonzerox <= rightFit[0]*nonzeroy**2 + rightFit[1]*nonzeroy + rightFit[2] + margin)).nonzero()[0]
# Again, extract left and right line pixel positions
leftx = nonzerox[leftLaneIndices]
lefty = nonzeroy[leftLaneIndices]
rightx = nonzerox[rightLaneIndices]
righty = nonzeroy[rightLaneIndices]
# Fit new polynomials
return fitPoly(warpedImg.shape, leftx, lefty, rightx, righty)
# %% [markdown]
# ### Find start of a line using histogram peak, then apply sliding window
# %%
leftX, leftY, rightX, rightY, imgWithLanePixels = findLanePixels(threshImgs[4], debug=True)
show_images([imgWithLanePixels], [testRoadImgFnames[1]], save=False, save_prefix='laneRectanglesAdjusted_')
# %% [markdown]
# ### Fit polynomial to detected line pixels.
# We treat y axis as free variable for our polynomial, since fit is vertical
# %%
leftFitX, rightFitX, plotY, leftFit, rightFit, imgWithPoly = fitPolynomialFromScratch(threshImgs[4], debug=True)
show_images([imgWithPoly], [testRoadImgFnames[1]], save=False, save_prefix='fitPoly_')
print("left fit", leftFit)
print("right fit", rightFit)
# %% [markdown]
# ## Draw Lane
# %%
def drawLane(undistImage, binaryWarped, Minv, leftFitX, rightFitX, plotY):
warp_zero = np.zeros_like(binaryWarped).astype(np.uint8)
colorWarp = np.dstack((warp_zero, warp_zero, warp_zero))
ptsLeft = np.array([np.transpose(np.vstack([leftFitX, plotY]))])
ptsRight = np.array([np.flipud(np.transpose(np.vstack([rightFitX, plotY])))])
pts = np.hstack((ptsLeft, ptsRight))
cv2.fillPoly(colorWarp, np.int_([pts]), (0, 255, 0))
ySize = undistImage.shape[0]
xSize = undistImage.shape[1]
newWarp = cv2.warpPerspective(colorWarp, Minv, (xSize, ySize))
result = cv2.addWeighted(undistImage, 1, newWarp, 0.3, 0)
return result
fittedLanes = list(map(lambda warped: fitPolynomialFromScratch(warped), threshImgs))
imgsWithLanes = []
for i in range(len(testRoadImages)):
imgsWithLanes.append(drawLane(testRoadImages[i], threshImgs[i], MInv, fittedLanes[i][0], fittedLanes[i][1], fittedLanes[i][2]))
show_images(imgsWithLanes, testRoadImgFnames, save=False, save_prefix='laneOnRoad_')
# %% [markdown]
# ## Line Outliers Detection: Get Intuition on Coefficients
# %%
leftFits = [];
rightFits = [];
for i in range(len(fittedLanes)):
leftFits.append(fittedLanes[i][3])
rightFits.append(fittedLanes[i][4])
print("left fit", fittedLanes[i][3])
print("right fit", fittedLanes[i][4])
print("diff", np.absolute(fittedLanes[i][3] - fittedLanes[i][4]))
print("---------")
print("----------")
print("----------")
print("all left fits", np.array(leftFits))
print("all right fits", np.array(leftFits))
# %% [markdown]
# ## Find Lane on Video: Complete Pipeline
#%%
ym_per_pix = 30/720
xm_per_pix = 3.7/700
class Line():
def __init__(self):
# was the line detected in the last iteration?
self.detected = False
# x values of the last n fits of the line
self.recent_xfitted = []
self.ploty = None
#average x values of the fitted line over the last n iterations
self.bestx = None
#polynomial coefficients averaged over the last n iterations
self.best_fit = None
#polynomial coefficients for the most recent fit
self.current_fit = None
#radius of curvature of the line in some units
self.radius_of_curvature = None
#difference in fit coefficients between last and new fits
self.diffs = np.array([0,0,0], dtype='float')
self.xLanePosPix = None
def addNewLineFit(self, imgShape, fitX, plotY, fit):
self.ploty = plotY
if self.current_fit is not None:
self.diffs = np.absolute(self.current_fit - fit)
# check if detected line is OK or not
if (self.diffs[0] <= 0.0003 and \
self.diffs[1] <= 0.1 and \
self.diffs[2] <= 150) or not self.detected:
self.current_fit = fit
self.recent_xfitted.append(fitX)
self.recent_xfitted = self.recent_xfitted[-20:]
self.bestx = np.mean(self.recent_xfitted, axis=0)
self.best_fit = np.polyfit(plotY, self.bestx, 2)
self.measureCurvatureRadiusAndLineBasePos(imgShape)
self.detected = True
else:
self.detected = False
def measureCurvatureRadiusAndLineBasePos(self, imgShape):
fit_cr = np.polyfit(self.ploty*ym_per_pix, self.bestx*xm_per_pix, 2)
yMax = np.max(self.ploty)
self.radius_of_curvature = ((1 + (2*fit_cr[0]*yMax*ym_per_pix+fit_cr[1])**2)**(3/2)) / abs(2*fit_cr[0])
self.xLanePosPix = self.best_fit[0]*yMax**2 + self.best_fit[1]*yMax + self.best_fit[2]
class LaneFinder():
def __init__(self):
self.imgShape = None
self.leftLine = Line()
self.rightLine = Line()
self.vehicleCenterOffset = None
def calculateVehicleCenterOffset(self):
laneCenter = (self.leftLine.xLanePosPix + self.rightLine.xLanePosPix) / 2
midX = self.imgShape[1] / 2
self.vehicleCenterOffset = (laneCenter - midX) * xm_per_pix
def formatVehiclePosition(self):
leftOrRight = "right"
if self.vehicleCenterOffset > 0:
leftOrRight = "left"
text = "Vehicle is " + str(round(abs(self.vehicleCenterOffset), 2)) + "m " + leftOrRight + " of center"
return text
def processNextFrame(self, img):
if self.imgShape is None:
self.imgShape = img.shape
undist = cv2.undistort(img, mtx, dist, None, mtx)
warped = cv2.warpPerspective(undist, M, (img.shape[1], img.shape[0]), flags=cv2.INTER_LINEAR)
binaryFiltered = combinedFiltering(warped)
distLinesLow = 2.5
distLinesHigh = 3.9
distBetweenLinesMeters = 3
if self.leftLine.xLanePosPix is not None and self.rightLine.xLanePosPix is not None:
distBetweenLinesMeters = abs(self.leftLine.xLanePosPix - self.rightLine.xLanePosPix)*xm_per_pix
leftLinePriorSearch = False;
rightLinePriorSearch = False;
# decide either to search around previous polynomial or start line search from scratch
if self.leftLine.detected and distBetweenLinesMeters >= distLinesLow and distBetweenLinesMeters <= distLinesHigh:
leftLinePriorSearch = True;
leftFitX, _, plotY, leftFit, _ = searchAroundPoly(binaryFiltered, self.leftLine.current_fit, self.rightLine.current_fit)
else:
leftFitX, _, plotY, leftFit, _, _ = fitPolynomialFromScratch(binaryFiltered)
# decide either to search around previous polynomial or start line search from scratch
if self.rightLine.detected and distBetweenLinesMeters >= distLinesLow and distBetweenLinesMeters <= distLinesHigh:
rightLinePriorSearch = True;
_, rightFitX, _, _, rightFit = searchAroundPoly(binaryFiltered, self.leftLine.current_fit, self.rightLine.current_fit)
else:
_, rightFitX, _, _, rightFit, _ = fitPolynomialFromScratch(binaryFiltered)
# add new fits to lines objects
self.leftLine.addNewLineFit(img.shape, leftFitX, plotY, leftFit)
self.rightLine.addNewLineFit(img.shape, rightFitX, plotY, rightFit)
self.calculateVehicleCenterOffset()
# draw lane
imgWithLane = drawLane(undist, binaryFiltered, MInv, self.leftLine.bestx, self.rightLine.bestx, plotY)
color=(0, 255, 0)
thick = 10
radiusOfCurvatureMean = np.mean([self.leftLine.radius_of_curvature, self.rightLine.radius_of_curvature])
curvatureText = 'Radius of Curvature = ' + str(round(radiusOfCurvatureMean, 2)) + '(m)'
cv2.putText(imgWithLane, curvatureText, (20, 100), cv2.LINE_AA, 2, color, thick)
cv2.putText(imgWithLane, self.formatVehiclePosition(), (20, 200), cv2.LINE_AA, 2, color, thick)
# used for debugging
debug=False
if debug:
distBetweenLinesText = "Distance between lines (m)" + str(round(distBetweenLinesMeters, 2))
cv2.putText(imgWithLane, distBetweenLinesText, (20, 260), cv2.LINE_AA, 2, color, thick)
cv2.putText(imgWithLane, "L Prior " + str(leftLinePriorSearch), (20, 320), cv2.LINE_AA, 2, color, thick)
cv2.putText(imgWithLane, "R Prior " + str(rightLinePriorSearch), (500, 320), cv2.LINE_AA, 2, color, thick)
return imgWithLane
# %% [markdown]
# ## Test Complete Pipeline on Test Images
# %%
processedImgs = []
for i in range(len(testRoadImages)):
laneFinder = LaneFinder()
processedImgs.append(laneFinder.processNextFrame(testRoadImages[i]))
show_images(processedImgs, testRoadImgFnames, save=False, save_prefix='processedFinal_')
# %% [markdown]
# ## Find Lanes on Video
# %%
outputFname1 = 'project_video_output.mp4'
# clip1 = VideoFileClip('project_video.mp4')
# laneFinder = LaneFinder()
# processedClip1 = clip1.fl_image(laneFinder.processNextFrame)
# processedClip1.write_videofile(outputFname1, audio=False)
# %%
HTML("""
<video width="960" height="540" controls>
<source src="{0}">
</video>
""".format(outputFname1))
# %%