在Windows环境下调用GPU部署yolov5模型

本文介绍了如何在Windows 10系统中,利用opencv4.4.0+opencv_contrib4.4.0+cuda10.2的环境,部署和运行yolov5模型。主要涉及yolo.h和main_yolo.cpp两个关键代码文件,其中main_yolo.cpp是主程序,可以处理图片、视频和摄像头输入。提供了编译参考博客链接和图片测试效果。
摘要由CSDN通过智能技术生成

在win10+opencv4.4.0+opencv_contrib4.4.0+cuda10.2环境下部署yolov5模型

编译过程参考了这一篇博客

https://blog.csdn.net/hai_fellow_Z/article/details/116002203

主要有两个代码yolo.h和main_yolo.cpp

主程序为main_yolo.cpp文件

以下代码main函数可以分别实现图片、视频、摄像头的调用


#include "yolo.h"  
#include <opencv2/opencv.hpp>
using namespace cv;


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

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

	string modelFile = this->netname;
	modelFile += ".onnx";
	modelFile = "F:\\code\\a14-yolov5-dnn-opencv\\x64\\Release\\yolov5s.onnx";
	
	this->net = readNet(modelFile);
	
	// 调用GPU
	net.setPreferableTarget(DNN_TARGET_CUDA);
	net.setPreferableBackend(DNN_BACKEND_CUDA);

	// 调用CPU
	/*net.setPreferableTarget(DNN_TARGET_CPU);
	net.setPreferableBackend(DNN_BACKEND_OPENCV);*/
	
	

}

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);
	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);
	//rectangle(frame, Point(left, top - int(1.5 * labelSize.height)), Point(left + int(1.5 * labelSize.width), top + baseLine), Scalar(0, 255, 0), FILLED);
	putText(frame, label, Point(left, top), FONT_HERSHEY_SIMPLEX, 0.75, Scalar(0, 255, 0), 1);
}

void YOLO
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值