Opencv DNN:基于SSD实现视频对象检测Python实现

Opencv DNN:基于SSD实现视频对象检测Python实现

一、内容

OpenCV DNN模块支持常见得对象检测模型SSD, 以及它的移动版Mobile Net-SSD,特别是后者在端侧边缘设备上可以实时计算,基于Caffe训练好的mobile-net SSD支持20类别对象检测。

训练好的模型我已经上传到百度云:
链接:https://pan.baidu.com/s/1zvIw1rkRvYqk33xwyAMjhg
提取码:n90t

使用模型实现预测的时候,需要读取图像作为输入,网络模型支持的输入数据是四维的输入,所以要把读取到的Mat对象转换为四维张量,OpenCV的提供的API为如下:
Mat cv::dnn::blobFromImage(
InputArray image,
double scalefactor = 1.0,
const Size & size = Size(),
const Scalar & mean = Scalar(),
bool swapRB = false,
bool crop = false,
int ddepth = CV_32F
)
image输入图像
scalefactor 默认1.0
size表示网络接受的数据大小
mean表示训练时数据集的均值
swapRB 是否互换Red与Blur通道
crop剪切
ddepth 数据类型
加载网络之后,推断调用的关键API如下:
Mat cv::dnn::Net::forward(
const String & outputName = String()
)
参数缺省值为空
对对象检测网络来说:
该API会返回一个四维的tensor,前两个维度是1,后面的两个维度,分别表示检测到BOX数量,以及每个BOX的坐标,对象类别,得分等信息。这里需要特别注意的是,这个坐标是浮点数的比率,不是像素值,所以必须转换为像素坐标才可以绘制BOX矩形。

介绍一个API, 获取网络各层执行时间与总的执行时间API:
int64 cv::dnn::Net::getPerfProfile(
std::vector< double > & timings
)
返回值是网络执行推断的时间
Timings是网络对应的各层执行时间

二、代码

import cv2 as cv

# 模型路径
model_bin = "D:/opencv_tutorial/data/models/ssd/MobileNetSSD_deploy.caffemodel"
config_text = "D:/opencv_tutorial/data/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"]

# 加载模型
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)
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()

三、结果

笔记本的内置摄像头,像素比较差。
在这里插入图片描述

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值