From 25f882efbd9a39d11a566394ce35b45dcd1376a7 Mon Sep 17 00:00:00 2001 From: yala Date: Tue, 12 Sep 2023 16:37:02 +0800 Subject: [PATCH 1/7] fix for ort v1.15 --- include/detector.h | 10 +++++++--- src/detector.cpp | 11 +++++++++-- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/include/detector.h b/include/detector.h index 113327d..f0be7ab 100644 --- a/include/detector.h +++ b/include/detector.h @@ -30,9 +30,13 @@ class YOLODetector static void getBestClassInfo(std::vector::iterator it, const int& numClasses, float& bestConf, int& bestClassId); - std::vector inputNames; - std::vector outputNames; bool isDynamicInputShape{}; cv::Size2f inputImageShape; -}; \ No newline at end of file + // Inputs + std::vector inputNodeNameAllocatedStrings; + std::vector inputNames; + // Outputs + std::vector outputNodeNameAllocatedStrings; + std::vector outputNames; +}; diff --git a/src/detector.cpp b/src/detector.cpp index c45ffe6..4b67ad4 100644 --- a/src/detector.cpp +++ b/src/detector.cpp @@ -48,8 +48,15 @@ YOLODetector::YOLODetector(const std::string& modelPath, for (auto shape : inputTensorShape) std::cout << "Input shape: " << shape << std::endl; - inputNames.push_back(session.GetInputName(0, allocator)); - outputNames.push_back(session.GetOutputName(0, allocator)); + // inputNames.push_back(session.GetInputName(0, allocator)); + // outputNames.push_back(session.GetOutputName(0, allocator)); + auto input_name = session.GetInputNameAllocated(0, allocator); + inputNodeNameAllocatedStrings.push_back(std::move(input_name)); + inputNames.push_back(inputNodeNameAllocatedStrings.back().get()); + + auto output_name = session.GetOutputNameAllocated(0, allocator); + outputNodeNameAllocatedStrings.push_back(std::move(output_name)); + outputNames.push_back(outputNodeNameAllocatedStrings.back().get()); std::cout << "Input name: " << inputNames[0] << std::endl; std::cout << "Output name: " << outputNames[0] << std::endl; From 85ce0928c091633b56981669b3d72bc6ed70cfd4 Mon Sep 17 00:00:00 2001 From: junchao-loongson Date: Tue, 19 Dec 2023 10:26:37 +0800 Subject: [PATCH 2/7] add v4l2 source --- CMakeLists.txt | 10 +++++-- src/main.cpp | 74 +++++++++++++++++++++++++++++++++++++------------- 2 files changed, 62 insertions(+), 22 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index be4d31c..fd0d7a9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,6 +5,7 @@ option(ONNXRUNTIME_DIR "Path to built ONNX Runtime directory." STRING) message(STATUS "ONNXRUNTIME_DIR: ${ONNXRUNTIME_DIR}") find_package(OpenCV REQUIRED) +#find_package(Qt5 COMPONENTS Widgets REQUIRED) include_directories("include/") @@ -16,16 +17,19 @@ add_executable(yolo_ort set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_STANDARD_REQUIRED ON) -target_include_directories(yolo_ort PRIVATE "${ONNXRUNTIME_DIR}/include") +target_include_directories(yolo_ort PRIVATE "${ONNXRUNTIME_DIR}/include/onnxruntime") # link_directories("${ONNXRUNTIME_DIR}/lib") target_compile_features(yolo_ort PRIVATE cxx_std_14) -target_link_libraries(yolo_ort ${OpenCV_LIBS}) if (WIN32) target_link_libraries(yolo_ort "${ONNXRUNTIME_DIR}/lib/onnxruntime.lib") endif(WIN32) if (UNIX) - target_link_libraries(yolo_ort "${ONNXRUNTIME_DIR}/lib/libonnxruntime.so") + target_link_libraries(yolo_ort "${ONNXRUNTIME_DIR}/lib/libonnxruntime.so" + ${OpenCV_LIBS} + # yolo_ort Qt5::Core + # yolo_ort Qt5::Widgets + ) endif(UNIX) diff --git a/src/main.cpp b/src/main.cpp index 29fa644..8b87e75 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,19 +1,28 @@ +#include #include #include +#include #include "cmdline.h" -#include "utils.h" #include "detector.h" +#include "utils.h" - -int main(int argc, char* argv[]) +void Delay(int time) // time*1000为秒数 { + clock_t now = clock(); + + while (clock() - now < time) + ; +} + +int main(int argc, char *argv[]) { const float confThreshold = 0.3f; const float iouThreshold = 0.4f; cmdline::parser cmd; cmd.add("model_path", 'm', "Path to onnx model.", true, "yolov5.onnx"); - cmd.add("image", 'i', "Image source to be detected.", true, "bus.jpg"); + cmd.add("image", 'i', "Image source to be detected.", false); + cmd.add("v4l2", 'v', "video dev node to be detected.", false); cmd.add("class_names", 'c', "Path to class names file.", true, "coco.names"); cmd.add("gpu", '\0', "Inference on cuda device."); @@ -23,37 +32,64 @@ int main(int argc, char* argv[]) const std::string classNamesPath = cmd.get("class_names"); const std::vector classNames = utils::loadNames(classNamesPath); const std::string imagePath = cmd.get("image"); + const std::string videoPath = cmd.get("v4l2"); const std::string modelPath = cmd.get("model_path"); - if (classNames.empty()) - { + if (classNames.empty()) { std::cerr << "Error: Empty class names file." << std::endl; return -1; } - YOLODetector detector {nullptr}; + if (imagePath.empty() && videoPath.empty()) { + std::cerr << "At least give one source! jpg or /dev/videox"< result; - try - { + try { detector = YOLODetector(modelPath, isGPU, cv::Size(640, 640)); std::cout << "Model was initialized." << std::endl; - - image = cv::imread(imagePath); - result = detector.detect(image, confThreshold, iouThreshold); - } - catch(const std::exception& e) - { + } catch (const std::exception &e) { std::cerr << e.what() << std::endl; return -1; } - utils::visualizeDetection(image, result, classNames); + if (!imagePath.empty()) { + image = cv::imread(imagePath); + result = detector.detect(image, confThreshold, iouThreshold); + utils::visualizeDetection(image, result, classNames); + cv::imshow("result", image); + // cv::imwrite("result.jpg", image); + cv::waitKey(0); + } else if (!videoPath.empty()) { + cv::VideoCapture cap(0); + if (!cap.isOpened()) { + std::cerr << "Error: Could not open camera." << std::endl; + return -1; + } - cv::imshow("result", image); - // cv::imwrite("result.jpg", image); - cv::waitKey(0); + while (true) { + cv::Mat frame; + cap >> frame; + if (frame.empty()) { + std::cerr << "Error: Could not read frame." << std::endl; + continue; + } + auto start_time = std::chrono::high_resolution_clock::now(); + result = detector.detect(frame, confThreshold, iouThreshold); + utils::visualizeDetection(frame, result, classNames); + auto end_time = std::chrono::high_resolution_clock::now(); + auto duration = std::chrono::duration_cast(end_time - start_time); + std::cout << "Execution time: " << duration.count() << " ms." << std::endl; + + cv::imshow("result", frame); + cv::waitKey(10); + // break; + } + } return 0; } From 173ba75862a107d860113690eafc20ffba92736e Mon Sep 17 00:00:00 2001 From: junchao98 Date: Tue, 19 Dec 2023 10:28:31 +0800 Subject: [PATCH 3/7] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7caa73f..2cc1493 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ C++ YOLO v5 ONNX Runtime inference code for object detection. ## Dependecies: - OpenCV 4.x -- ONNXRuntime 1.7+ +- ONNXRuntime 1.15+ - OS: Tested on Windows 10 and Ubuntu 20.04 - CUDA 11+ [Optional] From 2af773b4cb3b86aa27b983aeb762661158ca481b Mon Sep 17 00:00:00 2001 From: junchao98 Date: Tue, 19 Dec 2023 10:29:09 +0800 Subject: [PATCH 4/7] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2cc1493..49e3f06 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ C++ YOLO v5 ONNX Runtime inference code for object detection. ## Dependecies: - OpenCV 4.x - ONNXRuntime 1.15+ -- OS: Tested on Windows 10 and Ubuntu 20.04 +- OS: Tested on Windows 10 centos8 archlinux - CUDA 11+ [Optional] From 39c7cf0c63e283de4c4f3e84d3920f95905dcdaf Mon Sep 17 00:00:00 2001 From: junchao-loongson Date: Tue, 19 Dec 2023 10:32:20 +0800 Subject: [PATCH 5/7] add format file --- src/.clang-format | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/.clang-format diff --git a/src/.clang-format b/src/.clang-format new file mode 100644 index 0000000..5d4a8e7 --- /dev/null +++ b/src/.clang-format @@ -0,0 +1,23 @@ +--- +# Defaults for all languages. +BasedOnStyle: LLVM + +# Setting ColumnLimit to 0 so developer choices about where to break lines are maintained. +# Developers are responsible for adhering to the 120 character maximum. +ColumnLimit: 0 +DerivePointerAlignment: false +# Avoid adding spaces between tokens in GSL_SUPPRESS arguments. +# E.g., don't change "GSL_SUPPRESS(r.11)" to "GSL_SUPPRESS(r .11)". +WhitespaceSensitiveMacros: ["GSL_SUPPRESS"] + +# if you want to customize when working locally see https://clang.llvm.org/docs/ClangFormatStyleOptions.html for options. +# See ReformatSource.ps1 for a script to update all source according to the current options in this file. +# e.g. customizations to use Allman bracing and more indenting. +# AccessModifierOffset: -2 +# BreakBeforeBraces: Allman +# CompactNamespaces: false +# IndentCaseLabels: true +IndentWidth: 4 +# NamespaceIndentation: All + +... From 6c1b10db80a98a53421021f2dccc46a73f5613b5 Mon Sep 17 00:00:00 2001 From: junchao98 Date: Tue, 19 Dec 2023 10:33:17 +0800 Subject: [PATCH 6/7] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 49e3f06..538611e 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ C++ YOLO v5 ONNX Runtime inference code for object detection. ## Dependecies: - OpenCV 4.x - ONNXRuntime 1.15+ -- OS: Tested on Windows 10 centos8 archlinux +- OS: Tested on centos8 archlinux - CUDA 11+ [Optional] From 9b676a35154a3c8b7b43d2074f1a358c8a330374 Mon Sep 17 00:00:00 2001 From: yala Date: Tue, 25 Jun 2024 11:57:22 +0800 Subject: [PATCH 7/7] fix cmakelists. update readme --- CMakeLists.txt | 6 +++--- README.md | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index fd0d7a9..7322931 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,16 +17,16 @@ add_executable(yolo_ort set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_STANDARD_REQUIRED ON) -target_include_directories(yolo_ort PRIVATE "${ONNXRUNTIME_DIR}/include/onnxruntime") +target_include_directories(yolo_ort PRIVATE "${ONNXRUNTIME_DIR}/include/onnxruntime/core/session") # link_directories("${ONNXRUNTIME_DIR}/lib") target_compile_features(yolo_ort PRIVATE cxx_std_14) if (WIN32) - target_link_libraries(yolo_ort "${ONNXRUNTIME_DIR}/lib/onnxruntime.lib") + target_link_libraries(yolo_ort "${ONNXRUNTIME_DIR}/build/Linux/${CMAKE_BUILD_TYPE}/onnxruntime.lib") endif(WIN32) if (UNIX) - target_link_libraries(yolo_ort "${ONNXRUNTIME_DIR}/lib/libonnxruntime.so" + target_link_libraries(yolo_ort "${ONNXRUNTIME_DIR}/build/Linux/${CMAKE_BUILD_TYPE}/libonnxruntime.so" ${OpenCV_LIBS} # yolo_ort Qt5::Core # yolo_ort Qt5::Widgets diff --git a/README.md b/README.md index 538611e..98335e2 100644 --- a/README.md +++ b/README.md @@ -15,10 +15,10 @@ To build the project you should run the following commands, don't forget to chan ```bash mkdir build cd build -cmake .. -DONNXRUNTIME_DIR=path_to_onnxruntime -DCMAKE_BUILD_TYPE=Release +cmake .. -DONNXRUNTIME_DIR=path_to_onnxruntime -DCMAKE_BUILD_TYPE=RelWithDebInfo cmake --build . ``` - +CMAKE_BUILD_TYPE must be the same as when onnxruntime was built. ## Run Before running the executable you should convert your PyTorch model to ONNX if you haven't done it yet. Check the [official tutorial](https://github.com/ultralytics/yolov5/issues/251). @@ -26,7 +26,7 @@ Before running the executable you should convert your PyTorch model to ONNX if y Run from CLI: ```bash -./yolo_ort --model_path yolov5.onnx --image bus.jpg --class_names coco.names --gpu +./yolo_ort --model_path ../models/yolov5s.onnx --class_names ../models/coco.names --image ../images/bus.jpg --gpu # On Windows ./yolo_ort.exe with arguments as above ```