-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #68 from GiovanniFyc/openvinoExample
Add openvino example for c++
- Loading branch information
Showing
2 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#include <opencv2/opencv.hpp> | ||
#include <openvino/openvino.hpp> | ||
#include <opencv2/dnn.hpp> | ||
|
||
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<float> 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<int64>(); | ||
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<float>(); | ||
|
||
//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'; | ||
} | ||
} |