Skip to content

Commit afb8972

Browse files
Change CMake to be catkin package and small overall changes to run on ed_sensor_integration
1 parent 2318110 commit afb8972

File tree

3 files changed

+48
-63
lines changed

3 files changed

+48
-63
lines changed

CMakeLists.txt

Lines changed: 41 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,65 @@
1-
cmake_minimum_required(VERSION 3.14) # FetchContent requires 3.14+
1+
cmake_minimum_required(VERSION 3.14)
22

3-
set(PROJECT_NAME Yolov8OnnxRuntimeCPPInference)
3+
set(PROJECT_NAME yolo_onnx_ros)
44
project(${PROJECT_NAME} VERSION 0.0.1 LANGUAGES CXX)
55

6-
# -------------- Support C++17 for using filesystem ------------------#
76
set(CMAKE_CXX_STANDARD 17)
87
set(CMAKE_CXX_STANDARD_REQUIRED ON)
9-
set(CMAKE_CXX_EXTENSIONS ON)
10-
set(CMAKE_INCLUDE_CURRENT_DIR ON)
118

12-
# to show header files in Qt Creator
13-
file(GLOB_RECURSE HEADER_FILES include/*.h)
9+
find_package(catkin REQUIRED COMPONENTS
10+
roscpp
1411

15-
# -------------- Google Test ------------------#
16-
include(FetchContent)
17-
FetchContent_Declare(
18-
googletest
19-
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
2012
)
21-
22-
FetchContent_MakeAvailable(googletest)
23-
24-
enable_testing()
25-
26-
# -------------- OpenCV ------------------#
2713
find_package(OpenCV REQUIRED)
28-
include_directories(${OpenCV_INCLUDE_DIRS})
2914

30-
# -------------- ONNXRuntime ------------------#
31-
set(ONNXRUNTIME_VERSION 1.21.0)
15+
# ONNX Runtime (adjust to your local path)
3216
set(ONNXRUNTIME_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/../hero_sam.bak/onnxruntime-linux-x64-gpu-1.21.1")
3317
include_directories(${ONNXRUNTIME_ROOT}/include)
3418

35-
# -------------- Cuda ------------------#
19+
# CUDA (optional)
3620
add_definitions(-DUSE_CUDA=1)
3721
include_directories(/usr/local/cuda/include)
3822

39-
set(PROJECT_SOURCES
40-
src/main.cpp
41-
src/yolo_inference.cpp
42-
src/detection.cpp)
43-
44-
# Main executable (without tests)
45-
add_executable(${PROJECT_NAME} ${PROJECT_SOURCES})
46-
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
47-
48-
# Link libraries for main executable
49-
target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS} ${ONNXRUNTIME_ROOT}/lib/libonnxruntime.so)
23+
catkin_package(
24+
INCLUDE_DIRS include
25+
LIBRARIES ${PROJECT_NAME}_core
26+
CATKIN_DEPENDS roscpp
27+
DEPENDS OpenCV
28+
)
5029

51-
# Test executable
52-
add_executable(${PROJECT_NAME}_test
53-
test/yolo_test.cpp
54-
src/yolo_inference.cpp # Include only the source files needed for testing (not main.cpp)
55-
src/detection.cpp
30+
include_directories(
31+
include
32+
${catkin_INCLUDE_DIRS}
33+
${OpenCV_INCLUDE_DIRS}
5634
)
5735

58-
target_include_directories(${PROJECT_NAME}_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include)
36+
# Core library (to be linked by other packages)
37+
add_library(${PROJECT_NAME}_core
38+
src/yolo_inference.cpp
39+
src/detection.cpp
40+
)
41+
target_link_libraries(${PROJECT_NAME}_core
42+
${catkin_LIBRARIES}
43+
${OpenCV_LIBS}
44+
${ONNXRUNTIME_ROOT}/lib/libonnxruntime.so
45+
)
5946

60-
# Link libraries for test executable
61-
target_link_libraries(${PROJECT_NAME}_test
62-
gtest_main
63-
gmock_main
64-
${OpenCV_LIBS}
65-
${ONNXRUNTIME_ROOT}/lib/libonnxruntime.so
47+
# Node executable
48+
add_executable(${PROJECT_NAME}
49+
src/main.cpp
50+
)
51+
target_link_libraries(${PROJECT_NAME}
52+
${PROJECT_NAME}_core
6653
)
6754

68-
# Add test to CTest
69-
add_test(NAME yolo_tests COMMAND ${PROJECT_NAME}_test)
55+
# Install headers, library, and node so other packages can depend/link
56+
install(TARGETS ${PROJECT_NAME}_core ${PROJECT_NAME}
57+
ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
58+
LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
59+
RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
60+
)
61+
install(DIRECTORY include/ DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION})
7062

71-
# Download https://raw.githubusercontent.com/ultralytics/ultralytics/main/ultralytics/cfg/datasets/coco.yaml
72-
# and put it in the same folder of the executable file
63+
# Runtime assets (# Download https://raw.githubusercontent.com/ultralytics/ultralytics/main/ultralytics/cfg/datasets/coco.yaml)
7364
configure_file(/home/amigo/Documents/repos/hero_sam.bak/yolo_inference/data/coco.yaml ${CMAKE_CURRENT_BINARY_DIR}/coco.yaml COPYONLY)
74-
75-
# Copy yolov8n.onnx file to the same folder of the executable file
76-
configure_file(/home/amigo/Documents/repos/hero_sam.bak/yolo_inference/model/yolo11m.onnx ${CMAKE_CURRENT_BINARY_DIR}/yolo11m.onnx COPYONLY)
77-
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-
)
65+
configure_file(/home/amigo/Documents/repos/hero_sam.bak/yolo_inference/model/yolo11m.onnx ${CMAKE_CURRENT_BINARY_DIR}/yolo11m.onnx COPYONLY)

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<maintainer email="iasonth95@gmail.com">Iason Theodorou</maintainer>
1111

1212
<license>ToDo</license>
13+
<depend>roscpp</depend>
1314

1415
<buildtool_depend>catkin</buildtool_depend>
1516

src/detection.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
#include <fstream>
66
#include <random>
77
#include "yolo_inference.h"
8+
//#define LOGGING
89
std::vector<DL_RESULT> Detector(std::unique_ptr<YOLO_V8>& p, const cv::Mat& img) {
910

1011
std::vector<DL_RESULT> res;
1112
p->RunSession(img, res);
12-
13+
#ifdef LOGGING
1314
for (auto& re : res)
1415
{
1516
cv::RNG rng(cv::getTickCount());
@@ -39,13 +40,12 @@ std::vector<DL_RESULT> Detector(std::unique_ptr<YOLO_V8>& p, const cv::Mat& img)
3940
cv::Scalar(0, 0, 0),
4041
2
4142
);
42-
43-
44-
}
43+
}
4544
std::cout << "Press any key to exit" << std::endl;
4645
cv::imshow("Result of Detection", img);
4746
cv::waitKey(0);
4847
cv::destroyAllWindows();
48+
#endif
4949
return std::move(res);
5050

5151
}
@@ -92,7 +92,7 @@ std::vector<DL_RESULT> Detector(std::unique_ptr<YOLO_V8>& p, const cv::Mat& img)
9292

9393
int ReadCocoYaml(std::unique_ptr<YOLO_V8>& p) {
9494
// Open the YAML file
95-
std::ifstream file("coco.yaml");
95+
std::ifstream file("/home/amigo/Documents/repos/hero_sam.bak/yolo_inference/data/coco.yaml");
9696
if (!file.is_open())
9797
{
9898
std::cerr << "Failed to open file" << std::endl;
@@ -146,7 +146,7 @@ std::tuple<std::unique_ptr<YOLO_V8>, DL_INIT_PARAM> Initialize()
146146
params.rectConfidenceThreshold = 0.1;
147147
params.iouThreshold = 0.5;
148148
// Mayve change the model from V11 to V8
149-
params.modelPath = "yolo11m.onnx";
149+
params.modelPath = "/home/amigo/Documents/repos/yolo_onnx_ros/build/yolo11m.onnx";
150150
params.imgSize = { 640, 640 };
151151
#ifdef USE_CUDA
152152
params.cudaEnable = true;

0 commit comments

Comments
 (0)