基于GoogleNet Caffe模型 +OpenCV4.X实现的图像分类

#include <iostream>  
#include <opencv2/core/core.hpp>  
#include <opencv2/highgui/highgui.hpp>  
#include <opencv2\opencv.hpp>
#include<opencv2/dnn.hpp>
#include<fstream>

using namespace std;
using namespace cv;
using namespace cv::dnn;

String model_bin_file = "bvlc_googlenet.caffemodel";
String model_text_file = "bvlc_googlenet.prototxt";
String labels_txt_file = "synset_words.txt";
vector<String>readLabels();



int main()
{
	Mat input = imread("飞机.jpg");

	namedWindow("OUTPUT", WINDOW_AUTOSIZE);
	imshow("OUTPUT", input);
	vector<String>labels = readLabels();

	Net net = readNetFromCaffe(model_text_file, model_bin_file);
	if (net.empty())
	{
		cout << "read caffe model data No" << endl;
	}
	Mat inputBlob = blobFromImage(input, 1.0, Size(224, 224), Scalar(104, 117, 123));
	Mat prob;
	for (int i = 0; i < 10; 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("\n The input image classiffcation: %s,possible:%.2f", labels.at(classidx).c_str(), classProb);

	putText(input, labels.at(classidx), Point(20, 20), FONT_HERSHEY_PLAIN, 1.0, Scalar(0, 0, 255), 2, 8);
	imshow("OUTPUT", input);  

	waitKey(0);
	return 0;

	
}
vector<String>readLabels()
{
	vector<String>classNames;
	ifstream fp(labels_txt_file);
	if (!fp.is_open())
	{
		cout << "not open the file";
		exit(-1);
	}
	string name;
	while (!fp.eof())
	{
		getline(fp, name);
		if (name.length())
		{
			classNames.push_back(name.substr(name.find
			(' ') + 1));
		}
	}
	fp.close();
	return classNames;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

༄yi笑奈何

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值