当前位置:   article > 正文

C++ windows下使用openvino部署yoloV8_openvino c++

openvino c++

目录

准备版本:

准备事项:

选择配置界面:

下载界面:

​编辑  添加VS配置:

准备代码:

yolov8.h

yolov8.cpp

detect.cpp

如何找到并放置DLL: 


准备版本:

opencv 4.6.0

openvino 2024.0.0.14509

准备事项:

V8模型转换:  pt to xml

  1. from ultralytics import YOLO
  2. # Load a model
  3. model = YOLO("./models\yolov8s.pt") # load an official model
  4. # Export the model
  5. model.export(format="openvino")

 

openvino下载路径:点击跳转

选择配置界面:

下载界面:

  添加VS配置:

准备代码:

yolov8.h

  1. #pragma once
  2. #include<string>
  3. #include <iostream>
  4. #include <opencv2/dnn.hpp>
  5. #include <opencv2/imgproc.hpp>
  6. #include <opencv2/highgui.hpp>
  7. #include<openvino/openvino.hpp>
  8. #include <fstream>
  9. #include <vector>
  10. #include <random>
  11. struct Config {
  12. float confThreshold;
  13. float nmsThreshold;
  14. float scoreThreshold;
  15. int inpWidth;
  16. int inpHeight;
  17. std::string onnx_path;
  18. };
  19. struct Resize
  20. {
  21. cv::Mat resized_image;
  22. int dw;
  23. int dh;
  24. };
  25. struct Detection {
  26. int class_id;
  27. float confidence;
  28. cv::Rect box;
  29. };
  30. class YOLOV8 {
  31. public:
  32. YOLOV8(Config config);
  33. ~YOLOV8();
  34. void detect(cv::Mat& frame);
  35. private:
  36. float confThreshold;
  37. float nmsThreshold;
  38. float scoreThreshold;
  39. int inpWidth;
  40. int inpHeight;
  41. float rx; // the width ratio of original image and resized image
  42. float ry; // the height ratio of original image and resized image
  43. std::string onnx_path;
  44. Resize resize;
  45. ov::Tensor input_tensor;
  46. ov::InferRequest infer_request;
  47. ov::CompiledModel compiled_model;
  48. void initialmodel();
  49. void preprocess_img(cv::Mat& frame);
  50. void postprocess_img(cv::Mat& frame, float* detections, ov::Shape& output_shape);
  51. };

yolov8.cpp

  1. #include"yolov8.h"
  2. #include<iostream>
  3. #include<string>
  4. #include<time.h>
  5. using namespace cv;
  6. using namespace std;
  7. using namespace dnn;
  8. const vector<string> coconame = { "person",
  9. "bicycle",
  10. "car",
  11. "motorcycle",
  12. "airplane",
  13. "bus",
  14. "train",
  15. "truck",
  16. "boat",
  17. "traffic light",
  18. "fire hydrant",
  19. "stop sign",
  20. "parking meter",
  21. "bench",
  22. "bird",
  23. "cat",
  24. "dog",
  25. "horse",
  26. "sheep",
  27. "cow",
  28. "elephant",
  29. "bear",
  30. "zebra",
  31. "giraffe",
  32. "backpack",
  33. "umbrella",
  34. "handbag",
  35. "tie",
  36. "suitcase",
  37. "frisbee",
  38. "skis",
  39. "snowboard",
  40. "sports ball",
  41. "kite",
  42. "baseball bat",
  43. "baseball glove",
  44. "skateboard",
  45. "surfboard",
  46. "tennis racket",
  47. "bottle",
  48. "wine glass",
  49. "cup",
  50. "fork",
  51. "knife",
  52. "spoon",
  53. "bowl",
  54. "banana",
  55. "apple",
  56. "sandwich",
  57. "orange",
  58. "broccoli",
  59. "carrot",
  60. "hot dog",
  61. "pizza",
  62. "donut",
  63. "cake",
  64. "chair",
  65. "couch",
  66. "potted plant",
  67. "bed",
  68. "dining table",
  69. "toilet",
  70. "tv",
  71. "laptop",
  72. "mouse",
  73. "remote",
  74. "keyboard",
  75. "cell phone",
  76. "microwave",
  77. "oven",
  78. "toaster",
  79. "sink",
  80. "refrigerator",
  81. "book",
  82. "clock",
  83. "vase",
  84. "scissors",
  85. "teddy bear",
  86. "hair drier",
  87. "toothbrush" };
  88. YOLOV8::YOLOV8(Config config) {
  89. this->confThreshold = config.confThreshold;
  90. this->nmsThreshold = config.nmsThreshold;
  91. this->scoreThreshold = config.scoreThreshold;
  92. this->inpWidth = config.inpWidth;
  93. this->inpHeight = config.inpHeight;
  94. this->onnx_path = config.onnx_path;
  95. this->initialmodel();
  96. }
  97. YOLOV8::~YOLOV8() {}
  98. void YOLOV8::detect(Mat& frame) {
  99. preprocess_img(frame);
  100. infer_request.infer();
  101. const ov::Tensor& output_tensor = infer_request.get_output_tensor();
  102. ov::Shape output_shape = output_tensor.get_shape();
  103. float* detections = output_tensor.data<float>();
  104. this->postprocess_img(frame, detections, output_shape);
  105. }
  106. void YOLOV8::initialmodel() {
  107. ov::Core core;
  108. std::shared_ptr<ov::Model> model = core.read_model(this->onnx_path);
  109. ov::preprocess::PrePostProcessor ppp = ov::preprocess::PrePostProcessor(model);
  110. ppp.input().tensor().set_element_type(ov::element::u8).set_layout("NHWC").set_color_format(ov::preprocess::ColorFormat::RGB);
  111. ppp.input().preprocess().convert_element_type(ov::element::f32).convert_color(ov::preprocess::ColorFormat::RGB).scale({ 255, 255, 255 });// .scale({ 112, 112, 112 });
  112. ppp.input().model().set_layout("NCHW");
  113. ppp.output().tensor().set_element_type(ov::element::f32);
  114. model = ppp.build();
  115. this->compiled_model = core.compile_model(model, "CPU");
  116. this->infer_request = compiled_model.create_infer_request();
  117. }
  118. void YOLOV8::preprocess_img(Mat& frame) {
  119. try {
  120. float width = frame.cols;
  121. float height = frame.rows;
  122. cv::Size new_shape = cv::Size(inpWidth, inpHeight);
  123. float r = float(new_shape.width / max(width, height));
  124. int new_unpadW = int(round(width * r));
  125. int new_unpadH = int(round(height * r));
  126. cv::resize(frame, resize.resized_image, cv::Size(new_unpadW, new_unpadH), 0, 0, cv::INTER_AREA);
  127. resize.dw = new_shape.width - new_unpadW;
  128. resize.dh = new_shape.height - new_unpadH;
  129. cv::Scalar color = cv::Scalar(100, 100, 100);
  130. cv::copyMakeBorder(resize.resized_image, resize.resized_image, 0, resize.dh, 0, resize.dw, cv::BORDER_CONSTANT, color);
  131. this->rx = (float)frame.cols / (float)(resize.resized_image.cols - resize.dw);
  132. this->ry = (float)frame.rows / (float)(resize.resized_image.rows - resize.dh);
  133. float* input_data = (float*)resize.resized_image.data;
  134. input_tensor = ov::Tensor(compiled_model.input().get_element_type(), compiled_model.input().get_shape(), input_data);
  135. infer_request.set_input_tensor(input_tensor);
  136. }
  137. catch (const std::exception& e) {
  138. std::cerr << "exception: " << e.what() << std::endl;
  139. }
  140. catch (...) {
  141. std::cerr << "unknown exception" << std::endl;
  142. }
  143. }
  144. void YOLOV8::postprocess_img(Mat& frame, float* detections, ov::Shape& output_shape) {
  145. std::vector<cv::Rect> boxes;
  146. vector<int> class_ids;
  147. vector<float> confidences;
  148. int out_rows = output_shape[1];
  149. int out_cols = output_shape[2];
  150. const cv::Mat det_output(out_rows, out_cols, CV_32F, (float*)detections);
  151. for (int i = 0; i < det_output.cols; ++i) {
  152. const cv::Mat classes_scores = det_output.col(i).rowRange(4, 84);
  153. cv::Point class_id_point;
  154. double score;
  155. cv::minMaxLoc(classes_scores, nullptr, &score, nullptr, &class_id_point);
  156. if (score > 0.3) {
  157. const float cx = det_output.at<float>(0, i);
  158. const float cy = det_output.at<float>(1, i);
  159. const float ow = det_output.at<float>(2, i);
  160. const float oh = det_output.at<float>(3, i);
  161. cv::Rect box;
  162. box.x = static_cast<int>((cx - 0.5 * ow));
  163. box.y = static_cast<int>((cy - 0.5 * oh));
  164. box.width = static_cast<int>(ow);
  165. box.height = static_cast<int>(oh);
  166. boxes.push_back(box);
  167. class_ids.push_back(class_id_point.y);
  168. confidences.push_back(score);
  169. }
  170. }
  171. std::vector<int> nms_result;
  172. cv::dnn::NMSBoxes(boxes, confidences, this->scoreThreshold, this->nmsThreshold, nms_result);
  173. std::vector<Detection> output;
  174. for (int i = 0; i < nms_result.size(); i++)
  175. {
  176. Detection result;
  177. int idx = nms_result[i];
  178. result.class_id = class_ids[idx];
  179. result.confidence = confidences[idx];
  180. result.box = boxes[idx];
  181. output.push_back(result);
  182. }
  183. cout << "output_size:" << output.size() << endl;
  184. for (int i = 0; i < output.size(); i++)
  185. {
  186. auto detection = output[i];
  187. auto box = detection.box;
  188. auto classId = detection.class_id;
  189. // if (classId != 0) continue;
  190. auto confidence = detection.confidence;
  191. box.x = this->rx * box.x;
  192. box.y = this->ry * box.y;
  193. box.width = this->rx * box.width;
  194. box.height = this->ry * box.height;
  195. float xmax = box.x + box.width;
  196. float ymax = box.y + box.height;
  197. // detection box
  198. std::random_device rd;
  199. std::mt19937 gen(rd());
  200. std::uniform_int_distribution<int> dis(100, 255);
  201. cv::Scalar color = cv::Scalar(dis(gen),
  202. dis(gen),
  203. dis(gen));
  204. cv::rectangle(frame, cv::Point(box.x, box.y), cv::Point(xmax, ymax), color, 3);
  205. // Detection box text
  206. std::string classString = coconame[classId] + ' ' + std::to_string(confidence).substr(0, 4);
  207. cv::Size textSize = cv::getTextSize(classString, cv::FONT_HERSHEY_DUPLEX, 1, 2, 0);
  208. cv::Rect textBox(box.x, box.y - 40, textSize.width + 10, textSize.height + 20);
  209. cv::rectangle(frame, textBox, color, cv::FILLED);
  210. cv::putText(frame, classString, cv::Point(box.x + 5, box.y - 10), cv::FONT_HERSHEY_DUPLEX, 1, cv::Scalar(0, 0, 0), 2, 0);
  211. // cv::rectangle(frame, cv::Point(box.x, box.y - 20), cv::Point(xmax, box.y), cv::Scalar(0, 255, 0), cv::FILLED);
  212. // cv::putText(frame, coconame[classId], cv::Point(box.x, box.y - 5), cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 0, 0));
  213. }
  214. }

detect.cpp

  1. # include"yolov8.h"
  2. int main(int argc, char* argv[]) {
  3. try {
  4. //if (argc != 3) {
  5. // std::cout << "Usage:" << argv[0] << " <path_to_model> <path_to_image>" << std::endl;
  6. // return EXIT_FAILURE;
  7. //}
  8. //const std::string input_model_path{ argv[1] };
  9. //const std::string input_image_path{ argv[2] };
  10. std::string input_model_path = "E:\\yoloV8\\ultralytics-main\\models\\yolov8s_openvino_model\\yolov8s.xml";
  11. std::string input_image_path = "E:\\yoloV8\\ultralytics-main\\ultralytics\\assets\\bus.jpg";
  12. Config config = { 0.2,0.4,0.4,640,640, input_model_path };
  13. clock_t start, end;
  14. cv::Mat img = cv::imread(input_image_path);
  15. YOLOV8 yolomodel(config);
  16. start = clock();
  17. yolomodel.detect(img);
  18. end = clock();
  19. std::cout << "infer time = " << double(end - start) / CLOCKS_PER_SEC << "s" << std::endl;
  20. cv::imwrite("./result.jpg", img);
  21. }
  22. catch (const std::exception& ex) {
  23. std::cerr << ex.what() << std::endl;
  24. return EXIT_FAILURE;
  25. }
  26. return EXIT_SUCCESS;
  27. }

如何找到并放置DLL: 

为了防止程序运行缺少dll  可以把dll全部拷贝到程序输出所在目录里 也就是exe所在目录

dll所在目录:

 

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

闽ICP备14008679号