Object Detection with ImageAI in Python
For more details and information, please refer the YouTube video.
Video URL: https://youtu.be/--4CcIveKdY
Introduction
In this tutorial, we will be doing Object detection using ImageAI in Python. Object detection is a technology that falls under Computer Vision which deals with identifying and tracking objects present in images and videos. Object detection has multiple applications such as face detection, vehicle detection, pedestrian counting, self-driving cars, security systems, etc.
The two major objectives of object detection include:
- To identify all objects present in an image.
- Filter out the object of attention.
Setting up your Environment
In this tutorial, we will work through the installation of ImageAI.
To use ImageAI you need to install a few dependencies.
The first step is to have Python installed on your computer. Download and install Python 3 from the official Python website.
Once you have Python installed on your computer, install the following dependencies using pip
:
Python
$ pip install python 3.7.6
TensorFlow
$ pip install tensorflow
OpenCV
$ pip install opencv-python
Keras
$ pip install keras
ImageAI
$ pip install imageAI
Now download the TinyYOLOv3 model file that contains the classification model that will be used for object detection.
Performing Object Detection with ImageAI
Now let’s see how to actually use the ImageAI library.
We need the necessary folders:
- Object detection: root folder.
- models: stores pre-trained model.
- input: stores image file on which we want to perform object detection.
- output: stores image file with detected objects.
Open your preferred text editor for writing Python code and create a new file detector.py
.
Copy the codes below in your detector.py Python file.
from imageai.Detection import ObjectDetection //Import ObjectDetection class from the ImageAI library.detector = ObjectDetection() //create an instance of the class ObjectDetectionmodel_path = "./Model/yolo-tiny.h5"
input_path = "./Input/1.jpg"
output_path = "./Output/newimage.jpg" //specify the path from our input image, output image, and model.detector.setModelTypeAsTinyYOLOv3() //Using the pre-trained TinyYOLOv3 model, and hence we will use the setModelTypeAsTinyYOLOv3() function to load our model.detector.setModelPath(model_path) //function which accepts a string which contains the path to the pre-trained model.detector.loadModel() //Loads the model from the path specified above using the setModelPath() class method.detection = detector.detectObjectsFromImage(input_image=input_path, output_image_path=output_path) //function returns a dictionary which contains the names and percentage probabilities of all the objects detected in the image.for eachItem in detection:
print(eachItem["name"] , " : ", eachItem["percentage_probability"]) //The dictionary items can be accessed by traversing through each item in the dictionary.
Original image
After running the python file detector.py.
In the output, we can see the name of each detected object along with its percentage probability as shown below.
Output
Image with Object Detection
After the object detection, the resulting image looks like this:
Finally, we can see that ImageAI has successfully identified cars and persons in the image.