Auto Byte

专注未来出行及智能汽车科技

微信扫一扫获取更多资讯

Science AI

关注人工智能与其他前沿技术、基础学科的交叉研究与融合发展

微信扫一扫获取更多资讯

深度学习 + OpenCV,Python实现实时视频目标检测

使用 OpenCV 和 Python 上对实时视频流进行深度学习目标检测是非常简单的,我们只需要组合一些合适的代码,接入实时视频,随后加入原有的目标检测功能。

在本文中我们将学习如何扩展原有的目标检测项目,使用深度学习和 OpenCV 将应用范围扩展到实时视频流和视频文件中。这个任务会通过 VideoStream 类来完成。

  • 深度学习目标检测教程:http://www.pyimagesearch.com/2017/09/11/object-detection-with-deep-learning-and-opencv/
  • VideoStream 类教程:http://www.pyimagesearch.com/2016/01/04/unifying-picamera-and-cv2-videocapture-into-a-single-class-with-opencv/

现在,我们将开始把深度学习+目标检测的代码应用于视频流中,同时测量 FPS 处理速度。

使用深度学习和 OpenCV 进行视频目标检测

为了构建基于 OpenCV 深度学习的实时目标检测器,我们需要有效地接入摄像头/视频流,并将目标检测应用到每一帧里。

首先,我们打开一个新文件,将其命名为 real_time_object_detection.py,随后加入以下代码:

# import the necessary packages
from imutils.video import VideoStream
from imutils.video import FPS
import numpy as np
import argparse
import imutils
import time
import cv2

我们从第 2-8 行开始导入封包。在此之前,你需要 imutils 和 OpenCV 3.3。在系统设置上,你只需要以默认设置安装 OpenCV 即可(同时确保你遵循了所有 Python 虚拟环境命令)。

Note:请确保自己下载和安装的是 OpenCV 3.3(或更新版本)和 OpenCV-contrib 版本(适用于 OpenCV 3.3),以保证其中包含有深度神经网络模块。

下面,我们将解析这些命令行参数:

# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-p", "--prototxt", required=True,
	help="path to Caffe'deploy'prototxt file")
ap.add_argument("-m", "--model", required=True,
	help="path to Caffe pre-trained model")
ap.add_argument("-c", "--confidence", type=float, default=0.2,
	help="minimum probability to filter weak detections")
args = vars(ap.parse_args())

与此前的目标检测项目相比,我们不需要图像参数,因为在这里我们处理的是视频流和视频——除了以下参数保持不变:

--prototxt:Caffe prototxt 文件路径。

--model:预训练模型的路径。

--confidence:过滤弱检测的最小概率阈值,默认值为 20%。

随后,我们初始化类列表和颜色集:

# initialize the list of class labels MobileNet SSD was trained to
# detect, then generate a set of bounding box colors for each class
CLASSES = ["background", "aeroplane", "bicycle", "bird", "boat",
	"bottle", "bus", "car", "cat", "chair", "cow", "diningtable",
	"dog", "horse", "motorbike", "person", "pottedplant", "sheep",
	"sofa", "train", "tvmonitor"]
COLORS = np.random.uniform(0, 255, size=(len(CLASSES), 3))

在第 22-26 行,我们初始化 CLASS 标签,和相应的随机 COLORS。有关这些类的详细信息(以及网络的训练方式),请参考:http://www.pyimagesearch.com/2017/09/11/object-detection-with-deep-learning-and-opencv/

现在,我们加载自己的模型,并设置自己的视频流:

# load our serialized model from disk
print("[INFO] loading model...")
net = cv2.dnn.readNetFromCaffe(args["prototxt"], args["model"])
 
# initialize the video stream, allow the cammera sensor to warmup,
# and initialize the FPS counter
print("[INFO] starting video stream...")
vs = VideoStream(src=1).start()
time.sleep(2.0)
fps = FPS().start()

我们加载自己的序列化模型,提供对自己的 prototxt 和模型文件的引用(第 30 行),可以看到在 OpenCV 3.3 中,这非常简单。

下一步,我们初始化视频流(来源可以是视频文件或摄像头)。首先,我们启动 VideoStream(第 35 行),随后等待相机启动(第 36 行),最后开始每秒帧数计算(第 37 行)。VideoStream 和 FPS 类是 imutils 包的一部分。

现在,让我们遍历每一帧(如果你对速度要求很高,也可以跳过一些帧):

# loop over the frames from the video stream
while True:
	# grab the frame from the threaded video stream and resize it
	# to have a maximum width of 400 pixels
	frame = vs.read()
	frame = imutils.resize(frame, width=400)
 
	# grab the frame dimensions and convert it to a blob
	(h, w) = frame.shape[:2]
	blob = cv2.dnn.blobFromImage(frame, 0.007843, (300, 300), 127.5)
 
	# pass the blob through the network and obtain the detections and
	# predictions
	net.setInput(blob)
	detections = net.forward()

首先,我们从视频流中读取一帧(第 43 行),随后调整它的大小(第 44 行)。由于我们随后会需要宽度和高度,所以我们在第 47 行上进行抓取。随后将 frame 转换为一个有 dnn 模块的 blob(第 48 行)。

现在,我们设置 blob 为神经网络的输入(第 52 行),通过 net 传递输入(第 53 行),这给我们提供了 detections。

这时,我们已经在输入帧中检测到了目标,现在是时候看看置信度的值,以判断我们能否在目标周围绘制边界框和标签了:

# loop over the detections
	for i in np.arange(0, detections.shape[2]):
		# extract the confidence (i.e., probability) associated with
		# the prediction
		confidence = detections[0, 0, i, 2]
 
		# filter out weak detections by ensuring the `confidence` is
		# greater than the minimum confidence
		if confidence > args["confidence"]:
			# extract the index of the class label from the
			# `detections`, then compute the (x, y)-coordinates of
			# the bounding box for the object
			idx = int(detections[0, 0, i, 1])
			box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
			(startX, startY, endX, endY) = box.astype("int")
 
			# draw the prediction on the frame
			label = "{}: {:.2f}%".format(CLASSES[idx],
				confidence * 100)
			cv2.rectangle(frame, (startX, startY), (endX, endY),
				COLORS[idx], 2)
			y = startY - 15 if startY - 15 > 15 else startY + 15
			cv2.putText(frame, label, (startX, y),
				cv2.FONT_HERSHEY_SIMPLEX, 0.5, COLORS[idx], 2)

我们首先在 detections 内循环,记住一个图像中可以检测到多个目标。我们还需要检查每次检测的置信度(即概率)。如果置信度足够高(高于阈值),那么我们将在终端展示预测,并以文本和彩色边界框的形式对图像作出预测。让我们逐行来看一下:

在 detections 内循环,首先我们提取 confidence 值(第 59 行)。

如果 confidence 高于最低阈值(第 63 行),那么我们提取类标签索引(第 67 行),并计算检测到的目标的坐标(第 68 行)。

然后,我们提取边界框的 (x, y) 坐标(第 69 行),之后将用于绘制矩形和文本。

我们构建一个文本 label,包含 CLASS 名称和 confidence(第 72、73 行)。

我们还要使用类颜色和之前提取的 (x, y) 坐标在物体周围绘制彩色矩形(第 74、75 行)。

通常,我们希望标签出现在矩形上方,但是如果没有空间,我们将在矩形顶部稍下的位置展示标签(第 76 行)。

最后,我们使用刚才计算出的 y 值将彩色文本置于帧上(第 77、78 行)。

帧捕捉循环剩余的步骤还包括:(1)展示帧;(2)检查 quit 键;(3)更新 fps 计数器:

# show the output frame
	cv2.imshow("Frame", frame)
	key = cv2.waitKey(1) & 0xFF
 
	# if the `q` key was pressed, break from the loop
	if key == ord("q"):
		break
 
	# update the FPS counter
	fps.update()

上述代码块简单明了,首先我们展示帧(第 81 行),然后找到特定按键(第 82 行),同时检查「q」键(代表「quit」)是否按下。如果已经按下,则我们退出帧捕捉循环(第 85、86 行)。最后更新 fps 计数器(第 89 行)。

如果我们退出了循环(「q」键或视频流结束),我们还要处理这些:

# stop the timer and display FPS information
fps.stop()
print("[INFO] elapsed time: {:.2f}".format(fps.elapsed()))
print("[INFO] approx. FPS: {:.2f}".format(fps.fps()))
 
# do a bit of cleanup
cv2.destroyAllWindows()
vs.stop()

当我们跳出(exit)循环,fps 计数器 停止(第 92 行),每秒帧数的信息向终端输出(第 93、94 行)。

我们关闭窗口(第 97 行),然后停止视频流(第 98 行)。

如果你到了这一步,那就可以做好准备用自己的网络摄像头试试看它是如何工作的了。我们来看下一部分。

实时深度学习目标检测的结果

为了实时深度学习目标检测器正常运行,确保你使用本指南「Downloads」部分中的示例代码和预训练的卷积神经网络。(请打开原文链接,进入「Downloads」部分,输入自己的邮箱地址,获取所需代码和其他资料。)

打开终端,执行下列命令:

$ python real_time_object_detection.py \
	--prototxt MobileNetSSD_deploy.prototxt.txt \
	--model MobileNetSSD_deploy.caffemodel
[INFO] loading model...
[INFO] starting video stream...
[INFO] elapsed time: 55.07
[INFO] approx. FPS: 6.54

如果 OpenCV 能够访问你的摄像头,你可以看到带有检测到的目标的输出视频帧。我对样本视频使用了深度学习目标检测,结果如下:


图 1:使用深度学习和 OpenCV + Python 进行实时目标检测的短片。


注意深度学习目标检测器不仅能够检测到人,还能检测到人坐着的沙发和旁边的椅子——所有都是实时检测到的!

总结

今天的博客中,我们学习了如何使用深度学习 + OpenCV + 视频流来执行实时目标检测。我们通过下列两个教程完成了这一目标:

1. 使用深度学习和 OpenCV 进行目标检测(http://www.pyimagesearch.com/2017/09/11/object-detection-with-deep-learning-and-opencv/)

2. 在 OpenCV 上进行高效、线程化的视频流(http://www.pyimagesearch.com/2016/01/04/unifying-picamera-and-cv2-videocapture-into-a-single-class-with-opencv/)

最终结果是基于深度学习的目标检测器可以处理 6-8 个 FPS 的视频(当然,这也取决于你的系统速度)。

你还可以通过以下途径进一步提升速度:

1. 跳过帧。

2. 使用 MobileNet 的不同变体(速度更快,但是准确率下降)。

3. 使用 SqueezeNet 的量子化变体(我还未对此进行测试,但是我想应该会更快,因为它的网络足迹更小)。

工程工程实现目标检测视频OpenCV
131
博主,请问我出现 File "real_time_object_detection.py", line 30, in <module> net = cv2.dnn.readNetFromCaffe(args["prototxt"], args["model"]) AttributeError: 'module' object has no attribute 'dnn' 是什么原因呢