1.ImageAI
ImageAI是一个python库,它使开发人员、研究人员和学生能够使用简单的几行代码构建具有自包含的深入学习和计算机视觉功能的应用程序和系统。本文档提供了对ImageAI中可用的所有类和函数的详细了解,以及一些代码示例。ImageAI是一个由Moses Olafenwa和John Olafenwa开发的项目,后者是DeepQuest人工智能团队。
代码下载为:
https://github.com/OlafenwaMoses/ImageAI直接下载master代码
2.ImageAI 功能
- 图像预测
- 物体检测
- 视频对象检测和跟踪
ImageAI提供了4种不同的算法和模型类型来执行图像预测。要对任何图片执行图像预测,请执行以下简单步骤。为图像预测提供的4种算法包括挤压网、ResNet、InceptionV3和DenseNet。
要在应用程序开发中使用ImageAI,在安装ImageAI之前必须安装以下依赖项:
- Python3.5.1(及更高版本)
- Tensorflow 1.4.0(及更高版本)(Tensorflow 2.0即将推出)
- opencv
- Keras 2.x
3.编写第一个代码
from imageai.Prediction import ImagePrediction
import os
execution_path = os.getcwd()
prediction = ImagePrediction()
prediction.setModelTypeAsResNet()
prediction.setModelPath(os.path.join(execution_path, "resnet50_weights_tf_dim_ordering_tf_kernels.h5"))
prediction.loadModel()
predictions, probabilities = prediction.predictImage(os.path.join(execution_path, "1.jpg"), result_count=5 )
for eachPrediction, eachProbability in zip(predictions, probabilities):
print(eachPrediction , " : " , eachProbability)
重点训练文件:resnet50_weights_tf_dim_ordering_tf_kernels.h5,这个才是关键;
3.1 用的下载下来的原图
3.1.1查看预测结果
2020-04-25 21:30:40.596897: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): Host, Default Version
convertible : 52.4596631526947 和预测结果也是敞篷车高
sports_car : 37.61267066001892
pickup : 3.1751204282045364
car_wheel : 1.817500777542591
minivan : 1.7487037926912308
3.2 用自己的图片1
3.2.1查看预测结果
sports_car : 25.477179884910583 跑车
minivan : 14.489199221134186
racer : 10.40230467915535
fire_engine : 9.233023971319199
cab : 7.818394154310226
3.3 用自己的图片2
3.3.1查看预测结果
2020-04-25 21:42:48.617149: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2020-04-25 21:42:48.637912: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x7fb517e548b0 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2020-04-25 21:42:48.637930: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): Host, Default Version
pickup : 76.76142454147339 皮卡
grille : 5.927639454603195
jeep : 5.360781773924828
car_wheel : 3.3029526472091675
tow_truck : 1.787203922867775
4.循环进行判断车辆的照片
from imageai.Prediction import ImagePrediction
import os
execution_path = os.getcwd()
multiple_prediction = ImagePrediction()
multiple_prediction.setModelTypeAsResNet()
multiple_prediction.setModelPath(os.path.join(execution_path, "resnet50_weights_tf_dim_ordering_tf_kernels.h5"))
multiple_prediction.loadModel()
all_images_array = []
all_files = os.listdir(execution_path)
for each_file in all_files:
if(each_file.endswith(".jpg") or each_file.endswith(".png")):
all_images_array.append(each_file)
print(all_images_array)
results_array = multiple_prediction.predictMultipleImages(all_images_array, result_count_per_image=5)
for each_result in results_array:
predictions, percentage_probabilities = each_result["predictions"], each_result["percentage_probabilities"]
for index in range(len(predictions)):
print(predictions[index] , " : " , percentage_probabilities[index])
print("-----------------------")
4.1 运行结果
['2.jpg', '3.jpg', '1.jpg']
sports_car : 25.477179884910583
minivan : 14.489199221134186
racer : 10.40230467915535
fire_engine : 9.233023971319199
cab : 7.818394154310226
-----------------------
pickup : 76.76142454147339
grille : 5.927639454603195
jeep : 5.360781773924828
car_wheel : 3.3029526472091675
tow_truck : 1.787203922867775
-----------------------
convertible : 52.4596631526947
sports_car : 37.61267066001892
pickup : 3.1751204282045364
car_wheel : 1.817500777542591
minivan : 1.7487037926912308
-----------------------