Now Reading
Hands-On Guide To Object Detection Using YOLO

Hands-On Guide To Object Detection Using YOLO

Dr. Vaibhav Kumar
W3Schools

Computer vision has a lot of interesting applications and object detection is one of the most interesting application. With the advance computer vision techniques, the objects present in the images can be identified in seconds with great accuracy. Hundreds of images can be processed in a few minutes to detect objects in those images. There are many algorithms available now through which this object detection can be performed very fastly. YOLO is one of these popular object detection methods.

In this article, we will learn how to detect objects present in the images. For the detection of objects, we will use the YOLO (You Only Look Once) algorithm and demonstrate this task on a few images. In the result, we will get the image with captioned and highlighted objects with their probability of correct detection.

How does YOLO work?

YOLO, abbreviated as You Only Look Once, was proposed as a real-time object detection technique by Joseph Redmon et al in their research work. It frames object detection in images as a regression problem to spatially separated bounding boxes and associated class probabilities. In this approach, a single neural network divides the image into regions and predicts bounding boxes and probabilities for each region. The neural network predicts bounding boxes and class probabilities directly from full images in one evaluation. The base YOLO model processes images in real-time at 45 frames per second. The pre-trained YOLO network weights are provided that can be used directly in any implementation and hence no need to train a model on example images. 



Object Detection using YOLO

This code was implemented in Google Colab and the .py file was downloaded.

# -*- coding: utf-8 -*-
"""AlexNet.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/14eAKHD0zCHJpw5u8rRWxxxxxxxxxxx
"""

To start using the ImageAI, first, we need to have some installations. If you are working in Colab, then execute the below line as it is given. If you are working on your local system, run it on command prompt after removing ! from the beginning. The first command will install the ImageAI dependencies and the second command will download the weights of the pre-trained convolutional neural network. 

!pip3 install https://github.com/OlafenwaMoses/ImageAI/releases/download/2.0.2/imageai-2.0.2-py3-none-any.whl

!wget https://github.com/OlafenwaMoses/ImageAI/releases/download/1.0/yolo.h5

The latest version of TensorFlow that is 2.0 does not support the session and graph network, so to use the same, we need to install a previous version of TensorFlow and Keras.

!pip install tensorflow== 1.14.0
!pip install keras== 2.2.0

To verify the current versions, we can use the below lines of codes.

import tensorflow as tf
print(tf.__version__)

Now, we will import the library for object detection. The os library is required to handle the image directories and the time library is required to obtain the time taken in detecting objects.

#Library
from imageai.Detection import ObjectDetection
import os
import time

We will obtain the current working directory from where the images can be accessed during the program execution.

#Current working directory
exec_path = os.getcwd()

In the next step, we will define the object of Yolo through which we can access the required functions for image detection. 

#Yolo
yolo_obj = ObjectDetection()
yolo_obj.setModelTypeAsYOLOv3()
yolo_obj.setModelPath( os.path.join(exec_path , "yolo.h5"))
yolo_obj.loadModel()

The below library is used for image handling.

#Library for image
from PIL import Image

Now, we are ready with almost everything. Here, we will start performing the image detection task on a few images on by one. 

Object detection in the first image

See Also
Own Data For Object Detection

#First Image
Image.open("img1.jpg")

#Detecting objects in the first image
start = time.time()

detections = yolo_obj.detectObjectsFromImage(input_image = os.path.join(exec_path , "img1.jpg"), output_image_path = os.path.join(exec_path , "out_img1.jpg"))

print('sec',time.time() - start)

for objects in detections:
    print(objects["name"] , " : " , objects["percentage_probability"] )

Image.open("out_img1.jpg")




Object Detection Using YOLO


























As we can see above the YOLO algorithm has detected a dog and a cat in the image with probabilities 100% and 94.76% respectively. Now, we will repeat the same process with a few more images.

Object detection in the second image

#Second Image
Image.open("img2.jpg")





















#Detecting objects in the second image
start = time.time()

detections = yolo_obj.detectObjectsFromImage(input_image = os.path.join(exec_path , "img2.jpg"), output_image_path = os.path.join(exec_path , "out_img2.jpg"))

print('sec',time.time() - start)

for objects in detections:
    print(objects["name"] , " : " , objects["percentage_probability"] )

Image.open("out_img2.jpg")








Object Detection Using YOLO

Object detection in the third image

#Third image
Image.open("img3.jpg")

























#Detecting objects in the third image
start = time.time()

detections = yolo_obj.detectObjectsFromImage(input_image = os.path.join(exec_path , "img3.jpg"), output_image_path = os.path.join(exec_path , "out_img3.jpg"))

print('sec',time.time() - start)

for objects in detections:
    print(objects["name"] , " : " , objects["percentage_probability"] )

Image.open("out_img3.jpg")































Object Detection Using YOLO






















Object detection in the fourth image

#Fourth image
Image.open("img4.jpg")






































#Detecting objects in the fourth image
start = time.time()

detections = yolo_obj.detectObjectsFromImage(input_image = os.path.join(exec_path , "img4.jpg"), output_image_path = os.path.join(exec_path , "out_img_4.jpg"))

print('sec',time.time() - start)

for objects in detections:
    print(objects["name"] , " : " , objects["percentage_probability"] )

Image.open("out_img_4.jpg")






Object Detection Using YOLO



































So, in this way, we can detect objects in the images using the YOLO. With every detected object, it gives the probability as well of correct detection. 

What Do You Think?

If you loved this story, do join our Telegram Community.


Also, you can write for us and be one of the 500+ experts who have contributed stories at AIM. Share your nominations here.

Copyright Analytics India Magazine Pvt Ltd

Scroll To Top