Real Time Object Detection using OpenCV and Python

One of the core components of a smart city is automated traffic management. And that got us thinking – could we build our own vehicle detection model that could play a part in smart traffic management?

Think about it – if you could integrate a vehicle detection system in a traffic light camera or a camera mounted on a building, you could easily track a number of useful things simultaneously:

  • How many vehicles are present at the traffic junction during the day?
  • What time does the traffic build up?
  • What kind of vehicles are traversing the junction (heavy vehicles, cars, etc.)?
  • Is there a way to optimize the traffic and distribute it through a different street?

And so on. The applications are endless!

The idea behind Object Detection

Object detection is a fascinating field in computer vision. It goes to a whole new level when we’re dealing with video data. The complexity rises up a notch, but so do the rewards!

There are a number of sub-tasks we can perform in object detection, such as counting the number of objects, finding the relative size of the objects, or finding the relative distance between the objects. All these sub-tasks are important as they contribute to solving some of the toughest real-world problems.

Problems

First we used Yolov3 to detect and track vehicules on the road. Problem is that we wanted real time object detection. Yolov3 is a very heavy program to detect vehicules with CPU. Even switching to GPU will not be optimal because it will scan every frame of the livestream/video to look for vehicules.

We then looked at Yolov3 that could run on a Raspberry Pi. It could detect objects using opencv and python. The Object Detection opencv method we've used here will use a sweet balance between speed and accuracy. Unlike many other object detection methods, we will be able to run this in real time with a good amount of accuracy. Sadly, this version didn't satisfy our needs either because we wanted to count the different cars on the video/livestream.

if you're interested in the code, you can view a tutorial and download it here!

We will focus on the unsupervised way of object detection in videos, i.e., object detection without using any labeled data. We will use the technique of frame differencing. A video is a set of frames stacked together in the right sequence. So, when we see an object moving in a video, it means that the object is at a different location at every consecutive frame.

Let's start with building our code!