opencv进阶-YOLOv4检测交通标志

Posted 嵌入式爱好者-超

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了opencv进阶-YOLOv4检测交通标志相关的知识,希望对你有一定的参考价值。

这次练习目的有两个:
1.尝试在只替换权重文件、描述文件以及类别文件后,确认是否能进行自定义检测,结果表明想法是可行的。
2.YOLOv4支持opencv4.4版本。之前使用的是opencv4.1,发生报错,就安装了opencv4.4版的,并在上面进行检测,没有报错,还能检测出交通标志。

全部代码

#include <opencv2/opencv.hpp>
#include <opencv2/dnn.hpp>
#include <fstream>
#include <iostream>
#include <algorithm>
#include <cstdlib>
using namespace std;
using namespace cv;
using namespace cv::dnn;
void image_detection();

String yolo_cfg = "D:/opencv-4.4.0/models/yolov4-trafficlights/yolov4-trafficlights.cfg";
String yolo_model = "D:/opencv-4.4.0/models/yolov4-trafficlights/yolov4-trafficlights.weights";

int main()

	image_detection();


void image_detection() 
	//加载网络模型
	Net net = readNetFromDarknet(yolo_cfg, yolo_model);

	//net.setPreferableBackend(DNN_BACKEND_INFERENCE_ENGINE);
	net.setPreferableTarget(DNN_TARGET_CPU);
	net.setPreferableBackend(DNN_BACKEND_OPENCV);

	std::vector<String> outNames = net.getUnconnectedOutLayersNames();
	for (int i = 0; i < outNames.size(); i++) 
		printf("output layer name : %s\\n", outNames[i].c_str());
	

	vector<string> classNamesVec;
	ifstream classNamesFile("D:/opencv-4.1.0/models/yolov4-trafficlights/trafficlights.names");
	if (classNamesFile.is_open())
	
		string className = "";
		while (std::getline(classNamesFile, className))
			classNamesVec.push_back(className);
	

	// 加载图像
	
	Mat frame = imread("D:/images/5.jpg");

		//imshow("input", frame);

		Mat inputBlob = blobFromImage(frame, 1 / 255.F, Size(416, 416), Scalar(), true, false);
		net.setInput(inputBlob);

		// 输出检测频率和每帧耗时
		std::vector<Mat> outs;
		net.forward(outs, outNames);
		vector<double> layersTimings;
		double freq = getTickFrequency() / 1000;
		double time = net.getPerfProfile(layersTimings) / freq;
		ostringstream ss;
		ss << "FPS" << 1000 / time << ";time:" << time << "ms";
		putText(frame, ss.str(), Point(20, 20), FONT_HERSHEY_PLAIN, 1, Scalar(0, 0, 255), 2, 8);

		// 输出检测框和置信度
		vector<Rect> boxes;
		vector<int> classIds;
		vector<float> confidences;
		for (size_t i = 0; i < outs.size(); ++i)
		
			// Network produces output blob with a shape NxC where N is a number of
			// detected objects and C is a number of classes + 4 where the first 4
			// numbers are [center_x, center_y, width, height]
			float* data = (float*)outs[i].data;
			for (int j = 0; j < outs[i].rows; ++j, data += outs[i].cols)
			
				Mat scores = outs[i].row(j).colRange(5, outs[i].cols);
				Point classIdPoint;
				double confidence;
				minMaxLoc(scores, 0, &confidence, 0, &classIdPoint);
				if (confidence > 0.5)
				
					int centerX = (int)(data[0] * frame.cols);
					int centerY = (int)(data[1] * frame.rows);
					int width = (int)(data[2] * frame.cols);
					int height = (int)(data[3] * frame.rows);
					int left = centerX - width / 2;
					int top = centerY - height / 2;

					classIds.push_back(classIdPoint.x);
					confidences.push_back((float)confidence);
					boxes.push_back(Rect(left, top, width, height));
				
			
		

		vector<int> indices;
		NMSBoxes(boxes, confidences, 0.5, 0.2, indices);
		for (size_t i = 0; i < indices.size(); ++i)
		
			int idx = indices[i];
			Rect box = boxes[idx];
			String className = classNamesVec[classIds[idx]];
			putText(frame, format("%.2f,%s", confidences[idx], className.c_str()), box.tl(), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(255, 0, 0), 2, 8);
			rectangle(frame, box, Scalar(0, 0, 255), 2, 8, 0);
		

		imshow("YOLOv3-Detections", frame);
	//	char c = waitKey(5);
	//	if (c == 27)  // ESC退出
	//		break;
	//	

	//capture.release();//释放资源
	waitKey(0);
	return;


效果展示


reference:Darknet YoloV4 Windows10下数据训练及测试(二)darknet训练自己的数据

以上是关于opencv进阶-YOLOv4检测交通标志的主要内容,如果未能解决你的问题,请参考以下文章

opencv进阶-YOLOV4模型-实时物体检测

opencv进阶-YOLOV4模型-实时物体检测

opencv进阶-Windows10系统下YOLOv4—Darknet训练

opencv进阶-Windows10系统下YOLOv4—Darknet训练

Qt+YOLOv4实现目标检测

Qt+YOLOv4实现目标检测