DEV Community

Cover image for how to perform real-time vehicle detection in python
Jordan Kalebu
Jordan Kalebu

Posted on • Updated on

how to perform real-time vehicle detection in python

The original article can be found at kalebujordan.dev

Hi guys,

In this article, I will guide you on how to do real-time vehicle detection in python using the OpenCV library and trained cascade classifier in just a few lines of code.

a brief about vehicle detection

Real-time vehicle detection is one of the many application of object detection, whereby focuses on detecting cars within an image together with the location coordinates.

where is being used?

  • vehicle detection together with road detection is heavily applied on ** self-driving cars*, for a car to navigate safely along the road, it has to know where other cars are positioned so as it can avoid a collision.

  • Also vehicle detection is being used in traffic surveillance systems in such a way it can detect the traffic based on number vehicle and use that data to manage and control it

what will you build?

In this tutorial, we will learn how to perform Real-time vehicle detection in a video or from camera streams using OpenCV and a pre-trained cascade model.

Requirements

To able to follow through with this tutorial you’re supposed to have the following on your machine

installation

$ pip install opencv-python
Enter fullscreen mode Exit fullscreen mode

Pre-trained Cascade classifier

As I have explained earlier, we are not going to be training our model to spot cars in video frames from scratch instead we gonna use a pre-trained one.

These trained cascade classifiers are usually being stored in the XML format, therefore you should download the cascade that was trained to detect cars and have it in the project directory.

To download the trained cascade mode click-here

Demo video with cars in it

You can actually use any video you want as long it has cars in it, the cascade model will be able to detect them.

If you would like to use the same video I used for this article Download here;

Project Directory

Your project directory should look like this

.
├── app.py
├── cars.mp4
└── haarcascade_car.xml
Enter fullscreen mode Exit fullscreen mode

Let's get hands dirty

Now let's begin by building what we have just talked about, using you have the XML Model and demo video on your project directory.

Loading our Model

use cv2.CascadeClassifier() to load the trained haarcascade model as shown in the code below.

import cv2
cars_cascade = cv2.CascadeClassifier('haarcascade_car.xml')
Enter fullscreen mode Exit fullscreen mode

Detecting cars in a video

we will use the detectMultiScale () method to detect and to get the coordinates of vehicles in the video frames.

The detectMultiScale () method receives 3 parameters to actually give your coordinates as shown below

  • Grayscale image
  • scaleFactor
  • minNeighbors

Grayscale image specify the image to be processed, in our case a grayscale image is going to be image fetched from the video streams.

ScaleFactor specify how much the image size is reduced at each image scale, you can learn more about it here, a good value is mostly chosen as 1.05

minNeighbors specify how many neighbors each candidate rectangle should have to retain it, this parameter will affect the quality of the detected faces.

A higher value results in fewer detections but with higher quality usually, 3-6 is a good value for it

Syntax to detect cars + their positional coordinates

cars = cars_cascade.detectMultiScale(frame, scaleFactor, minNeighbors)
Enter fullscreen mode Exit fullscreen mode

When you run the above line of code it will perform cars detection in the frame image and then return to us all coordinates of cars found (diagonal coordinates point).

Drawing rectangle around detected cars

After detecting all the coordinates of all the cars in a frame, we to draw a rectangle around it for us to able to see the detection process visually.

We will use the cv2.rectangle() method to draw a rectangle around every detected car using diagonal coordinate points returned by our cascade classifier.

Syntax to use the cv2.rectangle () method


cv2.rectangle(frame , point1, point2, color = (), thickness=value)
Enter fullscreen mode Exit fullscreen mode

building a function to do all the detection process

We need to condense what we just learned and put into a single function that receives image frames and then draws rectangles around it using the detected coordinates just as shown below.

def detect_cars(frame):
    cars = cars_cascade.detectMultiScale(frame, 1.15, 4)
    for (x, y, w, h) in cars:
        cv2.rectangle(frame, (x, y), (x+w,y+h), color=(0, 255, 0), thickness=2)
    return frame
Enter fullscreen mode Exit fullscreen mode

Building a function to simulate the detection process

finally let's add a single function to simulate the whole process from loading the video, to perform vehicle detection by calling the detect_cars function, and then render a frame with detected vehicles on the screen.

def Simulator():
    CarVideo = cv2.VideoCapture('cars.mp4')
    while CarVideo.isOpened():
        ret, frame = CarVideo.read()
        controlkey = cv2.waitKey(1)
        if ret:        
            cars_frame = detect_cars(frame)
            cv2.imshow('frame', cars_frame)
        else:
            break
        if controlkey == ord('q'):
            break

    CarVideo.release()
    cv2.destroyAllWindows()
Enter fullscreen mode Exit fullscreen mode

Add these two lines so as we make sure that we are running our python code as a script.

if __name__ == '__main__':
    Simulator()
Enter fullscreen mode Exit fullscreen mode

Let's bundle everything together

Now we know how to do each independent piece of our detection script, it's time to put them together so as we can run it.

Once you put all the concept we learned above into one app.py, your code is going to look just as shown below

import cv2
cars_cascade = cv2.CascadeClassifier('haarcascade_car.xml')

def detect_cars(frame):
    cars = cars_cascade.detectMultiScale(frame, 1.15, 4)
    for (x, y, w, h) in cars:
        cv2.rectangle(frame, (x, y), (x+w,y+h), color=(0, 255, 0), thickness=2)
    return frame

  def Simulator():
    CarVideo = cv2.VideoCapture('cars.mp4')
    while CarVideo.isOpened():
        ret, frame = CarVideo.read()
        controlkey = cv2.waitKey(1)
        if ret:        
            cars_frame = detect_cars(frame)
            cv2.imshow('frame', cars_frame)
        else:
            break
        if controlkey == ord('q'):
            break

    CarVideo.release()
    cv2.destroyAllWindows()

if __name__ == '__main__':
    Simulator()
Enter fullscreen mode Exit fullscreen mode

We have reached the end of our article, hope you learned something, now share it with your fellow friends on Twitter and other developer communities

Follow me on Twiter

I recommend you to also check this;

GitHub logo Kalebu / Real-time-Vehicle-Dection-Python

A python project that does real-time vehicle detection using a trained car-cascade Model

Hi guys, This repository consist of a source code of script to detect cars in a video/camera frame and then draw rectangaluar boxes around them.

The ML algorithms used for detecting cars and bounding boxes coordinates is a pretrained cascade model Haarcascade car.

Become a patron

Where is the full article ?

The full article for this project is originally published on my blog with an article with title Real-time vehicle detection in python

Getting started

Firstly we have to clone the project repository or download the zip of project and then extract it.

git clone https://github.com/Kalebu/Real-time-Vehicle-Dection-Python
cd Real-time-Vehicle-Dection-Python
Real-time-Vehicle-Dection-Python ->
Enter fullscreen mode Exit fullscreen mode

Dependencies

Now once we have the project repo in our local directory, now lets install the dependecies required to run our script

pip install opencv-python
Enter fullscreen mode Exit fullscreen mode

Sample video

The sample video we used in this project is cars.mp4 which will come as you download or clone the repository, to load…




Top comments (0)