十行代码搞定目标检测

十行代码搞定目标检测插图

大数据文摘出品

编译:邢畅、宁静

计算机视觉是人工智能的一个重要领域,是关于计算机和软件系统的科学,可以对图像和场景进行识别、理解。计算机视觉还包括图像识别、目标检测、图像生成、图像超分辨率重建等多个领域。由于存在大量的实际需求,目标检测可能是计算机视觉中最有意义的领域。

目标检测是指计算机和软件系统对图像或场景中的目标进行定位和识别的任务。目标检测已广泛应用于人脸检测、车辆检测、人流量统计、网络图像、安防系统和无人驾驶等多个领域。

在应用或系统中使用目标检测方法,以及基于这些方法构建新的应用都不是简单的任务。早期目标检测的实现基于经典算法,比如流行的计算机视觉库OpenCV中支持的算法。然而,这些经典算法在不同的条件下无法获得稳定的性能。

2012年深度学习的突破性进展和迅速普及,使得R-CNN、Fast-RCNN、Faster-RCNN、RetinaNet以及快速、高度准确的SSD、YOLO等目标检测算法应运而生。这些基于深度学习、机器学习的算法,需要一定的数学以及深度学习框架基础。有数百万的专业计算机程序员和软件开发人员想要集成和创建基于目标检测算法的新产品。同时由于理解和实际使用较为复杂,一直无法实现。如何开发出高效的目标检测代码呢?ImageAI就应运而生了

 十行代码搞定目标检测插图(1)

ImageAI让代码变得简洁

ImageAI是一个python库,只需要几行代码,就可以让程序员和软件开发人员轻松地将最先进的计算机视觉技术集成到他们现有的或新的应用中,ImageAI已经在Github上开源。

Github地址:
https://github.com/OlafenwaMoses/ImageAI

十行代码搞定目标检测插图(2)

ImageAI目前支持使用在ImageNet-1000数据集上训练的4种不同机器学习算法进行图像预测和训练。ImageAI还支持使用在COCO数据集上训练的RetinaNet,YOLOv3和TinyYOLOv3进行对象检测,视频检测和对象跟踪。最后,ImageAI允许训练自定义模型,以执行新目标的检测和识别。

ImageAI库有依赖其他Python库,所以在使用ImageAI开发之前还需要导入其他的包。

准备工作

使用ImageAI实现目标检测,只需进行以下4步:

  1. 在你的电脑上安装Python
  2. 安装ImageAI,配置依赖环境
  3. 下载目标检测模块文件
  4. 运行样例代码(只需10行)
下面一步步详细展开:
1)从Python官网下载并安装Python3
链接地址:

https://python.org

2)用pip命令安装以下依赖包:
i. Tensorflow

1
<span class="code-snippet_outer"><span class="code-snippet__attribute">pip</span> install tensorflow</span>
ii. Numpy

1
<span class="code-snippet_outer"><span class="code-snippet__attribute">pip</span> install numpy</span>
iii. SciPy

1
<span class="code-snippet_outer"><span class="code-snippet__attribute">pip</span> install scipy</span>
iv. OpenCV

1
<span class="code-snippet_outer"><span class="code-snippet__attribute">pip</span> install opencv-python</span>
v. Pillow

1
<span class="code-snippet_outer"><span class="code-snippet__attribute">pip</span> install pillow</span>
vi. Matplotlib

1
<span class="code-snippet_outer"><span class="code-snippet__attribute">pip</span> install matplotlib</span>
vii. H5py

1
<span class="code-snippet_outer"><span class="code-snippet__attribute">pip</span> install h5py</span>
viii. Keras

1
<span class="code-snippet_outer"><span class="code-snippet__attribute">pip</span> install keras</span>
ix. ImageAI

1
<span class="code-snippet_outer">pip3 <span class="code-snippet__keyword">install</span> imageai <span class="code-snippet__comment">--upgrade</span></span>
注意:第一次安装ImageAI库,需要下载对应版本的.whl文件,文摘菌的电脑是Python3的环境,所以下载了imageai-2.0.2-py3-none-any.whl 文件,然后转移到相应的文件夹下,执行如下命令即可安装:

1
<span class="code-snippet_outer"><span class="code-snippet__selector-tag">pip</span> <span class="code-snippet__selector-tag">install</span> <span class="code-snippet__selector-tag">imageai-2</span><span class="code-snippet__selector-class">.0</span><span class="code-snippet__selector-class">.2-py3-none-any</span><span class="code-snippet__selector-class">.whl</span></span>
.whl文件链接地址:

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

3)下载用于目标检测的RetinaNet模型文件

链接地址:

https://github.com/OlafenwaMoses/ImageAI/releases/download/1.0/resnet50_coco_best_v2.0.1.h5

开启10行代码的目标检测
到这里你已经安装好了所有的依赖项,可以开始编写你的第一个目标检测的代码了。

创建一个Python文件并命名(如FirstDetection.py),然后将下面的代码写入该文件。将RetinaNet模型文件和要检测的图像复制到包含Python文件的文件夹中。

FirstDetection.py:

1
<span class="code-snippet_outer">from imageai.<span class="code-snippet__type">Detection</span> <span class="code-snippet__keyword">import</span> ObjectDetection</span>
1
<span class="code-snippet_outer"><span class="code-snippet__keyword">import</span> os</span>
1
<span class="code-snippet_outer"><br></span>
1
<span class="code-snippet_outer"><br></span>
1
<span class="code-snippet_outer">execution_path = os.getcwd()</span>
1
<span class="code-snippet_outer"><br></span>
1
<span class="code-snippet_outer"><br></span>
1
<span class="code-snippet_outer">detector = <span class="code-snippet__type">ObjectDetection</span>()</span>
1
<span class="code-snippet_outer">detector.setModelTypeAsRetinaNet()</span>
1
<span class="code-snippet_outer">detector.setModelPath( os.path.<span class="code-snippet__built_in">join</span>(execution_path , <span class="code-snippet__string">"resnet50_coco_best_v2.0.1.h5"</span>))</span>
1
<span class="code-snippet_outer">detector.loadModel()</span>
1
<span class="code-snippet_outer">detections = detector.detectObjectsFromImage(input_image=os.path.<span class="code-snippet__built_in">join</span>(execution_path , <span class="code-snippet__string">"image.jpg"</span>), output_image_path=os.path.<span class="code-snippet__built_in">join</span>(execution_path , <span class="code-snippet__string">"imagenew.jpg"</span>))</span>
1
<span class="code-snippet_outer"><br></span>
1
<span class="code-snippet_outer"><br></span>
1
<span class="code-snippet_outer"><span class="code-snippet__keyword">for</span> eachObject <span class="code-snippet__keyword">in</span> detections:</span>
1
<span class="code-snippet_outer">   <span class="code-snippet__built_in">print</span>(eachObject[<span class="code-snippet__string">"name"</span>] , <span class="code-snippet__string">" : "</span> , eachObject[<span class="code-snippet__string">"percentage_probability"</span>] )</span>
然后运行代码,等待结果输出。结果显示后,就可以在FirstDetection.py所在的文件夹下找到保存下来的新图像。下面有两个新图像的示例。

目标检测前:

十行代码搞定目标检测插图(3)

图片来源:alzheimers.co.uk

十行代码搞定目标检测插图(4)

图片来源:Wikicommons

目标检测后:

十行代码搞定目标检测插图(5)
在Spyder编译器中运行结果如下所示:

 十行代码搞定目标检测插图(6)

输出的目标检测精度结果:
person  :  57.20391869544983
person  :  52.57977843284607
person  :  70.81094980239868
person  :  76.99859142303467
person  :  79.40077781677246
bicycle  :  81.0384213924408
person  :  83.66722464561462
person  :  89.41188454627991
truck  :  60.61040759086609
person  :  69.65749859809875
bus  :  97.92424440383911
truck  :  83.94358158111572
car  :  72.50492572784424

十行代码搞定目标检测插图(7)

在Spyder编译器中运行结果如下所示:

十行代码搞定目标检测插图(8)

输出的目标检测精度结果:
person  :  62.45866417884827
person  :  58.67737531661987
person  :  69.44932341575623
person  :  71.84218168258667
person  :  59.53381657600403
person  :  54.65759038925171
motorcycle  :  65.84504842758179
bus  :  99.40318465232849
car  :  72.41445779800415
person  :  58.32530856132507
person  :  54.449981451034546
person  :  80.11815547943115
person  :  74.30745959281921
person  :  77.78302431106567
person  :  71.15439772605896
bicycle  :  69.92427110671997
person  :  66.17957353591919
bicycle  :  90.50283432006836
motorcycle  :  94.09030675888062

短短10行代码就出了目标检测的结果,这也太快了吧!本着对真理探索的热情,在弄懂里面的原理后,文摘菌将在下文对每行代码进行解读。

知其然知其所以然:代码解读

下面来解释一下这10行代码的工作原理:

1
<span class="code-snippet_outer">from imageai.<span class="code-snippet__type">Detection</span> <span class="code-snippet__keyword">import</span> ObjectDetection</span>
1
<span class="code-snippet_outer"><span class="code-snippet__keyword">import</span> os</span>
1
<span class="code-snippet_outer">execution_path = os.getcwd()</span>

在以上3行代码中,在第一行导入了ImageAI的目标检测类;在第二行导入了Python的os类;在第三行中定义了一个变量,保存Python文件、RetinaNet模型文件以及图像所在文件夹的路径。

1
<span class="code-snippet_outer">detector = ObjectDetection()</span>
1
<span class="code-snippet_outer">detector.setModelTypeAsRetinaNet()</span>
1
<span class="code-snippet_outer">detector.setModelPath( os.path.join(execution_path , <span class="code-snippet__string">"resnet50_coco_best_v2.0.1.h5"</span>))</span>
1
<span class="code-snippet_outer">detector.loadModel()</span>
1
<span class="code-snippet_outer">detections = detector.detectObjectsFromImage(input_image=os.path.join(execution_path , <span class="code-snippet__string">"image.jpg"</span>), output_image_path=os.path.join(execution_path , <span class="code-snippet__string">"imagenew.jpg"</span>))</span>
在以上5行代码中,我们在第一行中定义了一个目标检测类的实例;在第二行中将实例的模型类型设定为RetinaNet;在第三行中将模型路径设置为RetinaNet模型的路径;在第四行中将模型加载到目标检测类的实例中;在第五行中调用检测函数,并将文件输入输出路径作为参数传入。

1
<span class="code-snippet_outer"><span class="code-snippet__keyword">for</span> eachObject <span class="code-snippet__keyword">in</span> detections:</span>
1
<span class="code-snippet_outer">   <span class="code-snippet__built_in">print</span>(eachObject[<span class="code-snippet__string">"name"</span>] , <span class="code-snippet__string">" : "</span> , eachObject[<span class="code-snippet__string">"percentage_probability"</span>] )</span>

在以上两行代码中,第一行用来对detector.detectObjectsFromImage 函数返回的所有结果进行迭代,第二行用来输出图片中检测到的每个目标的名字及其概率。

自定义目标检测

除此外,ImageAI也支持强大的自定义目标检测。其中之一是能够提取图像中检测到的每个物体。只需要将参数


1
<span class="code-snippet_outer"><span class="code-snippet__attr">extract_detected_objects</span>=<span class="code-snippet__literal">True</span></span>

传入


1
<span class="code-snippet_outer">detectObjectsFromImage</span>

函数中,如下所示,目标检测类将为图像对象创建一个文件夹,提取每个图像,将每个子图像保存到创建的新文件夹中,并返回一个包含每个图像路径的数组。


1
<span class="code-snippet_outer">detections, extracted_images = detector.detectObjectsFromImage(input_image=os.path.<span class="code-snippet__keyword">join</span>(execution_path , <span class="code-snippet__string">"image.jpg"</span>), output_image_path=os.path.<span class="code-snippet__keyword">join</span>(execution_path , <span class="code-snippet__string">"imagenew.jpg"</span>), extract_detected_objects=True)</span>
将其应用到第一张图片上,得到如下结果:

十行代码搞定目标检测插图(9)

所有行人都被很好地提取了出来,为了节约空间这里只显示了一部分。

ImageAI的其他功能

ImageAI提供了许多功能,可用于目标检测任务的自定义和部署。其支持的功能包括:
  • 调整最小概率:默认情况下,概率小于50%的物体不会显示。对于需要高精度的情况,可以增加此值;对于需要检测所有可能对象的情况,可以减少此值。
  • 自定义目标检测:通过提供的CustomObject类,可以使检测类检测一个或几个特定目标。
  • 检测速度通过将检测速度设置为“fast”、“faster”或“fastest”,可以减少检测图像所需的时间。

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

  • 输出类型:可指定detectObjectsFromImage函数以文件或Numpy数组的形式返回图像

你可以在上文给出的官方GitHub链接中找到如何使用上述功能以及ImageAI中包含的其他计算机视觉功能的详细信息和文档。

相关报道:

https://towardsdatascience.com/object-detection-with-10-lines-of-code-d6cb4d86f606


实习/全职编辑记者招聘ing

加入我们,亲身体验一家专业科技媒体采写的每个细节,在最有前景的行业,和一群遍布全球最优秀的人一起成长。坐标北京·清华东门,在大数据文摘主页对话页回复“招聘”了解详情。简历请直接发送至zz@bigdatadigest.cn


志愿者介绍
后台回复志愿者”加入我们


十行代码搞定目标检测插图(10)
十行代码搞定目标检测插图(11)
十行代码搞定目标检测插图(12)
点「在看」的人都变好看了哦

    已同步到看一看

    发送中

    点赞