Image Recognition with 10 lines of code

With the rise and popularity of deep learning algorithms, there has been impressive progress in the field of Artificial Intelligence, especially in Computer Vision. The second decade of the 21st century has seen rapid adoption of Convolutional Neural Networks, invention of state-of-the-art algorithms, availability of massive training data and the invention of high performance and cost effective compute. One of the key concepts in Computer Vision is image classification; which is the ability of a software system to label correctly the dominant object in an image. Systems powered by current state-of-the-art algorithms have surpassed human capabilities in identifying objects in images, and this technology is being widely used by technology giants, large businesses and governments for purposes such as intelligent applications, websites, weather, research, surveillance and more.

However, the technology, just like the early days of computing and the world wide web is beyond the reach of prospective developers, programmers, small scale businesses and individuals that will largely benefit from it. This issue exists mainly because of lack of technical know-how to develop and use this technology independently. That is why we developed and released the ImageAI python library which abstracts the complex and time-consuming implementations of these computer vision algorithms, by providing developer and programmer friendly instructions and APIs, just as programming languages like Java, Python, .NET and others abstracted the programming instructions in Assembly and C. This article provides a step-by-step guide to performing state-of-the-art image prediction with only 10 lines of code.

ImageAI is a Python library built to empower developers to build applications and systems with self-contained Computer Vision capabilities. ImageAI’s documentation, sample codes and source code can be found on this it’s GitHub repository.

To get started, you need to be familiar with the basics of the Python programming language and use of pip to install libraries. You will have to follow only the following steps:

1. Install Python 3.5.1 or later version and pip

(Skip this section if you already have Python 3.5.1 or later installed)

2. Install the ImageAI dependencies

(Skip any of the installation instruction in this section if you already have the library installed )

- Tensorflow

pip3 install --upgrade tensorflow

- Numpy

pip3 install numpy

- SciPy

pip3 install scipy

- OpenCV

pip3 install opencv-python

- Matplotlib

pip3 install matplotlib

- h5py

pip3 install h5py

- Keras

pip3 install keras

3. Install the ImageAI library

pip3 install

4. Download the ResNet Model file which was trained on the ImageNet-1000 dataset and copy the file to your python project folder.

5. Create a python file with a name (for example, “”) and write the code below into it.


Sample image we are trying to predict

Code result:

sports_car : 90.61029553413391
car_wheel : 5.9294357895851135
racer : 0.9972884319722652
convertible : 0.8457873947918415
grille : 0.581052340567112

Believe me, that is all the code you need to perform image prediction on an image. Now let us break the code down so that you can understand how it works. The code above works as follows:

from imageai.Prediction import ImagePrediction
import os

The code above imports the ImageAI ImagePrediction class and the python os class.

execution_path = os.getcwd()

The above line creates a variable which holds the reference to the path that contains your python file (in this example, your and the ResNet model file.

prediction = ImagePrediction()
prediction.setModelPath(execution_path + "\resnet50_weights_tf_dim_ordering_tf_kernels.h5")

In the lines above, we created an instance of the ImagePrediction() class in the first line, then we set the model type of the prediction object to ResNet by caling the .setModelTypeAsResNet() in the second line and then we set the model path of the prediction object to the path of the model file (resnet50_weights_tf_dim_ordering_tf_kernels.h5) we copied to the project folder folder , in the third line.

predictions, percentage_probabilities = prediction.predictImage("C:\Users\MyUser\Downloads\sample.jpg", result_count=5)

In the above line, we defined 2 variables to be equal to the function called to predict an image, which is the .predictImage() function, into which we parsed the path to our image and also state the number of prediction results we want to have (values from 1 to 1000) parsing result_count=5 . The .predictImage() function will return 2 array objects with the first (predictions) being an array of predictions and the second (percentage_probabilities) being an array of the corresponding percentage probability for each prediction.

for index in range(len(predictions)):
print(predictions[index] ," : " ,percentage_probabilities[index])

The above line obtains each object in the predictions array, and also obtains the corresponding percentage probability from the percentage_probabilities, and finally prints the result of both to console.

The .predictImage() function takes in the path to an image and can also state the number of predictions we expect the function to return (optional, the default is 5). There are 1000 items in the ImageNet-1000 dataset on which the ResNet model was trained, which means the .predictImage function will return 1000 possible predictions, arranged in order of their probabilities.

With ImageAI, you can integrate image prediction code easily and conveniently into any application, website or system you build in python. There are other algorithms and model types supported in the ImageAI library, with some optimized for speed and others optimized for accuracy. With ImageAI, we hope to support more specialized aspects of Computer Vision including and not limited to image recognition in special environments and special fields and custom image prediction.

More examples and explanation on the ImageAI library is available via the link to its repository below:

We will be sharing more tutorials on the computer vision APIs supported by ImageAI soon.

ImageAI is an AI Commons Project. To learn more, visit the AI Commons website.