当前位置:网站首页>The DNN of OpenCV is used to read onnx as the prediction end to realize classification
The DNN of OpenCV is used to read onnx as the prediction end to realize classification
2022-04-23 21:57:00 【Three stone orders】
 Be careful :
 1、size Consistent with training ;
 2、 Used in training Normalize Words , because blobFromImage Variance is not supported , Need to rewrite blobFromImage;
 3、 Format of prediction results , Ensure that the corresponding indicators are read correctly ;
class ModelFlipClass
{
    
private:
	Net net;
public:
	bool Read(string pathNet);
	int Detect(string strImgPath);
};
bool ModelFlipClass::Read(string pathNet)
{
    
	try {
    
		net = readNet(pathNet);
	}
	catch (const std::exception&) {
    
		return false;
	}
	net.setPreferableBackend(cv::dnn::DNN_BACKEND_DEFAULT);
	net.setPreferableTarget(cv::dnn::DNN_TARGET_CPU);
	return true;
}
int ModelXRayFlipClass::Detect(string strImgPath)
{
    
	try 
	{
    
		Mat imgInput = imread(strImgPath, IMREAD_UNCHANGED);
		Mat imgNorm;
		normalize(imgInput, imgNorm, 0, 255, NORM_MINMAX);//16 The bit changes to 8 position 
		convertScaleAbs(imgNorm, imgNorm);
		Mat imRGB;
		cvtColor(imgNorm, imRGB, COLOR_GRAY2BGR);
		const int nHight = 400;// When training is needed transforms Of Resize bring into correspondence with 
		const int nWight = 200;
		Mat blob;
		blobFromImage(imRGB, blob, 1 / 255.0, cv::Size(nWight, nHight), Scalar(), true, false);
		net.setInput(blob);
		vector<cv::Mat> netOutputImg;
		net.forward(netOutputImg, net.getUnconnectedOutLayersNames());
		float* pdata = (float*)netOutputImg[0].data;
		vector<float> vecScore;
		for (size_t i = 0; i < 2; i++) // There are two kinds of , So here is 2
		{
    
			vecScore.push_back(pdata[i]);
		}
		auto maxPosition = max_element(vecScore.begin(), vecScore.end());
		int nMaxIndex = maxPosition - vecScore.begin();//vdbAmplitude The index when the absolute value of the value in is the largest 
		return nMaxIndex;
	}
	catch (const std::exception&) {
    
		return -1;
	}
}
版权声明
 本文为[Three stone orders]所创,转载请带上原文链接,感谢
 https://yzsam.com/2022/04/202204200615437287.html 
边栏推荐
猜你喜欢
 - 1.整理华子面经--1 
 - Question brushing plan -- backtracking method (I) 
 - FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ‘:app:stripDe 
 
 - IIS cannot load * woff,*. woff2,*. Solution of SVG file 
 - Thinkphp5 + data large screen display effect 
![[※ leetcode refers to offer 48. The longest substring without repeated characters (medium)]](/img/fa/99b64267fd890c6c40847548f077b3.png) - [※ leetcode refers to offer 48. The longest substring without repeated characters (medium)] 
 - Oracle ora-01033: Oracle initialization or shutdown in progressprocess solution 
 - Pipes and xargs 
 - Echerts add pie chart random color 
随机推荐
- DW basic course (II) 
- Pipes and xargs 
- [leetcode refers to offer 27. Image of binary tree (simple)] 
- Alibaba cloud responded to the disclosure of user registration information 
- [SDU chart team - core] enumeration of SVG attribute class design 
- [leetcode sword finger offer 10 - II. Frog jumping steps (simple)] 
- Chrome 94 引入具有争议的 Idle Detection API,苹果和Mozilla反对 
- Detectron2 usage model 
- 阿里云回应用户注册信息泄露事件 
- 危机即机遇,远程办公效率为何会提升? 
- This paper solves the cross domain problem of browser 
- Database experiment I database definition and data import 
- Question brushing plan - depth first search (II) 
- C# ftpHelper 
- 【SDU Chart Team - Core】SVG属性类设计之枚举 
- Online timing flow chart making tool 
- Database Experiment 8 trigger experiment 
- 一文解决浏览器跨域问题 
- Leaf Smecta 
- Thinkphp5 + data large screen display effect