From c4d335cc3e4fbc089e2bc63fcf398037248500ec Mon Sep 17 00:00:00 2001 From: GiovanniFyc <55267877+GiovanniFyc@users.noreply.github.com> Date: Wed, 20 Nov 2024 10:30:10 +0800 Subject: [PATCH 1/2] Add openvino example for c++ --- .../cppExample/openvino/CMakeLists.txt | 31 ++++++++++++++ .../cppExample/openvino/openvinoExample.cpp | 42 +++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 tools/inference/cppExample/openvino/CMakeLists.txt create mode 100644 tools/inference/cppExample/openvino/openvinoExample.cpp diff --git a/tools/inference/cppExample/openvino/CMakeLists.txt b/tools/inference/cppExample/openvino/CMakeLists.txt new file mode 100644 index 0000000..9a51193 --- /dev/null +++ b/tools/inference/cppExample/openvino/CMakeLists.txt @@ -0,0 +1,31 @@ +cmake_minimum_required(VERSION 3.5.1) + +project( + openvinoExample + LANGUAGES CXX + VERSION 1.0.0 +) + +set(CMAKE_CXX_STANDARD 17) + +if (MSVC) + add_compile_options(-nologo) + add_definitions(-DNOMINMAX) +else() + add_compile_options(-Wall) +endif() + +find_package(OpenVINO CONFIG REQUIRED) +find_package(OpenCV CONFIG REQUIRED core dnn imgcodecs imgproc) + +add_executable( + openvinoExample + openvinoExample.cpp +) +target_link_libraries( + openvinoExample + openvino::runtime + opencv_core + opencv_dnn + opencv_imgcodecs +) diff --git a/tools/inference/cppExample/openvino/openvinoExample.cpp b/tools/inference/cppExample/openvino/openvinoExample.cpp new file mode 100644 index 0000000..56cd46f --- /dev/null +++ b/tools/inference/cppExample/openvino/openvinoExample.cpp @@ -0,0 +1,42 @@ +#include +#include +#include + +int main(){ + try{ + ov::Core core; + ov::CompiledModel mCompiledModel = core.compile_model("DFINE.onnx","AUTO"); + cv::Mat imageMat = cv::imread("test.png"); + cv::cvtColor(imageMat, imageMat, cv::COLOR_BGR2RGB); + cv::Mat inferMat; + cv::dnn::blobFromImage(imageMat,inferMat,1.0 / 255.0); + std::vector im_shape = { (float)1/imageMat.rows, (float)1/imageMat.cols }; + auto ireq = mCompiledModel.create_infer_request(); + auto inputports = mCompiledModel.inputs(); + ov::Tensor input_tensor1(inputports[0].get_element_type(), { 1,3,640,640 }, inferMat.ptr()); + ireq.set_input_tensor(0,input_tensor1); + ov::Tensor input_tensor2(inputports[1].get_element_type(), { 1,2 }); + int64* input_tensor_data = input_tensor2.data(); + for (int i = 0; i < 2; i++) { + input_tensor_data[i] = 640; + } + ireq.set_input_tensor(1,input_tensor2); + ireq.infer(); + ov::Tensor labels_tensor = ireq.get_output_tensor(0); + ov::Tensor bboxs_tensor = ireq.get_output_tensor(1); + ov::Tensor scores_tensor = ireq.get_output_tensor(2); + float *bo = bboxs_tensor.data(); + + //example + float cx = bo[4] ; + float cy = bo[5] ; + float bx = bo[6] ; + float by = bo[7] ; + cv::rectangle(imageMat, cv::Rect(bo[0],bo[1], bo[2]-bo[0], bo[3]-bo[1]), cv::Scalar(0, 255, 0), 2); + cv::rectangle(imageMat, cv::Rect(cx,cy, bx-cx, by-cy), cv::Scalar(0, 255, 0), 2); + cv::imwrite("aimage.png",imageMat); + } + catch(const ov::Exception& e){ + std::cerr << e.what() << '\n'; + } +} From 62fcf8b639804b99632d534891a73a3b2a37284e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 20 Nov 2024 02:32:07 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tools/inference/cppExample/openvino/openvinoExample.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/inference/cppExample/openvino/openvinoExample.cpp b/tools/inference/cppExample/openvino/openvinoExample.cpp index 56cd46f..9b5430f 100644 --- a/tools/inference/cppExample/openvino/openvinoExample.cpp +++ b/tools/inference/cppExample/openvino/openvinoExample.cpp @@ -26,8 +26,8 @@ int main(){ ov::Tensor bboxs_tensor = ireq.get_output_tensor(1); ov::Tensor scores_tensor = ireq.get_output_tensor(2); float *bo = bboxs_tensor.data(); - - //example + + //example float cx = bo[4] ; float cy = bo[5] ; float bx = bo[6] ;