In [1]:
#importing some useful packages
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
import cv2
%matplotlib inline
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.
"""
#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
"""
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
# 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, β, λ)
import os
dir_list = os.listdir("test_images/")
In [2]:
# Building a lane finding pipeline
# TODO: Build your pipeline that will draw lane lines on the test_images
# then save them to the test_images directory.
# ---------------------- CONSTANTS -------------------------------
# define gaussian filter kernel size to reduce image noise
kernel_size = 7
# Define our parameters for Canny
low_threshold = 50
high_threshold = 150
# Define the Hough transform parameters
rho = 1 # distance resolution in pixels of the Hough grid
theta = np.pi/180 # angular resolution in radians of the Hough grid
threshold = 30 # minimum number of votes (intersections in Hough grid cell)
min_line_length = 15 #minimum number of pixels making up a line
max_line_gap = 10 # maximum gap in pixels between connectable line segments
# ROI scalings in percent of imageg size
# bottom left, top left, top right, bottom right
bottom_left_roi_scale_x = 0.1241
bottom_left_roi_scale_y = 0.92
top_left_roi_scale_x = 0.40
top_left_roi_scale_y = 0.65
top_right_roi_scale_x = 0.6
top_right_roi_scale_y = 0.65
bottom_right_scale_x = 0.9075
bottom_right_scale_y = 0.92
# ---------------------- PROCESSING -------------------------------
# Loading images from directory
for dir in dir_list:
print("test_images/"+dir)
# Load from path
image = mpimg.imread("test_images/"+dir)
imshape = image.shape
#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')
gray = grayscale(image)
# Blur the image with the gaussian filter
# gray = cv2.GaussianBlur(gray,(kernel_size, kernel_size),0)
gray = gaussian_blur(gray, kernel_size)
# Calculate edges with canny algorithm
edges = canny(gray, low_threshold, high_threshold)
# Because the second video have another size but the same side ratio define ROI in % of image size
# This would not work if image size not has same side ratio
#vertices = np.array([[(100,imshape[0]),(450, 320), (550, 320), (900,imshape[0])]], dtype=np.int32)
# imshape[0] = ROWS = y
# imshape[1] = COLS = x
# np.array([[(x,y),(x,y), (x,y), (x,y), ... n-coordiantes ... ]], dtype=np.int32)
vertices = np.array([[(imshape[1]*bottom_left_roi_scale_x,imshape[0]*bottom_left_roi_scale_y),(imshape[1]*top_left_roi_scale_x, imshape[0]*top_left_roi_scale_y), (imshape[1]*top_right_roi_scale_x, imshape[0]*top_right_roi_scale_y), (imshape[1]*bottom_right_scale_x,imshape[0]*bottom_right_scale_y)]], dtype=np.int32)
masked_edge_image = region_of_interest(edges, vertices)
# hough_lines function does not return lines - so I call the cv2.HoughLinesP function directly to get lines g
lines = cv2.HoughLinesP(masked_edge_image, rho, theta, threshold, np.array([]), min_line_length, max_line_gap)
line_image = np.zeros((masked_edge_image.shape[0], masked_edge_image.shape[1], 3), dtype=np.uint8)
draw_lines(line_image, lines)
left_count = 0
right_count = 0
left_slope_avg = 0
right_slope_avg = 0
left_pos_avg = 0
right_pos_avg = 0
# Tried to define integer vetors but they change their type later on!?
left_pos_avg = np.zeros((4,) , dtype=np.int);
right_pos_avg = np.zeros((4,) , dtype=np.int);
# Sorting and averaging the slope of the calculated line segments
for line in lines:
for x1,y1,x2,y2 in line:
#print('x1:', x1, ' y1:', y1, ' x2:', x2, ' y2:', y2 )
slope = ((y2-y1)/(x2-x1))
#print('Slope: ', slope)
if slope < 0:
#print('right')
right_count = right_count + 1
right_slope_avg = right_slope_avg + slope
right_pos_avg[0] = right_pos_avg[0] + x1 + x2
right_pos_avg[1] = right_pos_avg[1] + y1 + y2
else:
#print('left')
left_count = left_count +1
left_slope_avg = left_slope_avg + slope
left_pos_avg[0] = left_pos_avg[0] + x1 + x2
left_pos_avg[1] = left_pos_avg[1] + y1 + y2
# Averaging slope and x,y position for one frame
left_slope_avg = left_slope_avg / left_count
right_slope_avg = right_slope_avg / right_count
left_pos_avg = left_pos_avg / (left_count*2)
right_pos_avg = right_pos_avg / (right_count*2)
# Using line equation to calculate new averaged lines
# y = kx + d
# Calculating intersection of line with y axis with calculated slope
# and the averaged position
# d = y - kx
d_left = left_pos_avg[1] - left_slope_avg*left_pos_avg[0]
d_right = right_pos_avg[1] - right_slope_avg*right_pos_avg[0]
# Calculating x coordinates at max y (number of rows) --> where to start at the bottom of the image
# x = (y-d)/k
left_pos_avg[3] = imshape[0]
right_pos_avg[3] = imshape[0]
left_pos_avg[2] = (left_pos_avg[3] - d_left)/left_slope_avg
right_pos_avg[2] = (right_pos_avg[3] - d_right)/(right_slope_avg)
# Calculating second point for line drawing - 330 height is choosen so that they do not intersect
# --> Where to end the line so that it looks neat
left_pos_avg[0] =(330 - d_left)/(left_slope_avg)
left_pos_avg[1] = 330
right_pos_avg[0] =(330 - d_right)/(right_slope_avg)
right_pos_avg[1] = 330
# Drawing debug image
cv2.line(line_image, (int(right_pos_avg[0]), int(right_pos_avg[1])), (int(right_pos_avg[2]), int(right_pos_avg[3])), [0, 255, 0], 4)
cv2.line(line_image, (int(left_pos_avg[0]), int(left_pos_avg[1])), (int(left_pos_avg[2]), int(left_pos_avg[3])), [0, 0, 255], 4)
# Drawing output image --> make a blank copy for overlay
image_overlay = np.copy(image)*0
cv2.line(image_overlay, (int(right_pos_avg[0]), int(right_pos_avg[1])), (int(right_pos_avg[2]), int(right_pos_avg[3])), [0, 255, 0], 16)
cv2.line(image_overlay, (int(left_pos_avg[0]), int(left_pos_avg[1])), (int(left_pos_avg[2]), int(left_pos_avg[3])), [0, 0, 255], 16)
overlayed_image = weighted_img(image_overlay, image, 1.0, 0.8)
# ---------------------- DEBUG -------------------------------
# Debug messages - are there preprocessor directives in python!?
show_debug_messages = False
if show_debug_messages == True:
print('---------------------------------------------')
print('d_left: ', d_left)
print('d_right: ', d_right)
print('right count', right_count)
print('left_count', left_count)
print('left slope average: ', left_slope_avg)
print('right slope average: ', right_slope_avg)
print('right pos average x1: ', right_pos_avg[0], ' y1:', right_pos_avg[1], 'x2: ', right_pos_avg[2], ' y2:', right_pos_avg[3])
print('left pos average x1: ', left_pos_avg[0], ' y1:', left_pos_avg[1], 'x2: ', left_pos_avg[2], ' y2:', left_pos_avg[3])
# Show gaussian blurred imaged
#plt.imshow(gray, cmap='gray')
# Show edged picture
#plt.imshow(masked_edge_image, cmap = 'gray')
# Show segmented lines
#plt.imshow(line_image, cmap = 'gray')
draw_roi = False
if draw_roi == True:
cv2.line(overlayed_image, (int(imshape[1]*bottom_left_roi_scale_x), int(imshape[0]*bottom_left_roi_scale_y)), (int(imshape[1]*top_left_roi_scale_x), int(imshape[0]*top_left_roi_scale_y)), [0, 255, 0], 1)
cv2.line(overlayed_image, (int(imshape[1]*top_left_roi_scale_x), int(imshape[0]*top_left_roi_scale_y)), (int(imshape[1]*top_right_roi_scale_x), int(imshape[0]*top_right_roi_scale_y)), [0, 255, 0], 1)
cv2.line(overlayed_image, (int(imshape[1]*top_right_roi_scale_x), int(imshape[0]*top_right_roi_scale_y)), (int(imshape[1]*bottom_right_scale_x), int(imshape[0]*bottom_right_scale_y)), [0, 255, 0], 1)
cv2.line(overlayed_image, (int(imshape[1]*bottom_right_scale_x), int(imshape[0]*bottom_right_scale_y)), (int(imshape[1]*bottom_left_roi_scale_x), int(imshape[0]*bottom_left_roi_scale_y)), [0, 255, 0], 1)
# Show segmented lines
plt.imshow(overlayed_image)
# Because imread swappes R and B
cv2.imwrite("test_images_output/"+dir, cv2.cvtColor(overlayed_image, cv2.COLOR_RGB2BGR))
In [3]:
# Import everything needed to edit/save/watch video clips
from moviepy.editor import VideoFileClip
from IPython.display import HTML
from math import *
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
Redefining my constants because even on my on edge 24" monitor it is a long way to scroll up to the top.
Defining some globals to be able to keep data throughout the frames.
In [23]:
# ---------------------- CONSTANTS AND GLOBALS -------------------------------
# define gaussian filter kernel size to reduce image noise
kernel_size = 7
# Define our parameters for Canny
low_threshold = 50
high_threshold = 150
# Define the Hough transform parameters
rho = 1 # distance resolution in pixels of the Hough grid
theta = np.pi/180 # angular resolution in radians of the Hough grid
threshold = 30 # minimum number of votes (intersections in Hough grid cell)
min_line_length = 15 #minimum number of pixels making up a line
max_line_gap = 10 # maximum gap in pixels between connectable line segments
# ROI scalings in percent of imageg size
# bottom left, top left, top right, bottom right
bottom_left_roi_scale_x = 0.1241
bottom_left_roi_scale_y = 0.92
top_left_roi_scale_x = 0.40
top_left_roi_scale_y = 0.65
top_right_roi_scale_x = 0.6
top_right_roi_scale_y = 0.65
bottom_right_scale_x = 0.9075
bottom_right_scale_y = 0.92
# Using this like a c struct
class Statistics:
count = 0.0
mean = 0.0
var = 0.0
M2 = 0.0
filtered_d_right = Statistics()
filtered_d_left = Statistics()
filtered_d_right.mean = 5.0
filtered_d_right.var = 9999.0
filtered_d_left.mean = 640.0
filtered_d_left.var = 9999.0
slope_right = Statistics()
# Using apriori knowledge to initialize slope of line
slope_right.mean = -0.7
slope_right.var = 0.002
slope_left = Statistics()
# Using apriori knowledge to initialize slope of line
slope_left.mean = 0.6
slope_left.var = 0.002
filtered_slope_right = Statistics()
filtered_slope_right.var = 9999.0
filtered_slope_right.mean = -0.7
filtered_slope_left = Statistics()
filtered_slope_left.var = 99999.0
filtered_slope_left.mean = 0.6
plot_slope_left_filtered=[]
plot_slope_left=[]
plot_slope_right_filtered=[]
plot_slope_right=[]
plot_d_right_filtered=[]
plot_d_right=[]
plot_d_left_filtered=[]
plot_d_left=[]
frame=[]
frame_counter = 0
# Tried to define integer vetors but they change their type later on!?
left_pos_avg = np.zeros((4,) , dtype=np.int);
right_pos_avg = np.zeros((4,) , dtype=np.int);
def update(mean1, var1, mean2, var2):
new_mean = float(var2 * mean1 + var1 * mean2) / (var1 + var2)
new_var = 1./(1./var1 + 1./var2)
return (new_mean, new_var)
def predict(mean1, var1, mean2, var2):
new_mean = mean1 + mean2
new_var = var1 + var2
return (new_mean, new_var)
def calc_mean_welford(x, mean, M2, n):
n = n + 1
delta = x - mean
mean += delta/n
delta2 = x - mean
M2 += delta*delta2
return (mean, M2, n)
def calc_var_welford(n, M2, var):
if n < 2: # not enough sample points to estimate variance
var = var # using last variance
else:
var = (M2 / (n -1)) # Estimating variance
#print('right_slope_mean: ', slope_right.mean, ' var: ', slope_right.var)
return (var)
def process_image(image):
global left_pos_avg
global right_pos_avg
global slope_left
global slope_right
global filtered_slope_left
global filtered_slope_right
global filtered_d_right
global filtered_d_left
global plot_slope_left_filtered
global plot_slope_left
global plot_slope_right_filtered
global plot_slope_right
global plot_d_right_filtered
global plot_d_right
global plot_d_left_filtered
global plot_d_left
global frame
global frame_counter
frame_counter = frame_counter + 1
frame.append(frame_counter)
gray = grayscale(image)
# Blur the image with the gaussian filter
# gray = cv2.GaussianBlur(gray,(kernel_size, kernel_size),0)
gray = gaussian_blur(gray, kernel_size)
# Define our parameters for Canny
edges = canny(gray, low_threshold, high_threshold)
# Because the second video have another size but the same side ratio define ROI in % of image size
# This would not work if image size not has same side ratio
imshape = edges.shape
vertices = np.array([[(imshape[1]*bottom_left_roi_scale_x,imshape[0]*bottom_left_roi_scale_y),(imshape[1]*top_left_roi_scale_x, imshape[0]*top_left_roi_scale_y), (imshape[1]*top_right_roi_scale_x, imshape[0]*top_right_roi_scale_y), (imshape[1]*bottom_right_scale_x,imshape[0]*bottom_right_scale_y)]], dtype=np.int32)
masked_edge_image = region_of_interest(edges, vertices)
# hough_lines function does not return lines - so I call the cv2.HoughLinesP function directly to get lines g
lines = cv2.HoughLinesP(masked_edge_image, rho, theta, threshold, np.array([]), min_line_length, max_line_gap)
# Tried to define integer vetors but they change their type later on!?
frame_left_pos_avg = np.zeros((4,) , dtype=np.int);
frame_right_pos_avg = np.zeros((4,) , dtype=np.int);
slope_left.count = 0
slope_right.count = 0
right_slope_avg = slope_right.mean
left_slope_avg = slope_left.mean
# Sorting and averaging the slope of the calculated line segments
for line in lines:
for x1,y1,x2,y2 in line:
#print('x1:', x1, ' y1:', y1, ' x2:', x2, ' y2:', y2 )
slope = ((y2-y1)/(x2-x1))
#print('Slope: ', slope)
if (slope < 0.75 and slope > 0.55) or (slope < -0.55 and slope > -0.75):
if slope > 0:
#print('right')
frame_right_pos_avg[0] = frame_right_pos_avg[0] + x1 + x2
frame_right_pos_avg[1] = frame_right_pos_avg[1] + y1 + y2
slope_right.mean, slope_right.M2, slope_right.count = calc_mean_welford(slope, slope_right.mean, slope_right.M2, slope_right.count)
else:
#print('left')
frame_left_pos_avg[0] = frame_left_pos_avg[0] + x1 + x2
frame_left_pos_avg[1] = frame_left_pos_avg[1] + y1 + y2
slope_left.mean, slope_left.M2, slope_left.count = calc_mean_welford(slope, slope_left.mean, slope_left.M2, slope_left.count)
else:
print('Outlier slope... skipping')
#print('--------------------------------------------')
if slope_left.count > 0 and slope_right.count > 0:
left_pos_avg = frame_left_pos_avg / (slope_left.count*2)
right_pos_avg = frame_right_pos_avg / (slope_right.count*2)
slope_right.var = calc_var_welford(slope_right.count, slope_right.M2, slope_right.var)
#print('right_slope_mean: ', slope_right.mean, ' var: ', slope_right.var)
slope_left.var = calc_var_welford(slope_left.count, slope_left.M2, slope_left.var)
#print('left_slope_mean: ', slope_left.mean, ' var: ', slope_left.var)
right_slope_avg = slope_right.mean
left_slope_avg = slope_left.mean
else:
print('No detections on right or left line - using previous results')
#########################################################################
# Filtering calculated slopes
# ---------------------------- Right slope
plot_slope_right.append(slope_right.mean)
filtered_slope_right.mean, filtered_slope_right.var = update(filtered_slope_right.mean,filtered_slope_right.var,slope_right.mean,0.1)#slope_right.var)
filtered_slope_right.mean, filtered_slope_right.var = predict(filtered_slope_right.mean,filtered_slope_right.var,0.0,0.0025)#0.000001)
plot_slope_right_filtered.append(filtered_slope_right.mean)
right_slope_avg = filtered_slope_right.mean
# ---------------------------- left slope
plot_slope_left.append(slope_left.mean)
filtered_slope_left.mean, filtered_slope_left.var = update(filtered_slope_left.mean,filtered_slope_left.var,slope_left.mean,0.1)#slope_left.var)
filtered_slope_left.mean, filtered_slope_left.var = predict(filtered_slope_left.mean,filtered_slope_left.var,0.0,0.0025)#0.000001)
left_slope_avg = filtered_slope_left.mean
plot_slope_left_filtered.append(filtered_slope_left.mean)
#########################################################################
# Using line equation to calculate new lines from averaged slopes
# y = kx + d
# Calculating d translation on y axis of a line with the calculated slope running through the averages x,y coordinates
# d = y - kx
d_left = left_pos_avg[1] - left_slope_avg*left_pos_avg[0]
d_right = right_pos_avg[1] - right_slope_avg*right_pos_avg[0]
# ---------------------------- Right d
plot_d_right.append(d_right)
filtered_d_right.mean, filtered_d_right.var = update(filtered_d_right.mean,filtered_d_right.var,d_right,10.0)
filtered_d_right.mean, filtered_d_right.var = predict(filtered_d_right.mean,filtered_d_right.var,0.0,1.5)
plot_d_right_filtered.append(filtered_d_right.mean)
# ---------------------------- Left d
plot_d_left.append(d_left)
filtered_d_left.mean, filtered_d_left.var = update(filtered_d_left.mean,filtered_d_left.var,d_left,10.0)
filtered_d_left.mean, filtered_d_left.var = predict(filtered_d_left.mean,filtered_d_left.var,0.0,1.5)
plot_d_left_filtered.append(filtered_d_left.mean)
d_right = filtered_d_right.mean
d_left = filtered_d_left.mean
# Calculating x coordinates at max y (number of rows) --> where to start at the bottom of the image
# x = (y-d)/k
left_pos_avg[3] = imshape[0]
right_pos_avg[3] = imshape[0]
left_pos_avg[2] = (left_pos_avg[3] - d_left)/left_slope_avg
right_pos_avg[2] = (right_pos_avg[3] - d_right)/(right_slope_avg)
# Calculating second point for line drawing - 330 height is choosen so that they do not intersect
# --> Where to end the line so that it looks neat
corp_height = imshape[0] * 0.61
left_pos_avg[0] =(corp_height - d_left)/(left_slope_avg)
left_pos_avg[1] = corp_height
right_pos_avg[0] =(corp_height - d_right)/(right_slope_avg)
right_pos_avg[1] = corp_height
# Drawing output image --> make a blank copy for overlay
image_overlay = np.copy(image)*0
# Drawing calculated lines on overlay image
cv2.line(image_overlay, (int(right_pos_avg[0]), int(right_pos_avg[1])), (int(right_pos_avg[2]), int(right_pos_avg[3])), [0, 255, 0], 16)
cv2.line(image_overlay, (int(left_pos_avg[0]), int(left_pos_avg[1])), (int(left_pos_avg[2]), int(left_pos_avg[3])), [0, 0, 255], 16)
# Generating overlayed output image
result = weighted_img(image_overlay, image, 1.0, 0.8)
# ---------------------- DEBUG -------------------------------
# Debug messages - are there preprocessor directives in python!?
show_debug_messages = False
if show_debug_messages == True:
print('---------------------------------------------')
print('d_left: ', d_left)
print('d_right: ', d_right)
print('right count', right_count)
print('left_count', left_count)
print('left slope average: ', left_slope_avg)
print('right slope average: ', right_slope_avg)
print('right pos average x1: ', right_pos_avg[0], ' y1:', right_pos_avg[1], 'x2: ', right_pos_avg[2], ' y2:', right_pos_avg[3])
print('left pos average x1: ', left_pos_avg[0], ' y1:', left_pos_avg[1], 'x2: ', left_pos_avg[2], ' y2:', left_pos_avg[3])
show_hough_line_image = False
if show_hough_line_image == True:
line_image = np.zeros((image.shape[0], image.shape[1], 3), dtype=np.uint8)
cv2.line(line_image, (int(imshape[1]*bottom_left_roi_scale_x), int(imshape[0]*bottom_left_roi_scale_y)), (int(imshape[1]*top_left_roi_scale_x), int(imshape[0]*top_left_roi_scale_y)), [0, 255, 0], 1)
cv2.line(line_image, (int(imshape[1]*top_left_roi_scale_x), int(imshape[0]*top_left_roi_scale_y)), (int(imshape[1]*top_right_roi_scale_x), int(imshape[0]*top_right_roi_scale_y)), [0, 255, 0], 1)
cv2.line(line_image, (int(imshape[1]*top_right_roi_scale_x), int(imshape[0]*top_right_roi_scale_y)), (int(imshape[1]*bottom_right_scale_x), int(imshape[0]*bottom_right_scale_y)), [0, 255, 0], 1)
cv2.line(line_image, (int(imshape[1]*bottom_right_scale_x), int(imshape[0]*bottom_right_scale_y)), (int(imshape[1]*bottom_left_roi_scale_x), int(imshape[0]*bottom_left_roi_scale_y)), [0, 255, 0], 1)
draw_lines(line_image, lines)
result = weighted_img(line_image, image, 1.0, 0.8)
return result
In [24]:
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,1)
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)
In [25]:
red_patch = mpatches.Patch(color='red', label='d right')
green_patch = mpatches.Patch(color='green', label='d right filtered')
plt.legend(handles=[red_patch, green_patch])
plt.ylabel('Pixel')
plt.xlabel('Frame')
plt.plot(frame, plot_d_right, 'r--', frame, plot_d_right_filtered, 'g--')
plt.show()
In [26]:
red_patch = mpatches.Patch(color='red', label='d left')
green_patch = mpatches.Patch(color='green', label='d left filtered')
plt.legend(handles=[red_patch, green_patch])
plt.ylabel('Pixel')
plt.xlabel('Frame')
plt.plot(frame, plot_d_left, 'r--', frame, plot_d_left_filtered, 'g--')
plt.show()
In [27]:
red_patch = mpatches.Patch(color='red', label='slope left')
green_patch = mpatches.Patch(color='green', label='slope left filtered')
plt.legend(handles=[red_patch, green_patch])
plt.ylabel('Pixel')
plt.xlabel('Frame')
plt.plot(frame, plot_slope_left, 'r--', frame, plot_slope_left_filtered, 'g--')
plt.show()
In [28]:
red_patch = mpatches.Patch(color='red', label='slope right')
green_patch = mpatches.Patch(color='green', label='slope right filtered')
plt.legend(handles=[red_patch, green_patch])
plt.ylabel('Pixel')
plt.xlabel('Frame')
plt.plot(frame, plot_slope_right, 'r--', frame, plot_slope_right_filtered, 'g--')
plt.show()
In [29]:
HTML("""
<video width="960" height="540" controls>
<source src="{0}">
</video>
""".format(white_output))
Out[29]:
In [30]:
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,3)
clip2 = VideoFileClip('test_videos/solidYellowLeft.mp4')
yellow_clip = clip2.fl_image(process_image)
%time yellow_clip.write_videofile(yellow_output, audio=False)
In [31]:
red_patch = mpatches.Patch(color='red', label='d right')
green_patch = mpatches.Patch(color='green', label='d right filtered')
plt.legend(handles=[red_patch, green_patch])
plt.ylabel('Pixel')
plt.xlabel('Frame')
plt.plot(frame, plot_d_right, 'r--', frame, plot_d_right_filtered, 'g--')
plt.show()
In [32]:
red_patch = mpatches.Patch(color='red', label='d left')
green_patch = mpatches.Patch(color='green', label='d left filtered')
plt.legend(handles=[red_patch, green_patch])
plt.ylabel('Pixel')
plt.xlabel('Frame')
plt.plot(frame, plot_d_left, 'r--', frame, plot_d_left_filtered, 'g--')
plt.show()
In [33]:
red_patch = mpatches.Patch(color='red', label='slope left')
green_patch = mpatches.Patch(color='green', label='slope left filtered')
plt.legend(handles=[red_patch, green_patch])
plt.ylabel('Pixel')
plt.xlabel('Frame')
plt.plot(frame, plot_slope_left, 'r--', frame, plot_slope_left_filtered, 'g--')
plt.show()
In [34]:
red_patch = mpatches.Patch(color='red', label='slope right')
green_patch = mpatches.Patch(color='green', label='slope right filtered')
plt.legend(handles=[red_patch, green_patch])
plt.ylabel('Pixel')
plt.xlabel('Frame')
plt.plot(frame, plot_slope_right, 'r--', frame, plot_slope_right_filtered, 'g--')
plt.show()
In [35]:
HTML("""
<video width="960" height="540" controls>
<source src="{0}">
</video>
""".format(yellow_output))
Out[35]:
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.
In [36]:
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,2)
clip3 = VideoFileClip('test_videos/challenge.mp4')
print('Height: ' , clip3.h, ' Width: ', clip3.w)
challenge_clip = clip3.fl_image(process_image)
%time challenge_clip.write_videofile(challenge_output, audio=False)
In [37]:
red_patch = mpatches.Patch(color='red', label='d right')
green_patch = mpatches.Patch(color='green', label='d right filtered')
plt.legend(handles=[red_patch, green_patch])
plt.ylabel('Pixel')
plt.xlabel('Frame')
plt.plot(frame, plot_d_right, 'r--', frame, plot_d_right_filtered, 'g--')
plt.show()
In [38]:
red_patch = mpatches.Patch(color='red', label='d left')
green_patch = mpatches.Patch(color='green', label='d left filtered')
plt.legend(handles=[red_patch, green_patch])
plt.ylabel('Pixel')
plt.xlabel('Frame')
plt.plot(frame, plot_d_left, 'r--', frame, plot_d_left_filtered, 'g--')
plt.show()
In [39]:
red_patch = mpatches.Patch(color='red', label='slope left')
green_patch = mpatches.Patch(color='green', label='slope left filtered')
plt.legend(handles=[red_patch, green_patch])
plt.ylabel('Pixel')
plt.xlabel('Frame')
plt.plot(frame, plot_slope_left, 'r--', frame, plot_slope_left_filtered, 'g--')
plt.show()
In [40]:
red_patch = mpatches.Patch(color='red', label='slope right')
green_patch = mpatches.Patch(color='green', label='slope right filtered')
plt.legend(handles=[red_patch, green_patch])
plt.ylabel('Pixel')
plt.xlabel('Frame')
plt.plot(frame, plot_slope_right, 'r--', frame, plot_slope_right_filtered, 'g--')
plt.show()
In [41]:
HTML("""
<video width="960" height="540" controls>
<source src="{0}">
</video>
""".format(challenge_output))
Out[41]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]: