<jupyter_start><jupyter_text>Project: **Finding Lane Lines on the Road** ***Develop a pipeline to identify lane lines on the road. You must apply it on a series of individual images, provided in the *test_images* folder. Once you have a result that looks roughly like the image *line-segments-example* in the examples folder (also shown below), you'll need to try to average and/or extrapolate the line segments you've detected to map out the full extent of the lane lines.   **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.  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).**---      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    Import Packages<jupyter_code>#importing some useful packages
import matplotlib.pyplot as plt
import numpy as np
import cv2
%matplotlib inline<jupyter_output><empty_output><jupyter_text>Read in an Image<jupyter_code>#reading in an image
image = cv2.imread('test_images/solidWhiteRight.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

#printing out some stats and plotting
print('This image is:', type(image), 'with dimensions:', image.shape)
plt.imshow(image)  # if you wanted to show a single color channel image called 'gray', for example, call as plt.imshow(gray, cmap='gray')<jupyter_output>This image is: <class 'numpy.ndarray'> with dimensions: (540, 960, 3)<jupyter_text>Ideas for Lane Detection Pipeline **Some OpenCV functions 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  Helper Functions Below are some helper functions to help get you started.<jupyter_code>import math

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.
    `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, color=[255, 0, 0], thickness=2):
    """
    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
    """
    for line in lines:
        for x1,y1,x2,y2 in line:
            cv2.line(img, (x1, y1), (x2, y2), 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, lines

def weighted_img(img, initial_img, alpha=0.8, beta=1., gamma=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, alpha, img, beta, gamma)

def getKey(item):
    return item[0]<jupyter_output><empty_output><jupyter_text>Test ImagesBuild your pipeline to work on the images in the directory "test_images"<jupyter_code>import os

path = "test_images/"
files = os.listdir(path)
images = []

f, plots = plt.subplots((len(files)+3-1)//3, 3, figsize=(20,10))
plots = [plot for sublist in plots for plot in sublist]

for file, plot in zip(files, plots):
    image = cv2.cvtColor(cv2.imread(os.path.join(path, file)), cv2.COLOR_BGR2RGB)
    plot.set_title(file)
    plot.imshow(image)
    images.append((image, file))<jupyter_output><empty_output><jupyter_text>Build a Lane Finding Pipeline Build the pipeline and run your solution on all test_images.Try tuning the various parameters, especially the low and high Canny thresholds as well as the Hough lines parameters.<jupyter_code># TODO: Build your pipeline that will draw lane lines segments on the test_images

#aplly gray scale to all the images
images_copy = []
gray_scale_images = []
for element in images:
    gray_scale_images.append(grayscale(element[0]))
    images_copy.append(element[0])
#plt.imshow(gray_scale_images[0], cmap='gray')

#noise elimination with gaussean blur
blured_images = []
kernel_size = 7
for element in gray_scale_images:
    blured_images.append(gaussian_blur(element, kernel_size))
#plt.imshow(blured_images[0], cmap='gray')

#canny function for borders
lower_canny = 200
upper_canny = 240
canny_images = []
for element in blured_images:
    canny_images.append(canny(element, lower_canny, upper_canny))
#plt.imshow(canny_images[0], cmap='gray')
    
#region of interest
vertices = np.array([[[150,540], [400,330], [600,330], [850,540]]])
masked_images = []
for element in canny_images:
    masked_images.append(region_of_interest(element, vertices))
#plt.imshow(masked_images[0], cmap='gray')

#draw lines using hough transform
rho = 3
theta = np.pi/270
threshold = 10
min_line_len = 1
max_line_gap = 20
lines = []
     
hough_images = []
for element in masked_images:
    #get the lines coordinades and the drawn image
    temp_img, img_lines = hough_lines(element, rho, theta, threshold, min_line_len, max_line_gap)
    hough_images.append(temp_img)
    lines.append(img_lines)
#plt.imshow(hough_images[0])

#draw the founded boundaries
weighted_images = []
for original, hough in zip(images_copy, hough_images):
    weighted_images.append(weighted_img(hough, original))
plt.imshow(weighted_images[0])<jupyter_output><empty_output><jupyter_text>Improve the draw_lines() function**At this point, you should have the Hough line segments drawn onto the road. Extend your code to define a line to run the full length of the visible lane based on the line segments you identified with the Hough Transform. Try to average and/or extrapolate the line segments you've detected to map out the full extent of the lane lines. The 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.**<jupyter_code># TODO: Build your pipeline that will draw complete lane lines on the test_images
import operator

coordinates_per_image = []
images_left = [] #left coordinates
images_right = [] #right coordinates
for i in range(len(lines)):
    images_right.append([])
    images_left.append([])

#separate left and right coordinates
index = 0
for element in lines:
    coordinates_per_image.append(element)
    for e in element:
        for x1,y1,x2,y2 in e:
            if x1 < 500:
                images_left[index].append((x1,y1))
            else:
                images_right[index].append((x1,y1))
            if x2 < 500:
                images_left[index].append((x2,y2))
            else:
                images_right[index].append((x2,y2))
    index += 1

#order the list
print(type(images_left[0]))
print(images_left[0][0])

#images_left.sort(key=itemgetter(1))
#images_right.sort(key = sortSecond)
images_left.sort(key = operator.itemgetter(0))
print(len(images_left[0]))
print(images_left[0])


"""
for index in range(len(images_left[0])):
    if index < len(images_left[0]):
        full_line = cv2.line(images_copy[0], (images_left[0][index][0],images_left[0][index][1]),
                                (images_left[0][index+1][0], images_left[0][index+1][1]),  color=[255, 0, 0], thickness=5)
        
"""<jupyter_output><class 'list'>
(293, 462)
60
[(165, 535), (284, 447), (181, 537), (396, 373), (298, 437), (391, 372), (393, 369), (432, 342), (369, 386), (389, 373), (282, 450), (287, 445), (166, 534), (204, 505), (250, 484), (255, 479), (294, 440), (300, 437), (283, 458), (305, 442), (256, 469), (271, 457), (343, 412), (352, 406), (276, 464), (292, 442), (401, 364), (412, 357), (357, 402), (365, 389), (307, 432), (310, 429), (326, 425), (330, 423), (334, 419), (390, 377), (289, 445), (293, 441), (262, 474), (279, 451), (290, 443), (296, 440), (392, 370), (400, 365), (263, 474), (267, 470), (161, 537), (177, 525), (259, 477), (277, 453), (211, 500), (235, 484), (190, 531), (237, 493), (289, 444), (293, 442), (335, 412), (362, 391), (279, 461), (326, 426)]
StarcoderdataJupyterStructuredCleanDedup