day39:opencv与深度神经网络应用实例

1.加载深度学习模型

 

void visionagin:: Myreadnet()
{
	String model = "C:\\Users\\86176\\Downloads\\visionimage\\bvlc_googlenet.caffemodel";
	String config = "C:\\Users\\86176\\Downloads\\visionimage\\bvlc_googlenet.prototxt";
	dnn::Net net = dnn::readNet(model, config);
	if (net.empty())
	{
		cout << "read failed !" << endl;
	}
	vector<String> layernames;
	layernames=net.getLayerNames();
	for (int i = 0; i < layernames.size(); ++i)
	{
		int id = net.getLayerId(layernames[i]);
		Ptr<dnn::Layer> layer = net.getLayer(id);
		cout << "第" << to_string(id) << "层网络名:" << layernames[i] << " ,网络类型:" << layer->type.c_str() << endl;
	}
}

2.图像识别

void visionagin:: Myimagedetect()
{
	Mat img = imread("C:\\Users\\86176\\Downloads\\visionimage\\plane.jpeg");
	if (!img.data)
	{
		cout << "picture read failed!" << endl;
	}
	String model = "C:\\Users\\86176\\Downloads\\visionimage\\image_pattern\\tensorflow_inception_graph.pb";
	//字符串数组存放读出来的分类表
	vector<String>typelist;
	String txt= "C:\\Users\\86176\\Downloads\\visionimage\\image_pattern\\imagenet_comp_graph_label_strings.txt";
	//创建文件流
	ifstream file(txt);
	if (!file.is_open())
	{
		cout << "文件流打开失败!" << endl;
	}
	String type;
	while (!file.eof())
	{
		getline(file, type);
		typelist.push_back(type);
	}
	file.close();
	dnn::Net net = dnn::readNet(model);
	if (net.empty())
	{
		cout << "模型读取失败!" << endl;
	}
	//变换尺寸
	Mat blob=dnn::blobFromImage(img, 1.0f, Size(224, 224));
	//进行图像预测
	net.setInput(blob,"input");
	Mat prob;
	prob = net.forward("softmax2");

	Mat result = prob.reshape(1, 1);
	double maxval;//匹配的最大可能性
	Point maxp;//最大可能性坐标
	minMaxLoc(result, NULL, &maxval, NULL, &maxp);
	String outtype = typelist[maxp.x].c_str();
	cout << "识别结果是:" << outtype << endl;
	cout << "最大可能性:" << maxval << endl;

}

3.风格迁移

void visionagin:: MyneuralStyle()
{
	Mat img = imread("C:\\Users\\86176\\Downloads\\visionimage\\women.jfif");
	imshow("原图", img);
	//计算图像各通道均值
	Scalar meanimg = mean(img);
	Mat blob=dnn::blobFromImage(img, 1.0f, Size(256, 256), meanimg);
	String temp = "C:\\Users\\86176\\Downloads\\visionimage\\fast_style\\";
	String neuralstyle[] = { "the_wave.t7","mosaic.t7","feathers.t7","candy.t7","udnie.t7" };
	for (int i = 0; i < size(neuralstyle); ++i)
	{
		dnn::Net net = dnn::readNet(temp + neuralstyle[i]);
		net.setInput(blob);
		Mat out;//预测结果
		net.forward(out);
		int outchannels = out.size[1];
		int	outrows = out.size[2];
		int outcols=out.size[3];
		Mat result = Mat::zeros(outrows, outcols, CV_32FC3);//绘制结果的画布
		float* data = out.ptr<float>();
		for (int k = 0; k < outchannels; ++k)
		{
			for (int m = 0; m < outrows; ++m)
			{
				for (int n = 0; n < outcols; ++n)
				{
					result.at<Vec3f>(m, n)[k] = *data++;

				}
			}
		}
		//加上减去的均值
		result += meanimg;
		//result /= 255;//result为cv_32f类型,需归一化到0-1;
		result.convertTo(result, CV_8UC3);
		resize(result, result, Size(img.cols, img.rows));
		imshow("迁移后图" + to_string(i), result);
	}
}

 

4.性别检测

 

void visionagin:: Myfaceandgenderdetect()
{
	Mat image = imread("C:\\Users\\86176\\Downloads\\visionimage\\faces.jpg");
	resize(image, image, Size(800, 600));
	GaussianBlur(image, image, Size(3, 3),20);
	dnn::Net net = dnn::readNet("C:\\Users\\86176\\Downloads\\visionimage\\ch12_face_age\\opencv_face_detector_uint8.pb", "C:\\Users\\86176\\Downloads\\visionimage\\ch12_face_age\\opencv_face_detector.pbtxt");
	Mat inputimg = dnn::blobFromImage(image, 1.0f, Size(300, 300));
	Mat output1;
	net.setInput(inputimg,"data");//输入数据
	net.forward(output1,"detection_out");//进行预测
	//检测出人脸的区域的位置
	Mat facearea = Mat(output1.size[2], output1.size[3], CV_32F, output1.ptr<float>());
	//读取性别检测模型
	dnn::Net gendermodel = dnn::readNet("C:\\Users\\86176\\Downloads\\visionimage\\ch12_face_age\\gender_net.caffemodel", "C:\\Users\\86176\\Downloads\\visionimage\\ch12_face_age\\gender_deploy.prototxt");
	String gender[2] = { "boy","girl" };
	//对每个检测出的人脸
	for (int i = 0; i < facearea.rows; ++i)
	{
		double rate = facearea.at<float>(i, 2);//人脸的概率
		if (rate > 0.5)
		{
			int top_x = facearea.at<float>(i, 3) * image.cols;//facearea每行的第2列是概率,3,4,5,6是坐标位置,以比例表示
			int top_y = facearea.at<float>(i, 4) * image.rows;
			int down_x = facearea.at<float>(i, 5) * image.cols;
			int down_y = facearea.at<float>(i, 6) * image.rows;
			Rect rect = Rect(top_x, top_y, down_x - top_x, down_y - top_y);
			//对检测出的图像进行扩充,防止尺寸在真实图像之外
			Rect rectagin;
			rectagin.x = max(0, rect.x - 25);
			rectagin.y = max(0, rect.y - 25);
			rectagin.width = min(rect.width + 25, image.cols - 1);
			rectagin.height = min(rect.height+ 25, image.rows - 1);
			//人脸区域
			Mat face = image(rectagin);
			//对每个人脸变换尺寸
			Mat inputface = dnn::blobFromImage(face, 1.0, Size(227, 227));
			gendermodel.setInput(inputface);
			//预测性别男女概率,是2*1的矩阵
			Mat outputgenderrate=gendermodel.forward();
			float boy,girl;
			boy = outputgenderrate.at<float>(0);
			girl = outputgenderrate.at<float>(1);
			cout << boy << "-" << girl << endl;
			int num=0;
			if (boy > girl)
			{
				num=0;
			}
			else
			{
				num=1;
			}
			String gen = gender[num];
			//画框
			rectangle(image, rect, Scalar(0, 200, 0), 2, 8);
			putText(image, gen.c_str(), rect.tl(), FONT_HERSHEY_SIMPLEX, 0.8, Scalar(0, 0, 200));


		}
	}
	imshow("result", image);
	
	
	
	
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值