当一副图像放在我们面前时,

目标检测用灰度图 目标检测实现_目标检测

依赖于我们强大的大脑,几乎可以立即识别出其中包含的物体。而机器却需要花费大量时间和训练数据来识别这些物体。但随着硬件和深度学习的进步,这件事变得简单了。

以下面的图像为例,机器现在能够以令人难以置信的准确率识别图像中的不同物体。

目标检测用灰度图 目标检测实现_目标检测_02

物体检测在各种不同的领域得到了迅速的采用。具体来说,它可以帮助自动驾驶汽车安全地穿过人群、检测零件质量、人脸检测等等。

那么,什么是目标检测呢?

比如,我们需要给自动驾驶汽车做一个行人检测系统,假如此时汽车捕获的图像如下图所示,

目标检测用灰度图 目标检测实现_ide_03

图像显示我们的汽车正在靠近一个广场,一部分人正在车前过马路。由于交通标志可能不是那么清晰,汽车的行人检测系统此时应该准确识别人行走的位置,以便我们的汽车可以避开他们。

那么汽车系统怎么做以确认人的位置呢?它可以在人的周围创建一个边界框,以便系统确定人在图像中位置,然后再相应的决定怎么走,避免意外发生。

目标检测用灰度图 目标检测实现_github_04

我们做目标检测的目的有两个:

  • 识别图像中存在的所有对象及其位置
  • 过滤出需要关注的对象(比如,突然出现的小车)

本文我们将使用ImageAI库(https://github.com/OlafenwaMoses/ImageAI)实现目标检测。

为了使用ImageAI做目标检测,我们做一些准备工作:

  1. 安装Python
  2. 安装ImageAI及其相关依赖
  3. 下载目标检测模型文件
  4. 写代码
1. 安装Python

直接去https://www.python.org/,下载Python 3安装即可。

2. 安装ImageAI及其相关依赖

使用pip安装以下相关依赖:

pip install numpy
pip install scipy
pip install opencv-python
pip install tensorflow
pip install pillow
pip install matplotlib
pip install h5py
pip install keras
pip install https://github.com/OlafenwaMoses/ImageAI/releases/download/2.0.2/imageai-2.0.2-py3-none-any.whl
3. 下载目标检测模型文件

通过 https://github.com/OlafenwaMoses/ImageAI/releases/download/1.0/resnet50_coco_best_v2.0.1.h5 下载目标检测模型文件。

将上述准备工作完成后,我们就可以着手写代码啦!为了方便起见,我们可以创建一个名为object_detection.py的文件并把它和模型文件放在一个目录中。

目标检测用灰度图 目标检测实现_目标检测_05

from imageai.Detection import ObjectDetection
import os

model_name = 'resnet50_coco_best_v2.0.1.h5'
input_image = 'demo.jpg'
output_image = 'output.jpg'

execution_path = os.getcwd()

detector = ObjectDetection()
detector.setModelTypeAsRetinaNet()
detector.setModelPath( os.path.join(execution_path , model_name))
detector.loadModel()
detections = detector.detectObjectsFromImage(input_image=os.path.join(execution_path , input_image), output_image_path=os.path.join(execution_path , output_image))

for eachObject in detections:
    print(eachObject["name"] + " : " + str(eachObject["percentage_probability"]))
    print("-" * 50)
输入图像

目标检测用灰度图 目标检测实现_目标检测用灰度图_06

输出图像

目标检测用灰度图 目标检测实现_目标检测用灰度图_07

打印输出内容:

car : 51.269686222076416
--------------------------------------------------
person : 89.36232924461365
--------------------------------------------------
person : 59.20940041542053
--------------------------------------------------
person : 98.74696731567383
--------------------------------------------------

当然,我们也可以对视频进行目标检测!

from imageai.Detection import VideoObjectDetection
import os

model_name = 'resnet50_coco_best_v2.0.1.h5'
input_video = 'demo.mp4'
output_video = 'output'

execution_path = os.getcwd()

detector = VideoObjectDetection()
detector.setModelTypeAsRetinaNet()
detector.setModelPath( os.path.join(execution_path, model_name))
detector.loadModel()

video_path = detector.detectObjectsFromVideo(input_file_path=os.path.join(execution_path, input_video), output_file_path=os.path.join(execution_path, output_video), frames_per_second=20, log_progress=True)
print(video_path)
输入视频

目标检测用灰度图 目标检测实现_目标检测_08

输出视频

目标检测用灰度图 目标检测实现_目标检测用灰度图_09

参考

[1] https://www.analyticsvidhya.com/blog/2018/06/understanding-building-object-detection-model-python/

[2] https://github.com/OlafenwaMoses/ImageAI