c++ opencv调用yolo onnx文件

网上找了一段代码,测试c++ opencv调用yolo onnx文件

yolov8n.onnx opencv版本是4.12 ,另外测试了4.4和4.6版本的opencv运行有问题,可能对opencv版本有要求,有待研究,都在编译了contrib库的情况下测试的

cpp 复制代码
#include <opencv2/opencv.hpp>
#include <opencv2/dnn.hpp>
#include <iostream>
#include <chrono>

int main()
{
	// 加载 ONNX 模型
	std::string modelPath = "yolov8n.onnx";
	cv::dnn::Net net = cv::dnn::readNetFromONNX(modelPath);
	net.setPreferableBackend(cv::dnn::DNN_BACKEND_DEFAULT);
	net.setPreferableTarget(cv::dnn::DNN_TARGET_CPU);

	// 定义完整的COCO数据集类别名称
	std::vector<std::string> classes = {
		"person", "bicycle", "car", "motorcycle", "airplane", "bus", "train", "truck", "boat", "traffic light",
		"fire hydrant", "stop sign", "parking meter", "bench", "bird", "cat", "dog", "horse", "sheep", "cow",
		"elephant", "bear", "zebra", "giraffe", "backpack", "umbrella", "handbag", "tie", "suitcase", "frisbee",
		"skis", "snowboard", "sports ball", "kite", "baseball bat", "baseball glove", "skateboard", "surfboard",
		"tennis racket", "bottle", "wine glass", "cup", "fork", "knife", "spoon", "bowl", "banana", "apple",
		"sandwich", "orange", "broccoli", "carrot", "hot dog", "pizza", "donut", "cake", "chair", "couch",
		"potted plant", "bed", "dining table", "toilet", "tv", "laptop", "mouse", "remote", "keyboard", "cell phone",
		"microwave", "oven", "toaster", "sink", "refrigerator", "book", "clock", "vase", "scissors", "teddy bear",
		"hair drier", "toothbrush"
	};

	// 打开视频文件或摄像头
	// 0 表示默认摄像头,也可以替换为视频文件路径如 "video.mp4"
	cv::VideoCapture cap("nfs.mp4");

	// 检查视频是否成功打开
	if (!cap.isOpened()) {
		std::cerr << "Error: Unable to open video source" << std::endl;
		return -1;
	}

	// 获取视频的帧率
	double fps = cap.get(cv::CAP_PROP_FPS);
	if (fps == 0) fps = 30.0; // 默认帧率

	// 用于计算FPS的变量
	auto lastTime = std::chrono::high_resolution_clock::now();
	int frameCount = 0;
	double currentFps = 0.0;

	cv::Mat frame;
	while (true) {
		// 读取帧
		cap >> frame;

		// 检查是否成功读取帧
		if (frame.empty()) {
			std::cout << "End of video or error reading frame" << std::endl;
			break;
		}

		// 计算FPS
		frameCount++;
		auto currentTime = std::chrono::high_resolution_clock::now();
		auto elapsedTime = std::chrono::duration_cast<std::chrono::milliseconds>(currentTime - lastTime).count();

		if (elapsedTime >= 1000) { // 每秒更新一次FPS
			currentFps = frameCount / (elapsedTime / 1000.0);
			frameCount = 0;
			lastTime = currentTime;
		}

		// 将图像转换为blob格式
		cv::Mat blob = cv::dnn::blobFromImage(frame, 1 / 255.0, cv::Size(640, 640), cv::Scalar(0, 0, 0), true, false);
		net.setInput(blob);

		// 前向传播, 获取检测结果
		std::vector <cv::Mat> outputs;
		net.forward(outputs, net.getUnconnectedOutLayersNames());

		// output.size [ 1, 84, 8400]
		int rows = outputs[0].size[2];

		// 每个目标存储了多少个值(x,y,w,h+类别数)
		int length = outputs[0].size[1];

		// 转成单通道
		outputs[0] = outputs[0].reshape(1, length);

		// 按对角线翻转
		cv::transpose(outputs[0], outputs[0]);

		float* data = (float*)outputs[0].data;
		float xFactor = (float)frame.cols / 640;
		float yFactor = (float)frame.rows / 640;

		// 解析检测结果
		std::vector<int> classIds;
		std::vector<float> confidences;
		std::vector<cv::Rect> boxes;

		for (int i = 0; i < rows; i++)
		{
			// 存储每个类别的置信度
			cv::Mat scores(1, classes.size(), CV_32FC1, data + 4);
			cv::Point classId;
			double maxClassScore;
			// 读取最大置信度并获得它的索引
			cv::minMaxLoc(scores, 0, &maxClassScore, 0, &classId);

			if (maxClassScore > 0.1)
			{
				confidences.push_back(maxClassScore);
				classIds.push_back(classId.x);

				float x = data[0];
				float y = data[1];
				float w = data[2];
				float h = data[3];

				int left = int((x - 0.5 * w) * xFactor);
				int top = int((y - 0.5 * h) * yFactor);

				int width = int(w * xFactor);
				int height = int(h * yFactor);

				boxes.push_back(cv::Rect(left, top, width, height));
			}
			data += length;
		}

		// 执行非最大抑制,以消除具有较低置信度的冗余重叠框(NMS)
		std::vector<int> nmsResult;
		cv::dnn::NMSBoxes(boxes, confidences, 0.25, 0.7, nmsResult);
		for (int i = 0; i < nmsResult.size(); i++)
		{
			int idx = nmsResult[i];
			int classId = classIds[idx];
			float confidence = confidences[idx];
			cv::Rect box = boxes[idx];

			// 绘制检测框并显示类别名称
			cv::rectangle(frame, box, cv::Scalar(0, 0, 255), 2);
			cv::putText(frame, classes[classId] + ": " + std::to_string(confidence).substr(0, 4),
				cv::Point(box.x, box.y - 10), cv::FONT_HERSHEY_DUPLEX, 1, cv::Scalar(0, 0, 255));
		}

		// 在图像上显示FPS
		std::string fpsText = "FPS: " + std::to_string(static_cast<int>(currentFps));
		cv::putText(frame, fpsText, cv::Point(10, 30), cv::FONT_HERSHEY_SIMPLEX, 1, cv::Scalar(0, 255, 0), 2);

		// 显示结果
		cv::imshow("YOLO Detection", frame);

		// 按ESC键退出
		if (cv::waitKey(1) == 27) {
			break;
		}
	}

	// 释放资源
	cap.release();
	cv::destroyAllWindows();

	return 0;
}
相关推荐
望眼欲穿的程序猿18 分钟前
Win系统Vscode+CoNan+Cmake实现调试与构建
c语言·c++·后端
lzh2004091926 分钟前
【C++STL】List详解
开发语言·c++
luoyayun36133 分钟前
Qt/C++ 线程池TaskPool与 Worker 框架实践
c++·qt·线程池·taskpool
喵个咪44 分钟前
ASIO 定时器完全指南:类型解析、API 用法与实战示例
c++·后端
phdsky2 小时前
【设计模式】抽象工厂模式
c++·设计模式·抽象工厂模式
shayudiandian2 小时前
YOLOv8目标检测项目实战(从训练到部署)
人工智能·yolo·目标检测
雾岛听蓝2 小时前
C++ 入门核心知识点(从 C 过渡到 C++ 基础)
开发语言·c++·经验分享·visual studio
xlq223223 小时前
19.模版进阶(上)
c++
yuuki2332333 小时前
【C++】初识C++基础
c语言·c++·后端
小年糕是糕手3 小时前
【C++】类和对象(二) -- 构造函数、析构函数
java·c语言·开发语言·数据结构·c++·算法·leetcode