【TensorFlow+python3+ImageAI】十行代码实现目标检测 - Go语言中文社区

【TensorFlow+python3+ImageAI】十行代码实现目标检测


作者 | Moses Olafenwa

https://github.com/OlafenwaMoses/ImageAI

https://blog.csdn.net/guleileo/article/details/80767966

Dependencies

To use  ImageAI  in your application developments, you must have installed the following dependencies before you install  ImageAI  :


- Python 3.5.1 (and later versions) Download (Support for Python 2.7 coming soon) 
- pip3 Install 
- Tensorflow 1.4.0 (and later versions) Install or install via pip

 pip3 install --upgrade tensorflow 
- Numpy 1.13.1 (and later versions)  Install  or install via pip
 pip3 install numpy 
- SciPy 0.19.1 (and later versions)  Install  or install via pip
 pip3 install scipy 
- OpenCV  Install  or install via pip
 pip3 install opencv-python 
- Pillow  Install  or install via pip
 pip3 install pillow 
- Matplotlib  Install  or install via pip
 pip3 install matplotlib 
- h5py  Install  or install via pip
 pip3 install h5py 
- Keras 2.x  Install  or install via pip
 pip3 install keras 

Installation

To install ImageAI, run the python installation instruction below in the command line: 

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


or download the Python Wheel imageai-2.0.1-py3-none-any.whl and run the python installation instruction in the command line to the path of the file like the one below: 

pip3 install C:UserMyUserDownloadsimageai-2.0.1-py3-none-any.whl 

现在,你已经安装了需要的依赖库。接下来,你就可以编写第一段目标检测代码了。创建一个 Python 文件并为其命名 (例如,FirstDetection.py),然后写入下面的 10 行代码,并将 RetinaNet 模型文件和需要检测的图像复制到包含这个 python 文件的文件夹中。

FirstDetection.py

from imageai.Detection import ObjectDetectionimport osexecution_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"] )

然后运行代码,稍等片刻结果将显示在控制台中。一旦控制台打印出结果后,转到 FirstDetection.py 所在的文件夹,你将找到所保存的新图像。如下是两个原图像样本,检测后将保存新图像。

Before Detection:

640?wx_fmt=jpeg

Image Credit: alzheimers.co.uk

640?wx_fmt=jpeg

Image Credit: Wikicommons

After Detection:

640?wx_fmt=png

控制台打印的检测结果:

person : 55.8402955532074

person : 53.21805477142334

person : 69.25139427185059

person : 76.41745209693909

bicycle : 80.30363917350769

person : 83.58567953109741

person : 89.06581997871399

truck : 63.10953497886658

person : 69.82483863830566

person : 77.11606621742249

bus : 98.00949096679688

truck : 84.02870297431946

car : 71.98476791381836

640?wx_fmt=jpeg

控制台打印的检测结果:

person : 71.10445499420166

person : 59.28672552108765

person : 59.61582064628601

person : 75.86382627487183

motorcycle : 60.1050078868866

bus : 99.39600229263306

car : 74.05484318733215

person : 67.31776595115662

person : 63.53200078010559

person : 78.2265305519104

person : 62.880998849868774

person : 72.93365597724915

person : 60.01397967338562

person : 81.05944991111755

motorcycle : 50.591760873794556

motorcycle : 58.719027042388916

person : 71.69321775436401

bicycle : 91.86570048332214

motorcycle : 85.38855314254761

现在,我们来解释下这 10 行代码是如何工作的。

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"] )


在上面的2行代码中,第一行迭代执行 detector.detectObjectsFromImage 函数并返回所有的结果,然后在第二行打印出所检测到的每个目标的名称及其概率值。


ImageAI 支持许多强大的目标检测过程。其中之一就是能够提取图像中检测到的每个目标。如下所示,通过简单地解析将 extra_detected_objects = True 变为 detectObjectsFromImage 函数,目标检测类将为图像目标创建一个新的文件夹,提取每张图像,并将每张图像保存到新创建的文件夹中,同时返回一个包含每张图像路径的额外数组。


detections, extracted_images = detector.detectObjectsFromImage(input_image=os.path.join(execution_path , "image.jpg"), output_image_path=os.path.join(execution_path , "imagenew.jpg"), extract_detected_objects=True)


下面我们来看看在第一张图像上取得的目标检测结果:


640?wx_fmt=png


所有包含行人的图像都能被提取出来了,我没有保存所有的目标,因为它们会占用太多不必要的空间。


ImageAI 还提供了更多功能,可用于定制和生产功能部署所需的目标检测任务。一些支持的功能如下:


  • Adjusting Minimum Probability:默认情况下,检测概率低于 50% 的对象将不会显示或报告。你可以增加高确定性目标的检测概率,或者在需要检测所有可能对象的情况下降低该概率值。

  • Custom Objects Detection:使用所提供的 CustomObject 类,如此检测类函数将打印出一个或几个唯一目标的检测结果。

  • Detection Speed:通过将检测速度设置为“fast”、“faster”和“fastest”,以便缩短目标检测所需的时间。

  • Input Types:你可以指定并解析图像的文件路径,Numpy 数组或图像文件流作为输入图像

  • Output Types:你可以指定 detectObjectsFromImage 函数所返回的图像格式,可以是以文件或 Numpy 数组的形式。


版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/HelloZEX/article/details/80811474
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2021-06-20 10:46:52
  • 阅读 ( 691 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢