125 OpenCV DNN 基于SSD实现实时视频检测
代码
import cv2 as cv
model_bin = "../models/ssd/MobileNetSSD_deploy.caffemodel";
config_text = "../models/ssd/MobileNetSSD_deploy.prototxt";
objName = ["background",
"aeroplane", "bicycle", "bird", "boat",
"bottle", "bus", "car", "cat", "chair",
"cow", "diningtable", "dog", "horse",
"motorbike", "person", "pottedplant",
"sheep", "sofa", "train", "tvmonitor"];
# load caffe model
net = cv.dnn.readNetFromCaffe(config_text, model_bin)
# 获得所有层名称与索引
layerNames = net.getLayerNames()
lastLayerId = net.getLayerId(layerNames[-1])
lastLayer = net.getLayer(lastLayerId)
print(lastLayer.type)
# 检测
# cap = cv.VideoCapture(0)
cap = cv.VideoCapture("../images/sample.mp4")
while True:
ret, frame = cap.read()
if ret is False:
break
h, w = frame.shape[:2]
blobImage = cv.dnn.blobFromImage(frame, 0.007843, (300, 300), (127.5, 127.5, 127.5), True, False);
net.setInput(blobImage)
cvOut = net.forward()
for detection in cvOut[0,0,:,:]:
score = float(detection[2])
objIndex = int(detection[1])
if score > 0.5:
left = detection[3]*w
top = detection[4]*h
right = detection[5]*w
bottom = detection[6]*h
# 绘制
cv.rectangle(frame, (int(left), int(top)), (int(right), int(bottom)), (255, 0, 0), thickness=2)
cv.putText(frame, "score:%.2f, %s"%(score, objName[objIndex]),
(int(left) - 10, int(top) - 5), cv.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2, 8);
cv.imshow('video-ssd-demo', frame)
c = cv.waitKey(10)
if c == 27:
break
cv.waitKey(0)
cv.destroyAllWindows()
实验结果
解释
OpenCV DNN模块支持常见得对象检测模型SSD, 以及它的移动版Mobile Net-SSD,特别是后者在端侧边缘设备上可以实时计算,基于Caffe训练好的mobile-net SSD支持20类别对象检测。
SSD的mobilenet版本不仅可以检测图像,还可以检测视频,达到稳定实时的效果,基于124的分享内容,我稍微做了一下改动实现了在Python与C++中的基于SSD视频对象检测的代码,详细参见源码zip文件即可。
介绍一个API, 获取网络各层执行时间与总的执行时间API:
retval, timings = cv.dnn_Net.getPerfProfile( )
retval
网络执行推断的总时间timings
网络对应的各层执行时间
所有内容均来源于贾志刚老师的知识星球——OpenCV研习社,本文为个人整理学习,已获得贾老师授权,有兴趣、有能力的可以加入贾老师的知识星球进行深入学习。