Object Detection using ImageAI in Python

0
2888

Introduction To Object Detection using ImageAI in Python

Now, this is advanced technology using computer vision is object detection, and image classification in python. It deals with identifying the object present in images or videos by frame. Several application of object detection such as face detection, vehicle detection, self-driving cars, and much more application.

In this article, you will see how to perform object detection in Python with the help of the ImageAI library.

Deep Learning for Object Detection

There are several Deep learning techniques that have been proven state of the art for various object detection problems. The following are some of the commonly used deep learning approaches for object detection:

  • ImageAI
  • YOLO (You Only Look Once)
  • Region-based Convolutional Neural Networks

In the rest of this article, we will see what exactly ImageAI is and how to use it to perform object detection.

ImageAI

ImageAI is an object detection library built in python. With the help of ImageAI developers to build applications and systems with self-contained deep learning and computer vision capabilities using a few lines of straight forward code. ImageAI library has a capability implementation of almost all of the state-of-the-art deep learning algorithms like YOLOv3 and TinyYOLOv3.

ImageAI makes use of several APIs that work offline – that APIs work on object detection, object location, video detection, and object tracking APIs that can be called without internet access. ImageAI mainly works on pre-trained model and can easily be customized.

ImageAI, you can detect and recognize 80 different kinds of common, everyday objects.

Tutorial

In this part of the tutorial, we will work through the installation of ImageAI. I am assuming everyone working on Python3 new version.

First, install the required and dependency of ImageAI library.

TensorFlow

!pip install tensorflow

OpenCV

!pip install opencv-python
Keras
!pip install keras
ImageAI
!pip install imageAI

Now Start implementation with ImageAI

Now let’s see how to actually use the ImageAI library. I will  explain step by step how you can build your first project based on object detection model with ImageAI.

Step 1:-

Create an object_detect.py or object_detect.ipynb file that your preferred text editor for writing Python code.

Step 2:- 

Import object detection library

from imageai.Detection import ObjectDetection

Step 3:-

Now that you have imported imageAI library and the ObjectDetection class, the next thing is to create an instance of the class object detection.

detector = ObjectDetection()

NOTE:- if error while running the ImageAI library then update the keras>= 1.4 and TensorFlow  = 2.2.0.

Step 4:-

Let’s set the path of input file, output file, and model file.

#path of each file
 
models_path = "/content/yolo-tiny.h5"
input_path = "/content/input_test.jpg"
output_path = "/content/output_test.jpg

#path of each file

models_path = “/content/yolo-tiny.h5”

input_path = “/content/input_test.jpg”output_path = “/content/output_test.jpg

Step 6:-

After instantiating the ObjectDetection class we can now call various functions from the class. The class contains the following functions to call pre-trained models: setModelTypeAsRetinaNet(), setModelTypeAsYOLOv3(), and setModelTypeAsTinyYOLOv3().

For this tutorial, I will be using the pre-trained TinyYOLOv3 model, and hence we will use the setModelTypeAsTinyYOLOv3() function to load our model.

detector.setModelTypeAsTinyYOLOv3()

Next, we are going to call the function setModelPath(). This function accepts a string which contains the path to the pre-trained model:

detector.setModelPath(models_path)

This step calls the function loadModel() from the detector instance. It loads the model from the path specified above using the setModelPath() class method.

detector.loadModel()


Widget not in any sidebars

Step 7:- 

To detect objects in the image, we need to call the detectObjectsFromImage function using the detector object that we created in the previous section.

This function requires two arguments: input_image and output_image_path. input_image is the path where the image we are detecting is located, while the output_image_path parameter is the path to store the image with detected objects. This function returns a dictionary that contains the names and percentage probabilities of all the objects detected in the image.

detection = detector.detectObjectsFromImage(input_image=input_path, output_image_path=output_path)

Step 8:-

The dictionary items can be accessed by traversing through each item in the dictionary.

for eachItem in detection:

   print(eachItem[“name”] , ” : “, eachItem[“percentage_probability”])

Step 9:-
Output
car  :  54.72719073295593
car  :  58.94589424133301
car  :  62.59384751319885
car  :  74.07448291778564
car  :  91.10507369041443
car  :  97.26507663726807
car  :  97.55765795707703
person  :  53.6459743976593
person  :  56.59831762313843
person  :  72.28181958198547
 


Widget not in any sidebars

Conclusion

Object detection / Image Processing is a much more effective application in computer vision tasks. This article explains how to perform object detection in Python using the ImageAI library with the help of an example.

LEAVE A REPLY

Please enter your comment!
Please enter your name here