opencv DNN模块之使用SSD(caffe)实现视频对象检测

使用SSD(caffe)实现对视频中对象的检测

###代码
在访问速率优化上,采用指针形式

#include <opencv2/opencv.hpp>
#include <opencv2/dnn.hpp>
#include <iostream>

using namespace std;
using namespace cv;
using namespace cv::dnn;
String objNames[] = { "background",
"aeroplane", "bicycle", "bird", "boat",
"bottle", "bus", "car", "cat", "chair",
"cow", "diningtable", "dog", "horse",
"motorbike", "person", "pottedplant",
"sheep", "sofa", "train", "tvmonitor" };

int main(void) {

    string bin_model = "/work/opencv_dnn/ssd/MobileNetSSD_deploy.caffemodel";
    string protxt = "/work/opencv_dnn/ssd/MobileNetSSD_deploy.prototxt";
    // load network model
    Net net = readNetFromCaffe(protxt, bin_model);

    // 设置计算后台
    net.setPreferableBackend(DNN_BACKEND_OPENCV);
    net.setPreferableTarget(DNN_TARGET_CPU);
    namedWindow("检测画面",0);
    resizeWindow("检测画面",800,600);
    // 获取各层信息
    vector<string> layer_names = net.getLayerNames();
    for (int  i = 0; i < layer_names.size(); i++) {
        int id = net.getLayerId(layer_names[i]);
        auto layer = net.getLayer(id);
        printf("layer id : %d, type : %s, name : %s \n", id, layer->type.c_str(), layer->name.c_str());
    }
    VideoCapture capture;
    capture.open("/work/opencv_video/carcar.mp4");
    Mat frame;
    while (true) {
        bool ret = capture.read(frame);
        if (!ret) break;
        // 构建输入
        Mat blob = blobFromImage(frame, 0.007843, Size(300, 300), Scalar(127.5, 127.5, 127.5), false, false);
        net.setInput(blob, "data");
        // 执行推理
        Mat detection = net.forward("detection_out");
        Mat detectionMat(detection.size[2], detection.size[3], CV_32F, detection.ptr<float>());
        float confidence_threshold = 0.5;
        // 解析输出数据
        for (int i = 0; i < detectionMat.rows; i++) {
            float* curr_row = detectionMat.ptr<float>(i);
            int image_id = (int)(*curr_row++);
            size_t objIndex = (size_t)(*curr_row++);
            float score = *curr_row++;
            if (score > confidence_threshold) {
                float tl_x = (*curr_row++) * frame.cols;
                float tl_y = (*curr_row++) * frame.rows;
                float br_x = (*curr_row++) * frame.cols;
                float br_y = (*curr_row++) * frame.rows;
                Rect box((int)tl_x, (int)tl_y, (int)(br_x - tl_x), (int)(br_y - tl_y));
                rectangle(frame, box, Scalar(0, 0, 255), 2, 8, 0);
                putText(frame, objNames[objIndex].c_str(), box.tl(), FONT_HERSHEY_SIMPLEX, 1.5, Scalar(0, 255, 0), 3, 8);
            }
        }
        // measure time consume
        vector<double> layersTimings;
        double freq = getTickFrequency() / 1000.0;
        double time = net.getPerfProfile(layersTimings) / freq;
        ostringstream ss;
        ss << "FPS: " << 1000 / time << " ; time : " << time << " ms";

        // show
        putText(frame, ss.str(), Point(20, 20), FONT_HERSHEY_PLAIN, 1.0, Scalar(255, 0, 0), 2, 8);
        imshow("检测画面", frame);
        char c = waitKey(1);
        if (c == 27) { // ESC
            break;
        }
    }
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200210200547175.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3o5NjE5Njg1NDk=,size_16,color_FFFFFF,t_70)
    // 释放资源
    capture.release();
    waitKey(0);
    destroyAllWindows();
    return 0;
}

效果

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值