Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

CMakeLists.txt 3.6 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
  1. cmake_minimum_required(VERSION 3.5)
  2. set(PROJECT_NAME Yolov8OnnxRuntimeCPPInference)
  3. project(${PROJECT_NAME} VERSION 0.0.1 LANGUAGES CXX)
  4. # -------------- Support C++17 for using filesystem ------------------#
  5. set(CMAKE_CXX_STANDARD 17)
  6. set(CMAKE_CXX_STANDARD_REQUIRED ON)
  7. set(CMAKE_CXX_EXTENSIONS ON)
  8. set(CMAKE_INCLUDE_CURRENT_DIR ON)
  9. # -------------- OpenCV ------------------#
  10. find_package(OpenCV REQUIRED)
  11. include_directories(${OpenCV_INCLUDE_DIRS})
  12. # -------------- Compile CUDA for FP16 inference if needed ------------------#
  13. option(USE_CUDA "Enable CUDA support" ON)
  14. if (NOT APPLE AND USE_CUDA)
  15. find_package(CUDA REQUIRED)
  16. include_directories(${CUDA_INCLUDE_DIRS})
  17. add_definitions(-DUSE_CUDA)
  18. else ()
  19. set(USE_CUDA OFF)
  20. endif ()
  21. # -------------- ONNXRUNTIME ------------------#
  22. # Set ONNXRUNTIME_VERSION
  23. set(ONNXRUNTIME_VERSION 1.15.1)
  24. if (WIN32)
  25. if (USE_CUDA)
  26. set(ONNXRUNTIME_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/onnxruntime-win-x64-gpu-${ONNXRUNTIME_VERSION}")
  27. else ()
  28. set(ONNXRUNTIME_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/onnxruntime-win-x64-${ONNXRUNTIME_VERSION}")
  29. endif ()
  30. elseif (LINUX)
  31. if (USE_CUDA)
  32. set(ONNXRUNTIME_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/onnxruntime-linux-x64-gpu-${ONNXRUNTIME_VERSION}")
  33. else ()
  34. set(ONNXRUNTIME_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/onnxruntime-linux-x64-${ONNXRUNTIME_VERSION}")
  35. endif ()
  36. elseif (APPLE)
  37. set(ONNXRUNTIME_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/onnxruntime-osx-arm64-${ONNXRUNTIME_VERSION}")
  38. # Apple X64 binary
  39. # set(ONNXRUNTIME_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/onnxruntime-osx-x64-${ONNXRUNTIME_VERSION}")
  40. # Apple Universal binary
  41. # set(ONNXRUNTIME_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/onnxruntime-osx-universal2-${ONNXRUNTIME_VERSION}")
  42. else ()
  43. message(SEND_ERROR "Variable ONNXRUNTIME_ROOT is not set properly. Please check if your cmake project \
  44. is not compiled with `-D WIN32=TRUE`, `-D LINUX=TRUE`, or `-D APPLE=TRUE`!")
  45. endif ()
  46. include_directories(${PROJECT_NAME} ${ONNXRUNTIME_ROOT}/include)
  47. set(PROJECT_SOURCES
  48. main.cpp
  49. inference.h
  50. inference.cpp
  51. )
  52. add_executable(${PROJECT_NAME} ${PROJECT_SOURCES})
  53. if (WIN32)
  54. target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS} ${ONNXRUNTIME_ROOT}/lib/onnxruntime.lib)
  55. if (USE_CUDA)
  56. target_link_libraries(${PROJECT_NAME} ${CUDA_LIBRARIES})
  57. endif ()
  58. elseif (LINUX)
  59. target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS} ${ONNXRUNTIME_ROOT}/lib/libonnxruntime.so)
  60. if (USE_CUDA)
  61. target_link_libraries(${PROJECT_NAME} ${CUDA_LIBRARIES})
  62. endif ()
  63. elseif (APPLE)
  64. target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS} ${ONNXRUNTIME_ROOT}/lib/libonnxruntime.dylib)
  65. endif ()
  66. # For windows system, copy onnxruntime.dll to the same folder of the executable file
  67. if (WIN32)
  68. add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
  69. COMMAND ${CMAKE_COMMAND} -E copy_if_different
  70. "${ONNXRUNTIME_ROOT}/lib/onnxruntime.dll"
  71. $<TARGET_FILE_DIR:${PROJECT_NAME}>)
  72. endif ()
  73. # Download https://raw.githubusercontent.com/ultralytics/ultralytics/main/ultralytics/cfg/datasets/coco.yaml
  74. # and put it in the same folder of the executable file
  75. configure_file(coco.yaml ${CMAKE_CURRENT_BINARY_DIR}/coco.yaml COPYONLY)
  76. # Copy yolov8n.onnx file to the same folder of the executable file
  77. configure_file(yolov8n.onnx ${CMAKE_CURRENT_BINARY_DIR}/yolov8n.onnx COPYONLY)
  78. # Create folder name images in the same folder of the executable file
  79. add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
  80. COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/images
  81. )
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...