前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >用10行Python代码,实现AI目标检测技术!(Python是最好的语言)

用10行Python代码,实现AI目标检测技术!(Python是最好的语言)

作者头像
用户6133654
发布2019-10-10 11:40:03
6660
发布2019-10-10 11:40:03
举报
文章被收录于专栏:自学的仙叔自学的仙叔

今天为大家带来的内容是,用10行Python代码,实现AI目标检测技术!(Python是最好的语言),本文具有不错的参考意义,希望能够帮助到你!

提示:部分代码用图片方式呈现出来,方便观看及收藏,喜欢不忘关注点赞不迷路哦!

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

用10行Python代码,实现AI目标检测技术!(Python是最好的语言)

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

看完了代码,下面就来解读这10行Python代码的由来和实现原理。

我们开头所示的10行代码实现,就是要用到ImageAI。

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

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

1.在电脑上安装Python

2.安装ImageAI及其环境依赖

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

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

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

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

python.org/

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

1.Tensorflow

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">pip install tensorflow < /pre>

2.Numpy

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">pip install numpy < /pre>

3.SciPy

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">pip install scipy < /pre>

4.OpenCV

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">pip install opencv-python < /pre>

5.Pillow

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">pip install pillow < /pre>

6.Matplotlib

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">pip install matplotlib < /pre>

7.H5py

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">pip install h5py < /pre>

8.Keras

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">pip install keras < /pre>

9.ImageAI

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">pip install < /pre>

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

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

FirstDetection.py

用10行Python代码,实现AI目标检测技术!(Python是最好的语言)

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"></pre>

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

目标检测之前:

用10行Python代码,实现AI目标检测技术!(Python是最好的语言)

用10行Python代码,实现AI目标检测技术!(Python是最好的语言)

目标检测之后:

用10行Python代码,实现AI目标检测技术!(Python是最好的语言)

用10行Python代码,实现AI目标检测技术!(Python是最好的语言)

我们可以看到图像上显示了检测出的物体名称及概率。

解读10行代码

下面我们解释一下这10行代码的工作原理。

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">from imageai.Detection import ObjectDetection import os execution_path = os.getcwd() < /pre>

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

用10行Python代码,实现AI目标检测技术!(Python是最好的语言)

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

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">for eachObject in detections: print(eachObject["name"] + " : " + eachObject["percentage_probability"] ) < /pre>

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

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

用10行Python代码,实现AI目标检测技术!(Python是最好的语言)

我们用前面的第一张图像作为例子,可以得到图像中检测到的各个物体的单独图像:

用10行Python代码,实现AI目标检测技术!(Python是最好的语言)

ImageAI提供了很多功能,能够用于各类目标检测任务的自定义和生产部署(具体就不多介绍了)

诚然,单看这10行代码每一行,谈不上惊艳,也借助了不少库,但是仅用10行代码就能让我们轻松实现之前很麻烦的目标检测,还是能谈得上“给力”二字。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-10-05,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 自学的仙叔 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
图像识别
腾讯云图像识别基于深度学习等人工智能技术,提供车辆,物体及场景等检测和识别服务, 已上线产品子功能包含车辆识别,商品识别,宠物识别,文件封识别等,更多功能接口敬请期待。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档