OpenCV调用caffe分类模型-深度学习文档类资源-CSDN文库
源码
#include <iostream>
#include <fstream>
#include <sstream>
#include <opencv2/opencv.hpp>
#include <opencv2/dnn.hpp>
#include <time.h>
using namespace std;
using namespace cv;
using namespace ::dnn;
clock_t start, finish;
String model_file = "caffe/model/model.caffemodel";
String model_text = "caffe/model/deploy.prototxt";
double detect_NN(Mat detectImg, Net net)
{
if (net.empty())
{
cout << "no model!" << endl;
return -1;
}
Mat src = detectImg.clone();
if (src.empty())
{
return -1;
}
start = clock();
Mat inputBlob;
inputBlob = blobFromImage(src, 1.0, Size(227, 227), Scalar(92.71, 106.44, 118.11));
Mat prob;
for (int i = 0; i < 1; i++)
{
net.setInput(inputBlob, "data");
prob = net.forward("prob");
}
Mat probMat = prob.reshape(1, 1);
Point classNumber;
double classProb;
minMaxLoc(probMat, NULL, &classProb, NULL, &classNumber);
int classidx = classNumber.x;
printf("classidx is:%d\n", classidx);
printf("prob is %f\n", classProb);
finish = clock();
double duration = (double)(finish - start);
printf("run time is %f ms\n", duration);
return duration;
}
int main()
{
Net net = readNetFromCaffe(model_text, model_file);
Mat detectImg = imread("caffe/image/husky.jpg");
double runTime = detect_NN(detectImg, net);
system("pause");
return 0;
}
测试图片
运行结果
查询ImageNet1000分类结果可得,编号250为Siberian husky