OpenCV3 dnn 调用caff model

#include "stdafx.h"
#include <opencv2/dnn.hpp>
#include <opencv2/dnn/all_layers.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <time.h>
#include <iostream>
using namespace cv;
using namespace dnn;
using namespace std;
const char* keys =
"{ help  h |     | print help message  }"
"{ proto p |     | path to .prototxt   }"
"{ model m |     | path to .caffemodel }"
"{ image i |     | path to input image }"
"{ conf  c | 0.8 | minimal confidence  }";

const char* classNames[] = {
    "__background__",
    "aeroplane", "bicycle", "bird", "boat",
    "bottle", "bus", "car", "cat", "chair",
    "cow", "diningtable", "dog", "horse",
    "motorbike", "person", "pottedplant",
    "sheep", "sofa", "train", "tvmonitor"
};

static const int kInpWidth = 800;
static const int kInpHeight = 600;

int main(int argc, char** argv)
{

    String protoPath = "faster_rcnn_zf.prototxt";
    String modelPath = "ZF_faster_rcnn_final.caffemodel";
    //String imagePath = "c6.jpg";
    float confThreshold = 0.8;
    //CV_Assert(!protoPath.empty(), !modelPath.empty(), !imagePath.empty());
    CV_Assert(!protoPath.empty(), !modelPath.empty());

    //// Load a model. This class allows to create and manipulate comprehensive artificial neural networks
    Net net = readNetFromCaffe(protoPath, modelPath);
    VideoCapture capture("road.avi");
    clock_t start, end; double dur;
    while (1) {
        //Mat img = imread(imagePath);
        Mat img;
        capture >> img;
        //if (img.empty()) { break; }
        //    resize(img, img, Size(kInpWidth/2, kInpHeight/2));
        start = clock();
        Mat blob = blobFromImage(img, 1.0, Size(), Scalar(102.9801, 115.9465, 122.7717), false, false);
        Mat imInfo = (Mat_<float>(1, 3) << img.rows, img.cols, 1.6f);
        net.setInput(blob, "data");
        net.setInput(imInfo, "im_info");
        
        // Draw detections.
        Mat detections = net.forward();
        
        const float* data = (float*)detections.data;
        
#pragma omp parallel for
        //    for (size_t i = 0; i < detections.total(); i += 7)
        for (int i = 0; i < (int)detections.total(); i += 7)
        {
            // An every detection is a vector [id, classId, confidence, left, top, right, bottom]
            float confidence = data[i + 2];
            if (confidence > confThreshold)
            {
                int classId = (int)data[i + 1];
                int left = max(0, min((int)data[i + 3], img.cols - 1));
                int top = max(0, min((int)data[i + 4], img.rows - 1));
                int right = max(0, min((int)data[i + 5], img.cols - 1));
                int bottom = max(0, min((int)data[i + 6], img.rows - 1));

                // Draw a bounding box.
                rectangle(img, Point(left, top), Point(right, bottom), Scalar(0, 255, 0));

                // Put a label with a class name and confidence.
                String label = cv::format("%s, %.3f", classNames[classId], confidence);
                int baseLine;
                Size labelSize = cv::getTextSize(label, FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);

                top = max(top, labelSize.height);
                rectangle(img, Point(left, top - labelSize.height),
                    Point(left + labelSize.width, top + baseLine),
                    Scalar(255, 255, 255), FILLED);
                putText(img, label, Point(left, top), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 0));
            }
        }
        end = clock();
        dur = (double)(end - start);
        cout << "处理一帧图像耗时为" << dur / 1000 << "s" << endl;
        imshow("frame", img);
        waitKey(1);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/rj1457365980/article/details/83116077
DNN
今日推荐