C++调用Yolov3模型实现目标检测

2023-05-16

C++调用Yolov3模型实现目标检测

使用开源权重文件,此训练模型包含80种物体

文件下载地址:

预训练权重文件:
https://pjreddie.com/media/files/yolov3.weights

网络配置文件:
https://github.com/pjreddie/darknet/blob/master/cfg/yolov3.cfg

coco.names:
https://github.com/pjreddie/darknet/blob/master/data/coco.names

计算机环境:Visual Studio配置opencv

下面展示 代码

#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;

string pro_dir = "E:/process/VSproject/"; //项目根目录

float confThreshold = 0.5; // Confidence threshold
float nmsThreshold = 0.4;  // Non-maximum suppression threshold
int inpWidth = 416;  // Width of network's input image
int inpHeight = 416; // Height of network's input image
vector<string> classes;

// Remove the bounding boxes with low confidence using non-maxima suppression
void postprocess(Mat& frame, const vector<Mat>& out);
// Draw the predicted bounding box
void drawPred(int classId, float conf, int left, int top, int right, int bottom, Mat& frame);
// Get the names of the output layers
vector<String> getOutputsNames(const Net& net);
void detect_image(string image_path, string modelWeights, string modelConfiguration, string classesFile);
void detect_video(string video_path, string modelWeights, string modelConfiguration, string classesFile);

int main(int argc, char** argv)
{
 	// Give the configuration and weight files for the model
 	String modelConfiguration = pro_dir + "yolov3/yolov3.cfg";
 	String modelWeights = pro_dir + "yolov3/yolov3.weights";
 	string image_path = pro_dir + "yolov3/dog.jpg";
 	string classesFile = pro_dir + "yolov3/coco.names";// "coco.names";

 	//detect_image(image_path, modelWeights, modelConfiguration, classesFile);
 	string video_path = pro_dir + "yolov3/movie.avi";
 	detect_video(video_path, modelWeights, modelConfiguration, classesFile);
 	cv::waitKey(0);

 	return 0;
}

void detect_image(string image_path, string modelWeights, string modelConfiguration, string classesFile) {
 	// Load names of classes
 	ifstream ifs(classesFile.c_str());
 	string line;
 	while (getline(ifs, line)) classes.push_back(line);
 
 	// Load the network
 	Net net = readNetFromDarknet(modelConfiguration, modelWeights);
 	net.setPreferableBackend(DNN_BACKEND_OPENCV);
 	net.setPreferableTarget(DNN_TARGET_OPENCL);
 
 	// Open a video file or an image file or a camera stream.
 	string str, outputFile;
 	cv::Mat frame = cv::imread(image_path);
 
 	// Create a window
 	static const string kWinName = "Deep learning object detection in OpenCV";
 	namedWindow(kWinName, WINDOW_NORMAL);
 
 	// Stop the program if reached end of video
 	// Create a 4D blob from a frame.
 	Mat blob;
 	blobFromImage(frame, blob, 1 / 255.0, cvSize(inpWidth, inpHeight), Scalar(0, 0, 0), true, false);
 
 	//Sets the input to the network
 	net.setInput(blob);
 
 	// Runs the forward pass to get output of the output layers
 	vector<Mat> outs;
 	net.forward(outs, getOutputsNames(net));
 
 	// Remove the bounding boxes with low confidence
 	postprocess(frame, outs);
 
 	// Put efficiency information. The function getPerfProfile returns the overall time for inference(t) and the timings for each of the layers(in layersTimes)
 	vector<double> layersTimes;
 	double freq = getTickFrequency() / 1000;
 	double t = net.getPerfProfile(layersTimes) / freq;
 	string label = format("Inference time for a frame : %.2f ms", t);
 	putText(frame, label, Point(0, 15), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 255));
 
 	// Write the frame with the detection boxes
 	imshow(kWinName, frame);
 	cv::waitKey(30);
}

void detect_video(string video_path, string modelWeights, string modelConfiguration, string classesFile) {
	string outputFile = "./yolo_out_cpp.avi";;
	
	// Load names of classes
	ifstream ifs(classesFile.c_str());
	string line;
	while (getline(ifs, line)) classes.push_back(line);
	
	// Load the network
	Net net = readNetFromDarknet(modelConfiguration, modelWeights);
	net.setPreferableBackend(DNN_BACKEND_OPENCV);
	net.setPreferableTarget(DNN_TARGET_CPU);
	
	// Open a video file or an image file or a camera stream.
	VideoCapture cap;
	
	//VideoWriter video;
	Mat frame, blob;
	try {
	
		// Open the video file
		ifstream ifile(video_path);
		if (!ifile) throw("error");
		cap.open(video_path);
	}
	catch (...) {
		cout << "Could not open the input image/video stream" << endl;
		return;
	}
	
	// Create a window
	static const string kWinName = "Deep learning object detection in OpenCV";
	namedWindow(kWinName, WINDOW_NORMAL);
	
	// Process frames.
	while (waitKey(1) < 0)
	{
	
		// get frame from the video
		cap >> frame;
		
		// Stop the program if reached end of video
		if (frame.empty()) {
			cout << "Done processing !!!" << endl;
			cout << "Output file is stored as " << outputFile << endl;
			waitKey(3000);
			break;
		}
		
		// Create a 4D blob from a frame.
		blobFromImage(frame, blob, 1 / 255.0, cvSize(inpWidth, inpHeight), Scalar(0, 0, 0), true, false);
		
		//Sets the input to the network
		net.setInput(blob);
		
		// Runs the forward pass to get output of the output layers
		vector<Mat> outs;
		net.forward(outs, getOutputsNames(net));
		
		// Remove the bounding boxes with low confidence
		postprocess(frame, outs);
		
		// Put efficiency information. The function getPerfProfile returns the overall time for inference(t) and the timings for each of the layers(in layersTimes)
		vector<double> layersTimes;
		double freq = getTickFrequency() / 1000;
		double t = net.getPerfProfile(layersTimes) / freq;
		string label = format("Inference time for a frame : %.2f ms", t);
		putText(frame, label, Point(0, 15), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 255));
		
		// Write the frame with the detection boxes
		Mat detectedFrame;
		frame.convertTo(detectedFrame, CV_8U);
		
		//video.write(detectedFrame);
		imshow(kWinName, frame);
	}
	cap.release();
	
	//video.release();
}
// Remove the bounding boxes with low confidence using non-maxima suppression

void postprocess(Mat& frame, const vector<Mat>& outs)
{
	vector<int> classIds;
	vector<float> confidences;
	vector<Rect> boxes;

	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.
		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
			minMaxLoc(scores, 0, &confidence, 0, &classIdPoint);
			if (confidence > 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, confThreshold, nmsThreshold, indices);
	for (size_t i = 0; i < indices.size(); ++i)
	{
		int idx = indices[i];
		Rect box = boxes[idx];
		drawPred(classIds[idx], confidences[idx], box.x, box.y,
			box.x + box.width, box.y + box.height, frame);
	}
}

// Draw the predicted bounding box
void drawPred(int classId, float conf, int left, int top, int right, int bottom, Mat& frame)
{
	//Draw a rectangle displaying the bounding box
	rectangle(frame, Point(left, top), Point(right, bottom), Scalar(255, 178, 50), 3);
	//Get the label for the class name and its confidence
	string label = format("%.2f", conf);
	if (!classes.empty())
	{
		CV_Assert(classId < (int)classes.size());
		label = 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);
	rectangle(frame, Point(left, top - round(1.5*labelSize.height)), Point(left + round(1.5*labelSize.width), top + baseLine), Scalar(255, 255, 255), FILLED);
	putText(frame, label, Point(left, top), FONT_HERSHEY_SIMPLEX, 0.75, Scalar(0, 0, 0), 1);
}
// Get the names of the output layers
vector<String> getOutputsNames(const Net& net)
{
	static vector<String> names;
	if (names.empty())
	{
		//Get the indices of the output layers, i.e. the layers with unconnected outputs
		vector<int> outLayers = net.getUnconnectedOutLayers();
		//get the names of all the layers in the network
		vector<String> layersNames = net.getLayerNames();
		// Get the names of the output layers in names
		names.resize(outLayers.size());
		for (size_t i = 0; i < outLayers.size(); ++i)
			names[i] = layersNames[outLayers[i] - 1];
	}
	return names;
}
 

效果展示

在这里插入图片描述
在这里插入图片描述

后续将介绍如何使用openvino工具加速模型的推理速度

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C++调用Yolov3模型实现目标检测 的相关文章

  • YOLOv3 从入门到部署:(五)YOLOv3模型的部署(基于C++ opencv)

    文章目录 YOLOv3 从入门到部署 xff1a xff08 五 xff09 YOLOv3模型的部署 xff08 基于C 43 43 opencv xff09 目录关于opencv的DNN介绍代码讲解效果展示 YOLOv3 从入门到部署 x
  • 【darknet】【yolov3】训练踩坑

    本文已解决问题概述 xff1a 测试准确率时 xff0c 没有results 文件夹的访问权限 xff1a Segmentation fault 执行darknet 相关命令是 xff0c 无法找到 libcudart so 10 0 文件
  • 深度学习_NMS代码详解YOLOv3及Fast R-CNN例子

    先放Fast R CNN的NMS代码 这部分是关于 nms 实现的代码 后续再加下其他的版本 流程 xff1a 首先对检测结果的 score 取出最大的元素 xff0c 然后将置信度最高的框与其他框取交集 xff0c 计算 iou xff0
  • jetson nano 部署yoloV3,yoloV4,yoloV3-tiny,yoloV4-tiny

    转载自 xff1a jetson nano 部署yoloV3 yoloV4 yoloV3 tiny yoloV4 tiny dingding的专栏 CSDN博客 jetson nano 部署yoloV3 yoloV4 yoloV3 tiny
  • YOLOV4与YOLOV3的区别

    YOLOV4与YOLOV3的区别 A big bliss的博客 CSDN博客 yolov3和yolov4的区别 首先 xff0c 先大概的说下二者之间的差别 xff1a 1 特征提取网络的不同 2 激活函数的不同 3 loss的不同 4 数
  • YOLOv3

    YOLOv3 论文信息论文标题 xff1a 论文作者 xff1a 收录期刊 会议及年份 xff1a 论文学习YOLOv3 网络架构 xff1a YOLO 输出特征图解码 xff08 前向过程 xff09 xff1a 训练策略与损失函数 xf
  • yolov3 批量处理图片获得检测框坐标

    前言 yolov3实现检测 xff0c 需要所有检测框的坐标实现定位 在darknet master项目下实现检测框坐标的获取 前提 系统 xff1a Windows 语言 xff1a C 项目 xff1a darknet master 开
  • yolov3的训练(一)下载与训练

    darknet框架简介 https blog csdn net mao hui fei article details 113820303 AlexeyAB大佬的关于darknet的详细文档信息 https github com Alexe
  • 使用Keras版本的Yolov3训练自己的数据集和进行目标检测时,需要注意的一些问题

    最近因为工作需要 xff0c 使用了Yolo v3做目标检测 由于它自带的数据集完全不能够满足需要 xff0c 只能从头开始自己训练 当然这必须要用python来做了 xff0c 不能用C语言 首先 xff0c 我发现那个著名的Keras版
  • darknet yolov3 训练自己的数据集,Cannot load image ““ STB Reason: can‘t fopen报错解决

    项目场景 xff1a 利用darmet 43 yolov3训练自己的数据集 首先 图片标注 制作数据集 训练的过程 xff0c 网上有很多教程 xff0c 我主要参考了这两个 xff0c 他们里面有python代码 xff0c 可以直接用
  • 【Darknet-53】YOLOv3 backbone Darknet-53 详解

    文章目录 1 模型计算量与参数量2 Darknet 53网络3 感谢链接 1 模型计算量与参数量 模型计算量与参数量的计算方式主要有两种 xff0c 一种是使用thop库 xff0c 一种是使用torchsummaryX 使用pip ins
  • C++调用Yolov3模型实现目标检测

    C 43 43 调用Yolov3模型实现目标检测 使用开源权重文件 xff0c 此训练模型包含80种物体 文件下载地址 xff1a 预训练权重文件 xff1a https pjreddie com media files yolov3 we
  • KERAS-YOLOV3的代码走读

    KERAS YOLOV3的代码走读 GITHUB地址 xff1a https github com qqwweee keras yolo3 YOLOV3的论文中文翻译 xff1a https zhuanlan zhihu com p 349
  • Yolov3+C+++opencv+VS2015成功检测

    nbsp 前言 nbsp nbsp nbsp 最近在用yolov3进行目标检测 也有一个多星期了 想把最近做出的一些成果记录下来 供大家参考下 我的运行环境是C opencv VS2015 yolov3 下面将简单介绍下yolo的一些思想
  • YOLOv3 从入门到部署(四)YOLOv3模型导出onnx(基于pytorch)

    YOLOv3 从入门到部署 四 YOLOv3模型导出onnx 基于pytorch 文章目录 YOLOv3 从入门到部署 四 YOLOv3模型导出onnx 基于pytorch 目录 概述 pytorch导出onnx采坑 转onnx代码 使用D
  • 【darknet yolo】could not open file xx.cfg

    错误图如上 原因 data文件的编码不对 用notepad 打开发现 错误的 正确的 发现错误的比正确的每行换行的时候多了一个cr 解决 ubuntu下安装dos2unix 然后转换下 dos2unix 52 data
  • YOLO v3基于ROS应用记录

    有时候 就要敢于背上超出自己预料的包袱 真的努力后 你会发现自己要比想象的优秀很多 愿在别人眼里算不上梦想的梦想 成真 言归正传 记录下之前在ROS下跑yolov3的历程吧 感觉现在视觉感知领域用yolo的比faster RCNN多很多了
  • 目标检测标签文件txt转成xml

    最近在用ppyolo训练好的模型对新采集的数据进行标记 再人工微调 减少从头打标签的时间 但是推理保存的结果都是txt格式的 想要在labelimg中可视化 那就需要将txt转换成xml 以下代码即可完成这一功能 coding UTF 8
  • 【ModelArts系列】华为ModelArts Notebook训练yolov3模型(开发环境)

    一 参考资料 二 相关介绍 在ModelArts的 notebook中运行ModelZoo中模型 以yolov3为例 训练集为 COCO2014 运行环境 ModelArts notebook 模型 ModelZoo yolov3 数据集
  • 深入浅出Yolo系列之Yolov3&Yolov4&Yolov5&Yolox核心基础知识完整讲解

    因为工作原因 项目中经常遇到目标检测的任务 因此对目标检测算法会经常使用和关注 比如Yolov3 Yolov4算法 Yolov5算法 Yolox算法 当然 实际项目中很多的第一步 也都是先进行目标检测任务 比如人脸识别 多目标追踪 REID

随机推荐

  • JS实现快速排序(代码+讲解)

    OK xff0c 排序这一个篇章也快要结束了 这一篇主要说的是快速排序 xff0c 说的方式主要还是先说原理 xff0c 然后再用代码来进行实现 所谓快速排序 xff0c 就是分为三步走 xff1a 第一步 xff1a 选择第一个数字分离出
  • Object.defineProperty方法(详解)

    OK xff0c 这一篇主要想说一下Object defineProperty这个方法 这个方法也是Vue数据双向绑定原理的常见面试题 所以也是有必要好好掌握的哦 首先我们知道JS中是支持面向对象编程的 xff0c 也是有着对象和类这样的概
  • 原生JS实现Promise(详解)

    摘要 首先呢 xff0c Promise是异步中比较重要的知识点 xff0c 学习的最好方法就是掌握它的基本原理 所以这一篇主要说一下如何用JS来实现一个自己的promise 构造函数 首先我们来看一下我们是如何使用promise的 xff
  • 解决winscp连接ubuntu虚拟机连续超时

    1 禁用虚拟机网络 在windows系统找到网络适配器 xff0c 禁用VMnet1和VMnet8 2 更改网络连接模式并测试网络是否连通 菜单栏 虚拟机 设置 网络适配器 xff0c 将网络模式改为桥接模式 xff0c 勾选 复制物理网络
  • Http的各种请求方法(详解)

    摘要 我们知道 xff0c 当我们访问各种网页的时候 xff0c 之所以能够看到页面 xff0c 根本原因是发送了http请求然后得到了响应 xff0c 从而页面才会弹出来 再或者我们上传一些照片和视频时 xff0c 之所以可以上传成功也是
  • React中ref的使用方法和使用场景(详解)

    摘要 不管在Vue中还是React xff0c 如果我们想使用一个元素的DOM xff0c 不需要通过JS中操纵DOM的方法 xff0c 它们提供了一个专属的API就是ref 而Vue中的ref可能比较简单 xff0c 这一篇主要讲一下如何
  • 原生JS的拖拽属性draggable(详解)

    摘要 作为h5新增的属性draggable xff0c 它能够给与一切的html元素拖动的效果 而在这个属性之下 xff0c 也有着关于拖动效果的各个方法 而这一篇文章 xff0c 主要就是说一下关于draggable属性的使用以及工作场景
  • 一篇搞定JS的位运算(公式+力扣真题)--- 持续更新

    摘要 位操作 xff08 Bit Manipulation xff09 是程序设计中对位模式或二进制数的一元和二元操作 在许多古老的微处理器上 xff0c 位运算比加减运算略快 xff0c 通常位运算比乘除法运算要快很多 在现代编程语言中
  • 【C++实现HTTP服务器项目记录】HTTP报文处理

    文章目录 一 HTTP报文格式1 请求报文2 响应报文 二 解析HTTP请求报文1 有限状态机2 状态转换图3 代码实现 三 生成HTTP响应报文1 代码实现 四 内存映射五 获取文件属性六 高级I O1 聚集写2 解决大文件传输问题 一
  • 使用PX4 模拟无人机起降 jmavsim或Gazebo环境下

    安装PX4的教程较多 xff0c 可在linux xff0c macos windows上进行安装 推荐Linux 笔者使用的是Ubuntu20 04 原因如下图 xff1a 安装好PX4后 cd到PX4安装目录 xff0c 启动仿真器 x
  • C++标准库--IO类库

    文章目录 前言一 IO类二 文件输入输出1 fstream的操作2 文件模式 三 String流 前言 C 43 43 语言本身没有输入输出 xff0c 而是通过定义在标准库中的类型来处理IO xff0c 这些类型支持从文件 xff0c 控
  • 总线协议一(UART/RS232/RS485/IIC/SPI)

    目录 基础概述 xff1a 一 UART xff08 为串口通信方式 xff09 二 RS232协议 三 RS485协议 四 I2C总线协议 五 SPI总线 六 I2C和SPI的区别 基础概述 xff1a 总线的本质就是一根导线 xff0c
  • HTTP协议分析(完整版)

    HTTP协议 一 http协议简介二 http工作原理1 客户端连接到Web服务器2 发送HTTP请求3 服务器接受请求并返回HTTP响应4 释放连接TCP连接5 客户端浏览器解析HTML内容 三 http请求方法四 http状态码1 状态
  • 【linux网络编程】-UDP客户端

    客户端实现 1 注 xff1a 客户端实现中可以不用bind地址 xff0c 当你发送数据到指定服务端时 xff0c 操作系统会为客户端自动分配端口 span class token keyword int span span class
  • 删除字符串中的各种标点符号的方法

    1 用re findall w 43 string 匹配字母 xff0c 数字和下划线 xff0c 返回列表 2 用 join 列表 将列表中元素再拼接成字符串
  • 在ubuntu18.04中切换python版本(个人记录)

    首先通过ls命令查一下目前ubuntu系统中已安装的python版本 ls l usr bin grep python 然后通过以下命令查看目前有哪些可供选择的python版本 update alternatives list python
  • ORB-SLAM2建立自己的数据集

    ORB SLAM2建立自己的数据集 使用ZED2拍摄视频剪裁视频分割图片运行数据集记录 使用ZED2拍摄视频 这里没有使用程序将ZED2双目相机拍摄成两幅画面 xff0c 而是直接使用软件拍摄视频 若有大佬有相关代码还是很希望能发我下 拍的
  • STM32—DMA功能讲解串口发送

    目录 一 DMA基本介绍 1 DMA的定义 2 DMA数据传输 二 DMA功能框图 编辑 1 DMA请求 2 通道 3 仲裁器 三 DMA数据配置 1 传输方向及地址 2 传输数据大小及单位 3 传输完成 三 DMA初始化结构体详解 1 D
  • 最新 Xilinx vivado IP许可申请

    最新 Xilinx vivado IP许可申请 xilinx的fpga使用vivado开发 xff0c zynq系列fpga的SOC开发成为主流 xff0c 加快fpga开发 xff0c 也进一步提高了fpga开发的灵活性 xilinx提供
  • C++调用Yolov3模型实现目标检测

    C 43 43 调用Yolov3模型实现目标检测 使用开源权重文件 xff0c 此训练模型包含80种物体 文件下载地址 xff1a 预训练权重文件 xff1a https pjreddie com media files yolov3 we