opencv 调用 caffe model

为了将caffe训练好的model集成到C工程中,使用opencv调用caffe 模型的方法,使用如下方法:
如下代码为 opencv\sources\samples\dnn\caffe_googlenet.cpp文件,将caffe model 与deploy.prototxt,文件放目录下,代码中一定注意处为易出错的地方

#include
#include
#include
#include
using namespace cv;
using namespace cv::dnn;

#include
#include
#include
using namespace std;

/* Find best class for the blob (i. e. class with maximal probability) */
static void getMaxClass(const Mat &probBlob, int *classId, double *classProb)
{
Mat probMat = probBlob.reshape(1, 1); //reshape the blob to 1x1000 matrix
Point classNumber;

minMaxLoc(probMat, NULL, classProb, NULL, &classNumber);
*classId = classNumber.x;

}

static std::vector readClassNames(const char *filename = “synset_words.txt”)
{
std::vector classNames;

std::ifstream fp(filename);
if (!fp.is_open())
{
    std::cerr << "File with classes labels not found: " << filename << std::endl;
    exit(-1);
}

std::string name;
while (!fp.eof())
{
    std::getline(fp, name);
    if (name.length())
        classNames.push_back( name.substr(name.find(' ')+1) );
}

fp.close();
return classNames;

}

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

String modelTxt = "bvlc_googlenet.prototxt";//deploy用的prototxt文件
String modelBin = "bvlc_googlenet.caffemodel";//训练好的模型
String imageFile = (argc > 1) ? argv[1] : "space_shuttle.jpg";

Net net;
try {
    //! [Read and initialize network]
    net = dnn::readNetFromCaffe(modelTxt, modelBin);
    //! [Read and initialize network]
}
catch (cv::Exception& e) {
    std::cerr << "Exception: " << e.what() << std::endl;
    //! [Check that network was read successfully]
    if (net.empty())
    {
        std::cerr << "Can't load network by using the following files: " << std::endl;
        std::cerr << "prototxt:   " << modelTxt << std::endl;
        std::cerr << "caffemodel: " << modelBin << std::endl;
        std::cerr << "bvlc_googlenet.caffemodel can be downloaded here:" << std::endl;
        std::cerr << "http://dl.caffe.berkeleyvision.org/bvlc_googlenet.caffemodel" << std::endl;
        exit(-1);
    }
    //! [Check that network was read successfully]
}

//! [Prepare blob]
Mat img = imread(imageFile);
if (img.empty())
{
    std::cerr << "Can't read image from the file: " << imageFile << std::endl;
    exit(-1);
}

//GoogLeNet accepts only 224x224 BGR-images
Mat inputBlob = blobFromImage(img, 1.0f, Size(224, 224),
                              Scalar(104, 117, 123), false);   //载入图像到blob中
//! [Prepare blob]

Mat prob;
cv::TickMeter t;
for (int i = 0; i < 10; i++)
{
    CV_TRACE_REGION("forward");
    //! [Set input blob]
    net.setInput(inputBlob, "data");        // data为输入的blob的名称 一定注意!!!!
    //! [Set input blob]
    t.start();
    //! [Make forward pass]
    prob = net.forward("prob");                          //最后一个blob的名称,一定注意!!!
    //! [Make forward pass]
    t.stop();
}

//! [Gather output]
int classId;
double classProb;
getMaxClass(prob, &classId, &classProb);//find the best class
//! [Gather output]

//! [Print results]
std::vector classNames = readClassNames();
std::cout << "Best class: #" << classId << " '" << classNames.at(classId) << "'" << std::endl;
std::cout << "Probability: " << classProb * 100 << "%" << std::endl;
//! [Print results]
std::cout << "Time: " << (double)t.getTimeMilli() / t.getCounter() << " ms (average from " << t.getCounter() << " iterations)" << std::endl;

return 0;

} //main

如果要集成到其他地方,将main函数改为其他函数即可

你可能感兴趣的:(opencv 调用 caffe model)