程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Imageai (I) fast and simple image content prediction using Python image prediction

編輯:Python

brief introduction
ImageAI It's a python The library of , It enables developers to build applications and systems with deep learning and computer vision functions with a few lines of code . It is from Moses Olafenwa and John Olafenwa Developed and maintained by two .
Specifically git Address :https://github.com/OlafenwaMoses/ImageAI
Today, I will first introduce the use imageai Preparatory work , Installation and simple implementation image prediction
( The following content git There is a detailed introduction to )


preparation

First of all, of course python,imageai Only for now 3.5.1 Or later , It's usually used 3.6 Well
There are also the following :
Tensorflow>=1.4.0
Numpy >=1.13.1
SciPy >=0.19.1
OpenCV
Pillow
Matplotlib
h5py
Keras 2.x

pip3 install xxx


install
After doing the preparatory work, it is time to install , You can use it directly pip3 install as follows

pip3 install https://github.com/OlafenwaMoses/ImageAI/releases/download/2.1.0/imageai-2.1.0-py3-none-any.whl

Of course, you can also [imageai-2.1.0-py3-none-any.whl](https://github.com/OlafenwaMoses/ImageAI/releases/download/2.1.0/imageai-2.1.0-py3-none-any.whl) Download and install ``` pip3 install C:\User\MyUser\Downloads\imageai-2.1.0-py3-none-any.whl ```

Image Prediction
After installation, enter the topic , Prediction of image content .
ImageAI Provide 4 There are different algorithms and models for image prediction , And in ImageNet-1000 Training on the dataset .4 Two algorithms include SqueezeNet,ResNet,InceptionV3 and DenseNet.
Here are also the documents of four models after training .

  • SqueezeNet( The fastest prediction The accuracy is medium )
  • ResNet50 ( Rapid prediction The accuracy is high )
  • InceptionV3( The prediction speed is slow High accuracy )
  • DenseNet121( Prediction is slower The accuracy rate is the highest )

Okay , Choose a model , Download the trained content and you can start simple image prediction !

prediction.py

from imageai.Prediction import ImagePrediction
import os
import time
# timing
start = time.time()
# current path Include pictures that need to be predicted , Model file
execution_path = os.getcwd()
# Create a prediction class
prediction = ImagePrediction()
# Set the prediction model There are four kinds of
#SqueezeNet
#prediction.setModelTypeAsSqueezeNet()
#prediction.setModelPath(os.path.join(execution_path, "squeezenet_weights_tf_dim_ordering_tf_kernels.h5"))
#ResNet50
#prediction.setModelTypeAsInceptionV3()
#prediction.setModelPath(os.path.join(execution_path, "inception_v3_weights_tf_dim_ordering_tf_kernels.h5"))
#InceptionV3
#prediction.setModelTypeAsResNet()
#prediction.setModelPath(os.path.join(execution_path, "resnet50_weights_tf_dim_ordering_tf_kernels.h5"))
#DenseNet121
prediction.setModelTypeAsDenseNet()
prediction.setModelPath(os.path.join(execution_path, "DenseNet-BC-121-32.h5"))
prediction.loadModel()
# Forecast picture , And the number of predicted outputs
predictions, probabilities = prediction.predictImage(os.path.join(execution_path, "1.jpg"), result_count=5 )
# Closing time
end = time.time()
# Output results
for eachPrediction, eachProbability in zip(predictions, probabilities):
print(eachPrediction," : ", eachProbability)
print ("\ncost time:",end-start)

Look at the code and remove the comments It only takes about ten lines to complete the prediction of the image

Use it first git The picture provided on

I ran with all four models give the result as follows

---squeezenet_weights_tf_dim_ordering_tf_kernels.h5---
sports_car : 45.9977924823761
convertible : 38.13416361808777
grille : 7.812434434890747
minivan : 2.406269498169422
pickup : 2.211885526776314
cost time: 6.432089567184448
---inception_v3_weights_tf_dim_ordering_tf_kernels.h5---
convertible : 96.45639061927795
sports_car : 3.52620966732502
beach_wagon : 0.010831362305907533
car_wheel : 0.003940704118576832
pickup : 0.0006548482815560419
cost time: 15.786875247955322
---resnet50_weights_tf_dim_ordering_tf_kernels.h5---
convertible : 52.459555864334106
sports_car : 37.61284649372101
pickup : 3.1751200556755066
car_wheel : 1.817505806684494
minivan : 1.7487050965428352
cost time: 24.41988778114319
---DenseNet-BC-121-32.h5---
sports_car : 64.85629677772522
convertible : 13.065926730632782
beach_wagon : 8.915219455957413
car_wheel : 6.854388862848282
grille : 2.4432314559817314
cost time: 24.5520977973938

The probability predicts whether it is a sports car or a convertible .

I also picked a photo casually Have a try

result

Siberian_husky : 87.60582208633423 Siberian sled dog ( Two ha )
Eskimo_dog : 12.393762916326523 Eskimo Dog
malamute : 0.00020242005120962858 Alaskan sled dog
dogsled : 0.00015880526689215912
Norwegian_elkhound : 6.489584336577536e-06
cost time: 20.758772611618042

It turned out to be good ヽ( ̄▽ ̄)ノ

besides ,git It also introduces the prediction of multiple graphs , Predict changes in speed , Multithreading operation, etc . There is no explanation here , It's not a big change .
Let's call it a day ,imageai And image object detection / Separate , Object detection and tracking in the video will be introduced later

I put the code and model files on the network disk , If necessary, you can download it yourself
link : https://pan.baidu.com/s/13hUoa_0d3tla5d2pTfD1OA Extraction code : qne5

follow-up :
ImageAI ( Two ) Use Python Fast and simple object detection Object Detection
ImageAI ( 3、 ... and ) Use Python Fast and simple object detection in video Video Object Detection and Tracking


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved