Init: Update Intrusion-detection source code

This commit is contained in:
GuanYuankai 2025-09-15 14:15:58 +08:00
parent 407904cd65
commit 0d7bba41a1
11 changed files with 138 additions and 107 deletions

52
.gitignore vendored
View File

@ -1,24 +1,40 @@
# Build artifacts
build/
# Prerequisites
*.d
# Compiled Object files
*.slo
*.lo
*.o
*.a
*.so
*.obj
# Precompiled Headers
*.gch
*.pch
# Compiled Dynamic libraries
*.dylib
*.dll
# Fortran module files
*.mod
*.smod
# Compiled Static libraries
*.lai
*.la
*.a
*.lib
# Executables
*.exe
*.out
*.app
# IDE and editor files
.vscode/
.idea/
*.suo
*.ntvs*
*.njsproj
*.sln
*.swp
*.mp4
/build
/install
/.vscode
# Log files
*.log
# Dependency directories
vendor/
node_modules/
*.png
*.jpg
*.mp4

17
CMakeLists.txt Normal file
View File

@ -0,0 +1,17 @@
cmake_minimum_required(VERSION 3.4.1)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
#
project(EdgeProxyPlatform)
# 便CMake
message(STATUS "Running Top-Level CMake for ${PROJECT_NAME}")
# 使 add_subdirectory() services
# CMake services/intrusion-detection/CMakeLists.txt
add_subdirectory(services/intrusion-detection)
#
# add_subdirectory(services/vehicle-recognition)

1
libs/librknn_api.so Symbolic link
View File

@ -0,0 +1 @@
librknnrt.so

BIN
libs/librknnrt.so Normal file

Binary file not shown.

View File

@ -1,61 +1,61 @@
# CMake
cmake_minimum_required(VERSION 3.4.1)
project(rknn_yolov5_demo)
#
project(IntrusionDetectionService CXX)
# C++
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_FLAGS "-pthread")
# skip 3rd-party lib dependencies
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--allow-shlib-undefined")
# --- 1. 定义并查找公共库 (RKNN) ---
# 使 libs
set(COMMON_LIB_DIR ${CMAKE_SOURCE_DIR}/libs)
find_library(RKNNRT_LIBRARY rknnrt HINTS ${COMMON_LIB_DIR})
find_library(RKNNAPI_LIBRARY rknn_api HINTS ${COMMON_LIB_DIR})
# install target and libraries
set(CMAKE_INSTALL_PREFIX ${CMAKE_SOURCE_DIR}/install/rknn_yolov5_demo_${CMAKE_SYSTEM_NAME})
if(NOT RKNNRT_LIBRARY OR NOT RKNNAPI_LIBRARY)
message(FATAL_ERROR "Could not find RKNN libraries in ${COMMON_LIB_DIR}")
else()
message(STATUS "Found rknnrt: ${RKNNRT_LIBRARY}")
message(STATUS "Found rknn_api: ${RKNNAPI_LIBRARY}")
endif()
set(CMAKE_SKIP_INSTALL_RPATH FALSE)
set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
# rknn api
set(RKNN_API_PATH ${CMAKE_SOURCE_DIR}/../../runtime/RK3588/${CMAKE_SYSTEM_NAME}/librknn_api)
set(LIB_ARCH aarch64)
set(RKNN_RT_LIB ${CMAKE_SOURCE_DIR}/include/librknnrt.so)
#rga
set(RGA_PATH ${CMAKE_SOURCE_DIR}/include/3rdparty/rga/RK3588)
set(RGA_LIB ${RGA_PATH}/lib/Linux//${LIB_ARCH}/librga.so)
include_directories(${RKNN_API_PATH}/include)
include_directories(${CMAKE_SOURCE_DIR}/include/3rdparty)
include_directories(${RGA_PATH}/include)
# opencv
# --- 2. 定义并查找其他依赖 (OpenCV, RGA) ---
find_package(OpenCV REQUIRED)
set(CMAKE_INSTALL_RPATH "lib")
# rga include/3rdparty
set(RGA_PATH ${CMAKE_CURRENT_SOURCE_DIR}/include/3rdparty/rga/RK3588)
set(RGA_LIB ${RGA_PATH}/lib/Linux/aarch64/librga.so)
if(NOT EXISTS ${RGA_LIB})
message(FATAL_ERROR "RGA library not found at ${RGA_LIB}")
endif()
# rknn_yolov5_demo
include_directories( ${CMAKE_SOURCE_DIR}/include)
add_executable(rknn_yolov5_demo
# --- 3. ---
# () src "src/"
add_executable(intrusion_detection_service
src/main.cc
src/postprocess.cc
src/preprocess.cc
src/rkYolov5s.cc
)
target_link_libraries(rknn_yolov5_demo
${RKNN_RT_LIB}
${OpenCV_LIBS}
${RGA_LIB}
# --- 4. ---
# () include
target_include_directories(intrusion_detection_service PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include # <--
${OpenCV_INCLUDE_DIRS}
${RGA_PATH}/include
# : ${CMAKE_SOURCE_DIR}/include
)
# --- 5. ---
target_link_libraries(intrusion_detection_service
${RKNNRT_LIBRARY}
${RKNNAPI_LIBRARY}
${RGA_LIB}
${OpenCV_LIBS}
)
# install target and libraries
set(CMAKE_INSTALL_PREFIX ${CMAKE_SOURCE_DIR}/install/rknn_yolov5_demo_${CMAKE_SYSTEM_NAME})
install(TARGETS rknn_yolov5_demo DESTINATION ./)
install(PROGRAMS ${RKNN_RT_LIB} DESTINATION lib)
install(PROGRAMS ${RGA_LIB} DESTINATION lib)
install(DIRECTORY model DESTINATION ./)
message(STATUS "Configuration for IntrusionDetectionService is complete.")

View File

@ -11,16 +11,9 @@ int main(int argc, char **argv)
{
char *model_name = NULL;
const char* model_path = "/home/forlinx/rknn-cpp-Multithreading-main/model/RK3588/yolov5s-640-640.rknn";
// if (argc != 3)
// {
// printf("Usage: %s <rknn model> <jpg> \n", argv[0]);
// return -1;
// }
// 参数二,模型所在路径/The path where the model is located
// model_name = (char *)argv[1];
// 参数三, 视频/摄像头
// char *video_name = argv[2];
// 初始化rknn线程池/Initialize the rknn thread pool
int threadNum = 3;
@ -31,9 +24,14 @@ int main(int argc, char **argv)
return -1;
}
const char* WINDOW_NAME = "RTSP Fullscreen Display";
cv::namedWindow(WINDOW_NAME, cv::WINDOW_NORMAL);
cv::setWindowProperty(WINDOW_NAME, cv::WND_PROP_FULLSCREEN, cv::WINDOW_FULLSCREEN);
// const char *video_name = "/home/forlinx/Videos/test_video.mp4";
// cv::VideoCapture capture;
// if (strlen(video_name) == 1)
// capture.open((int)(video_name[0] - '0'));
// else
// capture.open(video_name);
setenv("OPENCV_FFMPEG_CAPTURE_OPTIONS", "rtsp_transport;tcp", 1);
printf("Set RTSP transport protocol to TCP\n");
std::string rtsp_url = "rtsp://admin:hzx12345@192.168.1.10:554/Streaming/Channels/1301";
cv::VideoCapture capture(rtsp_url, cv::CAP_FFMPEG);
@ -43,11 +41,14 @@ int main(int argc, char **argv)
return -1;
}
printf("RTSP stream opened successfully!\n");
// cv::VideoCapture capture;
// if (strlen(video_name) == 1)
// capture.open((int)(video_name[0] - '0'));
// else
// capture.open(video_name);
const char* WINDOW_NAME = "RTSP Fullscreen Display";
cv::namedWindow(WINDOW_NAME, cv::WINDOW_NORMAL);
cv::setWindowProperty(WINDOW_NAME, cv::WND_PROP_FULLSCREEN, cv::WINDOW_FULLSCREEN);
struct timeval time;
gettimeofday(&time, nullptr);

View File

@ -343,29 +343,18 @@ int post_process(int8_t *input0, int8_t *input1, int8_t *input2, int model_in_h,
int id = classId[n];
float obj_conf = objProbs[i];
if (id == 0)
{
group->results[last_count].box.left = (int)(clamp(x1, 0, model_in_w) / scale_w);
group->results[last_count].box.top = (int)(clamp(y1, 0, model_in_h) / scale_h);
group->results[last_count].box.right = (int)(clamp(x2, 0, model_in_w) / scale_w);
group->results[last_count].box.bottom = (int)(clamp(y2, 0, model_in_h) / scale_h);
group->results[last_count].prop = obj_conf;
char *label = labels[id];
strncpy(group->results[last_count].name, label, OBJ_NAME_MAX_SIZE);
group->results[last_count].box.left = (int)(clamp(x1, 0, model_in_w) / scale_w);
group->results[last_count].box.top = (int)(clamp(y1, 0, model_in_h) / scale_h);
group->results[last_count].box.right = (int)(clamp(x2, 0, model_in_w) / scale_w);
group->results[last_count].box.bottom = (int)(clamp(y2, 0, model_in_h) / scale_h);
group->results[last_count].prop = obj_conf;
char *label = labels[id];
strncpy(group->results[last_count].name, label, OBJ_NAME_MAX_SIZE);
// printf("result %2d: (%4d, %4d, %4d, %4d), %s\n", i, group->results[last_count].box.left,
// group->results[last_count].box.top,
// group->results[last_count].box.right, group->results[last_count].box.bottom, label);
last_count++;
}
// group->results[last_count].box.left = (int)(clamp(x1, 0, model_in_w) / scale_w);
// group->results[last_count].box.top = (int)(clamp(y1, 0, model_in_h) / scale_h);
// group->results[last_count].box.right = (int)(clamp(x2, 0, model_in_w) / scale_w);
// group->results[last_count].box.bottom = (int)(clamp(y2, 0, model_in_h) / scale_h);
// group->results[last_count].prop = obj_conf;
// char *label = labels[id];
// strncpy(group->results[last_count].name, label, OBJ_NAME_MAX_SIZE);
// last_count++;
// printf("result %2d: (%4d, %4d, %4d, %4d), %s\n", i, group->results[last_count].box.left,
// group->results[last_count].box.top,
// group->results[last_count].box.right, group->results[last_count].box.bottom, label);
last_count++;
}
group->count = last_count;

View File

@ -281,16 +281,23 @@ cv::Mat rkYolov5s::infer(cv::Mat &orig_img)
for (int i = 0; i < detect_result_group.count; i++)
{
detect_result_t *det_result = &(detect_result_group.results[i]);
sprintf(text, "%s %.1f%%", det_result->name, det_result->prop * 100);
// 打印预测物体的信息/Prints information about the predicted object
// printf("%s @ (%d %d %d %d) %f\n", det_result->name, det_result->box.left, det_result->box.top,
// det_result->box.right, det_result->box.bottom, det_result->prop);
int x1 = det_result->box.left;
int y1 = det_result->box.top;
int x2 = det_result->box.right;
int y2 = det_result->box.bottom;
rectangle(orig_img, cv::Point(x1, y1), cv::Point(x2, y2), cv::Scalar(256, 0, 0, 256), 3);
putText(orig_img, text, cv::Point(x1, y1 + 12), cv::FONT_HERSHEY_SIMPLEX, 0.4, cv::Scalar(255, 255, 255));
// ========================== 修改开始 ==========================
// 增加一个判断,只处理标签名称为 "person" 的结果
if (strcmp(det_result->name, "person") == 0)
{
sprintf(text, "%s %.1f%%", det_result->name, det_result->prop * 100);
// 打印预测物体的信息/Prints information about the predicted object
// printf("%s @ (%d %d %d %d) %f\n", det_result->name, det_result->box.left, det_result->box.top,
// det_result->box.right, det_result->box.bottom, det_result->prop);
int x1 = det_result->box.left;
int y1 = det_result->box.top;
int x2 = det_result->box.right;
int y2 = det_result->box.bottom;
rectangle(orig_img, cv::Point(x1, y1), cv::Point(x2, y2), cv::Scalar(256, 0, 0, 256), 3);
putText(orig_img, text, cv::Point(x1, y1 + 12), cv::FONT_HERSHEY_SIMPLEX, 0.4, cv::Scalar(255, 255, 255));
}
// ========================== 修改结束 ==========================
}
ret = rknn_outputs_release(ctx, io_num.n_output, outputs);