Counting Cars For Traffic Management

Learn how to count cars from CCTV camera footage.

Hey there,

Welcome to the first episode of the AI For BI Rocks! Newsletter.

Each week I'll share a practical example of how AI can be used to improve analytics.

In today's issue you'll learn how to count cars from CCTV camera footage.

Let's start!

Problem Statement

Suppose we work for a road and traffic authority and our goal is to improve the overall traffic flow to avoid congestion on the roads.

An important factor is the control of speed limits on highways. To set speed limits, we need detailed information about the traffic flow on the roads.

As a data analyst, we want to build an MVP to find out if it's possible to count traffic with the existing infrastructure of road cameras (CCTV).

For testing purposes, we were provided with some sample video footage. Here's what it looks like:

Solution Overview

To quickly build the MVP and test feasibility, we use an existing computer vision AI service to recognize objects in images (see Resources at the end for a list of such services).

The overall architecture of this use case looks as follows:

The solution architecture includes the following steps:

  1. Deploy a ready-made AI service on Microsoft Azure - in this case, Cognitive Services for Computer Vision (analysis layer).

  2. Prepare a small Python ETL script that loads the data from a web URL, applies the AI service and converts the JSON output to a flat CSV table (data layer).

  3. Upload the CSV file to a location from where it can be easily retrieved and visualized using our BI tool, in this case Power BI (user layer).

Walk-Through

We'll walk through these layers step by step.

Analysis Layer

Set up and configure a Computer Vision AI service - such as Microsoft Azure Cognitive Services in this example (alternatives in the resources below).

As it turns out, there are many use cases for computer vision that run under the same service.

Not only can you use the image processing service to recognize objects, but you can also extract text, recognize brand names and famous landmarks, tag images, and more.

We'll cover some of these in another issue.

Data Layer

To load and prepare our data, we can run a small Python script that performs the following steps (download code from the resources at the end).

The script will:

  • Authenticate to our Azure account

  • Import a TXT file and download the image URLs

  • Call the Computer Vision AI service as a synchronous operation (one API call per image). For Azure Cognitive Services, 20 images per minute is the limit for the free tier

Restrictions: Check the GDPR/data protection compliance of your data and your region. It's generally advisable to make sure you remove ANY PII from images, such as license plates or faces, before sending them to an AI service. For a small test dataset, you can either do this manually or use a specialized service for this if the dataset gets too large.

Also note the following restrictions from the Azure documentation so you can avoid or mitigate the effects of false negatives (missed objects):

  • Objects are generally not detected if they’re small (less than 5% of the image).

  • Objects are generally not detected if they’re arranged closely together (a stack of plates, for example).

The first point is most important to us:

To make sure that the objects of interest are large enough compared to the overall size of the image, we crop the images on the fly using a Content Delivery Network (CDN) - check the image URLs!

This way, we can focus on the parts of the image that really matter to us and ensure that a potential car object takes up more than 5% of the image area.

So, if you get poor results with an object detection AI service, try cropping or splitting the image into parts.

Now the output of the AI service should look like this:

As you can see, the AI service does not only provide the names of the recognized objects, but also a bit more context (e.g., the position of the object in the image).

All that's missing now is the post-processing to count the number of images.

The approach here's quite simple. We call a function that filters the output of the result according to two criteria:

  • Object property name ('car')

  • Confidence score of the detection

I chose a confidence score of 0.7, which translates to something like "The AI service is 70% sure that the detected object is a car."

You can experiment with the value and figure out where the sweet spot is for your use case.

The only task left is to get this into a nice table format:

As a final step in the data layer, we upload this table as a flat CSV file to a blob storage so that our BI software can access it.

User Layer

Let’s visualize our findings with Power BI (download PBIX files in the resources below).

We’ll apply the following steps:

  • Load data from Azure blob storage into Power BI

  • Check data formats for columns: index (numeric), count (numeric), and URL (string)

  • Power BI tip: Set the URL column to Image URL format.

Why should we do that? Wait a second…

In the Power BI report editor, we create two visuals:

  • A line graph that shows the number of counts over the index (can also be over time).

  • A table that contains the index, count and the URL field.

When you click on a specific data point, the data table will display the original image of the URL (thanks to the formatting!) so you can easily review your results:When you click on a specific data point, the data table will display the original image of the URL (thanks to the formatting!) so you can easily review your results:

With this, we built a quick prototype for traffic monitoring dashboards in probably less than a day.

Of course, this is still an MVP and we are far from moving to production!

But with this MVP, we can validate the following points:

  • How well does the AI service work?

  • Does another AI service work even better?

  • How valuable would this dashboard be to our users?

Check out the resources below to create this dashboard yourself!

How did you like this use case?

Drop me a reply and let me know!

And feel free to forward this newsletter to your colleagues!

I look forward to seeing you next week!

Resources

Computer Vision AI Services

Data preparation code

Power BI dashboard

AI-Powered Business Intelligence Book Cover

This use case was taken from my book AI-Powered Business Intelligence (O’Reilly) which you can read in detail here: https://www.aipoweredbi.com

Reply

or to participate.