YOLO object detection using Opencv with Python

Shubham Koli
5 min readMay 31, 2022

--

We’re going to learn in this blog YOLO object detection. Yolo is a deep learning algorythm which came out on may 2016 and it became quickly so popular because it’s so fast compared with the previous deep learning algorythm.

What is YOLO?

YOLO is an abbreviation for the term ‘You Only Look Once’. This is an algorithm that detects and recognizes various objects in a picture (in real-time). Object detection in YOLO is done as a regression problem and provides the class probabilities of the detected images.

YOLO algorithm employs convolutional neural networks (CNN) to detect objects in real-time. As the name suggests, the algorithm requires only a single forward propagation through a neural network to detect objects.

This means that prediction in the entire image is done in a single algorithm run. The CNN is used to predict various class probabilities and bounding boxes simultaneously.

The YOLO algorithm consists of various variants. Some of the common ones include tiny YOLO and YOLOv3.

How to install YOLO?

Let’s clear up a few things. YOLO is a deep learning algorythm, so itself doesn’t need any installation, what we need instead is a deep learning framework where to run te algorythm.

Here I’m going to describe the 3 most used and known frameworks compatible with YOLO and the advantages and disadvantages of each one:

  • Darknet : it’s the framework built from the developer of YOLO and made specifically for yolo.
    Advantage: it’s fast, it can work with GPU or CPU
    Disadvantage: it olny works with Linux os
  • Darkflow: it’s the adaptation of darknet to Tensorflow (another deep leanring framework).
    Advantage: it’s fast, it can work with GPU or CPU, and it’s also compatible with Linux, Windows and Mac.
    Disadvantage: the installation it’s really complex, especially on windows
  • Opencv: also opencv has a deep learning framework that works with YOLO. Just make sure you have opencv 3.4.2 at least.
    Advantage: it works without needing to install anything except opencv.
    Disadvantage: it only works with CPU, so you can’t get really high speed to process videos in real time.

Why the YOLO algorithm is important

YOLO algorithm is important because of the following reasons:

  • Speed: This algorithm improves the speed of detection because it can predict objects in real-time.
  • High accuracy: YOLO is a predictive technique that provides accurate results with minimal background errors.
  • Learning capabilities: The algorithm has excellent learning capabilities that enable it to learn the representations of objects and apply them in object detection.

How to use YOLO with Opencv

We will focus in this blog on how to use YOLO with Opencv. This is the best approach for beginners, to get quickly the algorythm working without doing complex installations.

Let’s start by importing the libraries Opencv and numpy and then we load the algorythm.

We import the classes:

We load the algorythm. The run the algorythm we need three files:

  • Weight file: it’s the trained model, the core of the algorythm to detect the objects.
  • Cfg file: it’s the configuration file, where there are all the settings of the algorythm.
  • Name files: contains the name of the objects that the algorythm can detect.

We then load the image where we want to perform the object detection and we also get its width and height.

Now that we have the algorythm ready to work and also the image, it’s time to pass the image into the network and do the detection.

Keep in mind that we can’t use right away the full image on the network, but first we need it to convert it to blob. Blob it’s used to extract feature from the image and to resize them. YOLO accepts three sizes:

  • 320×320 it’s small so less accuracy but better speed
  • 609×609 it’s bigger so high accuracy and slow speed
  • 416×416 it’s in the middle and you get a bit of both.

The outs on line 21 it’s the result of the detection. Outs is an array that conains all the informations about objects detected, their position and the confidence about the detection.

At this point the detection is done, and we only need to show the result on the screen.
We then loop trough the outs array, we calculate the confidence and we choose a confidence threshold.

On line 32 we set a threshold confidence of 0.5, if it’s greater we consider the object correctly detected, otherwise we skip it.
The threshold goes from 0 to 1. The closer to 1 the greater is the accuracy of the detection, while the closer to 0 the less is the accuracy but also it’s greater the number of the objects detected.

When we perform the detection, it happens that we have more boxes for the same object, so we should use another function to remove this “noise”.
It’s called Non maximum suppresion.

We finally extract all the informations and show them on the screen.

  • Box: contain the coordinates of the rectangle sorrounding the object detected.
  • Label: it’s the name of the object detected
  • Confidence: the confidence about the detection from 0 to 1.

It’s called Non maximum suppresion.

Conclusion

This blog has provided an overview of the YOLO algorithm and how it is used in object detection. This technique provides improved detection results compared to other object detection techniques such as Fast R-CNN and Retina-Net.

To summarize:

  • We have gained an overview of object detection and the YOLO algorithm.
  • We have gone through the main reasons why the YOLO algorithm is important.
  • We have learned how the YOLO algorithm works. We have also gained an understanding of the main techniques used by YOLO to detect objects.
  • We have learned the real-life applications of YOLO.

Happy learning!

Download Code from Github 👨‍💻

--

--

Shubham Koli

Data Science || Machine Learning || Computer Vision || NLP || Mechanical Engineer