-
Notifications
You must be signed in to change notification settings - Fork 13
/
Project.py
386 lines (310 loc) · 13.1 KB
/
Project.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
import cv2
import numpy as np
import matplotlib.pyplot as plt
import pickle
import matplotlib.image as mpimg
from moviepy.editor import VideoFileClip
from collections import deque
# Define a class to receive the characteristics of each line detection
class Line:
def __init__(self):
# was the line detected in the last iteration?
self.detected = False
# x and y values in last frame
self.x = None
self.y = None
# x intercepts for average smoothing
self.bottom_x = deque(maxlen=frame_num)
self.top_x = deque(maxlen=frame_num)
# Record last x intercept
self.current_bottom_x = None
self.current_top_x = None
# Record radius of curvature
self.radius = None
# Polynomial coefficients: x = A*y**2 + B*y + C
self.A = deque(maxlen=frame_num)
self.B = deque(maxlen=frame_num)
self.C = deque(maxlen=frame_num)
self.fit = None
self.fitx = None
self.fity = None
def get_curv(self):
self.radius = curvature(self.fit)
return self.radius
def get_intercepts(self):
bottom = self.fit[0] * 720 ** 2 + self.fit[1] * 720 + self.fit[2]
top = self.fit[2]
return bottom, top
def quick_search(self, nonzerox, nonzeroy):
"""
Assuming in last frame, lane has been detected. Based on last x/y coordinates, quick search current lane.
"""
x_inds = []
y_inds = []
if self.detected:
win_bottom = 720
win_top = 630
while win_top >= 0:
yval = np.mean([win_top, win_bottom])
xval = (np.median(self.A)) * yval ** 2 + (np.median(self.B)) * yval + (np.median(self.C))
x_idx = np.where((((xval - 50) < nonzerox)
& (nonzerox < (xval + 50))
& ((nonzeroy > win_top) & (nonzeroy < win_bottom))))
x_window, y_window = nonzerox[x_idx], nonzeroy[x_idx]
if np.sum(x_window) != 0:
np.append(x_inds, x_window)
np.append(y_inds, y_window)
win_top -= 90
win_bottom -= 90
if np.sum(x_inds) == 0:
self.detected = False # If no lane pixels were detected then perform blind search
return x_inds, y_inds, self.detected
def blind_search(self, nonzerox, nonzeroy, image):
"""
Sliding window search method, start from blank.
"""
x_inds = []
y_inds = []
if self.detected is False:
win_bottom = 720
win_top = 630
while win_top >= 0:
histogram = np.sum(image[win_top:win_bottom, :], axis=0)
if self == right:
base = np.argmax(histogram[640:]) + 640
else:
base = np.argmax(histogram[:640])
x_idx = np.where((((base - 50) < nonzerox) & (nonzerox < (base + 50))
& ((nonzeroy > win_top) & (nonzeroy < win_bottom))))
x_window, y_window = nonzerox[x_idx], nonzeroy[x_idx]
if np.sum(x_window) != 0:
x_inds.extend(x_window)
y_inds.extend(y_window)
win_top -= 90
win_bottom -= 90
if np.sum(x_inds) > 0:
self.detected = True
else:
y_inds = self.y
x_inds = self.x
return x_inds, y_inds, self.detected
def sort_idx(self):
"""
Sort x and y according to y index
"""
sorted_idx = np.argsort(self.y)
sorted_x_inds = self.x[sorted_idx]
sorted_y_inds = self.y[sorted_idx]
return sorted_x_inds, sorted_y_inds
def get_fit(self):
"""
Based on searched x and y coordinates, polyfit with second order.
Take median value in previous frames to smooth.
"""
self.fit = np.polyfit(self.y, self.x, 2)
self.current_bottom_x, self.current_top_x = self.get_intercepts()
self.bottom_x.append(self.current_bottom_x)
self.top_x.append(self.current_top_x)
self.current_bottom_x = np.median(self.bottom_x)
self.current_top_x = np.median(self.top_x)
self.x = np.append(self.x, self.current_bottom_x)
self.x = np.append(self.x, self.current_top_x)
self.y = np.append(self.y, 720)
self.y = np.append(self.y, 0)
self.x, self.y = self.sort_idx()
self.fit = np.polyfit(self.y, self.x, 2)
self.A.append(self.fit[0])
self.B.append(self.fit[1])
self.C.append(self.fit[2])
self.fity = self.y
self.fit = [np.median(self.A), np.median(self.B), np.median(self.C)]
self.fitx = self.fit[0] * self.fity ** 2 + self.fit[1] * self.fity + self.fit[2]
return self.fit, self.fitx, self.fity
def draw_area(undist, left_fitx, lefty, right_fitx, righty):
Minv = cv2.getPerspectiveTransform(dst, src)
# Create an image to draw the lines on
warp_zero = np.zeros(img_shape[0:2]).astype(np.uint8)
color_warp = np.dstack((warp_zero, warp_zero, warp_zero))
# Recast the x and y points into usable format for cv2.fillPoly()
# pts_left = np.array([np.transpose(np.vstack([left_fitx, lefty]))])
pts_left = np.array([np.flipud(np.transpose(np.vstack([left_fitx, lefty])))])
pts_right = np.array([np.transpose(np.vstack([right_fitx, righty]))])
pts = np.hstack((pts_left, pts_right))
# Draw lines
cv2.polylines(color_warp, np.int_([pts]),
isClosed=False, color=(200, 0, 0), thickness=30)
# Draw the lane onto the warped blank image
cv2.fillPoly(color_warp, np.int_([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
return cv2.addWeighted(undist, 1, newwarp, 0.3, 0)
def curvature(fit):
"""
calculate curvature from fit parameter
:param fit: [A, B, C]
:return: radius of curvature (in meters unit)
"""
ym_per_pix = 18 / 720 # meters per pixel in y dimension
xm_per_pix = 3.7 / 700 # meters per pixel in x dimension
fitx = fit[0] * ploty ** 2 + fit[1] * ploty + fit[2]
y_eval = np.max(ploty)
# Fit new polynomials to x,y in world space
fit_cr = np.polyfit(ploty * ym_per_pix, fitx * xm_per_pix, 2)
curverad = ((1 + (2 * fit_cr[0] * y_eval * ym_per_pix + fit_cr[1]) ** 2) ** 1.5) / \
np.absolute(2 * fit_cr[0])
return curverad
def car_pos(left_fit, right_fit):
"""
Calculate the position of car on left and right lane base (convert to real unit meter)
:param left_fit:
:param right_fit:
:return: distance (meters) of car offset from the middle of left and right lane
"""
xleft_eval = left_fit[0] * np.max(ploty) ** 2 + left_fit[1] * np.max(ploty) + left_fit[2]
xright_eval = right_fit[0] * np.max(ploty) ** 2 + right_fit[1] * np.max(ploty) + right_fit[2]
ym_per_pix = 18 / 720 # meters per pixel in y dimension
xm_per_pix = 3.7 / abs(xleft_eval - xright_eval) # meters per pixel in x dimension
xmean = np.mean((xleft_eval, xright_eval))
offset = (img_shape[1]/2 - xmean) * xm_per_pix # +: car in right; -: car in left side
y_eval = np.max(ploty)
left_fitx = left_fit[0] * ploty ** 2 + left_fit[1] * ploty + left_fit[2]
left_fit_cr = np.polyfit(ploty * ym_per_pix, left_fitx * 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_fitx = right_fit[0] * ploty ** 2 + right_fit[1] * ploty + right_fit[2]
right_fit_cr = np.polyfit(ploty * ym_per_pix, right_fitx * xm_per_pix, 2)
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])
mean_curv = np.mean([left_curverad, right_curverad])
return offset, mean_curv
def warp(img):
"""
Perspective Transformation
:param img:
:return: warped image
"""
# Compute and apply perspective transform
M = cv2.getPerspectiveTransform(src, dst)
warped = cv2.warpPerspective(img, M, (1280, 720), flags=cv2.INTER_NEAREST) # keep same size as input image
return warped
def luv_lab_filter(img, l_thresh=(195, 255), b_thresh=(140, 200), plot=False):
warped = warp(img)
l = cv2.cvtColor(warped, cv2.COLOR_RGB2LUV)[:, :, 0]
l_bin = np.zeros_like(l)
l_bin[(l >= l_thresh[0]) & (l <= l_thresh[1])] = 1
b = cv2.cvtColor(warped, cv2.COLOR_RGB2Lab)[:, :, 2]
b_bin = np.zeros_like(b)
b_bin[(b >= b_thresh[0]) & (b <= b_thresh[1])] = 1
combine = np.zeros_like(l)
combine[(l_bin == 1) | (b_bin == 1)] = 1
if plot is True:
plt.figure(figsize=(10, 6))
plt.subplot(231)
plt.imshow(warped)
plt.subplot(232)
plt.imshow(l_bin, cmap='gray')
plt.title('L channel')
plt.subplot(233)
plt.imshow(b_bin, cmap='gray')
plt.title('B channel')
plt.subplot(234)
# plt.imshow(s_bin, cmap='gray')
plt.title('S channel')
plt.subplot(235)
plt.imshow(combine, cmap='gray')
plt.title('Combination')
plt.subplot(236)
plt.imshow(img)
plt.show()
return combine
def undistort(img, mtx, dist):
"""
Use cv2.undistort to undistort
:param img: Assuming input img is RGB (imread by mpimg)
:param mtx: camera calibration parameter
:param dist: camera calibration parameter
:return: Undistorted img
"""
# transform to BGR to fit cv2.imread
img_BGR = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
dst_img = cv2.undistort(img_BGR, mtx, dist, None, mtx)
return cv2.cvtColor(dst_img, cv2.COLOR_BGR2RGB)
def process_image(img):
global mtx, dist, src, dst
# undist_img = undistort(img, mtx, dist)
undist_img = cv2.undistort(img, mtx, dist, None, mtx)
warped_binary = luv_lab_filter(undist_img, l_thresh=(215, 255),
b_thresh=(145, 200))
nonzerox, nonzeroy = np.nonzero(np.transpose(warped_binary))
if left.detected is True:
leftx, lefty, left.detected = left.quick_search(nonzerox, nonzeroy)
if right.detected is True:
rightx, righty, right.detected = right.quick_search(nonzerox, nonzeroy)
if left.detected is False:
leftx, lefty, left.detected = left.blind_search(nonzerox, nonzeroy, warped_binary)
if right.detected is False:
rightx, righty, right.detected = right.blind_search(nonzerox, nonzeroy, warped_binary)
left.y = np.array(lefty).astype(np.float32)
left.x = np.array(leftx).astype(np.float32)
right.y = np.array(righty).astype(np.float32)
right.x = np.array(rightx).astype(np.float32)
left_fit, left_fitx, left_fity = left.get_fit()
right_fit, right_fitx, right_fity = right.get_fit()
offset, mean_curv = car_pos(left_fit, right_fit)
result = draw_area(undist_img, left_fitx, left_fity, right_fitx, right_fity)
font = cv2.FONT_HERSHEY_SIMPLEX # 使用默认字体
text1 = 'Radius of Curvature: %d(m)'
text2 = 'Offset from center: %.2f(m)'
text3 = 'Radius of Curvature: Inf (m)'
if mean_curv < 3000:
cv2.putText(result, text1 % (int(mean_curv)),
(60, 100), font, 1.0, (255, 255, 255), thickness=2)
else:
cv2.putText(result, text3,
(60, 100), font, 1.0, (255, 255, 255), thickness=2)
cv2.putText(result, text2 % (-offset),
(60, 130), font, 1.0, (255, 255, 255), thickness=2)
return result
img_shape = (720, 1280)
img_size = [1280, 720] # width, height
src = np.float32([[490, 482], [810, 482],
[1250, 720], [0, 720]])
dst = np.float32([[0, 0], [1280, 0],
[1250, 720], [40, 720]])
ploty = np.linspace(0, img_shape[0] - 1, img_shape[0])
# import Camera Calibration Parameters
dist_pickle = "./wide_dist_pickle.p"
with open(dist_pickle, mode="rb") as f:
CalData = pickle.load(f)
mtx, dist = CalData["mtx"], CalData["dist"]
frame_num = 15 # latest frames number of good detection
left = Line()
right = Line()
video_output = './output_videos/project.mp4'
input_path = './test_videos/project_video.mp4'
clip1 = VideoFileClip(input_path)
# clip1 = VideoFileClip(input_path).subclip(0, 5)
final_clip = clip1.fl_image(process_image)
final_clip.write_videofile(video_output, audio=False)
# image = mpimg.imread('./test_images/test_ch7.jpg')
def draw(img):
undist = undistort(img, mtx, dist)
warped = warp(undist)
bin = luv_lab_filter(undist, l_thresh=(215, 255),
b_thresh=(145, 200))
out = process_image(img)
plt.figure(figsize=(10, 6))
plt.subplot(221)
plt.imshow(undist)
plt.title('Me')
plt.subplot(222)
plt.imshow(warped)
# plt.plot(left.x, left.y, color='green')
# plt.plot(right.x, right.y, color='green')
plt.subplot(223)
plt.imshow(bin, cmap='gray')
plt.subplot(224)
plt.imshow(out)
plt.show()
return