Python: Object Detection with ImageAI – Full Tutorial

Part of Computer Vision, object detection focuses on finding and tracking objects in images and videos. It’s used in various applications like recognizing faces, detecting vehicles, and ensuring security.

The main goals of object detection are to recognize all objects in an image and pinpoint the specific object of interest.

Codemagnet has come up with this In this tutorial to show you how to do object detection in Python using the ImageAI library.

ImageAI

It is a python library built to empower developers to build applications and systems with self-contained deep learning and Computer Vision capabilities using a few lines of straight forward code. ImageAI contains a Python implementation of almost all of the state-of-the-art deep learning algorithms like RetinaNet, YOLOv3, and TinyYOLOv3.

To use ImageAI you need to install a few dependencies. The first step is to have Python installed on your computer.

Once python is installed, install the following using pip

pip install tensorflow
pip install opencv-python
pip install keras
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

Create a folder first named as ObjectDetection and inside the main folder create three subfolder – model,input and output respectively and add your image and TinyYOLOv3 model file in respective folder. Then let’s first import object detection in your prefered editor

from imageai.Detection import ObjectDetection

Now that you have imported imageAI library and the ObjectDetection class , the next thing is to create an instance of the class ObjectDetection, as shown here:

detector = ObjectDetection()

Now, specify input and output image

model_path = "./models/yolo-tiny.h5"
input_path = "./input/test45.jpg"
output_path = "./output/newimage.jpg"

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

detector.setModelTypeAsTinyYOLOv3()

Full Code:

from imageai.Detection import ObjectDetection

detector = ObjectDetection()

model_path = "./models/yolo-tiny.h5"
input_path = "./input/tests.jpg"
output_path = "./output/newimage.jpg"

detector.setModelTypeAsTinyYOLOv3()
detector.setModelPath(model_path)
detector.loadModel()
detection = detector.detectObjectsFromImage(input_image=input_path, output_image_path=output_path)

for eachItem in detection:
    print(eachItem["name"] , " : ", eachItem["percentage_probability"])

Original Image

Output:

Now, You can see that ImageAI has successfully identified cars and persons in the image.

Explanation of the above code:

  1. from imageai.Detection import ObjectDetection: This line imports the ObjectDetection class from the Detection module of the imageai package, which is used for object detection.
  2. detector = ObjectDetection(): This line creates an instance of the ObjectDetection class, which will be used to perform object detection tasks.
  3. model_path = "./models/yolo-tiny.h5": This line sets the path to the pre-trained YOLO (You Only Look Once) model file for tiny objects.
  4. input_path = "./input/tests.jpg": This line sets the path to the input image file for object detection.
  5. output_path = "./output/newimage.jpg": This line sets the path where the output image with detected objects will be saved.
  6. detector.setModelTypeAsTinyYOLOv3(): This line sets the model type of the detector to TinyYOLOv3, indicating the specific model architecture to be used for detection.
  7. detector.setModelPath(model_path): This line sets the path to the YOLO model file for the detector to use for detection.
  8. detector.loadModel(): This line loads the YOLO model into memory, ready for object detection.
  9. detection = detector.detectObjectsFromImage(input_image=input_path, output_image_path=output_path): This line performs object detection on the input image using the loaded model and saves the output image with detected objects to the specified output path.
  10. for eachItem in detection:: This line starts a loop to iterate over each detected object in the detection result.
  11. print(eachItem["name"] , " : ", eachItem["percentage_probability"]): This line prints the name of each detected object along with the probability (in percentage) of the detection.

Conclusion,

In this article, we explored how to perform object detection using ImageAI in Python. Object detection is a technology that allows us to identify and track objects in images and videos. With ImageAI, we can use pre-trained models to detect objects in images and videos, making it easier to build applications for various tasks such as security systems, self-driving cars, and more. By following this tutorial, you’ve learned the basics of object detection and how to use ImageAI to implement it in Python.

Author

Sona Avatar

Written by

2 responses to “Python: Object Detection with ImageAI – Full Tutorial”

  1. […] comprehension is a concise way to create lists in Python. It allows you to create a new list by applying an expression to each item in an existing list (or […]

  2. […] Monkey patching in python refers to the practice of dynamically modifying or extending the behavior of a class or module at runtime. This allows developers to change the behavior of existing code without altering its source code, which can be useful for testing, debugging, or adding new features to third-party libraries. […]

Leave a Reply

Trending

CodeMagnet

Your Magnetic Resource, For Coding Brilliance

Programming Languages

Web Development

Data Science and Visualization

Career Section

<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-4205364944170772"
     crossorigin="anonymous"></script>