In this article, we will see how to use CCTV camera footage to calculate the vehicle speed detection of each individual vehicle passing on the road.

I will divide the tutorial into 4 parts but I will focus in particular on the part to calculate the speed of the vehicles:

1) Detect and track vehicles
2) Select the area where the vehicle is starting
3) Estimate the time for speed detection
4) Calculate average kilometer per hour

## 1) Detect and track vehicles

We have already talked about this topic in a previous article How Artificial Intelligence counts people and vehicles from CCTV cameras and again for vehicles and speed detection, it is important to recover the video stream from a CCTV camera. In my case, I will use a pre-recorded video, always from a CCTV camera, but in this way, I can explain the concept better, in the end, it works the same way.

In the image above you can see the example of what we are using but the traffic is present only in a well-defined area of the video, for this reason, it is better to define a Region Of Interest.

I talked about this concept also on How crop images with OpenCV and Python and if you want you can also read OpenCV ROI. In this case for convenience, we can use GIMP to identify the location of the points and insert them into our project. Here’s what the portion of the video analyzed by the code will look like.

Now you must first identify the vehicles and then assign an ID to each individual vehicle. I recently talked in this video about ” Object tracking from scratch – OpenCV, and python ” the basics of object tracking. If, on the other hand, you need a commercial use, I recommend my course: Object Detection (Opencv & Deep learning) course.

Simply by starting some code prepared previously with Yolov4 and Deep SORT this is the result.

### Speed detection and frame processing speed

As you can see in the video, if you don’t use a high-performance video card, there could be problems with processing speed. To get around the problem by skipping a few frames, the Speed detection will still be calculated with good precision. Thanks to object tracking, however, the speed of vehicles can be calculated correctly.

By adding three lines of code inside the loop that reads the frames, some of them can be ignored.

```    count += 1
if count % 3 != 0:
continue```

## 2) Select the area where the vehicle is starting

To perform speed detection correctly it is necessary to identify areas where the machines start their motion and where it ends. In addition to tracing, we have to draw polygons with OpenCV where we can see the passage of the machines.

At the code level, it is enough to indicate 4 points for each area, one way to do it is this

```area_1 = [(148, 494), (26, 600), (300, 718), (474, 626)]
area_2 = [(650, 575), (862, 549), (883, 392), (725, 375)]
area_3 = [(648, 343), (596, 213), (437, 218), (324, 352)]

for area in [area_1, area_2, area_3]
cv2.polylines(frame, [np.array(area, np.int32)], True, (15,220,10), 6)
```

This is the result

## 3) Estimate the time for speed detection

Now we need to figure out when the car leaves the first Region Of Interest and enters the second. We have already talked about the topic in this article “How Artificial Intelligence counts people and vehicles from CCTV cameras” as well.

To understand if the vehicle has left area number one and is subsequently in area number 3, a check is made of the conditions of all vehicles previously passed in area number 1.

```        if object_id in vehicles_entering:
result = cv2.pointPolygonTest(np.array(area_3, np.int32), (int(cx), int(cy)), False)

if result >= 0:
elapsed_time = time.time() - vehicles_entering[object_id]

if object_id not in vehicles_elapsed_time:
vehicles_elapsed_time[object_id] = elapsed_time

if object_id in vehicles_elapsed_time:
elapsed_time = vehicles_elapsed_time[object_id]```

In the elapsed_time variable, we have the time difference in the shift between the first and the next Region Of Interest. The time obtained associated with the ID allows us to assign each vehicle its time.

## 4) Calculate average kilometer per hour

We have everything we need to calculate speed. With the time and space variable, a small mathematical calculation is enough to obtain the required result and therefore the speed detection for each vehicle.

```                # Calc average Speed
distance = 22 # meters
a_speed_ms = distance / elapsed_time
a_speed_kh = a_speed_ms * 3.6```

Using the usual basic OpenCV functions to apply text and draw polygons, we can make the result perfectly visible even in the video. Here is an example of 2 cars whose speed detection was calculated from area1 to area3.

## Do you want to build your own custom project?

Speed detection is only the last part of a process that includes Object detection and Object tracking that allows us to develop our software and make the most of our CCTV cameras without having to purchase sophisticated sensors dedicated to the purpose.

I also covered this topic in my Object Detection (Opencv & Deep learning) course including the simplified use of Deep SORT for vehicles tracking including ready-to-download codes.