所有前置环境包都在Q群公告中的网盘链接中
YOLO交流Q群 936625049
一个pt模型
官网链接:https://visualstudio.microsoft.com/zh-hans/downloads/
群文件中有全部环境的百度云链接,下载即可
打开安装即可
注意勾选C++部分
然后等他慢慢下载,下完就可以打开VS了
如果说你前面已经安装过CUDA,然后再安装的VS,可能会出现后面VS识别不到CUDA的问题,建议打开CUDA安装包把VS的这个模块再安装一次。
这两个项目左边是yolov5官方项目 右边是tensorrt的项目
下载地址:
https://github.com/ultralytics/yolov5/tree/v6.1
https://github.com/Monday-Leo/Yolov5_Tensorrt_Win10
我给的网盘链接里都有
然后用vscode以文件夹的方式打开第一个项目
(注意点击右下角切换python的环境)
这两行给他改掉,default 里头分别输入pt和wts的名字
parser.add_argument( '--weights', default="yolov5.pt" , help='Input weights (.pt) file path (required)')
parser.add_argument('--output', default="yolov5.wts",help='Output (.wts) file path (optional)')
官网下载3.4.6版本opencv 我这里下好放网盘了
解压到你想放的目录
我的电脑->属性->高级系统设置->环境变量,在系统变量中找到Path(如没有,自行创建),并双击编辑,将opencv路径填入并保存,如D:\MyCodeEnv\opencv\opencv\build\x64\vc15\bin
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-dX3UJCzp-1655841797087)(C:\Users\QiuYuSY\Desktop\typora\C++ 部署步骤演示.assets\image-20220622020557114.png)]
下载Tensorrt 8.2.1.8 解压到你想的目录
1、 将TensorRT/lib下所有lib复制到C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.0\lib\x64目录下
2、将TensorRT/lib下所有dll复制到C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.0\bin下
3、将TensorRT/include下所有.h文件复制到C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.0\include下
4、我的电脑->属性->高级系统设置->环境变量,在系统变量中找到Path,并双击编辑,将TensorRT/lib路径填入并保存,如C:\Program Files\NVIDIA GPU Computing Toolkit\TensorRT-8.2.1.8\lib
CMAKE已经下好放网盘
点下一步安装即可
打开第二个项目中的的CMakeLists.txt,把我下面这段复制过去,然后修改Opencv、Tensorrt、dirent.h的目录,其中dirent.h在本仓库的include中,须绝对路径。我在这三个位置后面标了数字,把这三行改了就行
cmake_minimum_required(VERSION 2.6)
project(yolov5)
#change to your own path
##################################################
set(OpenCV_DIR "D:\\MyCodeEnv\\opencv\\opencv\\build") #1
set(TRT_DIR "C:\\Program Files\\NVIDIA GPU Computing Toolkit\\TensorRT-8.2.1.8") #2
set(OpenCV_INCLUDE_DIRS "C:\\Users\\QiuYuSY\\Desktop\\yolov5-6.1_tensorrt_CFHD\\include") #3
##################################################
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})
include_directories(${Dirent_INCLUDE_DIRS})
#change to your GPU own compute_XX
###########################################################################################
set(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS};-std=c++11;-g;-G;-gencode;arch=compute_86;code=sm_86) #4
###########################################################################################
####
enable_language(CUDA) # add this line, then no need to setup cuda path in vs
####
include_directories(${PROJECT_SOURCE_DIR}/include)
include_directories(${TRT_DIR}\\include)
# -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 " libraries: ${OpenCV_LIBS}")
message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")
include_directories(${OpenCV_INCLUDE_DIRS})
link_directories(${TRT_DIR}\\lib)
add_executable(yolov5 ${PROJECT_SOURCE_DIR}/yolov5.cpp ${PROJECT_SOURCE_DIR}/yololayer.cu ${PROJECT_SOURCE_DIR}/yololayer.h ${PROJECT_SOURCE_DIR}/preprocess.cu)
target_link_libraries(yolov5 "nvinfer" "nvinfer_plugin")
target_link_libraries(yolov5 ${OpenCV_LIBS})
target_link_libraries(yolov5 ${CUDA_LIBRARIES})
target_link_libraries(yolov5 Threads::Threads)
然后你参考这个网站https://developer.nvidia.com/cuda-gpus
根据你的显卡计算能力,在标了4的地方把两个86
改成你的显卡计算能力*10
比如我3060 是8.6 就写86
分别选择项目位置以及刚刚新建的bulid文件夹位置然后点下面的configure
选择你的vs版本 下面选x64 然后点结束
然后你等一会会出现Configure done
tip:这一步很容易出错,如果你出错了仔细看看上面的
CUDA是否安装VS包
PATH是否设置
CMAKELIST.TXT 路径是否正确
点击open project 打开项目,可以看到项目
yolov5 -s yolov5s.wts yolov5s.engine s
yolov5 -d yolov5s.engine ./pic
到这里,C++的全部环境部署就完成了,需要FPS部分文档,加Q群936625049
FPS部分视频:https://www.bilibili.com/video/BV1n3411u7sk
CSDN : https://blog.csdn.net/QiuYuSy/article/details/125375273?spm=1001.2014.3001.5501