当前位置:   article > 正文

Win10-TensorRT-Yolov5-v5.0 Demo运行笔记_trt yolov5 demo

trt yolov5 demo

配VS环境坑太多了,报错多的已经记不住了,总结几个重要的点:

参考资料:

使用tensorRT部署yolov5 v5.0—windows_Ring__Rain的博客-CSDN博客_tensorrt yolov5

win10 使用TensorRT部署 yolov5-v4.0(C++)_SongpingWang的博客-CSDN博客

Yolov5的3种tensorRT加速方式及3090测评结果(C++版和Python torchtrt版)_藏晖的博客-CSDN博客_tensorrt yolov5

GitHub - wang-xinyu/tensorrtx: Implementation of popular deep learning networks with TensorRT network definition API

一、需要注意的几个点:

1.确保VS的安装路径下可以找到以下四个文件,这几个文件是从Cuda路径下复制过来的,我的是在:

C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.3\extras\visual_studio_integration\MSBuildExtensions

2.在编译时确保yololayer.cu在CUDA环境下:

具体操作如下:

右键项目名称:

Build Dependencies---> Build Customizations--->选择CUDA

如果没有CUDA选项可点击Find Existing xxx, 打开CUDA xxx.targets,打开后重启一下VS

yololayer.cu属性设置:

3.VS2022可能不支持编译本项目

我在实测的时候发现会警告CUDA版本只支持2015--2017,有方法可以去除警告,但是在编译时会出现其他的错误,因此,我下载了VS2019,如果您有好的解决方案,欢迎分享

去除版本不支持的警告的方式:

win10搜索框输入powershell, 点击使用管理员方式运行(避免无修改权限)

切换到 cd "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.3\include\crt"

输入 notepad .\host_config.h

修改:

4.原版的CmakeLists.txt文件在实测的时候有个bug(可能是我环境的问题)

此处参考:https://github.com/wang-xinyu/tensorrtx/issues/792#issuecomment-958776836

实测可执行完整CmakeLists.txt文件如下:

  1. cmake_minimum_required(VERSION 3.0)
  2. #=========================================================
  3. project(yolov5) #1 工程名
  4. set(OpenCV_DIR "D:\\soft\\opencv\\opencv\\build") #2 opencv目录
  5. set(OpenCV_INCLUDE_DIRS ${OpenCV_DIR}\\include) #3
  6. set(OpenCV_LIB_DIRS ${OpenCV_DIR}\\x64\\vc15\\lib) #4
  7. set(OpenCV_Debug_LIBS "opencv_world455d.lib") #5
  8. set(OpenCV_Release_LIBS "opencv_world455.lib") #6
  9. set(TRT_DIR "E:\\CODE\\CPP\\TensorRT\\TensorRT-8.4.0.6") #7
  10. set(TRT_INCLUDE_DIRS ${TRT_DIR}\\include) #8
  11. set(TRT_LIB_DIRS ${TRT_DIR}\\lib) #9
  12. set(Dirent_INCLUDE_DIRS "E:\\CODE\\Python\\DL\\PYtorch\\YOLOV5\\sourceCode\\tensorrtx_yolov5_v5.0\\yolov5") #10
  13. #=========================================================
  14. set(CMAKE_CUDA_COMPILER "C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.3/bin/nvcc")
  15. add_definitions(-std=c++11)
  16. add_definitions(-DAPI_EXPORTS) #新添加的
  17. option(CUDA_USE_STATIC_CUDA_RUNTIME OFF)
  18. set(CMAKE_CXX_STANDARD 11)
  19. set(CMAKE_BUILD_TYPE Debug)
  20. set(THREADS_PREFER_PTHREAD_FLAG ON)
  21. find_package(Threads)
  22. # setup CUDA
  23. find_package(CUDA REQUIRED)
  24. message(STATUS " libraries: ${CUDA_LIBRARIES}")
  25. message(STATUS " include path: ${CUDA_INCLUDE_DIRS}")
  26. include_directories(${CUDA_INCLUDE_DIRS})
  27. ####
  28. #enable_language(CUDA) # add this line, then no need to setup cuda path in vs
  29. ####
  30. include_directories(${PROJECT_SOURCE_DIR}/include) #11
  31. include_directories(${TRT_INCLUDE_DIRS}) #12
  32. link_directories(${TRT_LIB_DIRS}) #13
  33. include_directories(${OpenCV_INCLUDE_DIRS}) #14
  34. link_directories(${OpenCV_LIB_DIRS}) #15
  35. include_directories(${Dirent_INCLUDE_DIRS}) #16
  36. # -D_MWAITXINTRIN_H_INCLUDED for solving error: identifier "__builtin_ia32_mwaitx" is undefined
  37. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Ofast -D_MWAITXINTRIN_H_INCLUDED")
  38. # setup opencv
  39. find_package(OpenCV QUIET
  40. NO_MODULE
  41. NO_DEFAULT_PATH
  42. NO_CMAKE_PATH
  43. NO_CMAKE_ENVIRONMENT_PATH
  44. NO_SYSTEM_ENVIRONMENT_PATH
  45. NO_CMAKE_PACKAGE_REGISTRY
  46. NO_CMAKE_BUILDS_PATH
  47. NO_CMAKE_SYSTEM_PATH
  48. NO_CMAKE_SYSTEM_PACKAGE_REGISTRY
  49. )
  50. message(STATUS "OpenCV library status:")
  51. message(STATUS " version: ${OpenCV_VERSION}")
  52. message(STATUS " lib path: ${OpenCV_LIB_DIRS}")
  53. message(STATUS " Debug libraries: ${OpenCV_Debug_LIBS}")
  54. message(STATUS " Release libraries: ${OpenCV_Release_LIBS}")
  55. message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")
  56. add_executable(yolov5 ${PROJECT_SOURCE_DIR}/yolov5.cpp ${PROJECT_SOURCE_DIR}/common.hpp ${PROJECT_SOURCE_DIR}/yololayer.cu ${PROJECT_SOURCE_DIR}/yololayer.h) #17
  57. target_link_libraries(yolov5 "nvinfer" "nvinfer_plugin") #18
  58. target_link_libraries(yolov5 debug ${OpenCV_Debug_LIBS}) #19
  59. target_link_libraries(yolov5 optimized ${OpenCV_Release_LIBS}) #20
  60. target_link_libraries(yolov5 ${CUDA_LIBRARIES}) #21
  61. target_link_libraries(yolov5 Threads::Threads)

5.其他:

CMake GUI打开项目时会默认以最高版本的VS打开(前边说了VS2022会报错),因此在生成项目后,可以直接进到build目录下,右键—>Open with

二、Demo流程:

1.下载源码,确保python环境正确:

下载tensorRT版本:Release yolov5-v5.0 · wang-xinyu/tensorrtx · GitHub

下载官方源码和权重: Release v5.0 - YOLOv5-P6 1280 models, AWS, Supervise.ly and YouTube integrations · ultralytics/yolov5 · GitHub

将下载的权重放到weights/文件夹下:

确保Yolov5的运行环境正确:

python detect.py --weights .\weights\yolov5s.pt --img 640 --conf 0.25 --source data/images/ 

缺哪个包补充安装即可

2.生成.wts文件

将trt版yolov5目录下的gen_wts.py复制到 yolov5的源码生成.wts文件

按大佬的叙述将dirent.h放到trt版yolov5目录下

  1. /*
  2. * Dirent interface for Microsoft Visual Studio
  3. *
  4. * Copyright (C) 1998-2019 Toni Ronkko
  5. * This file is part of dirent. Dirent may be freely distributed
  6. * under the MIT license. For all details and documentation, see
  7. * https://github.com/tronkko/dirent
  8. */
  9. #ifndef DIRENT_H
  10. #define DIRENT_H
  11. /* Hide warnings about unreferenced local functions */
  12. #if defined(__clang__)
  13. # pragma clang diagnostic ignored "-Wunused-function"
  14. #elif defined(_MSC_VER)
  15. # pragma warning(disable:4505)
  16. #elif defined(__GNUC__)
  17. # pragma GCC diagnostic ignored "-Wunused-function"
  18. #endif
  19. /*
  20. * Include windows.h without Windows Sockets 1.1 to prevent conflicts with
  21. * Windows Sockets 2.0.
  22. */
  23. #ifndef WIN32_LEAN_AND_MEAN
  24. # define WIN32_LEAN_AND_MEAN
  25. #endif
  26. #include <windows.h>
  27. #include <stdio.h>
  28. #include <stdarg.h>
  29. #include <wchar.h>
  30. #include <string.h>
  31. #include <stdlib.h>
  32. #include <malloc.h>
  33. #include <sys/types.h>
  34. #include <sys/stat.h>
  35. #include <errno.h>
  36. #include <ctype.h>
  37. /* Indicates that d_type field is available in dirent structure */
  38. #define _DIRENT_HAVE_D_TYPE
  39. /* Indicates that d_namlen field is available in dirent structure */
  40. #define _DIRENT_HAVE_D_NAMLEN
  41. /* Entries missing from MSVC 6.0 */
  42. #if !defined(FILE_ATTRIBUTE_DEVICE)
  43. # define FILE_ATTRIBUTE_DEVICE 0x40
  44. #endif
  45. /* File type and permission flags for stat(), general mask */
  46. #if !defined(S_IFMT)
  47. # define S_IFMT _S_IFMT
  48. #endif
  49. /* Directory bit */
  50. #if !defined(S_IFDIR)
  51. # define S_IFDIR _S_IFDIR
  52. #endif
  53. /* Character device bit */
  54. #if !defined(S_IFCHR)
  55. # define S_IFCHR _S_IFCHR
  56. #endif
  57. /* Pipe bit */
  58. #if !defined(S_IFFIFO)
  59. # define S_IFFIFO _S_IFFIFO
  60. #endif
  61. /* Regular file bit */
  62. #if !defined(S_IFREG)
  63. # define S_IFREG _S_IFREG
  64. #endif
  65. /* Read permission */
  66. #if !defined(S_IREAD)
  67. # define S_IREAD _S_IREAD
  68. #endif
  69. /* Write permission */
  70. #if !defined(S_IWRITE)
  71. # define S_IWRITE _S_IWRITE
  72. #endif
  73. /* Execute permission */
  74. #if !defined(S_IEXEC)
  75. # define S_IEXEC _S_IEXEC
  76. #endif
  77. /* Pipe */
  78. #if !defined(S_IFIFO)
  79. # define S_IFIFO _S_IFIFO
  80. #endif
  81. /* Block device */
  82. #if !defined(S_IFBLK)
  83. # define S_IFBLK 0
  84. #endif
  85. /* Link */
  86. #if !defined(S_IFLNK)
  87. # define S_IFLNK 0
  88. #endif
  89. /* Socket */
  90. #if !defined(S_IFSOCK)
  91. # define S_IFSOCK 0
  92. #endif
  93. /* Read user permission */
  94. #if !defined(S_IRUSR)
  95. # define S_IRUSR S_IREAD
  96. #endif
  97. /* Write user permission */
  98. #if !defined(S_IWUSR)
  99. # define S_IWUSR S_IWRITE
  100. #endif
  101. /* Execute user permission */
  102. #if !defined(S_IXUSR)
  103. # define S_IXUSR 0
  104. #endif
  105. /* Read group permission */
  106. #if !defined(S_IRGRP)
  107. # define S_IRGRP 0
  108. #endif
  109. /* Write group permission */
  110. #if !defined(S_IWGRP)
  111. # define S_IWGRP 0
  112. #endif
  113. /* Execute group permission */
  114. #if !defined(S_IXGRP)
  115. # define S_IXGRP 0
  116. #endif
  117. /* Read others permission */
  118. #if !defined(S_IROTH)
  119. # define S_IROTH 0
  120. #endif
  121. /* Write others permission */
  122. #if !defined(S_IWOTH)
  123. # define S_IWOTH 0
  124. #endif
  125. /* Execute others permission */
  126. #if !defined(S_IXOTH)
  127. # define S_IXOTH 0
  128. #endif
  129. /* Maximum length of file name */
  130. #if !defined(PATH_MAX)
  131. # define PATH_MAX MAX_PATH
  132. #endif
  133. #if !defined(FILENAME_MAX)
  134. # define FILENAME_MAX MAX_PATH
  135. #endif
  136. #if !defined(NAME_MAX)
  137. # define NAME_MAX FILENAME_MAX
  138. #endif
  139. /* File type flags for d_type */
  140. #define DT_UNKNOWN 0
  141. #define DT_REG S_IFREG
  142. #define DT_DIR S_IFDIR
  143. #define DT_FIFO S_IFIFO
  144. #define DT_SOCK S_IFSOCK
  145. #define DT_CHR S_IFCHR
  146. #define DT_BLK S_IFBLK
  147. #define DT_LNK S_IFLNK
  148. /* Macros for converting between st_mode and d_type */
  149. #define IFTODT(mode) ((mode) & S_IFMT)
  150. #define DTTOIF(type) (type)
  151. /*
  152. * File type macros. Note that block devices, sockets and links cannot be
  153. * distinguished on Windows and the macros S_ISBLK, S_ISSOCK and S_ISLNK are
  154. * only defined for compatibility. These macros should always return false
  155. * on Windows.
  156. */
  157. #if !defined(S_ISFIFO)
  158. # define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFIFO)
  159. #endif
  160. #if !defined(S_ISDIR)
  161. # define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
  162. #endif
  163. #if !defined(S_ISREG)
  164. # define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
  165. #endif
  166. #if !defined(S_ISLNK)
  167. # define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK)
  168. #endif
  169. #if !defined(S_ISSOCK)
  170. # define S_ISSOCK(mode) (((mode) & S_IFMT) == S_IFSOCK)
  171. #endif
  172. #if !defined(S_ISCHR)
  173. # define S_ISCHR(mode) (((mode) & S_IFMT) == S_IFCHR)
  174. #endif
  175. #if !defined(S_ISBLK)
  176. # define S_ISBLK(mode) (((mode) & S_IFMT) == S_IFBLK)
  177. #endif
  178. /* Return the exact length of the file name without zero terminator */
  179. #define _D_EXACT_NAMLEN(p) ((p)->d_namlen)
  180. /* Return the maximum size of a file name */
  181. #define _D_ALLOC_NAMLEN(p) ((PATH_MAX)+1)
  182. #ifdef __cplusplus
  183. extern "C" {
  184. #endif
  185. /* Wide-character version */
  186. struct _wdirent {
  187. /* Always zero */
  188. long d_ino;
  189. /* File position within stream */
  190. long d_off;
  191. /* Structure size */
  192. unsigned short d_reclen;
  193. /* Length of name without \0 */
  194. size_t d_namlen;
  195. /* File type */
  196. int d_type;
  197. /* File name */
  198. wchar_t d_name[PATH_MAX+1];
  199. };
  200. typedef struct _wdirent _wdirent;
  201. struct _WDIR {
  202. /* Current directory entry */
  203. struct _wdirent ent;
  204. /* Private file data */
  205. WIN32_FIND_DATAW data;
  206. /* True if data is valid */
  207. int cached;
  208. /* Win32 search handle */
  209. HANDLE handle;
  210. /* Initial directory name */
  211. wchar_t *patt;
  212. };
  213. typedef struct _WDIR _WDIR;
  214. /* Multi-byte character version */
  215. struct dirent {
  216. /* Always zero */
  217. long d_ino;
  218. /* File position within stream */
  219. long d_off;
  220. /* Structure size */
  221. unsigned short d_reclen;
  222. /* Length of name without \0 */
  223. size_t d_namlen;
  224. /* File type */
  225. int d_type;
  226. /* File name */
  227. char d_name[PATH_MAX+1];
  228. };
  229. typedef struct dirent dirent;
  230. struct DIR {
  231. struct dirent ent;
  232. struct _WDIR *wdirp;
  233. };
  234. typedef struct DIR DIR;
  235. /* Dirent functions */
  236. static DIR *opendir(const char *dirname);
  237. static _WDIR *_wopendir(const wchar_t *dirname);
  238. static struct dirent *readdir(DIR *dirp);
  239. static struct _wdirent *_wreaddir(_WDIR *dirp);
  240. static int readdir_r(
  241. DIR *dirp, struct dirent *entry, struct dirent **result);
  242. static int _wreaddir_r(
  243. _WDIR *dirp, struct _wdirent *entry, struct _wdirent **result);
  244. static int closedir(DIR *dirp);
  245. static int _wclosedir(_WDIR *dirp);
  246. static void rewinddir(DIR* dirp);
  247. static void _wrewinddir(_WDIR* dirp);
  248. static int scandir(const char *dirname, struct dirent ***namelist,
  249. int (*filter)(const struct dirent*),
  250. int (*compare)(const struct dirent**, const struct dirent**));
  251. static int alphasort(const struct dirent **a, const struct dirent **b);
  252. static int versionsort(const struct dirent **a, const struct dirent **b);
  253. static int strverscmp(const char *a, const char *b);
  254. /* For compatibility with Symbian */
  255. #define wdirent _wdirent
  256. #define WDIR _WDIR
  257. #define wopendir _wopendir
  258. #define wreaddir _wreaddir
  259. #define wclosedir _wclosedir
  260. #define wrewinddir _wrewinddir
  261. /* Compatibility with older Microsoft compilers and non-Microsoft compilers */
  262. #if !defined(_MSC_VER) || _MSC_VER < 1400
  263. # define wcstombs_s dirent_wcstombs_s
  264. # define mbstowcs_s dirent_mbstowcs_s
  265. #endif
  266. /* Optimize dirent_set_errno() away on modern Microsoft compilers */
  267. #if defined(_MSC_VER) && _MSC_VER >= 1400
  268. # define dirent_set_errno _set_errno
  269. #endif
  270. /* Internal utility functions */
  271. static WIN32_FIND_DATAW *dirent_first(_WDIR *dirp);
  272. static WIN32_FIND_DATAW *dirent_next(_WDIR *dirp);
  273. #if !defined(_MSC_VER) || _MSC_VER < 1400
  274. static int dirent_mbstowcs_s(
  275. size_t *pReturnValue, wchar_t *wcstr, size_t sizeInWords,
  276. const char *mbstr, size_t count);
  277. #endif
  278. #if !defined(_MSC_VER) || _MSC_VER < 1400
  279. static int dirent_wcstombs_s(
  280. size_t *pReturnValue, char *mbstr, size_t sizeInBytes,
  281. const wchar_t *wcstr, size_t count);
  282. #endif
  283. #if !defined(_MSC_VER) || _MSC_VER < 1400
  284. static void dirent_set_errno(int error);
  285. #endif
  286. /*
  287. * Open directory stream DIRNAME for read and return a pointer to the
  288. * internal working area that is used to retrieve individual directory
  289. * entries.
  290. */
  291. static _WDIR *_wopendir(const wchar_t *dirname)
  292. {
  293. wchar_t *p;
  294. /* Must have directory name */
  295. if (dirname == NULL || dirname[0] == '\0') {
  296. dirent_set_errno(ENOENT);
  297. return NULL;
  298. }
  299. /* Allocate new _WDIR structure */
  300. _WDIR *dirp = (_WDIR*) malloc(sizeof(struct _WDIR));
  301. if (!dirp)
  302. return NULL;
  303. /* Reset _WDIR structure */
  304. dirp->handle = INVALID_HANDLE_VALUE;
  305. dirp->patt = NULL;
  306. dirp->cached = 0;
  307. /*
  308. * Compute the length of full path plus zero terminator
  309. *
  310. * Note that on WinRT there's no way to convert relative paths
  311. * into absolute paths, so just assume it is an absolute path.
  312. */
  313. #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
  314. /* Desktop */
  315. DWORD n = GetFullPathNameW(dirname, 0, NULL, NULL);
  316. #else
  317. /* WinRT */
  318. size_t n = wcslen(dirname);
  319. #endif
  320. /* Allocate room for absolute directory name and search pattern */
  321. dirp->patt = (wchar_t*) malloc(sizeof(wchar_t) * n + 16);
  322. if (dirp->patt == NULL)
  323. goto exit_closedir;
  324. /*
  325. * Convert relative directory name to an absolute one. This
  326. * allows rewinddir() to function correctly even when current
  327. * working directory is changed between opendir() and rewinddir().
  328. *
  329. * Note that on WinRT there's no way to convert relative paths
  330. * into absolute paths, so just assume it is an absolute path.
  331. */
  332. #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
  333. /* Desktop */
  334. n = GetFullPathNameW(dirname, n, dirp->patt, NULL);
  335. if (n <= 0)
  336. goto exit_closedir;
  337. #else
  338. /* WinRT */
  339. wcsncpy_s(dirp->patt, n+1, dirname, n);
  340. #endif
  341. /* Append search pattern \* to the directory name */
  342. p = dirp->patt + n;
  343. switch (p[-1]) {
  344. case '\\':
  345. case '/':
  346. case ':':
  347. /* Directory ends in path separator, e.g. c:\temp\ */
  348. /*NOP*/;
  349. break;
  350. default:
  351. /* Directory name doesn't end in path separator */
  352. *p++ = '\\';
  353. }
  354. *p++ = '*';
  355. *p = '\0';
  356. /* Open directory stream and retrieve the first entry */
  357. if (!dirent_first(dirp))
  358. goto exit_closedir;
  359. /* Success */
  360. return dirp;
  361. /* Failure */
  362. exit_closedir:
  363. _wclosedir(dirp);
  364. return NULL;
  365. }
  366. /*
  367. * Read next directory entry.
  368. *
  369. * Returns pointer to static directory entry which may be overwritten by
  370. * subsequent calls to _wreaddir().
  371. */
  372. static struct _wdirent *_wreaddir(_WDIR *dirp)
  373. {
  374. /*
  375. * Read directory entry to buffer. We can safely ignore the return
  376. * value as entry will be set to NULL in case of error.
  377. */
  378. struct _wdirent *entry;
  379. (void) _wreaddir_r(dirp, &dirp->ent, &entry);
  380. /* Return pointer to statically allocated directory entry */
  381. return entry;
  382. }
  383. /*
  384. * Read next directory entry.
  385. *
  386. * Returns zero on success. If end of directory stream is reached, then sets
  387. * result to NULL and returns zero.
  388. */
  389. static int _wreaddir_r(
  390. _WDIR *dirp, struct _wdirent *entry, struct _wdirent **result)
  391. {
  392. /* Read next directory entry */
  393. WIN32_FIND_DATAW *datap = dirent_next(dirp);
  394. if (!datap) {
  395. /* Return NULL to indicate end of directory */
  396. *result = NULL;
  397. return /*OK*/0;
  398. }
  399. /*
  400. * Copy file name as wide-character string. If the file name is too
  401. * long to fit in to the destination buffer, then truncate file name
  402. * to PATH_MAX characters and zero-terminate the buffer.
  403. */
  404. size_t n = 0;
  405. while (n < PATH_MAX && datap->cFileName[n] != 0) {
  406. entry->d_name[n] = datap->cFileName[n];
  407. n++;
  408. }
  409. entry->d_name[n] = 0;
  410. /* Length of file name excluding zero terminator */
  411. entry->d_namlen = n;
  412. /* File type */
  413. DWORD attr = datap->dwFileAttributes;
  414. if ((attr & FILE_ATTRIBUTE_DEVICE) != 0)
  415. entry->d_type = DT_CHR;
  416. else if ((attr & FILE_ATTRIBUTE_DIRECTORY) != 0)
  417. entry->d_type = DT_DIR;
  418. else
  419. entry->d_type = DT_REG;
  420. /* Reset dummy fields */
  421. entry->d_ino = 0;
  422. entry->d_off = 0;
  423. entry->d_reclen = sizeof(struct _wdirent);
  424. /* Set result address */
  425. *result = entry;
  426. return /*OK*/0;
  427. }
  428. /*
  429. * Close directory stream opened by opendir() function. This invalidates the
  430. * DIR structure as well as any directory entry read previously by
  431. * _wreaddir().
  432. */
  433. static int _wclosedir(_WDIR *dirp)
  434. {
  435. if (!dirp) {
  436. dirent_set_errno(EBADF);
  437. return /*failure*/-1;
  438. }
  439. /* Release search handle */
  440. if (dirp->handle != INVALID_HANDLE_VALUE)
  441. FindClose(dirp->handle);
  442. /* Release search pattern */
  443. free(dirp->patt);
  444. /* Release directory structure */
  445. free(dirp);
  446. return /*success*/0;
  447. }
  448. /*
  449. * Rewind directory stream such that _wreaddir() returns the very first
  450. * file name again.
  451. */
  452. static void _wrewinddir(_WDIR* dirp)
  453. {
  454. if (!dirp)
  455. return;
  456. /* Release existing search handle */
  457. if (dirp->handle != INVALID_HANDLE_VALUE)
  458. FindClose(dirp->handle);
  459. /* Open new search handle */
  460. dirent_first(dirp);
  461. }
  462. /* Get first directory entry */
  463. static WIN32_FIND_DATAW *dirent_first(_WDIR *dirp)
  464. {
  465. if (!dirp)
  466. return NULL;
  467. /* Open directory and retrieve the first entry */
  468. dirp->handle = FindFirstFileExW(
  469. dirp->patt, FindExInfoStandard, &dirp->data,
  470. FindExSearchNameMatch, NULL, 0);
  471. if (dirp->handle == INVALID_HANDLE_VALUE)
  472. goto error;
  473. /* A directory entry is now waiting in memory */
  474. dirp->cached = 1;
  475. return &dirp->data;
  476. error:
  477. /* Failed to open directory: no directory entry in memory */
  478. dirp->cached = 0;
  479. /* Set error code */
  480. DWORD errorcode = GetLastError();
  481. switch (errorcode) {
  482. case ERROR_ACCESS_DENIED:
  483. /* No read access to directory */
  484. dirent_set_errno(EACCES);
  485. break;
  486. case ERROR_DIRECTORY:
  487. /* Directory name is invalid */
  488. dirent_set_errno(ENOTDIR);
  489. break;
  490. case ERROR_PATH_NOT_FOUND:
  491. default:
  492. /* Cannot find the file */
  493. dirent_set_errno(ENOENT);
  494. }
  495. return NULL;
  496. }
  497. /* Get next directory entry */
  498. static WIN32_FIND_DATAW *dirent_next(_WDIR *dirp)
  499. {
  500. /* Is the next directory entry already in cache? */
  501. if (dirp->cached) {
  502. /* Yes, a valid directory entry found in memory */
  503. dirp->cached = 0;
  504. return &dirp->data;
  505. }
  506. /* No directory entry in cache */
  507. if (dirp->handle == INVALID_HANDLE_VALUE)
  508. return NULL;
  509. /* Read the next directory entry from stream */
  510. if (FindNextFileW(dirp->handle, &dirp->data) == FALSE)
  511. goto exit_close;
  512. /* Success */
  513. return &dirp->data;
  514. /* Failure */
  515. exit_close:
  516. FindClose(dirp->handle);
  517. dirp->handle = INVALID_HANDLE_VALUE;
  518. return NULL;
  519. }
  520. /* Open directory stream using plain old C-string */
  521. static DIR *opendir(const char *dirname)
  522. {
  523. /* Must have directory name */
  524. if (dirname == NULL || dirname[0] == '\0') {
  525. dirent_set_errno(ENOENT);
  526. return NULL;
  527. }
  528. /* Allocate memory for DIR structure */
  529. struct DIR *dirp = (DIR*) malloc(sizeof(struct DIR));
  530. if (!dirp)
  531. return NULL;
  532. /* Convert directory name to wide-character string */
  533. wchar_t wname[PATH_MAX + 1];
  534. size_t n;
  535. int error = mbstowcs_s(&n, wname, PATH_MAX + 1, dirname, PATH_MAX+1);
  536. if (error)
  537. goto exit_failure;
  538. /* Open directory stream using wide-character name */
  539. dirp->wdirp = _wopendir(wname);
  540. if (!dirp->wdirp)
  541. goto exit_failure;
  542. /* Success */
  543. return dirp;
  544. /* Failure */
  545. exit_failure:
  546. free(dirp);
  547. return NULL;
  548. }
  549. /* Read next directory entry */
  550. static struct dirent *readdir(DIR *dirp)
  551. {
  552. /*
  553. * Read directory entry to buffer. We can safely ignore the return
  554. * value as entry will be set to NULL in case of error.
  555. */
  556. struct dirent *entry;
  557. (void) readdir_r(dirp, &dirp->ent, &entry);
  558. /* Return pointer to statically allocated directory entry */
  559. return entry;
  560. }
  561. /*
  562. * Read next directory entry into called-allocated buffer.
  563. *
  564. * Returns zero on success. If the end of directory stream is reached, then
  565. * sets result to NULL and returns zero.
  566. */
  567. static int readdir_r(
  568. DIR *dirp, struct dirent *entry, struct dirent **result)
  569. {
  570. /* Read next directory entry */
  571. WIN32_FIND_DATAW *datap = dirent_next(dirp->wdirp);
  572. if (!datap) {
  573. /* No more directory entries */
  574. *result = NULL;
  575. return /*OK*/0;
  576. }
  577. /* Attempt to convert file name to multi-byte string */
  578. size_t n;
  579. int error = wcstombs_s(
  580. &n, entry->d_name, PATH_MAX + 1,
  581. datap->cFileName, PATH_MAX + 1);
  582. /*
  583. * If the file name cannot be represented by a multi-byte string, then
  584. * attempt to use old 8+3 file name. This allows the program to
  585. * access files although file names may seem unfamiliar to the user.
  586. *
  587. * Be ware that the code below cannot come up with a short file name
  588. * unless the file system provides one. At least VirtualBox shared
  589. * folders fail to do this.
  590. */
  591. if (error && datap->cAlternateFileName[0] != '\0') {
  592. error = wcstombs_s(
  593. &n, entry->d_name, PATH_MAX + 1,
  594. datap->cAlternateFileName, PATH_MAX + 1);
  595. }
  596. if (!error) {
  597. /* Length of file name excluding zero terminator */
  598. entry->d_namlen = n - 1;
  599. /* File attributes */
  600. DWORD attr = datap->dwFileAttributes;
  601. if ((attr & FILE_ATTRIBUTE_DEVICE) != 0)
  602. entry->d_type = DT_CHR;
  603. else if ((attr & FILE_ATTRIBUTE_DIRECTORY) != 0)
  604. entry->d_type = DT_DIR;
  605. else
  606. entry->d_type = DT_REG;
  607. /* Reset dummy fields */
  608. entry->d_ino = 0;
  609. entry->d_off = 0;
  610. entry->d_reclen = sizeof(struct dirent);
  611. } else {
  612. /*
  613. * Cannot convert file name to multi-byte string so construct
  614. * an erroneous directory entry and return that. Note that
  615. * we cannot return NULL as that would stop the processing
  616. * of directory entries completely.
  617. */
  618. entry->d_name[0] = '?';
  619. entry->d_name[1] = '\0';
  620. entry->d_namlen = 1;
  621. entry->d_type = DT_UNKNOWN;
  622. entry->d_ino = 0;
  623. entry->d_off = -1;
  624. entry->d_reclen = 0;
  625. }
  626. /* Return pointer to directory entry */
  627. *result = entry;
  628. return /*OK*/0;
  629. }
  630. /* Close directory stream */
  631. static int closedir(DIR *dirp)
  632. {
  633. int ok;
  634. if (!dirp)
  635. goto exit_failure;
  636. /* Close wide-character directory stream */
  637. ok = _wclosedir(dirp->wdirp);
  638. dirp->wdirp = NULL;
  639. /* Release multi-byte character version */
  640. free(dirp);
  641. return ok;
  642. exit_failure:
  643. /* Invalid directory stream */
  644. dirent_set_errno(EBADF);
  645. return /*failure*/-1;
  646. }
  647. /* Rewind directory stream to beginning */
  648. static void rewinddir(DIR* dirp)
  649. {
  650. if (!dirp)
  651. return;
  652. /* Rewind wide-character string directory stream */
  653. _wrewinddir(dirp->wdirp);
  654. }
  655. /* Scan directory for entries */
  656. static int scandir(
  657. const char *dirname, struct dirent ***namelist,
  658. int (*filter)(const struct dirent*),
  659. int (*compare)(const struct dirent**, const struct dirent**))
  660. {
  661. int result;
  662. /* Open directory stream */
  663. DIR *dir = opendir(dirname);
  664. if (!dir) {
  665. /* Cannot open directory */
  666. return /*Error*/ -1;
  667. }
  668. /* Read directory entries to memory */
  669. struct dirent *tmp = NULL;
  670. struct dirent **files = NULL;
  671. size_t size = 0;
  672. size_t allocated = 0;
  673. while (1) {
  674. /* Allocate room for a temporary directory entry */
  675. if (!tmp) {
  676. tmp = (struct dirent*) malloc(sizeof(struct dirent));
  677. if (!tmp)
  678. goto exit_failure;
  679. }
  680. /* Read directory entry to temporary area */
  681. struct dirent *entry;
  682. if (readdir_r(dir, tmp, &entry) != /*OK*/0)
  683. goto exit_failure;
  684. /* Stop if we already read the last directory entry */
  685. if (entry == NULL)
  686. goto exit_success;
  687. /* Determine whether to include the entry in results */
  688. if (filter && !filter(tmp))
  689. continue;
  690. /* Enlarge pointer table to make room for another pointer */
  691. if (size >= allocated) {
  692. /* Compute number of entries in the new table */
  693. size_t num_entries = size * 2 + 16;
  694. /* Allocate new pointer table or enlarge existing */
  695. void *p = realloc(files, sizeof(void*) * num_entries);
  696. if (!p)
  697. goto exit_failure;
  698. /* Got the memory */
  699. files = (dirent**) p;
  700. allocated = num_entries;
  701. }
  702. /* Store the temporary entry to ptr table */
  703. files[size++] = tmp;
  704. tmp = NULL;
  705. }
  706. exit_failure:
  707. /* Release allocated file entries */
  708. for (size_t i = 0; i < size; i++) {
  709. free(files[i]);
  710. }
  711. /* Release the pointer table */
  712. free(files);
  713. files = NULL;
  714. /* Exit with error code */
  715. result = /*error*/ -1;
  716. goto exit_status;
  717. exit_success:
  718. /* Sort directory entries */
  719. qsort(files, size, sizeof(void*),
  720. (int (*) (const void*, const void*)) compare);
  721. /* Pass pointer table to caller */
  722. if (namelist)
  723. *namelist = files;
  724. /* Return the number of directory entries read */
  725. result = (int) size;
  726. exit_status:
  727. /* Release temporary directory entry, if we had one */
  728. free(tmp);
  729. /* Close directory stream */
  730. closedir(dir);
  731. return result;
  732. }
  733. /* Alphabetical sorting */
  734. static int alphasort(const struct dirent **a, const struct dirent **b)
  735. {
  736. return strcoll((*a)->d_name, (*b)->d_name);
  737. }
  738. /* Sort versions */
  739. static int versionsort(const struct dirent **a, const struct dirent **b)
  740. {
  741. return strverscmp((*a)->d_name, (*b)->d_name);
  742. }
  743. /* Compare strings */
  744. static int strverscmp(const char *a, const char *b)
  745. {
  746. size_t i = 0;
  747. size_t j;
  748. /* Find first difference */
  749. while (a[i] == b[i]) {
  750. if (a[i] == '\0') {
  751. /* No difference */
  752. return 0;
  753. }
  754. ++i;
  755. }
  756. /* Count backwards and find the leftmost digit */
  757. j = i;
  758. while (j > 0 && isdigit(a[j-1])) {
  759. --j;
  760. }
  761. /* Determine mode of comparison */
  762. if (a[j] == '0' || b[j] == '0') {
  763. /* Find the next non-zero digit */
  764. while (a[j] == '0' && a[j] == b[j]) {
  765. j++;
  766. }
  767. /* String with more digits is smaller, e.g 002 < 01 */
  768. if (isdigit(a[j])) {
  769. if (!isdigit(b[j])) {
  770. return -1;
  771. }
  772. } else if (isdigit(b[j])) {
  773. return 1;
  774. }
  775. } else if (isdigit(a[j]) && isdigit(b[j])) {
  776. /* Numeric comparison */
  777. size_t k1 = j;
  778. size_t k2 = j;
  779. /* Compute number of digits in each string */
  780. while (isdigit(a[k1])) {
  781. k1++;
  782. }
  783. while (isdigit(b[k2])) {
  784. k2++;
  785. }
  786. /* Number with more digits is bigger, e.g 999 < 1000 */
  787. if (k1 < k2)
  788. return -1;
  789. else if (k1 > k2)
  790. return 1;
  791. }
  792. /* Alphabetical comparison */
  793. return (int) ((unsigned char) a[i]) - ((unsigned char) b[i]);
  794. }
  795. /* Convert multi-byte string to wide character string */
  796. #if !defined(_MSC_VER) || _MSC_VER < 1400
  797. static int dirent_mbstowcs_s(
  798. size_t *pReturnValue, wchar_t *wcstr,
  799. size_t sizeInWords, const char *mbstr, size_t count)
  800. {
  801. /* Older Visual Studio or non-Microsoft compiler */
  802. size_t n = mbstowcs(wcstr, mbstr, sizeInWords);
  803. if (wcstr && n >= count)
  804. return /*error*/ 1;
  805. /* Zero-terminate output buffer */
  806. if (wcstr && sizeInWords) {
  807. if (n >= sizeInWords)
  808. n = sizeInWords - 1;
  809. wcstr[n] = 0;
  810. }
  811. /* Length of multi-byte string with zero terminator */
  812. if (pReturnValue) {
  813. *pReturnValue = n + 1;
  814. }
  815. /* Success */
  816. return 0;
  817. }
  818. #endif
  819. /* Convert wide-character string to multi-byte string */
  820. #if !defined(_MSC_VER) || _MSC_VER < 1400
  821. static int dirent_wcstombs_s(
  822. size_t *pReturnValue, char *mbstr,
  823. size_t sizeInBytes, const wchar_t *wcstr, size_t count)
  824. {
  825. /* Older Visual Studio or non-Microsoft compiler */
  826. size_t n = wcstombs(mbstr, wcstr, sizeInBytes);
  827. if (mbstr && n >= count)
  828. return /*error*/1;
  829. /* Zero-terminate output buffer */
  830. if (mbstr && sizeInBytes) {
  831. if (n >= sizeInBytes) {
  832. n = sizeInBytes - 1;
  833. }
  834. mbstr[n] = '\0';
  835. }
  836. /* Length of resulting multi-bytes string WITH zero-terminator */
  837. if (pReturnValue) {
  838. *pReturnValue = n + 1;
  839. }
  840. /* Success */
  841. return 0;
  842. }
  843. #endif
  844. /* Set errno variable */
  845. #if !defined(_MSC_VER) || _MSC_VER < 1400
  846. static void dirent_set_errno(int error)
  847. {
  848. /* Non-Microsoft compiler or older Microsoft compiler */
  849. errno = error;
  850. }
  851. #endif
  852. #ifdef __cplusplus
  853. }
  854. #endif
  855. #endif /*DIRENT_H*/

3.生成.exe文件

使用Cmake GUI编译生成, 需要自己在yolov5的的tensorRT目录下新建一个build文件夹:

重新生成后没有error就说明编译成功了:

4.生成engine文件

按照大佬的方式,将wts文件copy到trt版yolov5的build目录下:

执行:

yolov5.exe -s yolov5s.wts yolov5.engine s

5.测试结果

偷个懒,将yolov5源码的images目录直接复制到Release路径下

执行

yolov5.exe -d yolov5.engine images

 

生成了两张图片:


 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/900537
推荐阅读
相关标签
  

闽ICP备14008679号