基于opencv的googlenet 实现(opencv3.3)

//实现googlenet网络
#include<opencv2\opencv.hpp>
#include <opencv2\dnn.hpp>
using namespace std;
using namespace cv;
using namespace cv::dnn;
string model_text = "C:\\Users\\Administrator\\Desktop\\c++\\data_model\\googlenet\\bvlc_googlenet.prototxt";
string model_bin  = "C:\\Users\\Administrator\\Desktop\\c++\\data_model\\googlenet\\bvlc_googlenet.caffemodel";
string class_labels = "C:\\Users\\Administrator\\Desktop\\c++\\data_model\\googlenet\\synset_words.txt";

std::vector<String> readClassNames(string filename) //将文件内容以每行,读到一个String vector中
{
	std::vector<String> classNames;
	std::ifstream fp(filename);
	if (!fp.is_open())
	{
		cout << "file is no found" << endl;
		exit(-1);
	}
	std::string name;
	while (!fp.eof())//文件没有到尾部
	{
		std::getline(fp, name);  //将fp第一行赋值到name中
			if (name.length())
			{
				classNames.push_back(name.substr(name.find(" ") + 1));
			}

	}
	fp.close();
	return classNames;
}
int main()
{
	VideoCapture cap;
	cap.open(0);
	if(!cap.isOpened())
	{
		cout << "cammer is failed!" << endl;
	}
	Net net = readNetFromCaffe(model_text, model_bin);
	if (net.empty()) {

		cout << "model_text is empty" << endl;
		cout << "model_bin is empty" << endl;
	}
	Mat img, pro;
	int class_id;
	double classProb;
	while (1)
	{
		cap >> img;
		Mat inputBlob = blobFromImage(img, 1, Size(224, 224), Scalar(104, 117, 123));
		net.setInput(inputBlob, "data");
		pro = net.forward("prob");  //计算输出

		Mat proMax = pro.reshape(1, 1);//维度reshape成1*1000
		Point class_number;
		minMaxLoc(proMax, NULL, &classProb, NULL, &class_number);
		class_id = class_number.x;  //获得预测索引

		std::vector<String> classname = readClassNames(class_labels);

		cout << "Best class:#" << class_id << "," << classname.at(class_id) << "'" << endl;//根据预测索引找到文件中的类别名
		cout << "Probability:" << classProb * 100 << "%" << endl; //输出可能性
		imshow("googleNet", img);
		if (waitKey(100) ==27)
			break;
	}
	return 0;


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

apple_51426592

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

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

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

打赏作者

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

抵扣说明:

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

余额充值