Dashcam Lane Detection

handling video

Posted on September 25, 2017

Following my first and second attempts at lane detection with computer vision in Python, this third post will look at applying lane detection to dashcam video.

In my case, I’ve pulled a variety of footage from the DOD 460W dashcam in my 2007 Honda Fit. I tried to include highway and city routes, during both day and night.

Improved Codebase

I am applying the approach by Naoki Shibuya, which uses moviepy for splitting a video file into sequential images, and then applying the lane detection algorithm. This draws red markers over detected lanes in dashcam footage.


class LaneDetector:
    def __init__(self):
        self.left_lines  = deque(maxlen=QUEUE_LENGTH)
        self.right_lines = deque(maxlen=QUEUE_LENGTH)

    def mean_line(self, line, lines):
        if line is not None:
        if len(lines)>0:
            line = np.mean(lines, axis=0, dtype=np.int32)
            line = tuple(map(tuple, line))
        return line

    def process(self, image):
            white_yellow = select_white_yellow(image)
            gray         = convert_gray_scale(white_yellow)
            smooth_gray  = apply_smoothing(gray)
            edges        = detect_edges(smooth_gray)
            regions      = select_region(edges)
            lines        = hough_lines(regions)
            left_line, right_line = lane_lines(image, lines)
            left_line  = self.mean_line(left_line,  self.left_lines)
            right_line = self.mean_line(right_line, self.right_lines)
            return draw_lane_lines(image, (left_line, right_line))
            return image

def process_video(dirpath, video_file):
    video_outfile = os.path.splitext(video_file)[0] + '.mp4'
    detector = LaneDetector()
    clip = VideoFileClip(os.path.join(dirpath, video_file))
    processed = clip.fl_image(detector.process)
    print(os.path.join('output', video_file))
    processed.write_videofile(os.path.join('output', video_outfile), audio=False)

The Canny Edge Detection algorithm worked well once I cropped out the yellow text my dashcam overlays on the footage from the area of interest. However, it’s not perfect as you see in this sample clip where:

  • I change lanes and the right line detector goes wonky.
  • I pass a turn off and the left lane marker vanishes.
  • I pass a police officer writing a ticket to a fool in a jacked-up pickup truck.


Overall, this algorithm does a pretty good job of detecting lanes in dashcam footage, however taking 30 minutes or more to process 3 minutes of dashcam footage isn’t a reasonable approach. I would not want to drive in that vehicle!

Next Steps

There are two directions I’d like to follow up on:

  • Bundling up the current lane detection algortihm as a Docker image to scale it to process more video.
  • Trying out a machine learning approach, potentially offering better detection than the static behavior of the Canny algorithm.

More in this series…