Using the Python Couchbase Client
October 7, 2020Embracing Uncertainty
January 26, 2021What is Object Detection?
Object Detection is a computer vision technique for locating instances of objects in images or videos. In this blog, we will be exploring how to employ Object Detection software to identify objects present in a still image, but Object Detection has a number of usages in today’s technologies, including face detection, vehicle detection, pedestrian counting, self-driving cars, security systems, etc.
In the field of Object Detection, the two major objectives are to:
A: Identify all objects present in an image
B: Filter out the object of interest
How does it work?
Typically, there are three steps in an object detection framework. First, a model or algorithm is used to generate regions of interest or region proposals. These region proposals are a large set of bounding boxes spanning the full image. In the second step, visual features are extracted for each of the bounding boxes, they are evaluated and it is determined whether and which objects are present in the proposals. In the final post-processing step, overlapping boxes are combined into a single bounding box.
Image Classification vs Object Detection
People often confuse image classification and Object Detection scenarios. In general, if you want to classify an image into a certain category, you use image classification. On the other hand, if you aim to identify the location of objects in an image, and, for example, count the number of instances of an object, you can use Object Detection.
For this project, we will be writing an Object Detection script in Python, using a well-known python library called the ImageAI library.
What is ImageAI?
ImageAI is a python library that presents a simple interface, on top of infrastructure utilizing almost all of the major deep learning algorithms like RetinaNet, YOLOv3, and TinyYOLOv3. The intent of ImageAI is to empower developers to build applications and systems using Deep Learning and Computer Vision capabilities accessible with only a few lines of concise code.
One of the convenient features of ImageAI is that it offers a number of offline APIs to ensure functionality despite an internet connection. Some of these offline APIs include Object Detection, video detection, and object tracking. For these capabilities, ImageAI is based on a pre-trained model that is easily customizable.
In order to utilize the ImageAI library properly, we will need to be able to modify our working Python version to version 3.6. For this, we will be creating a sort of virtual environment using the Python library known as Conda.
Object Detection using ImageAI
(This project assumes you have Python installed)
For this project, you will need to download and run Miniconda, which is the installer for the Conda CLI. The Conda CLI will allow you to create a virtual python environment using a specific version of Python, much like NVM (Node Version Manager) of Javascript. Instructions to install Conda can be found here: https://docs.conda.io/en/latest/miniconda.html
Step 1: After Conda is installed, create an Anaconda environment with python version 3.6.
$ conda create -n retinanet python=3.6 anaconda
Step 2: Activate the environment and install the necessary packages.
$ source activate retinanet
$ pip install tensorflow==1.13.1 numpy==1.16.1 scipy opencv-python keras==2.2.4 imageai
Step 3: Download the Object Detection dataset from this link
Click here: https://github.com/OlafenwaMoses/ImageAI/releases/download/1.0/yolo.h5.
Step 4: In the same directory as the dataset, create a file named detector.py, and paste the following code:
from imageai.Detection import ObjectDetection
import os
execution_path = os.getcwd()
detector = ObjectDetection()
detector.setModelTypeAsRetinaNet()
detector.setModelPath( os.path.join(execution_path , “yolo.h5”))
detector.loadModel()
detections = detector.detectObjectsFromImage(input_image=os.path.join(execution_path , “test-image.jpeg”), output_image_path=os.path.join(execution_path , “processed_image.jpeg”))
for eachObject in detections:
print(eachObject[“name”] , “ : “ , eachObject[“percentage_probability”] )
Step 5: Add this photo to the folder with the script and dataset, and name it test-image.jpeg
Step 6: Execute the object detector script
$ python detector.py
This should have generated a new image file called processed_image.jpeg containing the original image modified by the object detection software, like so:
Conclusion
Object detection is a computer vision technique that allows us to identify and locate objects in an image. With this kind of identification and localization, object detection can be used to count objects in a scene and determine their precise locations, while also accurately labeling them. Object Detection is just one interesting application of AI in the field of Computer Vision, but this technology has proven to be foundational to the modern era of computers, as society’s needs have driven artificially intelligent technologies to become increasingly sophisticated. Today we demonstrated how to utilize an existing Python library and dataset, to create an artificially intelligent Object Detection script.