Self-Driving Car Engineer Nanodegree

Project: Finding Lane Lines on the Road


In this project, you will use the tools you learned about in the lesson to identify lane lines on the road. You can develop your pipeline on a series of individual images, and later apply the result to a video stream (really just a series of images). Check out the video clip "raw-lines-example.mp4" (also contained in this repository) to see what the output should look like after using the helper functions below.

Once you have a result that looks roughly like "raw-lines-example.mp4", you'll need to get creative and try to average and/or extrapolate the line segments you've detected to map out the full extent of the lane lines. You can see an example of the result you're going for in the video "P1_example.mp4". Ultimately, you would like to draw just one line for the left side of the lane, and one for the right.

In addition to implementing code, there is a brief writeup to complete. The writeup should be completed in a separate file, which can be either a markdown file or a pdf document. There is a write up template that can be used to guide the writing process. Completing both the code in the Ipython notebook and the writeup template will cover all of the rubric points for this project.


Let's have a look at our first image called 'test_images/solidWhiteRight.jpg'. Run the 2 cells below (hit Shift-Enter or the "play" button above) to display the image.

Note: If, at any point, you encounter frozen display windows or other confounding issues, you can always start again with a clean slate by going to the "Kernel" menu above and selecting "Restart & Clear Output".


The tools you have are color selection, region of interest selection, grayscaling, Gaussian smoothing, Canny Edge Detection and Hough Tranform line detection. You are also free to explore and try other techniques that were not presented in the lesson. Your goal is piece together a pipeline to detect the line segments in the image, then average/extrapolate them and draw them onto the image for display (as below). Once you have a working pipeline, try it out on the video stream below.


Your output should look something like this (above) after detecting line segments using the helper functions below

Your goal is to connect/average/extrapolate line segments to get output like this

Run the cell below to import some packages. If you get an import error for a package you've already installed, try changing your kernel (select the Kernel menu above --> Change Kernel). Still have problems? Try relaunching Jupyter Notebook from the terminal prompt. Also, see this forum post for more troubleshooting tips.

Import Packages


In [2]:
#importing some useful packages
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
import cv2
import os
import math
from scipy import misc
%matplotlib inline

# Import everything needed to edit/save/watch video clips
from moviepy.editor import VideoFileClip
from IPython.display import HTML

Ideas for Lane Detection Pipeline

Some OpenCV functions (beyond those introduced in the lesson) that might be useful for this project are:

cv2.inRange() for color selection
cv2.fillPoly() for regions selection
cv2.line() to draw lines on an image given endpoints
cv2.addWeighted() to coadd / overlay two images cv2.cvtColor() to grayscale or change color cv2.imwrite() to output images to file
cv2.bitwise_and() to apply a mask to an image

Check out the OpenCV documentation to learn about these and discover even more awesome functionality!

Helper Functions

Below are some helper functions to help get you started. They should look familiar from the lesson!


In [5]:
# Parameters for all helper functions
canny_low  = 50
canny_high = 150
kernel_size = 3
rho = 2                 # distance resolution of the Hough grid [pixel]
theta = np.pi/180       # angular resolution of the Hough grid [rad]
threshold = 15          # minimum number of votes (intersections in Hough grid cell)
min_line_len = 40       # minimum number of pixels making up a line
max_line_gap = 20       # maximum gap in pixels between connectable line segments
im_y = 540              # height of image
im_x = 960              # width of image
mask_y  = int(0.6*im_y) # upper y-coordinate of fillPoly trapezoidal mask region
mask_x1 = int(0.48*im_x)# upper left x-coordinate of fillPoly trapezoidal mask region 
mask_x2 = int(0.54*im_x)# upper right x-coordinate of fillPoly trapezoidal mask region 
    
left_x_prev  = []       # Holds last valid value of left_x array used in draw_lines()
left_y_prev  = []       # Holds last valid value of left_y array used in draw_lines()
right_x_prev = []       # Holds last valid value of right_x array used in draw_lines()
right_y_prev = []       # Holds last valid value of right_y array used in draw_lines()
img_dir = "test_images/"
img_out_dir="test_images_out/"

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 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.
    """
    #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, 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
    """
    global left_x_prev
    global left_y_prev
    global right_x_prev
    global right_y_prev
    global im_y
    global im_x
    global mask_y 
    left_slopes  = []
    right_slopes = []
    left_x  = [] 
    left_y  = []
    right_x = []
    right_y = []
    sl_pt_lt = {}
    sl_pt_rt = {}
    left_slope  = 0
    right_slope = 0
        
    for line in lines:
        for x1,y1,x2,y2 in line:
            slope = np.arctan((y2-y1)/(x2-x1))
            slope = slope*180/np.pi
            if slope < -15 and slope > -60:
                sl_pt_lt[slope] = (x1,x2,y1,y2)
                left_slopes.append(slope)
            elif slope > 15 and slope < 60:
                sl_pt_rt[slope] = (x1,x2,y1,y2)
                right_slopes.append(slope)
        
    left_mean    = np.mean(left_slopes)
    left_std     = np.std(left_slopes)
    keys_remove  = [s for s in sl_pt_lt.keys() if np.abs(left_mean-s) > left_std]
    for key_remove in keys_remove:
        del sl_pt_lt[key_remove]
    if len(list(sl_pt_lt.values())) == 0 :
        left_x = left_x_prev.copy()
        left_y = left_y_prev.copy()
    else:
        for val in list(sl_pt_lt.values()):
            left_x.extend(val[0:2])
            left_y.extend(val[2:])
        left_x_prev = left_x.copy()
        left_y_prev = left_y.copy()
        
    right_mean   = np.mean(right_slopes)
    right_std    = np.std(right_slopes)
    keys_remove  = [s for s in sl_pt_rt.keys() if np.abs(right_mean-s) > right_std]
    for key_remove in keys_remove:
        del sl_pt_rt[key_remove]
    if len(list(sl_pt_rt.values())) == 0 :
        right_x = right_x_prev.copy()
        right_y = right_y_prev.copy()
    else:
        for val in list(sl_pt_rt.values()):
            right_x.extend(val[0:2])
            right_y.extend(val[2:])
        right_x_prev = right_x.copy()
        right_y_prev = right_y.copy()
    
    left_coeff = np.polyfit(left_x, left_y, 1)
    pt_l1 =  (int((im_y-left_coeff[1])/left_coeff[0]),im_y) 
    pt_l2 =  (int((mask_y-left_coeff[1])/left_coeff[0]), mask_y) 
    cv2.line(img, pt_l1, pt_l2, color, thickness)
    
    right_coeff = np.polyfit(right_x, right_y,1)
    pt_r1 =  (int((im_y-right_coeff[1])/right_coeff[0]),im_y) 
    pt_r2 =  (int((mask_y-right_coeff[1])/right_coeff[0]), mask_y) 
    cv2.line(img, pt_r1, pt_r2, color, thickness)
    
      
def hough_lines(img, rho, theta, threshold, min_line_len, max_line_gap):
    """
    `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)
    line_img = np.zeros((img.shape[0], img.shape[1], 3), dtype=np.uint8)
    draw_lines(line_img, lines)
    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, β, λ)

# Function to implement single image processing pipeline
def process_image(img):
    # NOTE: The output you return should be a color image (3 channel) for processing video below
    # you should return the final output (image where lines are drawn on lanes)
    
    global im_y
    global im_x
    global mask_y
    global mask_x1
    global mask_x2
    
    vertices = np.array([[(0,im_y),(mask_x1,mask_y), (mask_x2,mask_y), (im_x,im_y)]], dtype=np.int32)

    img_gray = grayscale(img)
    img_blur = gaussian_blur(img_gray, kernel_size)
    img_edge = canny(img_blur, canny_low, canny_high)
    img_crop = region_of_interest(img_edge, vertices)
    img_line = hough_lines(img_crop,rho,theta,threshold,min_line_len,max_line_gap)
    img_out  = weighted_img(img_line, img, α=0.6, β=1., λ=0.)
    
    cv2.line(img_out,(vertices[0][0][0],vertices[0][0][1]),(vertices[0][1][0],vertices[0][1][1]), (0,0,255),2)
    cv2.line(img_out,(vertices[0][1][0],vertices[0][1][1]),(vertices[0][2][0],vertices[0][2][1]), (0,0,255),2)
    cv2.line(img_out,(vertices[0][2][0],vertices[0][2][1]),(vertices[0][3][0],vertices[0][3][1]), (0,0,255),2)
    cv2.line(img_out,(vertices[0][3][0],vertices[0][3][1]),(vertices[0][0][0],vertices[0][0][1]), (0,0,255),2)
    
    return img_out

Build the pipeline and run your solution on all test_images. Make copies into the test_images_output directory, and you can use the images in your writeup report.

Try tuning the various parameters, especially the low and high Canny thresholds as well as the Hough lines parameters.

Test Images

Build your pipeline to work on the images in the directory "test_images"
You should make sure your pipeline works well on these images before you try the videos.

Build a Lane Finding Pipeline


In [6]:
# Build your pipeline that will draw lane lines on the test_images
# then save them to the test_images directory.

img_names = os.listdir(img_dir)
for img_name in img_names:
    img= mpimg.imread(img_dir+img_name)
    img_out = process_image(img)
    misc.imsave(img_out_dir+img_name, img_out)
    plt.imshow(img_out)


Test on Videos

You know what's cooler than drawing lanes over images? Drawing lanes over video!

We can test our solution on two provided videos:

solidWhiteRight.mp4

solidYellowLeft.mp4

Note: if you get an import error when you run the next cell, try changing your kernel (select the Kernel menu above --> Change Kernel). Still have problems? Try relaunching Jupyter Notebook from the terminal prompt. Also, check out this forum post for more troubleshooting tips.

If you get an error that looks like this:

NeedDownloadError: Need ffmpeg exe. 
You can download it by calling: 
imageio.plugins.ffmpeg.download()

Follow the instructions in the error message and check out this forum post for more troubleshooting tips across operating systems.


In [4]:
# Import everything needed to edit/save/watch video clips
from moviepy.editor import VideoFileClip
from IPython.display import HTML

In [12]:
## process_image() has been defined in the Helper Functions cell of this notebook for code reuse with testing images and video.

Let's try the one with the solid white lane on the right first ...


In [5]:
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)


Before  9 8
After   3 6
[MoviePy] >>>> Building video test_videos_output/solidWhiteRight.mp4
[MoviePy] Writing video test_videos_output/solidWhiteRight.mp4
  0%|                                                  | 0/222 [00:00<?, ?it/s]
Before  9 8
After   3 6
Before  7 9
After   5 5
Before  12 8
After   7 5
Before  6 8
After   3 5
Before  7 3
After   4 2
  2%|▉                                         | 5/222 [00:00<00:04, 43.48it/s]
Before  6 4
After   4 2
Before  10 5
After   5 4
Before  7 2
After   3 1
Before  6 6
After   2 4
Before  7 6
After   4 4
Before  10 7
After   6 6
  5%|██                                       | 11/222 [00:00<00:04, 45.35it/s]
Before  10 8
After   7 5
Before  9 8
After   7 4
Before  11 6
After   8 2
Before  9 6
After   6 4
Before  9 7
After   7 4
  7%|██▉                                      | 16/222 [00:00<00:04, 46.65it/s]
Before  6 3
After   4 2
Before  8 3
After   5 2
Before  8 1
After   5 1
Before  7 3
After   4 1
Before  4 8
After   2 3
Before  7 9
After   5 6
 10%|████                                     | 22/222 [00:00<00:04, 48.30it/s]
Before  7 9
After   6 5
Before  7 6
After   5 3
Before  10 7
After   7 5
Before  8 7
After   5 5
Before  5 8
After   1 5
Before  5 9
After   3 6
 13%|█████▏                                   | 28/222 [00:00<00:03, 49.15it/s]
Before  9 4
After   7 2
Before  8 4
After   6 2
Before  5 2
After   4 2
Before  6 2
After   3 2
Before  7 3
After   4 1
Before  7 7
After   3 4
 15%|██████▎                                  | 34/222 [00:00<00:03, 50.15it/s]
Before  6 7
After   3 6
Before  9 4
After   7 3
Before  9 6
After   7 4
Before  9 11
After   5 8
Before  6 8
After   3 5
Before  6 7
After   4 3
 18%|███████▍                                 | 40/222 [00:00<00:03, 50.61it/s]
Before  9 4
After   6 3
Before  7 3
After   4 2
Before  8 3
After   3 2
Before  10 0
After   7 0
Before  7 4
After   4 2
 20%|████████▎                                | 45/222 [00:00<00:03, 48.66it/s]
Before  9 5
After   6 3
Before  8 6
After   6 5
Before  6 6
After   4 3
Before  6 5
After   2 2
Before  5 6
After   3 5
 23%|█████████▏                               | 50/222 [00:01<00:03, 47.24it/s]
Before  4 7
After   2 5
Before  7 6
After   4 4
Before  4 3
After   2 2
Before  7 3
After   4 1
Before  8 1
After   6 1
 25%|██████████▏                              | 55/222 [00:01<00:03, 46.43it/s]
Before  10 0
After   6 0
Before  10 6
After   5 3
Before  9 5
After   5 3
Before  9 6
After   4 3
Before  9 5
After   7 3
 27%|███████████                              | 60/222 [00:01<00:03, 46.01it/s]
Before  7 8
After   4 6
Before  7 7
After   4 5
Before  6 8
After   4 4
Before  7 7
After   6 5
Before  8 5
After   4 3
 29%|████████████                             | 65/222 [00:01<00:03, 45.09it/s]
Before  7 5
After   4 3
Before  7 2
After   5 2
Before  9 2
After   5 1
Before  5 8
After   3 4
Before  5 6
After   3 4
 32%|████████████▉                            | 70/222 [00:01<00:03, 45.32it/s]
Before  4 6
After   2 4
Before  6 5
After   3 2
Before  12 5
After   7 1
Before  6 7
After   3 4
Before  8 9
After   5 5
 34%|█████████████▊                           | 75/222 [00:01<00:03, 44.75it/s]
Before  8 6
After   6 4
Before  7 3
After   5 2
Before  9 4
After   5 3
Before  8 2
After   6 2
Before  7 3
After   4 2
 36%|██████████████▊                          | 80/222 [00:01<00:03, 45.08it/s]
Before  6 3
After   3 2
Before  8 5
After   4 3
Before  10 5
After   6 3
Before  8 5
After   5 2
Before  5 5
After   4 2
 38%|███████████████▋                         | 85/222 [00:01<00:03, 45.56it/s]
Before  8 7
After   6 5
Before  6 5
After   4 3
Before  7 8
After   3 5
Before  7 5
After   4 3
Before  9 4
After   5 3
 41%|████████████████▌                        | 90/222 [00:01<00:02, 45.28it/s]
Before  6 3
After   3 1
Before  9 3
After   7 1
Before  7 6
After   4 4
Before  8 5
After   6 2
Before  6 5
After   5 3
 43%|█████████████████▌                       | 95/222 [00:02<00:02, 45.58it/s]
Before  7 5
After   4 2
Before  6 5
After   3 3
Before  6 9
After   3 5
Before  6 7
After   3 4
Before  7 8
After   4 3
 45%|██████████████████                      | 100/222 [00:02<00:02, 45.54it/s]
Before  7 6
After   4 4
Before  8 2
After   7 1
Before  7 2
After   6 2
Before  7 4
After   5 2
Before  7 6
After   4 2
 47%|██████████████████▉                     | 105/222 [00:02<00:02, 45.39it/s]
Before  6 7
After   2 4
Before  8 5
After   5 2
Before  8 6
After   4 4
Before  10 7
After   8 4
Before  8 9
After   5 6
 50%|███████████████████▊                    | 110/222 [00:02<00:02, 45.66it/s]
Before  11 6
After   6 4
Before  5 6
After   2 4
Before  8 5
After   7 4
Before  5 6
After   4 4
Before  8 2
After   4 1
 52%|████████████████████▋                   | 115/222 [00:02<00:02, 45.10it/s]
Before  8 3
After   6 1
Before  7 4
After   4 2
Before  10 7
After   6 4
Before  6 6
After   3 3
Before  10 5
After   8 3
 54%|█████████████████████▌                  | 120/222 [00:02<00:02, 45.08it/s]
Before  8 8
After   3 4
Before  8 9
After   5 4
Before  10 9
After   6 6
Before  5 8
After   3 4
Before  9 7
After   8 4
 56%|██████████████████████▌                 | 125/222 [00:02<00:02, 45.07it/s]
Before  7 4
After   3 2
Before  7 4
After   5 2
Before  5 5
After   2 4
Before  8 8
After   4 6
Before  7 6
After   4 3
 59%|███████████████████████▍                | 130/222 [00:02<00:02, 45.31it/s]
Before  5 7
After   2 6
Before  6 8
After   3 6
Before  6 8
After   2 6
Before  7 10
After   6 4
Before  4 10
After   3 7
 61%|████████████████████████▎               | 135/222 [00:02<00:01, 44.38it/s]
Before  9 7
After   7 5
Before  8 9
After   5 5
Before  12 5
After   9 3
Before  6 3
After   4 1
Before  10 5
After   5 4
 63%|█████████████████████████▏              | 140/222 [00:03<00:01, 44.46it/s]
Before  9 7
After   6 4
Before  6 8
After   5 5
Before  9 7
After   6 5
Before  8 7
After   7 4
Before  8 10
After   7 5
 65%|██████████████████████████▏             | 145/222 [00:03<00:01, 44.51it/s]
Before  11 8
After   8 4
Before  11 11
After   6 6
Before  9 9
After   5 5
Before  9 5
After   6 2
Before  9 5
After   7 3
 68%|███████████████████████████             | 150/222 [00:03<00:01, 44.43it/s]
Before  9 4
After   4 1
Before  10 4
After   6 2
Before  9 6
After   5 5
Before  14 4
After   12 2
Before  5 7
After   3 4
 70%|███████████████████████████▉            | 155/222 [00:03<00:01, 44.50it/s]
Before  7 7
After   5 6
Before  9 9
After   5 5
Before  8 10
After   6 8
Before  9 8
After   6 5
Before  7 8
After   5 5
 72%|████████████████████████████▊           | 160/222 [00:03<00:01, 44.78it/s]
Before  11 6
After   5 4
Before  8 4
After   4 2
Before  9 5
After   5 4
Before  9 4
After   6 2
Before  6 6
After   3 4
 74%|█████████████████████████████▋          | 165/222 [00:03<00:01, 44.26it/s]
Before  6 7
After   3 4
Before  6 10
After   2 6
Before  10 10
After   7 7
Before  4 9
After   2 5
Before  6 8
After   3 7
 77%|██████████████████████████████▋         | 170/222 [00:03<00:01, 44.49it/s]
Before  11 9
After   8 6
Before  7 8
After   4 5
Before  6 6
After   4 4
Before  8 5
After   7 4
Before  11 4
After   8 3
 79%|███████████████████████████████▌        | 175/222 [00:03<00:01, 44.66it/s]
Before  8 4
After   4 2
Before  9 6
After   5 4
Before  10 7
After   6 5
Before  7 8
After   4 5
Before  7 8
After   5 6
 81%|████████████████████████████████▍       | 180/222 [00:03<00:00, 44.53it/s]
Before  8 7
After   5 4
Before  10 7
After   7 3
Before  9 7
After   6 5
Before  12 7
After   9 3
Before  9 7
After   5 4
 83%|█████████████████████████████████▎      | 185/222 [00:04<00:00, 44.45it/s]
Before  11 4
After   6 2
Before  7 5
After   4 4
Before  6 3
After   4 2
Before  6 2
After   4 1
Before  7 4
After   4 3
 86%|██████████████████████████████████▏     | 190/222 [00:04<00:00, 45.11it/s]
Before  10 7
After   7 5
Before  9 7
After   6 5
Before  11 7
After   6 4
Before  7 6
After   4 4
Before  6 10
After   4 7
 88%|███████████████████████████████████▏    | 195/222 [00:04<00:00, 44.72it/s]
Before  9 7
After   6 3
Before  9 7
After   7 4
Before  8 6
After   6 3
Before  12 4
After   9 2
Before  11 4
After   5 2
 90%|████████████████████████████████████    | 200/222 [00:04<00:00, 44.94it/s]
Before  9 4
After   6 2
Before  9 7
After   6 4
Before  11 4
After   10 2
Before  11 9
After   7 7
Before  6 9
After   3 5
 92%|████████████████████████████████████▉   | 205/222 [00:04<00:00, 44.49it/s]
Before  7 7
After   4 5
Before  9 7
After   6 4
Before  6 9
After   2 6
Before  6 9
After   3 6
Before  5 4
After   4 3
 95%|█████████████████████████████████████▊  | 210/222 [00:04<00:00, 43.15it/s]
Before  5 6
After   2 4
Before  5 7
After   3 5
Before  7 5
After   3 3
Before  4 5
After   2 2
Before  5 7
After   2 3
 97%|██████████████████████████████████████▋ | 215/222 [00:04<00:00, 43.03it/s]
Before  7 11
After   4 6
Before  5 8
After   2 4
Before  10 8
After   6 5
Before  8 8
After   7 5
Before  8 9
After   5 5
 99%|███████████████████████████████████████▋| 220/222 [00:04<00:00, 43.16it/s]
Before  8 8
After   4 6
100%|███████████████████████████████████████▊| 221/222 [00:04<00:00, 45.43it/s]
[MoviePy] Done.
[MoviePy] >>>> Video ready: test_videos_output/solidWhiteRight.mp4 

Wall time: 5.21 s

Play the video inline, or if you prefer find the video in your filesystem (should be in the same directory) and play it in your video player of choice.


In [6]:
HTML("""
<video width="960" height="540" controls>
  <source src="{0}">
</video>
""".format(white_output))


Out[6]:

Improve the draw_lines() function

At this point, if you were successful with making the pipeline and tuning parameters, you probably have the Hough line segments drawn onto the road, but what about identifying the full extent of the lane and marking it clearly as in the example video (P1_example.mp4)? Think about defining a line to run the full length of the visible lane based on the line segments you identified with the Hough Transform. As mentioned previously, try to average and/or extrapolate the line segments you've detected to map out the full extent of the lane lines. You can see an example of the result you're going for in the video "P1_example.mp4".

Go back and modify your draw_lines function accordingly and try re-running your pipeline. The new output should draw a single, solid line over the left lane line and a single, solid line over the right lane line. The lines should start from the bottom of the image and extend out to the top of the region of interest.

Now for the one with the solid yellow lane on the left. This one's more tricky!


In [12]:
yellow_output = 'test_videos_output/solidYellowLeft.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
##clip2 = VideoFileClip('test_videos/solidYellowLeft.mp4').subclip(0,5)
clip2 = VideoFileClip('test_videos/solidYellowLeft.mp4')
yellow_clip = clip2.fl_image(process_image)
%time yellow_clip.write_videofile(yellow_output, audio=False)


[MoviePy] >>>> Building video test_videos_output/solidYellowLeft.mp4
[MoviePy] Writing video test_videos_output/solidYellowLeft.mp4
100%|███████████████████████████████████████▉| 681/682 [00:08<00:00, 83.00it/s]
[MoviePy] Done.
[MoviePy] >>>> Video ready: test_videos_output/solidYellowLeft.mp4 

Wall time: 8.55 s

In [8]:
HTML("""
<video width="960" height="540" controls>
  <source src="{0}">
</video>
""".format(yellow_output))


Out[8]:

Writeup and Submission

If you're satisfied with your video outputs, it's time to make the report writeup in a pdf or markdown file. Once you have this Ipython notebook ready along with the writeup, it's time to submit for review! Here is a link to the writeup template file.

Optional Challenge

Try your lane finding pipeline on the video below. Does it still work? Can you figure out a way to make it more robust? If you're up for the challenge, modify your pipeline so it works with this video and submit it along with the rest of your project!


In [7]:
im_y = 720              # height of image
im_x = 1280              # width of image
mask_y  = int(0.6*im_y) # upper y-coordinate of fillPoly trapezoidal mask region
mask_x1 = int(0.48*im_x)# upper left x-coordinate of fillPoly trapezoidal mask region 
mask_x2 = int(0.54*im_x)# upper right x-coordinate of fillPoly trapezoidal mask region 

challenge_output = 'test_videos_output/challenge.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
##clip3 = VideoFileClip('test_videos/challenge.mp4').subclip(0,5)
clip3 = VideoFileClip('test_videos/challenge.mp4')
challenge_clip = clip3.fl_image(process_image)
%time challenge_clip.write_videofile(challenge_output, audio=False)


[MoviePy] >>>> Building video test_videos_output/challenge.mp4
[MoviePy] Writing video test_videos_output/challenge.mp4
100%|████████████████████████████████████████| 251/251 [00:08<00:00, 31.26it/s]
[MoviePy] Done.
[MoviePy] >>>> Video ready: test_videos_output/challenge.mp4 

Wall time: 8.93 s

In [8]:
HTML("""
<video width="960" height="540" controls>
  <source src="{0}">
</video>
""".format(challenge_output))


Out[8]:

In [ ]: