forked from udacity/CarND-LaneLines-P1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
project_01.py
259 lines (230 loc) · 10.9 KB
/
project_01.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
# importing some useful packages
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
import cv2
import os
import math
# Import everything needed to edit/save/watch video clips
from moviepy.editor import VideoFileClip
def grayscale(img):
"""Applies the Grayscale transform
This will return an image with only one color channel
but NOTE: to see the returned image as grayscale
(assuming your grayscaled image is called 'gray')
you should call plt.imshow(gray, cmap='gray')"""
return cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
# Or use BGR2GRAY if you read an image with cv2.imread()
# return cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
def colorselect(image, rth=200, gth=200, bth=200):
color_select = np.copy(image)
color_thresholds = (image[:, :, 0] < rth) | (image[:, :, 1] < gth) | (image[:, :, 2] < bth)
color_select[color_thresholds] = [0, 0, 0]
return color_select
def canny(img, low_threshold, high_threshold):
"""Applies the Canny transform"""
return cv2.Canny(img, low_threshold, high_threshold)
def gaussian_blur(img, kernel_size):
"""Applies a Gaussian Noise kernel"""
return cv2.GaussianBlur(img, (kernel_size, kernel_size), 0)
def region_of_interest(img, vertices):
"""
Applies an image mask.
Only keeps the region of the image defined by the polygon
formed from `vertices`. The rest of the image is set to black.
`vertices` should be a numpy array of integer points.
"""
#defining a blank mask to start with
mask = np.zeros_like(img)
#defining a 3 channel or 1 channel color to fill the mask with depending on the input image
if len(img.shape) > 2:
channel_count = img.shape[2] # i.e. 3 or 4 depending on your image
ignore_mask_color = (255,) * channel_count
else:
ignore_mask_color = 255
#filling pixels inside the polygon defined by "vertices" with the fill color
cv2.fillPoly(mask, vertices, ignore_mask_color)
#returning the image only where mask pixels are nonzero
masked_image = cv2.bitwise_and(img, mask)
return masked_image
def draw_lines(img, lines, sy, color=[255, 0, 0], thickness=2):
"""
NOTE: this is the function you might want to use as a starting point once you want to
average/extrapolate the line segments you detect to map out the full
extent of the lane (going from the result shown in raw-lines-example.mp4
to that shown in P1_example.mp4).
Think about things like separating line segments by their
slope ((y2-y1)/(x2-x1)) to decide which segments are part of the left
line vs. the right line. Then, you can average the position of each of
the lines and extrapolate to the top and bottom of the lane.
This function draws `lines` with `color` and `thickness`.
Lines are drawn on the image inplace (mutates the image).
If you want to make the lines semi-transparent, think about combining
this function with the weighted_img() function below
"""
avg_right_theta = 0
avg_right_b = 0
avg_right_m = 0
avg_left_theta = 0
avg_left_b = 0
avg_left_m = 0
num_right = 0
num_left = 0
try:
for line in lines:
x1 = line[0][0]
y1 = line[0][1]
x2 = line[0][2]
y2 = line[0][3]
# cv2.line(img, (x1, y1), (x2, y2), color, thickness) # x1,y1,x2,y2
theta = math.atan2(y2-y1,x2-x1) #
b = y1 - math.tan(theta)*x1
# b2 = y2 - math.tan(theta)*x2
if theta > 0.4:
avg_right_theta += theta
avg_right_b += b
num_right += 1
elif theta < -0.4:
avg_left_theta += theta
avg_left_b += b
num_left += 1
except TypeError:
pass
if num_left > 0:
avg_left_theta /= num_left
avg_left_m = math.tan(avg_left_theta)
avg_left_b /= num_left
#left_lower
y_ll = int(sy)
x_ll = int((sy-avg_left_b)/avg_left_m)
# left_upper
y_lu = int(sy/2*1.19)
x_lu = int((sy/2*1.19-avg_left_b)/avg_left_m)
# drawing lines
cv2.line(img, (x_ll,y_ll),(x_lu,y_lu), [0,255,255], 4)
if num_right > 0:
avg_right_theta /= num_right
avg_right_m = math.tan(avg_right_theta)
avg_right_b /= num_right
# right_lower
y_rl = int(sy)
x_rl = int((sy-avg_right_b)/avg_right_m)
# right_upper
y_ru = int(sy/2*1.19)
x_ru = int((sy/2*1.19-avg_right_b)/avg_right_m)
# drawing lines
cv2.line(img, (x_rl,y_rl),(x_ru,y_ru), [0,255,255], 4)
def hough_lines(img, rho, theta, threshold, min_line_len, max_line_gap, sy):
"""
`img` should be the output of a Canny transform.
Returns an image with hough lines drawn.
"""
lines = cv2.HoughLinesP(img, rho, theta, threshold, np.array([]), minLineLength=min_line_len, maxLineGap=max_line_gap)
# print("num of lines: ",len(lines))
line_img = np.zeros((img.shape[0], img.shape[1], 3), dtype=np.uint8)
draw_lines(line_img, lines, sy)
return line_img
# Python 3 has support for cool math symbols.
def weighted_img(img, initial_img, α=0.8, β=1, γ=0.):
"""
`img` is the output of the hough_lines(), An image with lines drawn on it.
Should be a blank image (all black) with lines drawn on it.
`initial_img` should be the image before any processing.
The result image is computed as follows:
initial_img * α + img * β + γ
NOTE: initial_img and img must be the same shape!
"""
return cv2.addWeighted(initial_img, α, img, β, γ)
# printing out some stats and plotting
# print('This image is:', type(image), 'with dimensions:', image.shape)
# if you wanted to show a single color channel image called 'gray', for example
# call as plt.imshow(gray, cmap='gray')
# plt.imshow(image)
# list images available for testing
# print(os.listdir("test_images/"))
# TODO: Build your pipeline that will draw lane lines on the test_images
# then save them to the test_images_output directory.
def process_image(image):
# NOTE: The output you return should be a color image (3 channel) for processing video below
# TODO: put your pipeline here,
# you should return the final output (image where lines are drawn on lanes)
colorfilt = colorselect(image, 190, 190, 0)
gray = grayscale(image)
blurred = gaussian_blur(gray,5)
edges1 = canny(blurred,75,175)
edges2 = canny(colorfilt,75,175)
edges = weighted_img(edges1,edges2,1,1,0)
#edges = canny(blurred,75,175)
sx, sy = image.shape[1],image.shape[0]
vertices = np.array([[(.06*sx,sy),(sx/2*.95, sy/2*1.19), (sx/2*1.06, sy/2*1.19), (.97*sx,sy)]], dtype=np.int32)
#vertices = np.array([[(.16*sx,.9*sy),(sx/2*.95, sy/2*1.19), (sx/2*1.06, sy/2*1.19), (.86*sx,.9*sy)]], dtype=np.int32)
masked_edges = region_of_interest(edges,vertices)
lines = hough_lines(masked_edges,1,np.pi/180,15,70,50, sy)
lanelines = weighted_img(lines,image)
return lanelines
def debug_process_image():
# NOTE: The output you return should be a color image (3 channel) for processing video below
# TODO: put your pipeline here,
# you should return the final output (image where lines are drawn on lanes)
for im_name in os.listdir("test_images/"):
# reading in an image
print("processing ", im_name, "...")
image = mpimg.imread('test_images/{}'.format(im_name))
### COPY of process_image(image) function
colorfilt = colorselect(image, 190, 190, 0)
gray = grayscale(image)
blurred = gaussian_blur(gray,5)
edges1 = canny(blurred,75,175)
edges2 = canny(colorfilt,75,175)
edges = weighted_img(edges1,edges2,1,1,0)
#edges = canny(blurred,75,175)
sx, sy = image.shape[1],image.shape[0]
vertices = np.array([[(.06*sx,sy),(sx/2*.95, sy/2*1.19), (sx/2*1.06, sy/2*1.19), (.97*sx,sy)]], dtype=np.int32)
#vertices = np.array([[(.16*sx,.9*sy),(sx/2*.95, sy/2*1.19), (sx/2*1.06, sy/2*1.19), (.86*sx,.9*sy)]], dtype=np.int32)
masked_edges = region_of_interest(edges,vertices)
lines = hough_lines(masked_edges,1,np.pi/180,15,70,50, sy)
lanelines = weighted_img(lines,image)
###
for i,coords in enumerate(vertices[0]):
cv2.line(lanelines,(vertices[0][i][0],vertices[0][i][1]),(vertices[0][(i+1)%len(vertices[0])][0],vertices[0][(i+1)%len(vertices[0])][1]),(0,0,255),2)
mpimg.imsave("test_images_output/{}_01_gray.png".format(im_name), colorfilt)
#mpimg.imsave("test_images_output/{}_02_blurred.png".format(im_name), blurred, cmap='gray')
mpimg.imsave("test_images_output/{}_03_edges.png".format(im_name), edges, cmap='gray')
mpimg.imsave("test_images_output/{}_04_masked_edges.png".format(im_name), masked_edges, cmap='gray')
mpimg.imsave("test_images_output/{}_05_lines.png".format(im_name), lines)
mpimg.imsave("test_images_output/{}_06_lanelines.png".format(im_name), lanelines)
print("done processing")
# debug_process_image()
def process_video():
for vid_name in os.listdir("test_videos/"):
white_output = 'test_videos_output/{}'.format(vid_name)
## To speed up the testing process you may want to try your pipeline on a shorter subclip of the video
## To do so add .subclip(start_second,end_second) to the end of the line below
## Where start_second and end_second are integer values representing the start and end of the subclip
## You may also uncomment the following line for a subclip of the first 5 seconds
##clip1 = VideoFileClip("test_videos/solidWhiteRight.mp4").subclip(0,5)
clip1 = VideoFileClip("test_videos/{}".format(vid_name))#.subclip(0,5)
white_clip = clip1.fl_image(process_image) #NOTE: this function expects color images!!
white_clip.write_videofile(white_output, audio=False)
clip1.reader.close()
clip1.audio.reader.close_proc()
clip1.reader.close()
clip1.audio.reader.close_proc()
del clip1
process_video()
#def process_image(image):
# # NOTE: The output you return should be a color image (3 channel) for processing video below
# # TODO: put your pipeline here,
# # you should return the final output (image where lines are drawn on lanes)
# gray = grayscale(image)
# blurred = gaussian_blur(gray,5)
# edges = canny(blurred,75,175)
# #edges = canny(blurred,75,175)
# sx, sy = image.shape[1],image.shape[0]
# vertices = np.array([[(.06*sx,sy),(sx/2*.95, sy/2*1.19), (sx/2*1.06, sy/2*1.19), (.97*sx,sy)]], dtype=np.int32)
# #vertices = np.array([[(.16*sx,.9*sy),(sx/2*.95, sy/2*1.19), (sx/2*1.06, sy/2*1.19), (.86*sx,.9*sy)]], dtype=np.int32)
# masked_edges = region_of_interest(edges,vertices)
# lines = hough_lines(masked_edges,1,np.pi/180,15,70,50, sy)
# #lines = hough_lines(masked_edges,2,np.pi/180,15,70,50, sy)
# lanelines = weighted_img(lines,image)
# return lanelines