C++学习笔记十六:使用OpenCv(c++)调用yolo模型实现目标检测

一、前言

因为之前都是直接使用python编程来实现目标检测,而且是直接使用模型。于是就想了解一下使用c++语言如何进行目标检测,也能帮助自己更好的熟悉c++的语法。简单起见,使用opencvdnn模块调用yolo模型的方式是个不错的选择。

二、环境配置

我是在win10系统中使用vs2019运行的程序,所以需要进行必要的环境配置,比如安装opencv以及在vs2019中添加opencv。详细可参考以下文章:

https://blog.csdn.net/qq321772514/article/details/90514538

三、代码实现

以下代码来自下面这篇文章(工程的Github链接也在里面):

https://blog.csdn.net/nihate/article/details/108850477

1、yolo.h

#pragma once
#include <fstream>
#include <sstream>
#include <iostream>
#include <opencv2/dnn.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>

using namespace cv;
using namespace dnn;
using namespace std;

struct Net_config
{
	float confThreshold; // Confidence threshold
	float nmsThreshold;  // Non-maximum suppression threshold
	int inpWidth;  // Width of network's input image
	int inpHeight; // Height of network's input image
	string classesFile;
	string modelConfiguration;
	string modelWeights;
	string netname;
};

class YOLO
{
public:
	YOLO(Net_config config);
	void detect(Mat& frame);
private:
	float confThreshold;
	float nmsThreshold;
	int inpWidth;
	int inpHeight;
	char netname[20];
	vector<string> classes;
	Net net;
	void postprocess(Mat& frame, const vector<Mat>& outs);
	void drawPred(int classId, float conf, 
				  int left, int top, int right, int bottom, Mat& frame);
};

Net_config yolo_nets[4] = {
	{0.5, 0.4, 416, 416,"coco.names", 
	"yolov3/yolov3.cfg", "yolov3/yolov3.weights", "yolov3"},
	
	{0.5, 0.4, 608, 608,"coco.names", 
	"yolov4/yolov4.cfg", "yolov4/yolov4.weights", "yolov4"},
	
	{0.5, 0.4, 320, 320,"coco.names", 
	"yolo-fastest/yolo-fastest-xl.cfg", 
	"yolo-fastest/yolo-fastest-xl.weights", "yolo-fastest"},
	
	{0.5, 0.4, 320, 320,"coco.names", 
	"yolobile/csdarknet53s-panet-spp.cfg", 
	"yolobile/yolobile.weights", "yolobile"}
};

2、main_yolo.cpp

#include "yolo.h"

YOLO::YOLO(Net_config config)
{
	cout << "Net use " << config.netname << endl;
	this->confThreshold = config.confThreshold;
	this->nmsThreshold = config.nmsThreshold;
	this->inpWidth = config.inpWidth;
	this->inpHeight = config.inpHeight;
	strcpy_s(this->netname, config.netname.c_str());

	ifstream ifs(config.classesFile.c_str());
	string line;
	while (getline(ifs, line)) this->classes.push_back(line);

	this->net = readNetFromDarknet(config.modelConfiguration, config.modelWeights);
	this->net.setPreferableBackend(DNN_BACKEND_OPENCV);
	this->net.setPreferableTarget(DNN_TARGET_CPU);
}

void YOLO::postprocess(Mat& frame, const vector<Mat>& outs)
// Remove the bounding boxes with low confidence using non-maxima suppression
{
	vector<int> classIds;
	vector<float> confidences;
	vector<Rect> boxes;
	//不同的模型的输出可能不一样,yolo的输出outs是[[[x,y,w,h,...],[],...[]]],
	//之所以多一维,是因为模型输入的frame是四维的,第一维表示帧数,如果只有一张图片推理,那就是1
	for (size_t i = 0; i < outs.size(); ++i)
	{
		// Scan through all the bounding boxes output from the network and keep only the
		// ones with high confidence scores. Assign the box's class label as the class
		// with the highest score for the box.
		//data是指针,每次从存储一个框的信息的地址跳到另一个框的地址
		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;
			// Get the value and location of the maximum score
			// 找到最大的score的索引,刚好对应80个种类的索引
			minMaxLoc(scores, 0, &confidence, 0, &classIdPoint);
			if (confidence > this->confThreshold)
			{
				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));
			}
		}
	}

	// Perform non maximum suppression to eliminate redundant overlapping boxes with
	// lower confidences
	vector<int> indices;
	NMSBoxes(boxes, confidences, this->confThreshold, this->nmsThreshold, indices);
	for (size_t i = 0; i < indices.size(); ++i)
	{
		int idx = indices[i];
		Rect box = boxes[idx];
		this->drawPred(classIds[idx], confidences[idx], box.x, box.y,
			box.x + box.width, box.y + box.height, frame);
	}
}

void YOLO::drawPred(int classId, float conf, 
	int left, int top, int right, int bottom, Mat& frame)   
// Draw the predicted bounding box
{
	//Draw a rectangle displaying the bounding box
	rectangle(frame, Point(left, top), Point(right, bottom), Scalar(0, 0, 255), 3);

	//Get the label for the class name and its confidence
	string label = format("%.2f", conf);
	if (!this->classes.empty())
	{
		CV_Assert(classId < (int)this->classes.size());
		label = this->classes[classId] + ":" + label;
	}

	//Display the label at the top of the bounding box
	int baseLine;
	Size labelSize = getTextSize(label, FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
	top = max(top, labelSize.height);
	putText(frame, label, Point(left, top), FONT_HERSHEY_SIMPLEX, 0.75, 
			Scalar(0, 255, 0), 1);
}

void YOLO::detect(Mat& frame)
{
	Mat blob;
	blobFromImage(frame, blob, 1 / 255.0, 
				  Size(this->inpWidth, this->inpHeight), 
				  Scalar(0, 0, 0), true, false);
	
	this->net.setInput(blob);
	vector<Mat> outs;
	this->net.forward(outs, this->net.getUnconnectedOutLayersNames());
	this->postprocess(frame, outs);

	vector<double> layersTimes;
	double freq = getTickFrequency() / 1000;
	double t = net.getPerfProfile(layersTimes) / freq;
	string label = format("%s Inference time : %.2f ms", this->netname, t);
	putText(frame, label, Point(0, 30), FONT_HERSHEY_SIMPLEX, 1, Scalar(0, 0, 255), 2);
	//imwrite(format("%s_out.jpg", this->netname), frame);
}

int main()
{
	YOLO yolo_model(yolo_nets[0]);
	string imgpath = "bus.jpg";
	Mat srcimg = imread(imgpath);
	yolo_model.detect(srcimg);

	static const string kWinName = "Deep learning object detection in OpenCV";
	namedWindow(kWinName, WINDOW_NORMAL);
	imshow(kWinName, srcimg);
	waitKey(0);
	destroyAllWindows();
}
  • 11
    点赞
  • 149
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
要在C++使用OpenCV YOLO,需要遵循以下步骤: 1. 下载并安装OpenCVYOLOv3。可以从官方网站下载OpenCV,而YOLOv3可以从GitHub下载。 2. 在C++代码中加载YOLOv3模型。可以使用OpenCV的dnn模块来加载模型。以下是一个示例: ``` cv::dnn::Net net = cv::dnn::readNetFromDarknet("yolov3.cfg", "yolov3.weights"); ``` 这将创建一个包含YOLOv3模型OpenCV深度学习网络。 3. 加载图像并将其传递给YOLOv3模型进行预测。可以使用OpenCV的imread函数加载图像,并使用cv::dnn::blobFromImage函数将其转换为模型所需的格式。以下是一个示例: ``` cv::Mat image = cv::imread("image.jpg"); cv::Mat blob = cv::dnn::blobFromImage(image, 1/255.0, cv::Size(416, 416), cv::Scalar(0,0,0), true, false); net.setInput(blob); cv::Mat detection = net.forward(); ``` 这将加载图像,将其转换为模型所需的格式,将其传递给模型进行预测,并返回检测结果。 4. 处理检测结果并在图像上绘制边界框。可以使用OpenCV的Mat类来处理检测结果,并使用cv::rectangle函数在图像上绘制边界框。以下是一个示例: ``` for (int i = 0; i < detection.rows; ++i) { float confidence = detection.at<float>(i, 4); if (confidence > 0.5) { int x1 = static_cast<int>(detection.at<float>(i, 0) * image.cols); int y1 = static_cast<int>(detection.at<float>(i, 1) * image.rows); int x2 = static_cast<int>(detection.at<float>(i, 2) * image.cols); int y2 = static_cast<int>(detection.at<float>(i, 3) * image.rows); cv::rectangle(image, cv::Point(x1, y1), cv::Point(x2, y2), cv::Scalar(0, 255, 0), 2); } } ``` 这将遍历检测结果,筛选出置信度大于0.5的检测结果,并在图像上绘制边界框。 这样,就可以在C++使用OpenCV YOLO进行目标检测了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

耐心的小黑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值