Object Detection classifier using Image_ai (code)

In this article, we are going to cover- Introduction to Object detection classifier, Importance of Object Detection, Pre-trained dataset (COCO), RetinaNet and about Image_ai python module.

When I started Machine learning, these fancy-fancy classifiers look very difficult to me but trust me these pre-trained models make it very easy than it seems to be. I will show you in this article, Just bear with me it’s really very easy !!!

You are here means, you are already familiar with machine learning languages like R or Python. If not I request you to go for basic here. In this article, I am using Python language. Let’s Dive…


Object detection is a computer vision technique for locating instances of objects in images or videos. Object detection algorithms typically leverage machine learning or deep learning to produce meaningful results. When humans look at images or videos, we can recognize and locate objects of interest within a matter of moments.

Object Detection Techniques

You can use a variety of techniques to perform object detection. Popular deep learning-based approaches using convolutional neural networks (CNNs), such as RetinaNet and YOLO v2, automatically learn to detect objects within images.

we are not covering all the techniques here. In this tutorial, I am been using RetinaNet technique only.

What is RetinaNet ?

It is discovered that there is an extreme foreground-background class imbalance problem in the one-stage detector and it is believed that this is the central cause which makes the performance of one-stage detectors inferior to two-stage detectors. In RetinaNet, a one-stage detector, by using focal loss, the lower loss is contributed by “easy” negative samples so that the loss is focusing on “hard” samples, which improves the prediction accuracy.

Number of Boxes Comparison in different Algorithm

  1. YOLOv1: 98 boxes
  2. YOLOv2: ~1k
  3. OverFeat: ~1–2k
  4. SSD: ~8–26k
  5. RetinaNet: ~100k.

want to learn more about RetinaNET: link

Python Module ImageAI

ImageAI provided very powerful yet easy to use classes and functions to perform Video Object Detection and Tracking and Video analysis. ImageAI allows you to perform all of these with state-of-the-art deep learning algorithms like RetinaNet, YOLOv3, and TinyYOLOv3. With ImageAI you can run detection tasks and analyze videos and live-video feeds from device cameras and IP cameras.

More about ImageAI: link

COCO DATASET — to train our model

The Common Objects in Context (COCO) dataset has 200,000 images with more than 500,000 object annotations in 80 categories. It is the most extensive publicly available object detection database. The average number of objects is 7.2 per image.

OMG !!!!!!!!!! Right

Want to download the dataset: link

Now it’s time to dive into the code.

Let’s download the required libraries

# for pyhton<3 pip and >3 pip3type this in your terminal
pip3 install -q imageai

That’s it !! what really …fantastic

Object classification in photos

from imageai.Detection import ObjectDetection
import os
execution_path = os.getcwd()detector = ObjectDetection()
#dataset to train modeldetector.setModelPath(os.path.join(execution_path,"/resnet50_coco_best_v2.0.1.h5"))
#load and save the output the filedetections=detector.detectObjectsFromImage(input_image=os.path.join(execution_path , "Akku.jpg"), output_image_path=os.path.join(execution_path , "output_file_name"))##give prediction likefor eachObject in detections:
print(eachObject["name"] , " : " , eachObject["percentage_probability"] )
#person : 90.98637104034424
#horse : 85.25654077529907
#pen : 83.02676677703857

That’s it !!!! Easy right

More Details:

ObjectDetection() : method used in imageAI.

resnet50_coco_best_v2.0.1.h5 : Dataset to train our model.

Replace “Akku.jpg” with your image file name. By the way, Akku is my nickname given by my bestie.

Replace “output_file_name” with your output image file name.

Passenger movie

The above code is for classifying in the image. If you want to do it in videos we have to do slight modification in our code.

Object classification in video

from imageai.Detection import VideoObjectDetection
import os
import cv2
execution_path = os.getcwd()#access you webcam
camera = cv2.VideoCapture(0)
detector = VideoObjectDetection()
detector.setModelPath(os.path.join(execution_path , "/resnet50_coco_best_v2.0.1.h5"))
video_path=detector.detectObjectsFromVideo(input_file_path=os.path.join(execution_path, "Akku.mp4"),
output_file_path=os.path.join(execution_path, "output_file_name"), frames_per_second=20, log_progress=True, minimum_percentage_probability=40, detection_timeout=120)

More Details:

VideoObjectDetection() : method used in image ai.

resnet50_coco_best_v2.0.1.h5 : Dataset to train our model.

Replace “Akku.mp4” with your video file name.

CV2 Module: To access your webcam

If you want to access a video with your camera replace a few lines a code.
{input_file_path=os.path.join(execution_path, “your_input_file_name.mp4”) } with {camera_input=camera}

These are parameters of VideoObjectDetectionframes_per_second=20 : No of frame per secondlog_progress=True : show the processing like
Processing Frame : 1
minimum_percentage_probability=40 :determine the integrity of the detection resultsdetection_timeout=120

I hope you like this article !!


ASE-Backend @Tekion