I Built My Own Self-Driving Car. Part #1

Learning to See: The Simplest Car Classifier

Max Larionov
2 min readAug 5, 2018

Difficult takes a day, impossible takes a week

— Kanye West

Introduction

I always had a dream of working on something huge and important to billions. It’s a known fact, that transportation plays a big role in our casual life.

American drivers spend just under an hour driving every day. [US Department of Transportation]

I believe that applying data science to transportation would help us to build a safe, fast, and reliable system. First of all, data will save us our most important asset: time. The way we think about transportation has not changed much for a couple of centuries. I believe, that there is a better way of organizing our daily transit. I believe we can build a system where emergency services get to their destinations in seconds and very little road accidents happen. I believe that data analysis is essential to the solution of such problems as saving energy, reducing noise and air pollution, minimizing road fatalities.

Let’s get it started

In this part of article I am going to build a very basic car detection classifier using Python and OpenCV. There is a variety of different object detection and classification techniques and I am going to pay particular attention to the use of Haar Cascades. However, the Haar Cascade classification itself would be covered very briefly in this particular article.

Let’s agree on using Python 3 and install opencv-python and numpy packages. We’ll also need dashcam footage to process. Here is a piece of code that is going to form a good baseline for the app:

Displaying a first frame of our video

This code opens a window with the first frame of our dashcam footage. Great job!

Haar Cascade Classifier

Training your own Haar Cascade classifier takes time, patience, and quite a bit of theoretical knowledge. Moreover, it needs a dataset of negative and positive samples in high resolution. Good thing is, due to the popularity of computer vision and autonomous vehicles, there are tons of pre-trained classifiers over the internet. Here is one that I am going to use. In fact, we are going to discuss Haar Cascades in detail once we will get to the optimization.

Here is a complete code sample:

A complete code sample

It will play our video in a new window drawing rectangles over detected cars.

OpenCV detects a car in front

To be continued…

In the next part, I will cover distance prediction and lane detection. I will also tell why it is a bad idea to use Haar Cascades and explain why I will be using convolutional neural networks instead.

--

--