为了将caffe训练好的model集成到C工程中,使用opencv调用caffe 模型的方法,使用如下方法:
如下代码为 opencv\sources\samples\dnn\caffe_googlenet.cpp文件,将caffe model 与deploy.prototxt,文件放目录下,代码中一定注意处为易出错的地方
#include <opencv2/dnn.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/core/utils/trace.hpp>
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 cha