tesorflow c++ gpu 调用 maskrcnn

opencv 最新版本已经支持gpu调用了,需要编译一下,暂时没有花时间去编译,实际使用中一直的是 tensorflow c++ gpu版本调用模型,速度较cpu要快很多,代码如下(多次测试出的结果,有不足的请指出):

#define COMPILER_MSVC
#define NOMINMAX
#define PLATFORM_WINDOWS   // 指定使用tensorflow/core/platform/windows/cpu_info.h


#include<iostream>
#include<opencv2/opencv.hpp>
#include"tensorflow/core/public/session.h"
#include "tensorflow/core/platform/env.h"
#include<iostream>
#include<string>
#include<io.h>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#pragma warning (disable:4996)

using namespace tensorflow;
using namespace cv;
using namespace std;
using std::cout;
using std::endl;




int main() {
	    const std::string model_path = "D:/frozen_inference_graph.pb";// tensorflow模型文件,注意不能含有中文
	    std::vector<tensorflow::Tensor> outputs;
	    Session* session;
	    Status status = NewSession(SessionOptions(), &session);
	    tensorflow::GraphDef graph_def;
	    status = ReadBinaryProto(Env::Default(), model_path, &graph_def);
	    status = session->Create(graph_def);

		//设置ouput节点
	    std::vector<std::string> output_nodes;
	    output_nodes.push_back("num_detections:0");
	    output_nodes.push_back("detection_boxes:0");
	    output_nodes.push_back("detection_scores:0");
	    output_nodes.push_back("detection_classes:0");
	    output_nodes.push_back("detection_masks:0");


		clock_t start = clock();
		Mat img = imread("E:/1.png");
		cv::cvtColor(img, img, cv::COLOR_BGR2RGB);//做一个红蓝通道值互换,对识别效果有一定的提升
		Mat img2 = imread("E:/1.png");//img已被修改,再读取一张原图作显示
		int height = img.rows;
		int width = img.cols;
		int depth = img.channels();
		

		//将图像转换未input数据
		tensorflow::Tensor input_tensor(DT_UINT8, TensorShape({ 1, height, width, depth }));
		const uint8* source_data = img.data;
		auto input_tensor_mapped = input_tensor.tensor<uint8, 4>();

		for (int i = 0; i < height; i++) {
			const uint8* source_row = source_data + (i * width * depth);
			for (int j = 0; j < width; j++) {
				const uint8* source_pixel = source_row + (j * depth);
				for (int c = 0; c < depth; c++) {
					const uint8* source_value = source_pixel + c;
					input_tensor_mapped(0, i, j, c) = *source_value;
				}
			}
		}
		std::vector<std::pair<std::string, tensorflow::Tensor>> inputs = {
			{ "image_tensor:0", input_tensor },
		};

		//开始识别计算
		status = session->Run(inputs, { output_nodes }, {}, &outputs);

		//获取识别的数据
		auto mapp = outputs[0].tensor<float, 1>();
		auto detec = outputs[1].tensor<float, 3>();
		auto score = outputs[2].tensor<float, 2>();
		auto mask = outputs[4].tensor<float, 4>();
		auto class1 = outputs[3].tensor<float, 2>();
		
		//显示边界框和轮廓
		for (int i = 0; i < outputs[0].tensor<float, 1>()(0); i++) {
			if (score(0, i) > 0.5) {
				Rect  box = Rect(detec(0, i, 1) * (img.cols - 1), detec(0, i, 0) * (img.rows - 1), detec(0, i, 3) * img.cols - detec(0, i, 1) * (img.cols - 1) + 1, detec(0, i, 2) * img.rows - detec(0, i, 0) * (img.rows - 1) + 1);
				Mat objectMask(33, 33, CV_32F);//根据不同模型提取的轮廓设定v2(15,15),resnet50、resnet101(33,33),具体根据配置文件
				for (int r = 0; r < 33; r++) {
					for (int c = 0; c < 33; c++) {
						objectMask.at<float>(r, c) = mask(0, i, r, c);
					}
				}
				resize(objectMask, objectMask, Size(box.width, box.height));
				Mat mask = (objectMask);
				Mat coloredRoi = img2(box);
				coloredRoi.convertTo(coloredRoi, CV_8UC3);
				std::vector<Mat> contours;
				Mat hierarchy;
				mask.convertTo(mask, CV_8U);
				findContours(mask, contours, hierarchy, RETR_CCOMP, 1); // 提取轮廓
				drawContours(coloredRoi, contours, -1, Scalar(0, 205, 0), 2, LINE_8, hierarchy, 100); // 绘制轮廓
				coloredRoi.copyTo(img2(box), mask);
			}
		}
		cout << clock() - start << endl;
		namedWindow("img", 0);
		imshow("img", img2);
		cv::waitKey(0);
	return 0;
}

头文件可以删除些,作了些别的处理,所以头文件有点多。

需要配置下tensorflow c++的环境,这里提供免费下载:https://download.csdn.net/download/qq_39731633/12208694

在vs2015上进行的配置,具体配置如下:

1、包含目录

2、库目录

3、附加依赖

qq:2036511045 欢迎讨论

 

 

 

 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值