赞
踩
确保已经完成了yolov5的环境配置并实现了训练自己的数据集,得到权重文件
本文整体结构和流程参考:Yolov5训练自己的数据集+TensorRT加速+Qt部署
完善知识体系的知识:win32和x86以及x64的区别
本人使用的环境记录:TensorRT版本不同,对应的lib库会有不同
Win10
cuda 11.1
cudnn 8.5.0
TensorRT 8.2.1.8
Visual Studio 2019
Opencv 4.5.5
Qt 5.14.2
头文件包含目录:
D:\Program Files\TensorRT-8.2.1.8\include
D:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.1\include
D:\OpenCV\v455\opencv\build\include
D:\OpenCV\v455\opencv\build\include\opencv2
lib 库目录:TensorRT的lib文件夹不分x64和Win32,Ohttps://blog.csdn.net/qq_22487889/article/details/127040950penCV只有x64,CUDA分x64和Win32
D:\Program Files\TensorRT-8.2.1.8\lib
D:\OpenCV\v455\opencv\build\x64\vc15\lib
D:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.1\lib\x64
D:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.1\lib\Win32
//一般不用最后一行的Win32
附加依赖项:根据环境的不同,需要依赖的lib文件可能不同(注意注意:不要在附加依赖项中添加成了dll文件!!!!)
//前5个(一般不用)是CUDA/vxx.x/lib/Win32拥有的,5-8是x64所拥有的
//再后面除了opencv以外都是TensorRT拥有的,但是我用的
cuda.lib
cudadevrt.lib
cudart_static.lib
OpenCL.lib
cudart.lib
cublas.lib
cudnn.lib
cudnn64_8.lib
myelin64_1.lib
nvinfer.lib
nvinfer_plugin.lib
nvonnxparser.lib
nvparsers.lib
opencv_world455.lib //opencv版本不同就不一样
由于TensorRT8.2.18没有myelin64_1.lib,会报错:无法打开输入文件“myelin64_1.lib“,如果是没有该库文件的,删除myelin64_1.lib这一行
.pt文件→.wts文件→.engine文件
此处参考Yolov5训练自己的数据集+TensorRT加速+Qt部署的 3.1 节即可
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc.hpp>
#include<iostream>
using namespace cv;
using namespace std;
int main() {
Mat image = Mat::zeros(300, 600, CV_8UC3);
circle(image, Point(300, 200), 100, Scalar(0, 255, 120), -100);
circle(image, Point(400, 200), 100, Scalar(255, 255, 255), -100);
imshow("show window", image);
waitKey(0);
return 0;
}
结果:
参考:
BiliBili:win10 使用TensorRT部署 yolov5-v4.0(C++)(二)
CSDN:win10 使用TensorRT部署 yolov5-v4.0(C++)
如果使用的是自己的数据集,记得修改 yololayer.h 头文件,主要是修改分类和识别框
错误记录:直接使用参考博客的 CMakeLists 会出现错误,需要添加tensorrtx/yolov5/CMakeLists.txt的
add_definitions(-DAPI_EXPORTS)
注意是 yolov5-5.0 版本,最后我的 CMakeLists 如下:文件目录要和自己的对应
cmake_minimum_required(VERSION 2.8)
#=========================================================
project(yolov5) #1 工程名
set(OpenCV_DIR "D:\\OpenCV\\v455\\opencv\\build") #2 opencv目录
set(OpenCV_INCLUDE_DIRS ${OpenCV_DIR}\\include) #3
set(OpenCV_LIB_DIRS ${OpenCV_DIR}\\x64\\vc15\\lib) #4
set(OpenCV_Debug_LIBS "opencv_world455d.lib") #5
set(OpenCV_Release_LIBS "opencv_world455.lib") #6
set(TRT_DIR "D:\\Program Files\\TensorRT-8.2.1.8") #7
set(TRT_INCLUDE_DIRS ${TRT_DIR}\\include) #8
set(TRT_LIB_DIRS ${TRT_DIR}\\lib) #9
set(Dirent_INCLUDE_DIRS "D:\\DeepLearning\\tensorrtx-yolov5-v5.0\\yolov5") #10
#=========================================================
add_definitions(-std=c++11)
add_definitions(-DAPI_EXPORTS)
option(CUDA_USE_STATIC_CUDA_RUNTIME OFF)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_BUILD_TYPE Debug)
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads)
# setup CUDA
find_package(CUDA REQUIRED)
message(STATUS " libraries: ${CUDA_LIBRARIES}")
message(STATUS " include path: ${CUDA_INCLUDE_DIRS}")
include_directories(${CUDA_INCLUDE_DIRS})
####
enable_language(CUDA) # add this line, then no need to setup cuda path in vs
####
include_directories(${PROJECT_SOURCE_DIR}/include) #11
include_directories(${TRT_INCLUDE_DIRS}) #12
link_directories(${TRT_LIB_DIRS}) #13
include_directories(${OpenCV_INCLUDE_DIRS}) #14
link_directories(${OpenCV_LIB_DIRS}) #15
include_directories(${Dirent_INCLUDE_DIRS}) #16
# -D_MWAITXINTRIN_H_INCLUDED for solving error: identifier "__builtin_ia32_mwaitx" is undefined
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Ofast -D_MWAITXINTRIN_H_INCLUDED")
# setup opencv
find_package(OpenCV QUIET
NO_MODULE
NO_DEFAULT_PATH
NO_CMAKE_PATH
NO_CMAKE_ENVIRONMENT_PATH
NO_SYSTEM_ENVIRONMENT_PATH
NO_CMAKE_PACKAGE_REGISTRY
NO_CMAKE_BUILDS_PATH
NO_CMAKE_SYSTEM_PATH
NO_CMAKE_SYSTEM_PACKAGE_REGISTRY
)
message(STATUS "OpenCV library status:")
message(STATUS " version: ${OpenCV_VERSION}")
message(STATUS " lib path: ${OpenCV_LIB_DIRS}")
message(STATUS " Debug libraries: ${OpenCV_Debug_LIBS}")
message(STATUS " Release libraries: ${OpenCV_Release_LIBS}")
message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")
add_executable(yolov5 ${PROJECT_SOURCE_DIR}/yolov5.cpp ${PROJECT_SOURCE_DIR}/common.hpp ${PROJECT_SOURCE_DIR}/yololayer.cu ${PROJECT_SOURCE_DIR}/yololayer.h) #17
target_link_libraries(yolov5 "nvinfer" "nvinfer_plugin") #18
target_link_libraries(yolov5 debug ${OpenCV_Debug_LIBS}) #19
target_link_libraries(yolov5 optimized ${OpenCV_Release_LIBS}) #20
target_link_libraries(yolov5 ${CUDA_LIBRARIES}) #21
target_link_libraries(yolov5 Threads::Threads)
yolov5.exe -s best20220918.wts best20220918.engine s
生成成功:
yolov5.exe -d best20220918.engine ./image_dir
将模型封装成动态链接库(.dll)的形式,从而在Qt中调用。
用于完善自己知识体系的知识:
VS中空项目、win32项目、控制台程序的区别
该节的内容比较长,新写了一个文章,内容请看:VS2019 - 动态库的编写和调用
本节参考:
CSDN:VS2019环境下C++动态链接库(DLL)的创建与调用
BIliBili:Visual Studio 2019-编写C++动态链接库
BiliBili:Visual Studio 2019-显式调用C++动态库
该节的内容请看:(win10) yolov5-v5.0导出动态链接库-TensorRT+VS2019+CMake
本节新增参考参考:CSDN:yolov5动态链接库DLL导出(TensorRT)
BiliBili:win10 使用TensorRT部署 yolov5-v4.0(C++)(三)
上述采用的是yolov5-v4.0,而我的是yolov5-v5.0,因此代码是需要根据自己的版本进行更改的
//main.cpp
#pragma once
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/dnn.hpp>
#include "Detection.h"
#include "dirent.h"
#include "yololayer.h"
using namespace std;
int main()
{
Connect connect;
YOLOV5* yolo_dll = connect.Create_YOLOV5_Object();
cv::VideoCapture capture(0);
if (!capture.isOpened()) {
std::cout << "Error opening video stream or file" << std::endl;
return -1;
}
yolo_dll->Initialize("./best20220918.engine", 0);
while (1)
{
cv::Mat frame;
capture >> frame;
vector<cv::Rect> Boxes;
vector<const char*> ClassLables;
yolo_dll->Detecting(frame, Boxes, ClassLables);
cv::imshow("output", frame);
cv::waitKey(1);
}
connect.Delete_YOLOV5_Object(yolo_dll);
return 0;
}
#将此路径加入项目属性包含目录中
D:\DeepLearning\tensorrtx-yolov5-v5.0_dll\yolov5
D:\Program Files\TensorRT-8.2.1.8\include
D:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.1\include
D:\OpenCV\v455\opencv\build\include
D:\OpenCV\v455\opencv\build\include\opencv2
#将此路径加入项目属性的库目录中,也就是我们刚刚生成dll的文件目录
#TensorRT的lib文件夹不分x64和Win32,OpenCV只有x64,CUDA分x64和Win32
D:\DeepLearning\tensorrtx-yolov5-v5.0_dll\yolov5\build_dll\Release
D:\Program Files\TensorRT-8.2.1.8\lib
D:\OpenCV\v455\opencv\build\x64\vc15\lib
D:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.1\lib\x64
D:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.1\lib\Win32
#在输入链接器添加依赖库
yolov5.lib
#前5个(一般不用)是CUDA/vxx.x/lib/Win32拥有的,5-8是x64所拥有的
#再后面除了opencv以外都是TensorRT拥有的
cuda.lib
cudadevrt.lib
cudart_static.lib
OpenCL.lib
cudart.lib
cublas.lib
cudnn.lcucuda
cudnn64_8.lib
nvinfer.lib
nvinfer_plugin.lib
nvonnxparser.lib
nvparsers.lib
opencv_world455.lib
opencv_world455d.lib //opencv版本不同就不一样
if (img.rows < 320 || img.cols < 320)
方法1(我没有使用):Visual Studio 2019 配置 Qt5.14.2 开发环境并部署,参考:该博客的五、利用Qt进行部署
该方法要注意:Qt中使用的编译器(MinGW or MSVC)必须和编译库使用的编译器相同!!!
参考:OpenCV中Rect()函数常用操作和使用详解(含绘图示例)+Rectangle()函数用法 按照自己需要选取参数。
我增加的代码如下:
for (auto r : Boxes) {
cout << "Boxes:" << r << endl;
cout << "左上:" << r.tl() << "右下" << r.br() << "\t";
}
输出如图:可以看出Boxes的参数是【宽 × 长 from 左上角】
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。