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.4 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
  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. endif ()
  43. include_directories(${PROJECT_NAME} ${ONNXRUNTIME_ROOT}/include)
  44. set(PROJECT_SOURCES
  45. main.cpp
  46. inference.h
  47. inference.cpp
  48. )
  49. add_executable(${PROJECT_NAME} ${PROJECT_SOURCES})
  50. if (WIN32)
  51. target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS} ${ONNXRUNTIME_ROOT}/lib/onnxruntime.lib)
  52. if (USE_CUDA)
  53. target_link_libraries(${PROJECT_NAME} ${CUDA_LIBRARIES})
  54. endif ()
  55. elseif (LINUX)
  56. target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS} ${ONNXRUNTIME_ROOT}/lib/libonnxruntime.so)
  57. if (USE_CUDA)
  58. target_link_libraries(${PROJECT_NAME} ${CUDA_LIBRARIES})
  59. endif ()
  60. elseif (APPLE)
  61. target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS} ${ONNXRUNTIME_ROOT}/lib/libonnxruntime.dylib)
  62. endif ()
  63. # For windows system, copy onnxruntime.dll to the same folder of the executable file
  64. if (WIN32)
  65. add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
  66. COMMAND ${CMAKE_COMMAND} -E copy_if_different
  67. "${ONNXRUNTIME_ROOT}/lib/onnxruntime.dll"
  68. $<TARGET_FILE_DIR:${PROJECT_NAME}>)
  69. endif ()
  70. # Download https://raw.githubusercontent.com/ultralytics/ultralytics/main/ultralytics/cfg/datasets/coco.yaml
  71. # and put it in the same folder of the executable file
  72. configure_file(coco.yaml ${CMAKE_CURRENT_BINARY_DIR}/coco.yaml COPYONLY)
  73. # Copy yolov8n.onnx file to the same folder of the executable file
  74. configure_file(yolov8n.onnx ${CMAKE_CURRENT_BINARY_DIR}/yolov8n.onnx COPYONLY)
  75. # Create folder name images in the same folder of the executable file
  76. add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
  77. COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/images
  78. )
Tip!

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

Comments

Loading...