只需10行Python代码,我们就能实现计算机视觉中目标检测。

from imageai.Detection import ObjectDetection
import os
execution_path = os.getcwd()
detector = ObjectDetection()
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 , "image.jpg"), output_image_path=os.path.join(execution_path , "imagenew.jpg"))
for eachObject in detections:
print(eachObject["name"] + " : " + eachObject["percentage_probability"] )

没错,用这寥寥10行代码,就能实现目前AI产品中应用广泛的目标检测技术。

看完了代码,下面容我们聊聊目标检测背后的技术背景,并解读这10行Python代码的由来和实现原理。

如何借助ImageAI轻松实现目标检测

使用ImageAI执行目标检测,你只需以下4步:

1.在电脑上安装Python

2.安装ImageAI及其环境依赖

3.下载目标检测模块文件

4.运行示例代码,就是我们展示的那10行

下面我们一步步详细讲解。

1)从Python官网下载和安装Python 3

2)通过pip安装如下环境依赖

1.Tensorflow

pip install tensorflow

2.Numpy

pip install numpy

3.SciPy

pip install scipy

4.OpenCV

pip install opencv-python

5.Pillow

pip install pillow

6.Matplotlib

pip install matplotlib

7.H5py

pip install h5py

8.Keras

pip install keras

9.ImageAI

pip install

3)通过该 链接下载RetinaNet 模型文件用于目标检测。

到了这里我们已经安装好了所有依赖,就可以准备写自己的首个目标检测代码了。 创建一个Python文件,为其命名(比如FirstDetection.py),然后将如下代码写到文件中,再把RetinaNet模型文件以及你想检测的图像拷贝到包含该Python文件的文件夹里。

运行代码,等待控制台打印结果。等控制台打印出结果后,就可以打开FirstDetection.py所在的文件夹,你就会发现有新的图像保存在了里面。比如下面两张示例图像,以及执行目标检测后保存的两张新图像。

目标检测之前:

from imageai.Detection import ObjectDetection

import os

execution_path = os.getcwd()

在上面3行代码中,我们在第一行导入了ImageAI目标检测类,在第二行导入Python os类,在第三行定义了一个变量,获取通往我们的Python文件、RetinaNet模型文件和图像所在文件夹的路径。

detector = ObjectDetection()

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 , "image.jpg"), output_image_path=os.path.join(execution_path , "imagenew.jpg"))

在上面5行代码中,我们在第一行定义我们的目标检测类,在第二行设定RetinaNet的模型类型,在第三行将模型路径设置为RetinaNet模型的路径,在第四行将模型加载到目标检测类中,然后我们在第五行调用检测函数,并在输入和输出图像路径中进行解析。

for eachObject in detections:

print(eachObject["name"] + " : " + eachObject["percentage_probability"] )

在上面两行代码中,我们迭代了第一行中detector.detectObjectFromImage函数返回的所有结果,然后打印出第二行中模型对图像上每个物体的检测结果(名称和概率)。

ImageAI支持很多强大的目标检测自定义功能,其中一项就是能够提取在图像上检测到的每个物体的图像。只需将附加参数extract_detected_objects=True解析为detectObjectsFromImage函数,如下所示,目标检测类就会为图像物体创建一个文件夹,提取每张图像,将它们保存在新创建的文件夹中,并返回一个包含通过每张图像的路径的额外数组。