当前位置:   article > 正文

使用openvino编写YOLOv10的推理代码(c++版)_c++怎么用opvino

c++怎么用opvino

1.yolov10推理代码

  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <openvino/openvino.hpp>
  5. #include <opencv2/opencv.hpp>
  6. std::vector<cv::Scalar> colors = { cv::Scalar(0, 0, 255) , cv::Scalar(0, 255, 0) , cv::Scalar(255, 0, 0) ,
  7. cv::Scalar(255, 100, 50) , cv::Scalar(50, 100, 255) , cv::Scalar(255, 50, 100) };
  8. const std::vector<std::string> class_names = {
  9. "person", "bicycle", "car", "motorcycle", "airplane", "bus", "train", "truck", "boat", "traffic light",
  10. "fire hydrant", "stop sign", "parking meter", "bench", "bird", "cat", "dog", "horse", "sheep", "cow",
  11. "elephant", "bear", "zebra", "giraffe", "backpack", "umbrella", "handbag", "tie", "suitcase", "frisbee",
  12. "skis", "snowboard", "sports ball", "kite", "baseball bat", "baseball glove", "skateboard", "surfboard",
  13. "tennis racket", "bottle", "wine glass", "cup", "fork", "knife", "spoon", "bowl", "banana", "apple",
  14. "sandwich", "orange", "broccoli", "carrot", "hot dog", "pizza", "donut", "cake", "chair", "couch",
  15. "potted plant", "bed", "dining table", "toilet", "tv", "laptop", "mouse", "remote", "keyboard", "cell phone",
  16. "microwave", "oven", "toaster", "sink", "refrigerator", "book", "clock", "vase", "scissors", "teddy bear",
  17. "hair drier", "toothbrush" };
  18. using namespace cv;
  19. using namespace dnn;
  20. Mat letterbox(const cv::Mat& source)
  21. {
  22. int col = source.cols;
  23. int row = source.rows;
  24. int _max = MAX(col, row);
  25. Mat result = Mat::zeros(_max, _max, CV_8UC3);
  26. source.copyTo(result(Rect(0, 0, col, row)));
  27. return result;
  28. }
  29. int main(int argc, char* argv[])
  30. {
  31. int64 start = cv:: getTickCount();
  32. ov::Core core;
  33. auto compiled_model = core.compile_model("/home/master/yolov10/yolov10x.xml");
  34. ov::InferRequest infer_request = compiled_model.create_infer_request();
  35. Mat img = cv::imread("/home/master/yolov10/12.jpg");
  36. Mat letterbox_img = letterbox(img);
  37. float scale = letterbox_img.size[0] / 640.0;
  38. Mat blob = blobFromImage(letterbox_img, 1.0 / 255.0, Size(640, 640), Scalar(), true);
  39. auto input_port = compiled_model.input();
  40. ov::Tensor input_tensor(input_port.get_element_type(), input_port.get_shape(), blob.ptr(0));
  41. infer_request.set_input_tensor(input_tensor);
  42. infer_request.infer();
  43. auto output = infer_request.get_output_tensor(0);
  44. auto output_shape = output.get_shape();
  45. std::cout << "The shape of output tensor:" << output_shape << std::endl;
  46. int rows = output_shape[2];
  47. int dimensions = output_shape[1];
  48. float* data = output.data<float>();
  49. Mat output_buffer(output_shape[1], output_shape[2], CV_32F, data);
  50. float score_threshold = 0.15;
  51. std::vector<int> class_ids;
  52. std::vector<float> class_scores;
  53. std::vector<Rect> boxes;
  54. for (int i = 0; i < output_buffer.rows; i++) {
  55. float confidence = output_buffer.at<float>(i, 4);
  56. if (confidence > score_threshold) {
  57. int xmin = output_buffer.at<float>(i, 0) * scale;
  58. int ymin = output_buffer.at<float>(i, 1) * scale;
  59. int xmax = output_buffer.at<float>(i, 2) * scale;
  60. int ymax = output_buffer.at<float>(i, 3) * scale;
  61. int width = xmax - xmin;
  62. int height = ymax - ymin;
  63. int ID = (int)output_buffer.at<float>(i, 5);
  64. boxes.push_back(Rect(xmin, ymin, width, height));
  65. class_scores.push_back(confidence);
  66. class_ids.push_back(ID);
  67. }
  68. }
  69. for (size_t i = 0; i < boxes.size(); i++)
  70. {
  71. rectangle(img, boxes[i], colors[class_ids[i] % 6], 2, 8);
  72. std::string label = class_names[class_ids[i]] + ":" + std::to_string(class_scores[i]).substr(0, 4);
  73. Size textSize = cv::getTextSize(label, FONT_HERSHEY_SIMPLEX, 0.5, 1, 0);
  74. Rect textBox(boxes[i].tl().x, boxes[i].tl().y - 15, textSize.width, textSize.height+5);
  75. cv::rectangle(img, textBox, colors[class_ids[i] % 6], FILLED);
  76. putText(img, label, Point(boxes[i].tl().x, boxes[i].tl().y - 5), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(255, 255, 255));
  77. }
  78. int64 end = cv::getTickCount();
  79. double frequency = cv::getTickFrequency();
  80. double duration = (end - start) / frequency;
  81. std::cout << "Execution time: " << duration << " seconds" << std::endl;
  82. namedWindow("result", WINDOW_AUTOSIZE);
  83. cv::imwrite("../resultv10.jpg", img);
  84. imshow("result", img);
  85. waitKey(0);
  86. destroyAllWindows();
  87. return 0;
  88. }

2.CMakeLists

  1. cmake_minimum_required(VERSION 3.0.0)
  2. project(yolo5 VERSION 0.1.0 LANGUAGES C CXX)
  3. include(CTest)
  4. enable_testing()
  5. set(INC_DIR /opt/intel/openvino/runtime/include)
  6. set(LINK_DIR /opt/intel/openvino/runtime/lib/intel64)
  7. set(CMAKE_CXX_STANDARD 17)
  8. include_directories(${INC_DIR})
  9. link_directories(${LINK_DIR})
  10. link_libraries(libopenvino.so)
  11. add_executable(yolo10 yolo.cpp)
  12. target_link_libraries(yolo10 libopenvino.so)
  13. set(CPACK_PROJECT_NAME ${PROJECT_NAME})
  14. SET(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
  15. include(CPack)
  16. find_package(OpenCV)
  17. include_directories(${OpenCV_INCLUDE_DIRS})
  18. target_link_libraries(yolo10 ${OpenCV_LIBS})

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/942428
推荐阅读
相关标签
  

闽ICP备14008679号