C++ OpenCV调用caffe分类模型

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值