ImageAI的设计秉承简单性,它支持一系列最新的机器学习算法,用于图像预测,自定义图像预测,目标检测,视频目标检测,视频目标跟踪和图像预测训练。
ImageAI当前使用在ImageNet-1000数据集上训练的4种不同的机器学习算法( SqueezeNet, ResNet, InceptionV3 and DenseNet)来支持图像识别预测和训练。
ImageAI还使用在COCO数据集上训练的RetinaNet,YOLOv3和TinyYOLOv3支持目标检测,视频目标检测和目标跟踪。最后,ImageAI允许训练自定义模型以执行新对象的检测和识别。
最终,ImageAI将为计算机视觉的更广泛,更专业的方面提供支持,包括但不限于在特殊环境和特定领域中的图像识别。
安装ImageAI首先要有相关的依赖库:
Python 3.5.1 (或之后的版本)
Tensorflow 1.4.0 (或之后的版本)
OpenCV
Keras 2.x
安装完依赖库之后就可以安装ImageAI了。打开终端,执行下面语句即可:
pip3 install imageai --upgrade
目前ImageAI的最新版本为2.1.5
Image Prediction可以实现识别任务。用于图像预测的4种算法包括SqueezeNet,ResNet,InceptionV3和DenseNet。这些算法中的每一个都有各自的模型文件,本次演示使用的是SqueezeNet模型文件。
直接上代码:
from imageai.Prediction import ImagePrediction
import os
# 当前路径 包含需要预测的图片,模型文件
execution_path = os.getcwd()
# 创建预测类
prediction = ImagePrediction()
# 设置预测模型 有以下四种:setModelTypeAsInceptionV3()、setModelTypeAsResNet()、setModelTypeAsDenseNet()、setModelTypeAsSqueezeNet()
# SqueezeNet
prediction.setModelTypeAsSqueezeNet()
prediction.setModelPath(os.path.join(execution_path, "squeezenet_weights_tf_dim_ordering_tf_kernels.h5"))
prediction.loadModel(prediction_speed="fast")
# 预测图片,以及结果预测输出数目
predictions, probabilities = prediction.predictImage(os.path.join(execution_path, "01.jpg"), result_count=5)
# 输出结果
for eachPrediction, eachProbability in zip(predictions, probabilities):
print(eachPrediction, " : ", eachProbability)
输入图片为
输出结果为置信度最高的前五名
现在,ImageAI支持3种输入类型,它们是图像文件的文件路径(默认),图像的numpy数组和图像文件流。要使用numpy数组或文件流输入执行图像预测,只需在.predictImage()函数(预测单张图片)或.predictMultipleImages()函数(预测多张图片,输入为列表)中声明输入类型
predictions, probabilities = prediction.predictImage(image_array, result_count=5 , input_type="array" ) # For numpy array input type
predictions, probabilities = prediction.predictImage(image_stream, result_count=5 , input_type="stream" ) # For file stream input type
ImageAI提供了非常方便且功能强大的方法来对图像执行对象检测并从图像中提取每个对象。对象检测类支持RetinaNet,YOLOv3和TinyYOLOv3。
下面是利用RetinaNet来进行目标检测:
from imageai.Detection import ObjectDetection
import os
# 当前路径 包含需要预测的图片,模型文件
execution_path = os.getcwd()
# 创建检测类
detector = ObjectDetection()
# 设置检测模型 有以下三种:setModelTypeAsYOLOv3,setModelTypeAsRetinaNet,setModelTypeAsTinyYOLOv3
detector.setModelTypeAsRetinaNet()
detector.setModelPath( os.path.join(execution_path , "resnet50_coco_best_v2.0.1.h5"))
detector.loadModel()
# 预测图片,为一字典,包括类别,置信度和位置信息
detections = detector.detectObjectsFromImage(input_image=os.path.join(execution_path , "02.jpg"), output_image_path=os.path.join(execution_path , "image2new.jpg"), minimum_percentage_probability=30)
for eachObject in detections:
print(eachObject["name"] , " : ", eachObject["percentage_probability"], " : ", eachObject["box_points"] )
输入图片:
输出结果为:
TinyYOLOv3来进行目标检测
detector = ObjectDetection()
detector.setModelTypeAsTinyYOLOv3()
detector.setModelPath( os.path.join(execution_path , "yolo-tiny.h5"))
detector.loadModel()
YOLOv3来进行目标检测
detector = ObjectDetection()
detector.setModelTypeAsYOLOv3()
detector.setModelPath( os.path.join(execution_path , "yolo.h5"))
detector.loadModel()
ImageAI现在支持摄像机输入,支持实时视频检测。使用OpenCV中的VideoCapture()功能,你可以从设备加载摄像头实时视频流ImageAI的detectObjectsFromVideo()和detectCustomObjectsFromVideo()功能。支持检测视频文件中对象的所有功能也可用于检测摄像机的实时视频源中的对象。
from imageai.Detection import VideoObjectDetection
import os
import cv2
execution_path = os.getcwd()
camera = cv2.VideoCapture(0)
detector = VideoObjectDetection()
detector.setModelTypeAsRetinaNet()
detector.setModelPath(os.path.join(execution_path , "resnet50_coco_best_v2.0.1.h5"))
detector.loadModel()
video_path = detector.detectObjectsFromVideo(
camera_input=camera,
output_file_path=os.path.join(execution_path, "camera_detected_video"),
frames_per_second=20, log_progress=True, minimum_percentage_probability=40)
(1)ImageAI可以实现的任务可以分为三大类:图像识别、目标检测、视频检测目标跟踪
RetinaNet,YOLOv3和TinyYOLOv3支持目标检测,视频目标检测和目标跟踪。 SqueezeNet, ResNet, InceptionV3 and DenseNet来支持图像识别预测和训练。
(2)ImagePrediction、ObjectDetection、VideoObjectDetection三个类分别对应可实现的三大任务:图像预测、目标检测、视频检测。
2.1–ImagePrediction类下的两个重要函数:
ImagePrediction.predictImage():预测单张图片
ImagePrediction.predictMultipleImages():预测多张图片
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("-----------------------")
2.2–ObjectDetection类下的两个重要函数:
ObjectDetection.detectObjectsFromImage():
# extract_detected_objects=True可提取检测到的目标存放到objects_path 中
detections, objects_path = detector.detectObjectsFromImage(input_image=os.path.join(execution_path , "image3.jpg"), output_image_path=os.path.join(execution_path , "image3new.jpg"), minimum_percentage_probability=30, display_percentage_probability=True, display_object_name=True,extract_detected_objects=True)
for eachObject, eachObjectPath in zip(detections, objects_path):
print(eachObject["name"] , " : " , eachObject["percentage_probability"], " : ", eachObject["box_points"] )
print("Object's image saved in " + eachObjectPath)
print("--------------------------------")
ObjectDetection…detectCustomObjectsFromImage():可进行自定义对象检测,屏蔽不想检测的对象
custom_objects = detector.CustomObjects(car=True, motorcycle=True)
detections = detector.detectCustomObjectsFromImage(custom_objects=custom_objects, input_image=os.path.join(execution_path , "image3.jpg"), output_image_path=os.path.join(execution_path , "image3custom.jpg"), minimum_percentage_probability=30)
更多信息可以参考ImageAI 中文文档