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

2023-05-16

一、前言

因为之前都是直接使用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();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

  • 气动爬行机器人设计

    气动爬行机器人设计 简介项目规则想法原理电控原理腿部机构转向机构其他结构 结语 简介 最近在学校做一个气动的爬行机器人 xff0c 算是一个二级项目 xff0c 虽然名义上有分组 xff0c 但是基本上是我自己在做 xff0c 觉得有些东西
  • ImportError: cannot import name 'Flask'解决方法

    在写bug的时候发现了这么一个错误 xff0c ImportError cannot import name Flask xff0c 自己建一个test脚本 xff0c 代码复制过去后发现可以运行 xff0c 经检查发现脚本文件的名称可能和
  • python lambda表达式详解

    64 python lambda表达式详解 1 lambda简介 先来看一段代码示例 xff1a 第一行是lambda声明 xff0c x y相当于传入的参数 xff0c 整个函数会返回x 43 y的值 lambda作为一个表达式 xff0
  • CNN(卷积神经网络)详解

    CNN卷积神经网络详解 Why CNN局部感受野 local receptive fields 权值共享 Shared weights and biases 池化 Pooling 总的来看 Why CNN 首先回答这样一个问题 xff0c
  • mysql索引笔记 innodb null

    1 列有null 索引不失效 2 is not null 不会用索引 3 模糊查询 左边匹配 不会用索引 4 组合索引最左原则 5 设置索引 不加长度 默认长度为字段长度如varcher 255 单个索引字节不超过767 组合索引不超过30
  • Mysql避免索引失效

    要尽量避免这些不走索引的sql xff1a SELECT 96 sname 96 FROM 96 stu 96 WHERE 96 age 96 43 10 61 30 不会使用索引 xff0c 因为所有索引列参与了计算 SELECT 96
  • MySQL中的semi-join半连接

    MySQL中的semi join半连接
  • JDK8 lamdba

    1 方法调用
  • try catch finally执行顺序

    try catch finally执行顺序 内容简介代码示例执行结果 xff1a 分析 内容简介 代码中经常会在catch 或者finally中再次抛出异常 xff0c 传给调用者 xff08 如文件上传处理工具类 xff09 xff0c
  • C++中全局变量的使用

    在C 43 43 中全局变量的定义与使用做一下下面简单的记录 xff0c 方便日后查阅 xff0c 也与大家一起学习 1 全局变量的声明 1 在头文件 h中进行声明 xff0c 如果在此文件之外也要使用该变量 xff0c 则在变量声明前加e
  • mysql innodb

    mysql s sql优化 io 使用limit返回用到的字段 xff0c 不要返回太多无用字段和一些大字段 避免索引失效 创建索引 dd
  • C语言之什么是大小端,数组大小端,位域大小端,结构体大小端

    公众号 xff1a 嵌入式不难 本文仅供参考学习 xff0c 如有错误之处 xff0c 欢迎留言指正 理解大小端之前必须明白的三点 内存地址始终以字节为单位大小端只针对基本数据类型存在位域的情况下 xff0c 小端模式下先定义的位域从LSB
  • <Linux开发> linux应用开发-之-can通信开发例程

    xff1c Linux开发 xff1e linux应用开发 之 can通信开发例程 一 简介 对于Can通信的相关介绍 xff0c 读作不过多介绍了 xff0c 网上其它网友的介绍有很多 二 环境搭建 本次测试can通信的应用例程是运行在u
  • 使用RGBD相机模拟激光数据,用于move_base中添加新图层避障功能

    参考文章 xff1a ROS depthimage to laserscan ROS导航 向cost map中添加超声波障碍图层 一 RGBD模拟激光雷达数据 我使用的是RealSense双目相机 xff0c 首先使用的是ros自带的功能包
  • 公安视图库1400的协议

    一 平台注册 注销 1 1 注册 注销流程 注册流程1如图所示 图 1 下级平台主动向上级平台注册 xff1b 注册鉴权信息 xff08 用户名和密码 xff09 由上级平台提供 xff1b 1 2 报文说明 1 2 1 下级第一次注册 P
  • linux cmake交叉编译

    linux cmake交叉编译 linux cmake交叉编译下载测试添加依赖库需要注意的问题 linux cmake交叉编译 linux cmake交叉编译 xff0c 使用环境 xff1a ubuntu cmake gcc arm 10
  • OpenCV中使用RANSAC算法实现多张图像拼接

    思路 xff1a xff08 1 xff09 获取图像的特征点 xff0c 将每张图片的特征点保存到一个vector中 xff1b xff08 2 xff09 通过特征点匹配的方法 xff0c 找到每张图片的共有特征点 xff0c 并将其保
  • 数据安全--安全网关

    简介 对于数据安全来说 xff0c 安全网关是数据安全建设中极其重要的一部分 xff0c 我这里把它做了几种分类 xff0c 如下 xff1b 对内 xff1a 零信任安全网关 xff08 7层和4层 xff09 对外 xff1a 应用安全
  • idea使用svn拉取源码,创建分支,合并分支教程

    svn拉取源码 xff0c 创建分支 xff0c 合并分支教程 最近对svn的分支使用比较感兴趣了 xff0c 花时间研究了一下 xff0c 分享给大家 xff1a 此文章记录了idea使用svn的过程 xff1a 包括拉取源码 建立分支
  • Qt中QSS的简单使用

    样式表的组成 样式表由两大元素组成 xff1a 选择器和声明 选择器实际上可以理解为对象 xff0c 声明则是对该对象的属性的设置 样式表的使用 方法一 xff1a 在代码中调用setStyleSheet 来设置样式 xff1b 方法二 x

随机推荐

  • jetson nano pytorch安装及文件系统扩容

    jetson nano扩容 我安装的系统是16GB的 xff0c 如果不扩容的话文件系统只有16GB 首先安装gparted span class token function sudo span span class token func
  • Robust Real-time UAV Replanning Using Guided Gradient-based Optimization and Topological Paths

    fastplanner2 摘要相关工作基于梯度的路径优化拓扑路径规划 路径制导轨迹优化A 优化失效分析B 问题公式化 拓扑路径搜索A 拓扑等价关系B 拓扑路径图C 路径缩短和修剪 实时拓扑路径规划 这篇论文是港科大开源的无人机运动规划fas
  • curl 参数配置详情

    第一类 xff1a 对于下面的这些option的可选参数 xff0c value应该被设置一个bool类型的值 xff1a CURLOPT AUTOREFERER 当根据Location 重定向时 xff0c 自动设置header中的Ref
  • 分区隔离与数据安全交换技术

    一 背景 网络的物理隔离是很多网络设计者都不愿意的选择 xff0c 网络上要承载专用的业务 xff0c 其安全性一定要得到保障 然而网络的建设就是为了互通的 xff0c 没有数据的共享 xff0c 网络的作用也缩水了不少 xff0c 因此网
  • g++编译命令大全

    gcc amp g 43 43 现在是gnu中最主要和最流行的c amp c 43 43 编译器 g 43 43 是c 43 43 的命令 xff0c 以 cpp为主 xff0c 对于c语言后缀名一般为 c 这时候命令换做gcc即可 其实是
  • Ubuntu 14.04 配置iptables防火墙

    Ubuntu默认安装是没有开启任何防火墙的 xff0c 为了学习redis数据库 xff0c 需要启用防火墙设置 whereis iptables 查看系统是否安装防火墙可以看到 iptables sbin iptables usr sha
  • Qt显示汉字乱码问题

    转载自http blog csdn net u012790503 article details 52485263 测试1 新建test工程用于测试 xff0c main c文件内容如下 xff1a span class hljs prep
  • Anaconda使用总结

    原文地址 xff1a https www jianshu com p 2f3be7781451 序 Python易用 xff0c 但用好却不易 xff0c 其中比较头疼的就是包管理和Python不同版本的问题 xff0c 特别是当你使用Wi
  • A*寻路算法介绍

    你是否在做一款游戏的时候想创造一些怪兽或者游戏主角 xff0c 让它们移动到特定的位置 xff0c 避开墙壁和障碍物呢 xff1f 如果是的话 xff0c 请看这篇教程 xff0c 我们会展示如何使用A星寻路算法来实现它 xff01 在网上
  • RTK固定解什么意思

    RTK固定解什么意思 金丝大环刀的回答 知乎 RTK固定解什么意思 知乎 rtk固定解一般是指载波相位窄巷整周模糊度已经固定之后的解算结果 narrow int xff0c 精度可达厘米级至毫米级 除此之外 xff0c 还有宽巷解 xff0
  • QtCreate由MinGW编译的项目,换为MSVC编译器后编译无法通过

    解决方法 xff1a 1 将文件的编码格式设置为utf 8且BOM格式 xff1b 2 在 pro文件中添加如下代码 msvc QMAKE CFLAGS 43 61 utf 8 QMAKE CXXFLAGS 43 61 utf 8 其中方法
  • 2D nav goal 后小车不能沿着路径走

    2D nav goal 后小车不能沿着路径走 解决方法 xff1a 下载turtlebot3的代码 xff0c 编译 xff0c 把原来的模型改成turtlebot3的 真正的错因 xff1a xacro在melodic版本上的inorde
  • vscode配置header指令添加头部注释或KoroFileHeader自动添加头部解释

    1 xff08 1 xff09 新建Python文件输入header添加头部注释 vscode gt file gt Perferences gt User Snippets gt python 也可选其他语言 xff1a 则会生成pyth
  • Android Okhttp工具类的封装(okhttpUtils)

    Android Okhttp工具类 Android Okhttp工具类的封装 xff08 okhttpUtils xff09 近期的项目频繁使用到了网络交互 xff0c 采用的是第三方类库okhttp 以下是我对okhttp主要使用到的方法
  • 通过requests登录店小秘解决验证码问题

    登录常用手段就是 request post请求 selenium等自动化工具 这里讲一下使用requests实现自动登录 网站 现在网站登录基本上都有验证码 xff0c requests登录的难点在于将验证码与账号联系起来 xff0c 这里
  • 关于ubuntu18系统~/.bashrc文件中ros环境无法生效问题

    项目场景 xff1a ubuntu18 43 melodic 问题描述 xff1a 这几天一直遇到一个问题 xff0c 就是ubuntu18中ros的环境问题 xff0c 最开始的时候是一直用到好好的ros系统 xff0c 突然说无法识别r
  • 关于move_base无法加载的问题[move_base-11] process has died

    项目场景 xff1a kinetic下使用move base进行导航 问题描述 xff1a 之前move base使用的好好的 xff0c 结果突然之间不能用了 xff0c 出现move base功能包无法加载的情况 报错如下 xff1a
  • Jetson Nano利用普通引脚进行PWM波输出

    这篇博客我就简单介绍一下好了 xff0c 不深入讲了 需要注意的是 xff0c 我这里用到了ROS xff0c 所以运行之前需要安装ROS 初始化中有一个12和50 xff0c 分别代表频率 xff0c 完之后他就会让pin12自动输出50
  • Git基础 - git tag 一文真正的搞懂git标签的使用

    1 什么是tag 1 1 tag的简单理解 tag 中文我们可以称它为 标签 简单的理解 xff0c tag 就是 对某次 commit 的一个标识 xff0c 相当于起了一个别名 例如 xff0c 在项目发布某个版本的时候 xff0c 针
  • C++学习笔记十六:使用OpenCv(c++)调用yolo模型实现目标检测

    一 前言 因为之前都是直接使用python编程来实现目标检测 xff0c 而且是直接使用模型 于是就想了解一下使用c 43 43 语言如何进行目标检测 xff0c 也能帮助自己更好的熟悉c 43 43 的语法 简单起见 xff0c 使用op