how to perform real-time vehicle detection in python
Tags: python, machinelearning, tutorial, datascience
Published: 2020-11-07 09:42:59

>The [original article](https://kalebujordan.dev/real-time-vehicle-detection-using-python/) can be found at [kalebujordan.dev](https://kalebujordan.dev/)

Hi guys,

In this article, I will guide you on how to do **real-time vehicle detection** in python using the [OpenCV](https://opencv.org/) 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

- [OpenCV](https://opencv.org/)
- [Cars Cascade model](https://github.com/Kalebu/Real-time-Vehicle-Dection-Python/blob/master/haarcascade_car.xml)
- [Demo Video](https://github.com/Kalebu/Real-time-Vehicle-Dection-Python/blob/master/cars.mp4)

#### installation

```bash
$ pip install opencv-python
```

#### 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](https://github.com/Kalebu/Real-time-Vehicle-Dection-Python/blob/master/haarcascade_car.xml)**

#### 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](https://github.com/Kalebu/Real-time-Vehicle-Dection-Python/blob/master/cars.mp4);

#### Project Directory
Your project directory should look like this

```bash
.
├── app.py
├── cars.mp4
└── haarcascade_car.xml
```

### 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.

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

#### 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

```python
cars = cars_cascade.detectMultiScale(frame, scaleFactor, minNeighbors)
```

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 
​
```python
cv2.rectangle(frame , point1, point2, color = (), thickness=value)
```

### 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.

```python
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
```

#### 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.

```python
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()
```

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

```python
if __name__ == '__main__':
    Simulator()
```

####  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

```python
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()
```

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](https://twitter.com/j_kalebu)* 

#### I recommend you to also check this;

-   [How to convert picture to sound in Python](https://kalebujordan.dev/convert-an-image-text-sound/)
-   [Getting started with image processing using a pillow](https://kalebujordan.dev/image-processing-with-pillow-2/)
-   [How to detect Edges in a picture using OpenCV Canny algorithm]https://kalebujordan.dev/edge-detection-using-gaussianblur-algorithm-in-python/)
-   [How to perform text classification using TensorFlow in python](hhttps://kalebujordan.dev/text-classification-using-tensorflow/)
-   [How to perform sentiment analysis on Twitter data](https://kalebujordan.dev/analyze-tweets-sentiment-using-python/)


{% github https://github.com/Kalebu/Real-time-Vehicle-Dection-Python %}