Count vehicles on images from directory

In this tutorial, we will talk about object counting and precisely vehicle counting. We will see how to count the cars in the photos present in a specific folder.

The photo is an example of what we will base our code (these images can be found in the download folder).

count vehicole dir

How do we go on?

We basically need to carry out an object detection project and these are the steps:

  1. Identify the cars on image
  2. Loop and counting on a images in a folder

1. Identify the cars on image

Before writing the code for the Count Vehicles we need to find the cars that are present in the single photo. In this project I used the YOLO v4 deep learning model, you can decide whether to download the files I have made available to you or do your training by choosing the most suitable parameters for your project (in this case I recommend you also see Train YOLO to detect a custom object (online with free GPU)).

Basic installations

We are working with Python so this is the prerequisite. Then we install OpenCV, if you have already done so, ignore this step.

pip install opencv-python

Identify the machines on the single photo

All the functions necessary for this project are already present in the vehicle_counting.py file, we just have to include it in the main project and use it.

import cv2
from vehicle_detector import VehicleDetector

# Load Veichle Detector
vd = VehicleDetector()

Let’s try it on an image

img = cv2.imread(images/pexels-charles-kettor-1077785.jpg)

vehicle_boxes = vd.detect_vehicles(img)


for box in vehicle_boxes:
        x, y, w, h = box

        cv2.rectangle(img, (x, y), (x + w, y + h), (25, 0, 180), 3)

cv2.imshow("Cars", img)
cv2.waitKey(0)

If we start the script we will see the first result. If there are any doubts about these steps, I invite you to see my tutorials on OpenCV to understand the procedure in detail.

Yolo v4 cars

Look at the photo above, it seems to work perfectly. As you can see, it also recognizes only car parts and the tiny car in the distance.

Identify the machines on the single photo

All the fThe vehicle_boxes variable is an array that contains all the red boxes with the coordinates. To know how many cars there are in the photo we just have to count the boxes in the array, which is simple to do.

vehicle_count = len(vehicle_boxes)

2. Loop and counting on a images in a folder

To count all the cars in the folder images, we need to create a loop that allows us to analyze image by image automatically. By using the Python Glob library and integrating it into the code we will achieve our goal.

import glob

...


# Load images from a folder
images_folder = glob.glob("images/*.jpg")

vehicles_folder_count = 0

We can rewrite the second part of the Count Vehicles like this:

# Loop through all the images
for img_path in images_folder:
    print("Img path", img_path)
    img = cv2.imread(img_path)

    vehicle_boxes = vd.detect_vehicles(img)
    vehicle_count = len(vehicle_boxes)

    # Update total count
    vehicles_folder_count += vehicle_count

    for box in vehicle_boxes:
        x, y, w, h = box

        cv2.rectangle(img, (x, y), (x + w, y + h), (25, 0, 180), 3)

        cv2.putText(img, "Vehicles: " + str(vehicle_count), (20, 50), 0, 2, (100, 200, 0), 3)

    cv2.imshow("Cars", img)
    cv2.waitKey(1)

print("Total current count", vehicles_folder_count)

here are 3 examples