這部份接續專案後續程式建立 pipeline及測試圖檔、影片之成果
測試圖檔及pipeline程式說明如下:[1]
import os
os.listdir("test_images/")
產出結果如下:
構建pipeline,針對所有test_images上圖檔提供解決方案。 將副本複製到test_images_output目錄中,您可以使用這些圖像寫入成果報告中。
試著調整各種參數,包括Canny門檻值以及Hough門檻值參數,提供較佳的車道道呈現。
# Import everything needed to edit/save/watch video clips
from moviepy.editor import VideoFileClip
from IPython.display import HTML
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_image = grayscale(image)
gaus_blur = gaussian_blur(gray_image, 3)
edges = canny(gaus_blur, 50,150)
imshape = image.shape
vertices = np.array([[(0,imshape[0]),(450, 320), (500, 320), (imshape[1],imshape[0])]], dtype=np.int32)
masked = region_of_interest(edges, vertices)
rho = 2 #distance resolution in pixels of the Hough grid
theta = np.pi/180 #angular resolution in radians of the Hough grid
threshold = 5 #minimum number of votes (intersections in Hough grid cell)
min_line_len = 10 #minimum number of pixels making up a line
max_line_gap = 20 #maximum gap in pixels between connectable line segments
line_image = hough_lines(masked, rho, theta, threshold, min_line_len, max_line_gap)
result = weighted_img(line_image, image)
return result
images = os.listdir("test_images/")
for img_file in images:
#print(img_file)
# Skip all files starting with line.
if img_file[0:4] == 'line':
continue
image = mpimg.imread('test_images/' + img_file)
weighted = process_image(image)
plt.imshow(weighted)
#break
mpimg.imsave('test_images/lines-' + img_file, weighted)
產出:
white_output = 'test_videos_output/solidWhiteRight.mp4'
## 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/solidWhiteRight.mp4")
white_clip = clip1.fl_image(process_image) #NOTE: this function expects color images!!
%time white_clip.write_videofile(white_output, audio=False)
HTML("""
<video width="960" height="540" controls>
<source src="{0}">
</video>
""".format(white_output))