使用轻量级yolov2网络实现图像识别,预测(c/c++,opencv,opencvdnn)

废话不多说,直接上代码,干货满满!!!

opencv中的dnn模块是很好用的!!!

网上学习的代码,联系------侵删

#include 
#include 

#include 
#include 
#include 
#include 
using namespace std;
using namespace cv;
using namespace cv::dnn;


double confidenceThreshold = 0.25;


// 加载网络模型
String modelConfiguration = "C:\\Users\\Administrator\\Desktop\\train\\yolov2-tiny-voc.cfg";//网络
String modelBinary = "C:\\Users\\Administrator\\Desktop\\train\\yolov2-tiny-voc.weights";//权重
//string clsNames = "C:\\Users\\Administrator\\Desktop\\train\\voc.names";

int main()
{
	//调用readNetFromDarknet()函数,函数详细知识请看我上篇博客https://blog.csdn.net/qq_43475606/article/details/109659799
	dnn::Net net = readNetFromDarknet(modelConfiguration, modelBinary);
	if (net.empty())
	{
		cout<<"Could not load net...";
		return -1;
	}

	// 加载分类信息
	vector<string> classNamesVec;
	ifstream classNamesFile("C:\\Users\\Administrator\\Desktop\\train\\voc.names");//打开类名文件
	if (classNamesFile.is_open())
	{
		string className = "NULL";
		while (std::getline(classNamesFile, className))
			classNamesVec.push_back(className);
	}



	// 加载图像
	Mat frame = imread("car.jpg");//读取图像
	Mat inputBlob = blobFromImage(frame, 1 / 255.F, Size(416, 416), Scalar(), true, false);
	//调用blobFromImage,详情看我上篇博客https://blog.csdn.net/qq_43475606/article/details/109659799
	net.setInput(inputBlob, "data");




	// 进行目标检测
	Mat detectionMat = net.forward("detection_out");//不要用中文
	vector<double> layersTimings;
	double freq = getTickFrequency() / 1000;
	double time = net.getPerfProfile(layersTimings) / freq;
	ostringstream ss;
	ss << "detect_time: " << time << " ms";
	putText(frame, ss.str(), Point(20, 20), 0, 0.5, Scalar(122, 54, 35));




	// 输出结果
	//对识别的复选框进行处理,不然会有多个复选框,详情请看博客https://blog.51cto.com/gloomyfish/2095418
	for (int i = 0; i < detectionMat.rows; i++)
	{
		const int probability_index = 5;
		const int probability_size = detectionMat.cols - probability_index;
		float *prob_array_ptr = &detectionMat.at<float>(i, probability_index);
		size_t objectClass = max_element(prob_array_ptr, prob_array_ptr + probability_size) - prob_array_ptr;
		float confidence = detectionMat.at<float>(i, (int)objectClass + probability_index);
		if (confidence > confidenceThreshold)
		{
			float x = detectionMat.at<float>(i, 0);
			float y = detectionMat.at<float>(i, 1);
			float width = detectionMat.at<float>(i, 2);
			float height = detectionMat.at<float>(i, 3);
			int xLeftBottom = static_cast<int>((x - width / 2) * frame.cols);
			int yLeftBottom = static_cast<int>((y - height / 2) * frame.rows);
			int xRightTop = static_cast<int>((x + width / 2) * frame.cols);
			int yRightTop = static_cast<int>((y + height / 2) * frame.rows);
			Rect object(xLeftBottom, yLeftBottom,
				xRightTop - xLeftBottom,
				yRightTop - yLeftBottom);
			rectangle(frame, object, Scalar(0, 0, 255), 2, 8);
			if (objectClass < classNamesVec.size())
			{
				ss.str("");
				ss << confidence;
				String conf(ss.str());
				String label = String(classNamesVec[objectClass]) + ": " + conf;
				int baseLine = 0;
				Size labelSize = getTextSize(label, FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
				rectangle(frame, Rect(Point(xLeftBottom, yLeftBottom),
					Size(labelSize.width, labelSize.height + baseLine)),
					Scalar(255, 255, 255), CV_FILLED);
				putText(frame, label, Point(xLeftBottom, yLeftBottom + labelSize.height),
					FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 0));
			}
		}
	}
	imshow("检测", frame);
	waitKey(0);

	return 0;
}

识别效果如下图

使用轻量级yolov2网络实现图像识别,预测(c/c++,opencv,opencvdnn)_第1张图片
代码中使用的3个文件:
yolov2-tiny-voc.cfg
yolov2-tiny-voc.weights
voc.names
请加QQ群 109530331,问我要,嘻嘻嘻嘻!!!!

你可能感兴趣的:(目标检测,图像识别,opencv,计算机视觉)