Counting vehicle traffic using COMPUTER VISION4

Sometimes we have tasks for calculating the client flow. We can count queues, filling up public places, etc.

Imagine that we are given the task of calculating the flow of cars in a certain place at different times. The only thing that comes to mind is that a person will actually have to manually make an approximate calculation for certain indicators.

Let’s try to automate this task, since at the moment we have a huge amount of tools and computing power.

First, let’s decide on the source of the videos. For example, you can take the portal… On this portal, various cameras are shared with others, which have a high-quality image and a good location (the road and cars are clearly visible)

As an example of a camera, take

This camera is perfect for an example, then we will try to complicate the task.

To get frames from the camera, we need to connect to the stream of the camera itself. Go to the source code and find a link to the video stream from the current camera.

Having this link, we can receive frames from this stream using Python and OpenCV.

import cv2
import time

video_stream_widget = cv2.VideoCapture('')
video_stream_widget.set(cv2.CAP_PROP_FPS, 5)
success, frame =
prev = 0

while success:
    time_elapsed = time.time() - prev
    success, frame =

    if time_elapsed > 1. / 5:
        prev = time.time()

    cv2.imshow('Weacom', cv2.resize(frame, (1280, 1080)))
    key = cv2.waitKey(1)
    if key == ord('s'):

Since our stream goes faster than we have time to read frames, we forcibly slow down the stream to the value we need, about once a second.

Now that we have the frames, our task is to apply the algorithm for tracking cars. To do this, take a bunch of Yolo + Deepsort.

As a ready-made implementation, we will use –… This repository as a whole already contains everything that we need, it remains only to transfer it to ourselves and modify it for the task.

First, let’s clone the repository:

>> git clone --recurse-submodules

And install all the necessary libraries:

>> pip install -r requirements.txt

Since yolo is trained on the MS Coco dataset, we need to leave in recognition only those classes that we need, namely bus, car, truck. Let’s change the configuration to classes 4 6 8.

Let’s run the code out of the box and see the result of the work. For fun, I tried to run it on another random camera:

python –source –classes 2 5 7 –show-vid

In general, we see that the algorithm is working, tracking is on. After observing the algorithm for a while, I closed the window.

Let’s remember our task – we need to calculate traffic for a certain period of time.

Visually, the algorithm seems to be doing this already – but in fact, the accuracy of the algorithm suffers greatly – the set id already clearly exceeds the number of visually visible cars.

In this case, we just need to add counters for each new ID.

To do this, let’s make changes to

Add all the unique IDs of the identified cars to the list:

for j, (output, conf) in enumerate(zip(outputs, confs)): 

    bboxes = output[0:4]
    id = output[4]
    cls = output[5]

And at the end, we just remove duplicates and display the length of the list.


Let’s run the algorithm again – let’s look at the results – it looks better.

In general, this algorithm can be left for testing.

In the next articles, we will look at multithreaded monitoring of streams from different cameras.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *