I heard that image recognition is difficult, and ten lines of code are used for Python image recognition.

 

With the rise and popularity of deep learning algorithms, the field of artificial intelligence has made impressive progress, especially in the field of computer vision. The second decade of the 21st century saw the rapid adoption of convolutional neural networks, the invention of state-of-the-art algorithms, the availability of large amounts of training data, and the invention of high-performance and cost-effective computing. A key concept in computer vision is image classification; this is the ability of a software system to correctly label the dominant object in an image.

ImageAI is a Python library designed to help developers build applications and systems with self-contained computer vision capabilities.

1. Install Python 3.5.1 or higher and pip

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

https://www.python.org/downloads/

2. Install ImageAI dependencies

- 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

- Hard

pip3 install hard

3. Install ImageAI library

pip3 install https://github.com/OlafenwaMoses/ImageAI/raw/master/dist/imageai-1.0.2-py3-none-any.whl

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

https://github.com/fchollet/deep-learning-models/releases/download/v0.2/resnet50_weights_tf_dim_ordering_tf_kernels.h5

5. Create a file named python (eg "FirstPrediction.py") and write the following code in it.

from imageai.Prediction import ImagePrediction

import them

execution_path = os.getcwd()

prediction = ImagePrediction()

prediction.setModelTypeAsResNet()

prediction.setModelPath( execution_path + " esnet50_weights_tf_dim_ordering_tf_kernels.h5")

prediction.loadModel()

predictions, percentage_probabilities = prediction.predictImage("C:UsersMyUserDownloadssample.jpg", result_count=5)

for index in range(len(predictions)):

print(predictions[index] + " : " + percentage_probabilities[index])

sample.jpg

Code result:

sports_car : 90.61029553413391

car_wheel : 5.9294357895851135

racer : 0.9972884319722652

convertible : 0.8457873947918415

grille : 0.581052340567112

code description

Now let's break down the code to understand how it works. The above code works as follows:

from imageai.Prediction import ImagePrediction

import them

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

execution_path = os.getcwd()

The above code creates a variable that holds a reference to the path containing the python file (FirstPrediction.py in this case) and the ResNet model file.

prediction = ImagePrediction()

prediction.setModelTypeAsResNet()

prediction.setModelPath(execution_path +“ resnet50_weights_tf_dim_ordering_tf_kernels.h5”)

In the code above, we created an instance of the ImagePrediction() class in the first line, then set the model type of the prediction object to ResNet by calling .setModelTypeAsResNet() in the second line, and then set the model path to predict Copy the object to the path of the model file (resnet50_weights_tf_dim_ordering_tf_kernels.h5) and copy it to the project folder folder on the third line.

predictions, percentage_probabilities = prediction.predictImage("C:UsersMyUserDownloadssample.jpg", result_count=5)

In the above line we define 2 variables which are equal to the function that is called to predict the image, this function is the .predictImage() function where we parse the path of the image and also indicate the prediction we want The number of results has (values ​​from 1 to 1000) parse result_count = 5. The .predictImage() function will return an array with the first (2-level array of object predictions) being the predictions and the second (array percentage_probabilities) being the corresponding percentage probabilities for each prediction.

for index in range(len(predictions)):

print(predictions[index] + " : " + percentage_probabilities[index])

The above line gets the predictions for each object in the array, and also gets the probability from the corresponding percentage_probabilities, and finally prints the results of both to the console.

The .predictImage() function will take the image in the path and also specify the number of predictions we expect the function to return (optional, defaults to 5). There are 1000 items in the ImageNet-1000 dataset on which the ResNet model was trained, which means that the .predictImage function will return 1000 possible predictions, ordered by their probability.

With ImageAI, you can easily and conveniently integrate image prediction code into any application, website or system you build in python. The ImageAI library supports additional algorithms and model types, some optimized for speed and others for accuracy. With ImageAI, we hope to support more specialized aspects of computer vision, including but not limited to image recognition in special environments and special fields, and custom image prediction

The editor also has its own place for learning and communication, everyone can come and learn together, 719+139+688, there is also a copy of the latest learning materials in 2018 prepared by the editor for everyone, whether it is Xiaobai or a great god, welcome to come communicate with.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324816681&siteId=291194637