Giter VIP home page Giter VIP logo

imageai's Introduction

ImageAI

A python library built to empower developers to build applications and systems with self-contained Computer Vision capabilities


Built with simplicity in mind, ImageAI supports a list of state-of-the-art Machine Learning algorithms for image recognition. ImageAI currently supports image recognition using 4 different Machine Learning algorithms trained on the ImageNet-1000 dataset, meaning any application built with ImageAI can recognize 1000 distinct objects including vehicles, animals, plants, places, electronics, indoor objects, outdoor objects and more.
Eventually, ImageAI will provide support for a wider and more specialized aspects of Computer Vision including and not limited to image recognition in special environments and special fields, object detection and custom image prediction.



Dependencies

To use ImageAI in your application developments, you must have installed the following dependencies before you install ImageAI :

- Python 3.5.1 (and later versions) Download (Support for Python 2.7 coming soon)
- pip3 Install
- Tensorflow 1.4.0 (and later versions) Install
- Numpy 1.13.1 (and later versions) Install
- SciPy 0.19.1 (and later versions) Install

Installation

To install ImageAI, download the Python Wheel imageai-1.0.1-py3-none-any.whl and run the python installation instruction in the Windows command line to the path of the file like the one below:

pip3 install C:\User\MyUser\Downloads\imageai-1.0.1-py3-none-any.whl

Using ImageAI

ImageAI provides 4 different algorithms and model types to perform image prediction. To perform image prediction on any picture, take the following simple steps. The 4 algorithms provided for image prediction include SqueezeNet, ResNet, InceptionV3 and DenseNet. Each of these algorithms have individual model files which you must use depending on the choice of your algorithm. To download the model file for your choice of algorithm, click on any of the links below:

- SqueezeNet (Size = 4.82 mb, fastest prediction time and moderate accuracy)
- ResNet by Microsoft Research (Size = 98 mb, fast prediction time and high accuracy)
- InceptionV3 by Google Brain team (Size = 91.6 mb, slow prediction time and higher accuracy)
- DenseNet by Facebook AI Research (Size = 31.6 mb, slower prediction time and highest accuracy)

After you download the model file of your choice, you will need to download one more file which will be a JSON file that contains the model mapping for all the 1000 objects supported. Find the link below:

- imagenet_class_index.json

Great! Once you have downloaded these two files, start a new python project, and then copy these two files to your project folder where your python files (.py files) will be . Then create a python file and give it a name; an example is FirstPrediction.py. Then write the code below into the python file:

FirstPrediction.py

from imageai.Prediction import ImagePrediction
import os

execution_path = os.getcwd() prediction = ImagePrediction() prediction.setModelTypeAsResNet() prediction.setModelPath( execution_path + "\resnet50_weights_tf_dim_ordering_tf_kernels.h5") prediction.setJsonPath(execution_path + "\imagenet_class_index.json") prediction.loadModel()

predictions, percentage_probabilities = prediction.predictImage("C:\Users\MyUser\Downloads\sample.jpg", result_count=5) for index in range(len(predictions)): print(predictions[index] + " : " + percentage_probabilities[index])

Sample Result:

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


The code above works as follows:
from imageai.Prediction import ImagePrediction
import os

The code above imports the ImageAI library and the python os class.
execution_path = os.getcwd()

The above line obtains the path to the folder that contains your python file (in this example, your FirstPrediction.py) .

prediction = ImagePrediction()
prediction.setModelTypeAsResNet()
prediction.setModelPath(execution_path + "\resnet50_weights_tf_dim_ordering_tf_kernels.h5")
prediction.setJsonPath(execution_path + "\imagenet_class_index.json")
In the lines above, we created and 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, 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 python file folder in the third line, and then set the json path of the prediction object to the path of the json file (imagenet_class_index.json) you copied to the python file folder.

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 obtain the corresponding percentage probability from the percentage_probabilities, and finally prints the result of both to console.



Using ImageAI in MultiThreading

When developing programs that run heavy task on the deafult thread like User Interfaces (UI), you should consider running your predictions in a new thread. When running image prediction using ImageAI in a new thread, you must take note the following:
- You can create your prediction object, set its model type, set model path and json path outside the new thread.
- The .loadModel() must be in the new thread and image prediction (predictImage()) must take place in th new thread.
Take a look of a sample code below on image prediction using multithreading:

from imageai.Prediction import ImagePrediction
import os
import threading

execution_path = os.getcwd()

prediction = ImagePrediction() prediction.setModelTypeAsResNet() prediction.setModelPath( execution_path + "\resnet50_weights_tf_dim_ordering_tf_kernels.h5") prediction.setJsonPath(execution_path + "\imagenet_class_index.json")

picturesfolder = os.environ["USERPROFILE"] + "\Pictures\" allfiles = os.listdir(picturesfolder)

class PredictionThread(threading.Thread):

def __init__(self):
    threading.Thread.__init__(self)

def run(self):
    prediction.loadModel()
    for eachPicture in allfiles:
        if eachPicture.endswith(".png") or eachPicture.endswith(".jpg"):
            predictions, percentage_probabilities = prediction.predictImage(picturesfolder + eachPicture, result_count=1)
            for index in range(len(predictions)):
                print(predictions[index] + " : " + percentage_probabilities[index])

predictionThread = PredictionThread () predictionThread.start()


Sample Application

As a demonstration of the what you can do with ImageAI, we have built a complete AI powered Photo gallery for Windows called IntelliP , using ImageAI and UI framework Kivy. Follow this link to download page of the application and its source code.

Documentation

The ImageAI library currently supports only image prediction via the ImagePrediction class.

The ImagePrediction class

The ImagePrediction class can be used to perform image prediction in any python application by initiating it and calling the available functions below:
- setModelTypeAsSqueezeNet() This function should be called should you chose to use the SqueezeNet model file for the image prediction. You only need to call it once.
- setModelTypeAsResNet() This function should be called should you chose to use the ResNet model file for the image prediction. You only need to call it once.
- setModelTypeAsInceptionV3() This function should be called should you chose to use the InceptionV3Net model file for the image prediction. You only need to call it once.
- setModelTypeAsDenseNet This function should be called should you chose to use the DenseNet model file for the image prediction. You only need to call it once.
- setModelPath() You need to call this function only once and parse the path to the model file path into it. The model file type must correspond to the model type you set.
- setJsonPath() You need to call this function only once and parse the path to the imagenet_class_index.json into it.
- loadModel() You need to call this function once only before you attempt to call the predictImage() function .
- predictImage() To perform image prediction on an image, you will call this function and parse in the path to the image file you want to predict, and also state the number of predictions result you will want to have returned by the function (1 to 1000 posible results). This functions returns two arrays. The first one is an array of predictions while the second is an array of corresponding percentage probabilities for each prediction in the prediction array. You can call this function as many times as you need for as many images you want to predict and at any point in your python program as far as you have called the required functions to set model type, set model file path, set json file path and load the model.

Contact Developers

Moses Olafenwa
Website: https://moses.specpal.science
Twitter: @OlafenwaMoses
Medium : @guymodscientist
Facebook : moses.olafenwa


John Olafenwa
Website: https://john.specpal.science
Twitter: @johnolafenwa
Medium : @johnolafenwa
Facebook : olafenwajohn

imageai's People

Contributors

johnolafenwa avatar olafenwamoses avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.