当前位置:   article > 正文

YOLOv5 实例分割 用 OPenCV DNN C++ 部署_opencv yolov5 实例分割

opencv yolov5 实例分割

Pre:

如果之前从没接触过实例分割,建议先了解一下实例分割的输出是什么。

实例分割两个关键输出是:mask系数、mask原型

本文参考自该项目(这么优秀的代码当然要给star!):GitHub - UNeedCryDear/yolov5-seg-opencv-onnxruntime-cpp: yolov5 segmentation with onnxruntime and opencv

目录

Pre:

一、代码总结

1、实例分割输出 与 目标检测输出 的区别?

2、如何获得目标的mask?

二、源码

yolov5_seg_utils.h

yolov5_seg_utils.cpp

yolo_seg.h

yolo_seg.cpp

main.cpp

三、效果


一、代码总结

1、实例分割输出 与 目标检测输出 的区别?

还是写一点实例分割部分的东西吧,互相交流下,嘿嘿。

yolov5n-seg.onnx  一张图片输入会获得两个输出,分别为

output0:  float32[1, 25200,117] 。25200是输出anchor的数量,117是4个anchor坐标信息、1个置信度、80个类别概率、32个mask系数。

output1:  float32[1,32,160,160]。即一张图片输出的一组mask原型 ,你可以理解为网络32张mask。在网络输入图上突出前景的mask,共32张,然后尺寸缩小到尺寸为160*160。

2、如何获得目标的mask?

这段代码读起来还挺废脑细胞的。

首先要指出的是,这个mask是相对于目标框的,不是相对于原图的。

获得目标框的mask:

  1. 检测框区域映射到mask原型上,然后在原型上裁剪下该区域。
  2. mask系数与目标框区域原型作矩阵乘法,其结果然后进行sigmod运算,阈值化(01二值),便获得突出目标的mask
  3. 目标mask映射到原图上

对应源码:

  1. void GetMask2(const Mat& maskProposals, const Mat& mask_protos, OutputSeg& output, const MaskParams& maskParams) {
  2. int seg_channels = maskParams.segChannels;
  3. int net_width = maskParams.netWidth;
  4. int seg_width = maskParams.segWidth;
  5. int net_height = maskParams.netHeight;
  6. int seg_height = maskParams.segHeight;
  7. float mask_threshold = maskParams.maskThreshold;
  8. Vec4f params = maskParams.params;
  9. Size src_img_shape = maskParams.srcImgShape;
  10. Rect temp_rect = output.box;
  11. // 把已经到原图的检测框坐标信息 映射到 获得mask原型分支的输入尺寸上【160, 160】
  12. int rang_x = floor((temp_rect.x * params[0] + params[2]) / net_width * seg_width);
  13. int rang_y = floor((temp_rect.y * params[1] + params[3]) / net_height * seg_height);
  14. int rang_w = ceil(((temp_rect.x + temp_rect.width) * params[0] + params[2]) / net_width * seg_width) - rang_x;
  15. int rang_h = ceil(((temp_rect.y + temp_rect.height) * params[0] + params[3]) / net_width * seg_height) - rang_y;
  16. //
  17. rang_w = MAX(rang_w, 1);
  18. rang_h = MAX(rang_h, 1);
  19. if (rang_x + rang_w > seg_width){
  20. if (seg_width - rang_x > 0)
  21. rang_w =seg_width -rang_x;
  22. else
  23. rang_x -= 1;
  24. }
  25. if (rang_y + rang_h > seg_height) {
  26. if (seg_height - rang_y > 0)
  27. rang_h = seg_height - rang_y;
  28. else
  29. rang_y -= 1;
  30. }
  31. vector<Range> roi_ranges;
  32. roi_ranges.push_back(Range(0,1));
  33. roi_ranges.push_back(Range::all());
  34. roi_ranges.push_back(Range(rang_y, rang_h+rang_y));
  35. roi_ranges.push_back(Range(rang_x, rang_w+rang_x));
  36. // 裁剪mask原型
  37. Mat temp_mask_protos = mask_protos(roi_ranges).clone(); // 剪裁原型,保存检测框内部的原型,其余位置清零, 以此来获得感兴趣区域(roi)
  38. Mat protos = temp_mask_protos.reshape(0, { seg_channels, rang_w*rang_h});// 检测至检测框大小?
  39. // mask系数与mask原型做矩阵乘法
  40. Mat matmul_res = (maskProposals * protos).t(); // mask系数【1,32】 与 mask原型【32, h*w】进行矩阵相称
  41. Mat masks_feature = matmul_res.reshape(1,{rang_h, rang_w}); //【1,h,w】
  42. Mat dest, mask;
  43. // sigmod
  44. cv::exp(-masks_feature, dest);
  45. dest = 1.0 / (1.0 + dest);
  46. // 检测框坐标 映射到 原图尺寸
  47. int left = floor((net_width / seg_width * rang_x - params[2]) / params[0]);
  48. int top = floor((net_width / seg_height * rang_y - params[3]) / params[1]);
  49. int width = ceil(net_width / seg_height * rang_w / params[0]);
  50. int height = ceil(net_height / seg_height * rang_h / params[1]);
  51. // 检测框mask缩放到原图尺寸
  52. resize(dest, mask, Size(width, height), INTER_NEAREST);
  53. // 阈值化
  54. mask = mask(temp_rect - Point(left, top)) > mask_threshold;
  55. output.boxMask = mask;
  56. }

二、源码

yolov5_seg_utils.h

  1. #pragma once
  2. #include<iostream>
  3. #include <numeric>
  4. #include<opencv2/opencv.hpp>
  5. #define YOLO_P6 false //是否使用P6模型
  6. #define ORT_OLD_VISON 12 //ort1.12.0 之前的版本为旧版本API
  7. struct OutputSeg {
  8. int id; //结果类别id
  9. float confidence; //结果置信度
  10. cv::Rect box; //矩形框
  11. cv::Mat boxMask; //矩形框内mask,节省内存空间和加快速度
  12. };
  13. struct MaskParams {
  14. int segChannels = 32;
  15. int segWidth = 160;
  16. int segHeight = 160;
  17. int netWidth = 640;
  18. int netHeight = 640;
  19. float maskThreshold = 0.5;
  20. cv::Size srcImgShape;
  21. cv::Vec4d params;
  22. };
  23. bool CheckParams(int netHeight, int netWidth, const int* netStride, int strideSize);
  24. void DrawPred(cv::Mat& img, std::vector<OutputSeg> result, std::vector<std::string> classNames, std::vector<cv::Scalar> color);
  25. void LetterBox(const cv::Mat& image, cv::Mat& outImage,
  26. cv::Vec4d& params, //[ratio_x,ratio_y,dw,dh]
  27. const cv::Size& newShape = cv::Size(640, 640),
  28. bool autoShape = false,
  29. bool scaleFill = false,
  30. bool scaleUp = true,
  31. int stride = 32,
  32. const cv::Scalar& color = cv::Scalar(114, 114, 114));
  33. void GetMask(const cv::Mat& maskProposals, const cv::Mat& maskProtos, std::vector<OutputSeg>& output, const MaskParams& maskParams);
  34. void GetMask2(const cv::Mat& maskProposals, const cv::Mat& maskProtos, OutputSeg& output, const MaskParams& maskParams);

yolov5_seg_utils.cpp

  1. #pragma once
  2. #include "yolov5_seg_utils.h"
  3. using namespace cv;
  4. using namespace std;
  5. bool CheckParams(int netHeight, int netWidth, const int* netStride, int strideSize) {
  6. if (netHeight % netStride[strideSize - 1] != 0 || netWidth % netStride[strideSize - 1] != 0)
  7. {
  8. cout << "Error:_netHeight and _netWidth must be multiple of max stride " << netStride[strideSize - 1] << "!" << endl;
  9. return false;
  10. }
  11. return true;
  12. }
  13. void LetterBox(const cv::Mat& image, cv::Mat& outImage, cv::Vec4d& params, const cv::Size& newShape,
  14. bool autoShape, bool scaleFill, bool scaleUp, int stride, const cv::Scalar& color)
  15. {
  16. if (false) {
  17. int maxLen = MAX(image.rows, image.cols);
  18. outImage = Mat::zeros(Size(maxLen, maxLen), CV_8UC3);
  19. image.copyTo(outImage(Rect(0, 0, image.cols, image.rows)));
  20. params[0] = 1;
  21. params[1] = 1;
  22. params[3] = 0;
  23. params[2] = 0;
  24. }
  25. cv::Size shape = image.size();
  26. float r = std::min((float)newShape.height / (float)shape.height,
  27. (float)newShape.width / (float)shape.width);
  28. if (!scaleUp)
  29. r = std::min(r, 1.0f);
  30. float ratio[2]{ r, r };
  31. int new_un_pad[2] = { (int)std::round((float)shape.width * r),(int)std::round((float)shape.height * r) };
  32. auto dw = (float)(newShape.width - new_un_pad[0]);
  33. auto dh = (float)(newShape.height - new_un_pad[1]);
  34. if (autoShape)
  35. {
  36. dw = (float)((int)dw % stride);
  37. dh = (float)((int)dh % stride);
  38. }
  39. else if (scaleFill)
  40. {
  41. dw = 0.0f;
  42. dh = 0.0f;
  43. new_un_pad[0] = newShape.width;
  44. new_un_pad[1] = newShape.height;
  45. ratio[0] = (float)newShape.width / (float)shape.width;
  46. ratio[1] = (float)newShape.height / (float)shape.height;
  47. }
  48. dw /= 2.0f;
  49. dh /= 2.0f;
  50. if (shape.width != new_un_pad[0] && shape.height != new_un_pad[1])
  51. {
  52. cv::resize(image, outImage, cv::Size(new_un_pad[0], new_un_pad[1]));
  53. }
  54. else {
  55. outImage = image.clone();
  56. }
  57. int top = int(std::round(dh - 0.1f));
  58. int bottom = int(std::round(dh + 0.1f));
  59. int left = int(std::round(dw - 0.1f));
  60. int right = int(std::round(dw + 0.1f));
  61. params[0] = ratio[0];
  62. params[1] = ratio[1];
  63. params[2] = left;
  64. params[3] = top;
  65. cv::copyMakeBorder(outImage, outImage, top, bottom, left, right, cv::BORDER_CONSTANT, color);
  66. }
  67. void GetMask(const cv::Mat& maskProposals, const cv::Mat& maskProtos, std::vector<OutputSeg>& output, const MaskParams& maskParams) {
  68. //cout << maskProtos.size << endl;
  69. int seg_channels = maskParams.segChannels;
  70. int net_width = maskParams.netWidth;
  71. int seg_width = maskParams.segWidth;
  72. int net_height = maskParams.netHeight;
  73. int seg_height = maskParams.segHeight;
  74. float mask_threshold = maskParams.maskThreshold;
  75. Vec4f params = maskParams.params;
  76. Size src_img_shape = maskParams.srcImgShape;
  77. Mat protos = maskProtos.reshape(0, { seg_channels,seg_width * seg_height });
  78. Mat matmul_res = (maskProposals * protos).t();
  79. Mat masks = matmul_res.reshape(output.size(), { seg_width,seg_height });
  80. vector<Mat> maskChannels;
  81. split(masks, maskChannels);
  82. for (int i = 0; i < output.size(); ++i) {
  83. Mat dest, mask;
  84. //sigmoid
  85. cv::exp(-maskChannels[i], dest);
  86. dest = 1.0 / (1.0 + dest);
  87. Rect roi(int(params[2] / net_width * seg_width), int(params[3] / net_height * seg_height), int(seg_width - params[2] / 2), int(seg_height - params[3] / 2));
  88. dest = dest(roi);
  89. resize(dest, mask, src_img_shape, INTER_NEAREST);
  90. //crop
  91. Rect temp_rect = output[i].box;
  92. mask = mask(temp_rect) > mask_threshold;
  93. output[i].boxMask = mask;
  94. }
  95. }
  96. void GetMask2(const Mat& maskProposals, const Mat& mask_protos, OutputSeg& output, const MaskParams& maskParams) {
  97. int seg_channels = maskParams.segChannels;
  98. int net_width = maskParams.netWidth;
  99. int seg_width = maskParams.segWidth;
  100. int net_height = maskParams.netHeight;
  101. int seg_height = maskParams.segHeight;
  102. float mask_threshold = maskParams.maskThreshold;
  103. Vec4f params = maskParams.params;
  104. Size src_img_shape = maskParams.srcImgShape;
  105. Rect temp_rect = output.box;
  106. // 把已经到原图的检测框坐标信息 映射到 获得mask原型分支的输入尺寸上【160, 160】
  107. int rang_x = floor((temp_rect.x * params[0] + params[2]) / net_width * seg_width);
  108. int rang_y = floor((temp_rect.y * params[1] + params[3]) / net_height * seg_height);
  109. int rang_w = ceil(((temp_rect.x + temp_rect.width) * params[0] + params[2]) / net_width * seg_width) - rang_x;
  110. int rang_h = ceil(((temp_rect.y + temp_rect.height) * params[0] + params[3]) / net_width * seg_height) - rang_y;
  111. //
  112. rang_w = MAX(rang_w, 1);
  113. rang_h = MAX(rang_h, 1);
  114. if (rang_x + rang_w > seg_width){
  115. if (seg_width - rang_x > 0)
  116. rang_w =seg_width -rang_x;
  117. else
  118. rang_x -= 1;
  119. }
  120. if (rang_y + rang_h > seg_height) {
  121. if (seg_height - rang_y > 0)
  122. rang_h = seg_height - rang_y;
  123. else
  124. rang_y -= 1;
  125. }
  126. vector<Range> roi_ranges;
  127. roi_ranges.push_back(Range(0,1));
  128. roi_ranges.push_back(Range::all());
  129. roi_ranges.push_back(Range(rang_y, rang_h+rang_y));
  130. roi_ranges.push_back(Range(rang_x, rang_w+rang_x));
  131. // 裁剪mask原型
  132. Mat temp_mask_protos = mask_protos(roi_ranges).clone(); // 剪裁原型,保存检测框内部的原型,其余位置清零, 以此来获得感兴趣区域(roi)
  133. Mat protos = temp_mask_protos.reshape(0, { seg_channels, rang_w*rang_h});// 检测至检测框大小?
  134. // mask系数与mask原型做矩阵乘法
  135. Mat matmul_res = (maskProposals * protos).t(); // mask系数【1,32】 与 mask原型【32, h*w】进行矩阵相称
  136. Mat masks_feature = matmul_res.reshape(1,{rang_h, rang_w}); //【1,h,w】
  137. Mat dest, mask;
  138. // sigmod
  139. cv::exp(-masks_feature, dest);
  140. dest = 1.0 / (1.0 + dest);
  141. // 检测框坐标 映射到 原图尺寸
  142. int left = floor((net_width / seg_width * rang_x - params[2]) / params[0]);
  143. int top = floor((net_width / seg_height * rang_y - params[3]) / params[1]);
  144. int width = ceil(net_width / seg_height * rang_w / params[0]);
  145. int height = ceil(net_height / seg_height * rang_h / params[1]);
  146. // 检测框mask缩放到原图尺寸
  147. resize(dest, mask, Size(width, height), INTER_NEAREST);
  148. // 阈值化
  149. mask = mask(temp_rect - Point(left, top)) > mask_threshold;
  150. output.boxMask = mask;
  151. }
  152. void DrawPred(Mat& img, vector<OutputSeg> result, std::vector<std::string> classNames, vector<Scalar> color) {
  153. Mat mask = img.clone();
  154. for (int i=0; i< result.size(); i++){
  155. int left, top;
  156. left = result[i].box.x;
  157. top = result[i].box.y;
  158. int color_num =i;
  159. // 目标画框
  160. rectangle(img, result[i].box, color[result[i].id], 2, 8);
  161. // 目标mask,这里非目标像素值为0
  162. mask(result[i].box).setTo(color[result[i].id], result[i].boxMask);
  163. string label = classNames[result[i].id] + ":" + to_string(result[i].confidence);
  164. // 框左上角打印信息
  165. int baseLine;
  166. Size labelSize = getTextSize(label, FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
  167. top = max(top, labelSize.height);
  168. putText(img, label, Point(left, top), FONT_HERSHEY_SIMPLEX, 0.5, color[result[i].id], 2);
  169. }
  170. addWeighted(img, 0.5, mask, 0.5, 0, img);
  171. }

yolo_seg.h

  1. #pragma once
  2. #include<iostream>
  3. #include<opencv2/opencv.hpp>
  4. #include "yolov5_seg_utils.h"
  5. class YoloSeg {
  6. public:
  7. YoloSeg() {
  8. }
  9. ~YoloSeg() {}
  10. /** \brief Read onnx-model
  11. * \param[out] read onnx file into cv::dnn::Net
  12. * \param[in] modelPath:onnx-model path
  13. * \param[in] isCuda:if true and opencv built with CUDA(cmake),use OpenCV-GPU,else run it on cpu.
  14. */
  15. bool ReadModel(cv::dnn::Net& net, std::string& netPath, bool isCuda);
  16. /** \brief detect.
  17. * \param[in] srcImg:a 3-channels image.
  18. * \param[out] output:detection results of input image.
  19. */
  20. bool Detect(cv::Mat& srcImg, cv::dnn::Net& net, std::vector<OutputSeg>& output);
  21. #if(defined YOLO_P6 && YOLO_P6==true)
  22. const int _netWidth = 1280; //ONNX图片输入宽度
  23. const int _netHeight = 1280; //ONNX图片输入高度
  24. const int _segWidth = 320; //_segWidth=_netWidth/mask_ratio
  25. const int _segHeight = 320;
  26. const int _segChannels = 32;
  27. #else
  28. const int _netWidth = 640; //ONNX图片输入宽度
  29. const int _netHeight = 640; //ONNX图片输入高度
  30. const int _segWidth = 160; //_segWidth=_netWidth/mask_ratio
  31. const int _segHeight = 160;
  32. const int _segChannels = 32;
  33. #endif // YOLO_P6
  34. float _classThreshold = 0.25;
  35. float _nmsThreshold = 0.45;
  36. float _maskThreshold = 0.5;
  37. public:
  38. std::vector<std::string> _className = { "person", "bicycle", "car", "motorcycle", "airplane", "bus", "train", "truck", "boat", "traffic light",
  39. "fire hydrant", "stop sign", "parking meter", "bench", "bird", "cat", "dog", "horse", "sheep", "cow",
  40. "elephant", "bear", "zebra", "giraffe", "backpack", "umbrella", "handbag", "tie", "suitcase", "frisbee",
  41. "skis", "snowboard", "sports ball", "kite", "baseball bat", "baseball glove", "skateboard", "surfboard",
  42. "tennis racket", "bottle", "wine glass", "cup", "fork", "knife", "spoon", "bowl", "banana", "apple",
  43. "sandwich", "orange", "broccoli", "carrot", "hot dog", "pizza", "donut", "cake", "chair", "couch",
  44. "potted plant", "bed", "dining table", "toilet", "tv", "laptop", "mouse", "remote", "keyboard", "cell phone",
  45. "microwave", "oven", "toaster", "sink", "refrigerator", "book", "clock", "vase", "scissors", "teddy bear",
  46. "hair drier", "toothbrush" };//类别名,换成自己的模型需要修改此项
  47. };

yolo_seg.cpp

  1. #include"yolo_seg.h"
  2. using namespace std;
  3. using namespace cv;
  4. using namespace cv::dnn;
  5. bool YoloSeg::ReadModel(Net& net, string& netPath, bool isCuda = false) {
  6. try {
  7. net = readNet(netPath);
  8. #if CV_VERSION_MAJOR==4 &&CV_VERSION_MINOR==7&&CV_VERSION_REVISION==0
  9. net.enableWinograd(false); //bug of opencv4.7.x in AVX only platform ,https://github.com/opencv/opencv/pull/23112 and https://github.com/opencv/opencv/issues/23080
  10. //net.enableWinograd(true); //If your CPU supports AVX2, you can set it true to speed up
  11. #endif
  12. }
  13. catch (const std::exception&) {
  14. return false;
  15. }
  16. if (isCuda) {
  17. //cuda
  18. net.setPreferableBackend(cv::dnn::DNN_BACKEND_CUDA);
  19. net.setPreferableTarget(cv::dnn::DNN_TARGET_CUDA); //or DNN_TARGET_CUDA_FP16
  20. }
  21. else {
  22. //cpu
  23. cout << "Inference device: CPU" << endl;
  24. net.setPreferableBackend(cv::dnn::DNN_BACKEND_DEFAULT);
  25. net.setPreferableTarget(cv::dnn::DNN_TARGET_CPU);
  26. }
  27. return true;
  28. }
  29. bool YoloSeg::Detect(Mat& srcImg, Net& net, vector<OutputSeg>& output) {
  30. Mat blob;
  31. output.clear();
  32. int col = srcImg.cols;
  33. int row = srcImg.rows;
  34. Mat netInputImg;
  35. Vec4d params;
  36. LetterBox(srcImg, netInputImg, params, cv::Size(_netWidth, _netHeight));
  37. blobFromImage(netInputImg, blob, 1 / 255.0, cv::Size(_netWidth, _netHeight), cv::Scalar(0, 0, 0), true, false);
  38. //**************************************************************************************************************************************************/
  39. //如果在其他设置没有问题的情况下但是结果偏差很大,可以尝试下用下面两句语句
  40. // If there is no problem with other settings, but results are a lot different from Python-onnx , you can try to use the following two sentences
  41. //
  42. //$ blobFromImage(netInputImg, blob, 1 / 255.0, cv::Size(_netWidth, _netHeight), cv::Scalar(104, 117, 123), true, false);
  43. //$ blobFromImage(netInputImg, blob, 1 / 255.0, cv::Size(_netWidth, _netHeight), cv::Scalar(114, 114,114), true, false);
  44. //****************************************************************************************************************************************************/
  45. net.setInput(blob);
  46. std::vector<cv::Mat> net_output_img;
  47. //*********************************************************************************************************************************
  48. //net.forward(net_output_img, net.getUnconnectedOutLayersNames());
  49. //opencv4.5.x和4.6.x这里输出不一致,推荐使用下面的固定名称输出
  50. // 如果使用net.forward(net_output_img, net.getUnconnectedOutLayersNames()),需要确认下net.getUnconnectedOutLayersNames()返回值中output0在前,output1在后,否者出错
  51. //
  52. // The outputs of opencv4.5.x and 4.6.x are inconsistent.Please make sure "output0" is in front of "output1" if you use net.forward(net_output_img, net.getUnconnectedOutLayersNames())
  53. //*********************************************************************************************************************************
  54. vector<string> output_layer_names{ "output0","output1" };
  55. net.forward(net_output_img, output_layer_names); //获取output的输出
  56. std::vector<int> class_ids;//结果id数组
  57. std::vector<float> confidences;//结果每个id对应置信度数组
  58. std::vector<cv::Rect> boxes;//每个id矩形框
  59. std::vector<vector<float>> picked_proposals; //output0[:,:, 5 + _className.size():net_width]===> for mask
  60. int net_width = _className.size() + 5 + _segChannels;// 80 + 5 + 32 = 117
  61. int out0_width= net_output_img[0].size[2];
  62. // assert(net_width == out0_width, "Error Wrong number of _className or _segChannels"); //模型类别数目不对或者_segChannels设置错误
  63. int net_height = net_output_img[0].size[1];// 25200
  64. float* pdata = (float*)net_output_img[0].data;
  65. for (int r = 0; r < net_height; r++) { //lines
  66. float box_score = pdata[4];
  67. if (box_score >= _classThreshold) {
  68. cv::Mat scores(1, _className.size(), CV_32FC1, pdata + 5); // 可是 后面不只是有80个类别的概率;
  69. Point classIdPoint;
  70. double max_class_socre;
  71. minMaxLoc(scores, 0, &max_class_socre, 0, &classIdPoint);
  72. max_class_socre = (float)max_class_socre;
  73. if (max_class_socre >= _classThreshold) {
  74. vector<float> temp_proto(pdata + 5 + _className.size(), pdata + net_width); // Mask Coeffcients,mask的掩码系数
  75. picked_proposals.push_back(temp_proto);
  76. //rect [x,y,w,h]
  77. float x = (pdata[0] - params[2]) / params[0]; //x
  78. float y = (pdata[1] - params[3]) / params[1]; //y
  79. float w = pdata[2] / params[0]; //w
  80. float h = pdata[3] / params[1]; //h
  81. int left = MAX(int(x - 0.5 * w + 0.5), 0);
  82. int top = MAX(int(y - 0.5 * h + 0.5), 0);
  83. class_ids.push_back(classIdPoint.x);
  84. confidences.push_back(max_class_socre * box_score);
  85. boxes.push_back(Rect(left, top, int(w + 0.5), int(h + 0.5)));
  86. }
  87. }
  88. pdata += net_width;//下一行
  89. }
  90. //NMS
  91. vector<int> nms_result;
  92. cv::dnn::NMSBoxes(boxes, confidences, _classThreshold, _nmsThreshold, nms_result);
  93. std::vector<vector<float>> temp_mask_proposals;
  94. Rect holeImgRect(0, 0, srcImg.cols, srcImg.rows);
  95. for (int i = 0; i < nms_result.size(); ++i) {
  96. int idx = nms_result[i];
  97. OutputSeg result;
  98. result.id = class_ids[idx];
  99. result.confidence = confidences[idx];
  100. result.box = boxes[idx] & holeImgRect;
  101. temp_mask_proposals.push_back(picked_proposals[idx]);
  102. output.push_back(result);
  103. }
  104. MaskParams mask_params;
  105. mask_params.params = params;
  106. mask_params.srcImgShape = srcImg.size();
  107. for (int i = 0; i < temp_mask_proposals.size(); ++i) {
  108. GetMask2(Mat(temp_mask_proposals[i]).t(), net_output_img[1], output[i], mask_params); // 注意这里是net_output_img[1],为原型mask
  109. }
  110. //******************** ****************
  111. // 老版本的方案,如果上面GetMask2出错,建议使用这个。
  112. // If the GetMask2() still reports errors , it is recommended to use GetMask().
  113. // Mat mask_proposals;
  114. //for (int i = 0; i < temp_mask_proposals.size(); ++i)
  115. // mask_proposals.push_back(Mat(temp_mask_proposals[i]).t());
  116. //GetMask(mask_proposals, net_output_img[1], output, mask_params);
  117. //*****************************************************/
  118. if (output.size())
  119. return true;
  120. else
  121. return false;
  122. }

main.cpp

  1. #include <iostream>
  2. #include<opencv2/opencv.hpp>
  3. #include "yolo_seg.h"
  4. #include<sys/time.h>
  5. using namespace std;
  6. using namespace cv;
  7. using namespace dnn;
  8. int main() {
  9. //yolov5(); //https://github.com/UNeedCryDear/yolov5-opencv-dnn-cpp
  10. string model_path = "/home/jason/PycharmProjects/pytorch_learn/yolo/yolov5-7.0/yolov5n-seg.onnx";
  11. YoloSeg test;
  12. Net net;
  13. if (test.ReadModel(net, model_path, true)) {
  14. cout << "read net ok!" << endl;
  15. }
  16. else {
  17. return -1;
  18. }
  19. //生成随机颜色
  20. vector<Scalar> color;
  21. srand(time(0));
  22. for (int i = 0; i < 80; i++) {
  23. int b = rand() % 256;
  24. int g = rand() % 256;
  25. int r = rand() % 256;
  26. color.push_back(Scalar(b, g, r));
  27. }
  28. VideoCapture capture(0);
  29. struct timeval t1, t2;
  30. double timeuse;
  31. Mat img;
  32. while (1) {
  33. capture >> img;
  34. vector<OutputSeg> result;
  35. gettimeofday(&t1, NULL);
  36. bool find = test.Detect(img, net, result);
  37. gettimeofday(&t2, NULL);
  38. if (find) {
  39. DrawPred(img, result, test._className, color);
  40. }
  41. else {
  42. cout << "Detect Failed!"<<endl;
  43. }
  44. timeuse = (t2.tv_sec - t1.tv_sec) + (double)(t2.tv_usec - t1.tv_usec)/1000000; //s
  45. string label = "duration:" + to_string(timeuse*1000); //ms
  46. putText(img, label, Point(30,30), FONT_HERSHEY_SIMPLEX,0.5, Scalar(0,0,255), 2, 8);
  47. imshow("result", img);
  48. if(waitKey(1)=='q') break;
  49. }
  50. return 0;
  51. }

三、效果

图片输入:

摄像头输入:

 Yolov5n-seg用OpenCV DNN 进行C++部署,在CPU上居然200多ms,我承认与yolov5 检测相比输出变大,运算量也会增加。但是Yolov5n-seg在Python推理才100ms左右,可能是python的矩阵运算确实很快??有大佬说下原因吗?

觉得有帮助点个赞哦!

 

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

闽ICP备14008679号