Skip to content

Commit 8aeb657

Browse files
Fixed structurre of CMakeLists and package.xml and logged with console bridge
1 parent 3a55f75 commit 8aeb657

File tree

3 files changed

+112
-71
lines changed

3 files changed

+112
-71
lines changed

CMakeLists.txt

Lines changed: 80 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,17 @@ cmake_minimum_required(VERSION 3.14)
33
project(sam_onnx_ros)
44

55
# -------------- CMake Policies ------------------#
6-
#add_compile_options(-Wall -Werror=all)
7-
#add_compile_options(-Wextra -Werror=extra)
6+
# add_compile_options(-Wall -Werror=all)
7+
# add_compile_options(-Wextra -Werror=extra)
88

99
# -------------- Support C++17 for using filesystem ------------------#
1010
set(CMAKE_CXX_STANDARD 17)
1111
set(CMAKE_CXX_STANDARD_REQUIRED ON)
12-
set(CMAKE_CXX_EXTENSIONS ON)
13-
#set(CMAKE_INCLUDE_CURRENT_DIR ON)
1412
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
13+
1514
# -------------- OpenCV ------------------#
1615
find_package(OpenCV REQUIRED)
17-
include_directories(${OpenCV_INCLUDE_DIRS})
18-
16+
find_package(console_bridge REQUIRED)
1917

2018
# -------------- ONNXRuntime ------------------#
2119
set(ONNXRUNTIME_VERSION 1.21.0)
@@ -26,94 +24,124 @@ include_directories(${ONNXRUNTIME_ROOT}/include)
2624
add_definitions(-DUSE_CUDA=1)
2725
include_directories(/usr/local/cuda/include)
2826

27+
# -------------- Models ------------------#
28+
# TODO: Find proper folder Copy sam_<model>.onnx file to the same folder of the executable file
29+
configure_file(~/Documents/repos/hero_sam.bak/sam_inference/model/SAM_mask_decoder.onnx ${CMAKE_CURRENT_BINARY_DIR}/SAM_mask_decoder.onnx COPYONLY)
30+
configure_file(~/Documents/repos/hero_sam.bak/sam_inference/model/SAM_encoder.onnx ${CMAKE_CURRENT_BINARY_DIR}/SAM_encoder.onnx COPYONLY)
31+
32+
2933
find_package(catkin REQUIRED
3034
COMPONENTS
31-
# roscpp
32-
# tue_config
33-
# tue_filesystem
34-
# code_profiler
35-
35+
rosconsole
36+
console_bridge
3637
#onnxruntime_ros
3738
)
3839

40+
find_package(console_bridge REQUIRED)
41+
3942
# ------------------------------------------------------------------------------------------------
4043
# CATKIN EXPORT
4144
# ------------------------------------------------------------------------------------------------
4245

4346
catkin_package(
4447
INCLUDE_DIRS include
45-
#LIBRARIES ${PROJECT_NAME}
46-
LIBRARIES sam_onnx_ros_core
48+
LIBRARIES ${PROJECT_NAME}_lib
4749
CATKIN_DEPENDS
48-
DEPENDS OpenCV
50+
DEPENDS OpenCV console_bridge
4951
)
5052

5153
# ------------------------------------------------------------------------------------------------
5254
# BUILD
5355
# ------------------------------------------------------------------------------------------------
5456

55-
include_directories(
56-
include
57-
SYSTEM
58-
${OpenCV_INCLUDE_DIRS}
59-
${catkin_INCLUDE_DIRS}
60-
)
61-
62-
set(PROJECT_SOURCES
63-
src/main.cpp
57+
# Build core library
58+
add_library(${PROJECT_NAME}_lib
6459
src/sam_inference.cpp
6560
src/segmentation.cpp
6661
src/utils.cpp
6762
)
6863

69-
# Build core library (no main.cpp here)
70-
add_library(sam_onnx_ros_core
71-
src/sam_inference.cpp
72-
src/segmentation.cpp
73-
src/utils.cpp
64+
target_include_directories(${PROJECT_NAME}_lib
65+
PUBLIC
66+
include
67+
SYSTEM
68+
${OpenCV_INCLUDE_DIRS}
69+
${catkin_INCLUDE_DIRS}
70+
${console_bridge_INCLUDE_DIRS}
71+
${ONNXRUNTIME_ROOT}/include
7472
)
75-
target_link_libraries(sam_onnx_ros_core
73+
74+
75+
target_link_libraries(${PROJECT_NAME}_lib
7676
${OpenCV_LIBS}
7777
${catkin_LIBRARIES}
7878
${ONNXRUNTIME_ROOT}/lib/libonnxruntime.so
7979
)
80-
target_include_directories(sam_onnx_ros_core PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
8180

8281
# Main executable links the core lib
83-
add_executable(${PROJECT_NAME} src/main.cpp)
84-
target_link_libraries(${PROJECT_NAME} sam_onnx_ros_core)
82+
add_executable(${PROJECT_NAME}
83+
src/main.cpp
84+
)
8585

86-
# Copy sam_<model>.onnx file to the same folder of the executable file
87-
configure_file(~/Documents/repos/hero_sam.bak/sam_inference/model/SAM_mask_decoder.onnx ${CMAKE_CURRENT_BINARY_DIR}/SAM_mask_decoder.onnx COPYONLY)
88-
configure_file(~/Documents/repos/hero_sam.bak/sam_inference/model/SAM_encoder.onnx ${CMAKE_CURRENT_BINARY_DIR}/SAM_encoder.onnx COPYONLY)
86+
target_link_libraries(${PROJECT_NAME}
87+
${PROJECT_NAME}_lib
88+
${catkin_LIBRARIES}
89+
${ONNXRUNTIME_ROOT}/lib/libonnxruntime.so
90+
)
91+
92+
# ------------------------------------------------------------------------------------------------
93+
# Install Targets
94+
# ------------------------------------------------------------------------------------------------
95+
96+
install(
97+
DIRECTORY include/
98+
DESTINATION ${CATKIN_GLOBAL_INCLUDE_DESTINATION}
99+
)
89100

90-
# Create folder name images in the same folder of the executable file
91-
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
92-
COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/images
101+
install(
102+
TARGETS
103+
${PROJECT_NAME}_lib
104+
ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
105+
LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
106+
RUNTIME DESTINATION ${CATKIN_GLOBAL_BIN_DESTINATION}
93107
)
94108

95-
# # Enable testing
109+
install(
110+
TARGETS
111+
${PROJECT_NAME}
112+
DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
113+
)
114+
115+
# ------------------------------------------------------------------------------------------------
116+
# Testing
117+
# ------------------------------------------------------------------------------------------------
96118
if (CATKIN_ENABLE_TESTING)
97-
# find_package(catkin_lint_cmake REQUIRED)
98-
# catkin_add_catkin_lint_test("-W2 --ignore HEADER_OUTSIDE_PACKAGE_INCLUDE_PATH")
119+
find_package(catkin_lint_cmake REQUIRED)
120+
catkin_add_catkin_lint_test("-W2 --ignore HEADER_OUTSIDE_PACKAGE_INCLUDE_PATH")
99121

100-
# Utils unit tests (no models needed)
122+
# Utils unit tests (no models needed)
101123
catkin_add_gtest(utils_tests test/test_utils.cpp)
102124
if(TARGET utils_tests)
103-
target_link_libraries(utils_tests sam_onnx_ros_core GTest::gtest_main ${catkin_LIBRARIES})
104-
target_include_directories(utils_tests PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
125+
target_link_libraries(
126+
utils_tests
127+
${PROJECT_NAME}_lib
128+
${catkin_LIBRARIES}
129+
GTest::gtest
130+
GTest::gtest_main
131+
)
132+
#target_include_directories(utils_tests PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
105133
endif()
106134

107135
# SAM integration-ish tests (may need models)
108136
catkin_add_gtest(sam_tests test/sam_test.cpp)
109137
if(TARGET sam_tests)
110-
target_link_libraries(sam_tests sam_onnx_ros_core GTest::gtest_main ${catkin_LIBRARIES})
111-
target_include_directories(sam_tests PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
138+
target_link_libraries(
139+
sam_tests
140+
${PROJECT_NAME}_lib
141+
${catkin_LIBRARIES}
142+
GTest::gtest
143+
GTest::gtest_main
144+
)
145+
#target_include_directories(sam_tests PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
112146
endif()
113147
endif()
114-
115-
116-
117-
#If you want to debug
118-
# set(CMAKE_BUILD_TYPE Debug)
119-
# set(CMAKE_CXX_FLAGS_DEBUG "-g")

package.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,18 @@
1212

1313
<license>BSD</license>
1414

15+
1516
<buildtool_depend>catkin</buildtool_depend>
1617

18+
<depend>libconsole-bridge-dev</depend>
19+
1720
<build_depend>libopencv-dev</build_depend>
1821
<build_depend>onnxruntime_ros</build_depend>
22+
<build_depend>rosconsole</build_depend>
1923

2024
<exec_depend>libopencv-dev</exec_depend>
2125
<exec_depend>onnxruntime_ros</exec_depend>
26+
<exec_depend>rosconsole</exec_depend>
2227

2328
<test_depend>catkin_lint_cmake</test_depend>
2429

src/sam_inference.cpp

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include "sam_inference.h"
22
#include "utils.h"
3+
34
#include <regex>
5+
#include <console_bridge/console.h>
46

57
#define benchmark
68
//#define ROI
@@ -50,7 +52,7 @@ const char *SAM::CreateSession(SEG::DL_INIT_PARAM &iParams) {
5052
if (result) {
5153
Ret = "[SAM]:Your model path is error.Change your model path without "
5254
"chinese characters.";
53-
std::cout << Ret << std::endl;
55+
CONSOLE_BRIDGE_logWarn("%s", Ret);
5456
return Ret;
5557
}
5658
try {
@@ -109,7 +111,7 @@ const char *SAM::CreateSession(SEG::DL_INIT_PARAM &iParams) {
109111
std::string str_result = std::string(str1) + std::string(str2);
110112
char *merged = new char[str_result.length() + 1];
111113
std::strcpy(merged, str_result.c_str());
112-
std::cout << merged << std::endl;
114+
CONSOLE_BRIDGE_logWarn("%s", merged);
113115
delete[] merged;
114116
return "[SAM]:Create _session failed.";
115117
}
@@ -190,13 +192,17 @@ const char *SAM::TensorProcess_(clock_t &starttime_1, const cv::Mat &iImg,
190192
double post_process_time =
191193
(double)(starttime_4 - starttime_3) / CLOCKS_PER_SEC * 1000;
192194
if (_cudaEnable) {
193-
std::cout << "[SAM_encoder(CUDA)]: " << pre_process_time << "ms pre-process, "
194-
<< process_time << "ms inference, " << post_process_time
195-
<< "ms post-process." << std::endl;
195+
CONSOLE_BRIDGE_logInform("[SAM_encoder(CUDA)]: %.2fms pre-process, %.2fms inference, "
196+
"%.2fms post-process.",
197+
pre_process_time, process_time,
198+
post_process_time
199+
);
196200
} else {
197-
std::cout << "[SAM_encoder(CPU)]: " << pre_process_time << "ms pre-process, "
198-
<< process_time << "ms inference, " << post_process_time
199-
<< "ms post-process." << std::endl;
201+
CONSOLE_BRIDGE_logInform("[SAM_encoder(CPU)]: %.2fms pre-process, %.2fms inference, "
202+
"%.2fms post-process.",
203+
pre_process_time, process_time,
204+
post_process_time
205+
);
200206
}
201207
#endif // benchmark
202208

@@ -305,20 +311,24 @@ const char *SAM::TensorProcess_(clock_t &starttime_1, const cv::Mat &iImg,
305311
double post_process_time =
306312
(double)(starttime_4 - starttime_3) / CLOCKS_PER_SEC * 1000;
307313
if (_cudaEnable) {
308-
std::cout << "[SAM_decoder(CUDA)]: " << pre_process_time << "ms pre-process, "
309-
<< process_time << "ms inference, " << post_process_time
310-
<< "ms post-process." << std::endl;
314+
CONSOLE_BRIDGE_logInform("[SAM_decoder(CUDA)]: %.2fms pre-process, %.2fms inference, "
315+
"%.2fms post-process.",
316+
pre_process_time, process_time,
317+
post_process_time
318+
);
311319
} else {
312-
std::cout << "[SAM_decoder(CPU)]: " << pre_process_time << "ms pre-process, "
313-
<< process_time << "ms inference, " << post_process_time
314-
<< "ms post-process." << std::endl;
320+
CONSOLE_BRIDGE_logInform("[SAM_decoder(CPU)]: %.2fms pre-process, %.2fms inference, "
321+
"%.2fms post-process.",
322+
pre_process_time, process_time,
323+
post_process_time
324+
);
315325
}
316326
#endif // benchmark
317327
break;
318328
}
319329

320330
default:
321-
std::cout << "[SAM]: " << "Not support model type." << std::endl;
331+
CONSOLE_BRIDGE_logWarn("[SAM]: " "Not support model type.");
322332
}
323333
return RET_OK;
324334
}
@@ -350,8 +360,7 @@ char *SAM::WarmUpSession_(SEG::MODEL_TYPE _modelType)
350360
double post_process_time =
351361
(double)(starttime_4 - starttime_1) / CLOCKS_PER_SEC * 1000;
352362
if (_cudaEnable) {
353-
std::cout << "[SAM(CUDA)]: " << "Cuda warm-up cost " << post_process_time
354-
<< " ms. " << std::endl;
363+
CONSOLE_BRIDGE_logInform("[SAM(CUDA)]: Cuda warm-up cost %.2f ms.", post_process_time);
355364
}
356365
break;
357366
}
@@ -417,8 +426,7 @@ char *SAM::WarmUpSession_(SEG::MODEL_TYPE _modelType)
417426
double post_process_time =
418427
(double)(starttime_4 - starttime_1) / CLOCKS_PER_SEC * 1000;
419428
if (_cudaEnable) {
420-
std::cout << "[SAM(CUDA)]: " << "Cuda warm-up cost " << post_process_time
421-
<< " ms. " << std::endl;
429+
CONSOLE_BRIDGE_logInform("[SAM(CUDA)]: Cuda warm-up cost %.2f ms.", post_process_time);
422430
}
423431

424432
break;

0 commit comments

Comments
 (0)