OpenCVDNN模块教程(四)Mask
本⽂为OpenCV DNN模块官⽅教程的扩展,介绍如何使⽤OpenCV加载TensorFlow Object Detection API训练的模型做实例分割,以Mask-RCNN为例来检测缺陷。TensorFlow Object Detection API的github链接地址如下:
github/tensorflow/models/tree/master/research/object_detection
本⽂以TensorFlow 1.x为例(TF2.x等后续稳定⽀持OpenCV后介绍),介绍OpenCV DNN模块调⽤Mask-RCNN模型做实例分割的步骤如下:
(1) 下载或⾃⼰训练⽣成 .pb 格式的模型⽂件。本⽂以⾃⼰训练好的缺陷检测模型frozen_inference_graph.pb为例:
(2) 使⽤指令⽤.pb⽂件⽣成.pbtxt⽂件, Mask-RCNN使⽤tf_text_graph_mask_rcnn.py,指令如下:
主要参数三个:
--input 输⼊.pb模型⽂件完整路径;
--output 输出.pbtxt⽂件完整路径;
--config 输⼊config⽂件完整路径
完整指令:
python tf_text_graph_mask_rcnn.py --input E:\Practice\TensorFlow\DataSet\mask_defects2\model\export\frozen_inference_graph.pb --output
E:\Practice\TensorFlow\DataSet\mask_defects2\model\export\frozen_inference_graph.pbtxt --config
E:\Practice\TensorFlow\DataSet\mask_defects2\model\train\mask_rcnn_inception_fig
运⾏结果:
(3) 配置OpenCV4.4,加载图⽚测试,代码如下:
1. #include <fstream>
2. #include <sstream>
3. #include <iostream>
4. #include <string.h>
5.
6. #include <opencv2/dnn.hpp>
7. #include <opencv2/imgproc.hpp>
8. #include <opencv2/highgui.hpp>
9.
10.
11. using namespace cv;
12. using namespace dnn;
13. using namespace std;
14.
15. // Initialize the parameters
16. float confThreshold = 0.4; // Confidence threshold
17. float maskThreshold = 0.5; // Mask threshold
18.
19. vector<string> classes;
20. vector<Scalar> colors;
21.
22. // Draw the predicted bounding box, colorize and show the mask on the image
23. void drawBox(Mat& frame, int classId, float conf, Rect box, Mat& objectMask)
24. {
25.  //Draw a rectangle displaying the bounding box
26.  rectangle(frame, Point(box.x, box.y), Point(box.x + box.width, box.y + box.height), Scalar(255, 178, 50), 5);
27.
28.  //Get the label for the class name and its confidence
29.  string label = format('%.2f', conf);
30.  if (!pty())
31.  {
32.    CV_Assert(classId < (int)classes.size());
32.    CV_Assert(classId < (int)classes.size());
33.    label = classes[classId] + ':' + label;
34.  }
35.
36.  //Display the label at the top of the bounding box
37.  int baseLine;
38.  Size labelSize = getTextSize(label, FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
39.  box.y = max(box.y, labelSize.height);
40.  //rectangle(frame, Point(box.x, box.y - round(1.5*labelSize.height)), Point(box.x + round(1.5*labelSize.width), box.y + baseLine), Scalar(255, 255, 255), FILLED);
41.  putText(frame, label, Point(box.x, box.y), FONT_HERSHEY_SIMPLEX, 0.75, Scalar(0, 255, 0), 2);
42.
43.  Scalar color = colors[classId%colors.size()];
44.
45.  // Resize the mask, threshold, color and apply it on the image
46.  resize(objectMask, objectMask, Size(box.width, box.height));
47.  Mat mask = (objectMask > maskThreshold);
48.  Mat coloredRoi = (0.5 * color + 0.7 * frame(box));
49.  vertTo(coloredRoi, CV_8UC3);
50.
51.  // Draw the contours on the image
52.  vector<Mat> contours;
53.  Mat hierarchy;
54.  vertTo(mask, CV_8U);
55.  findContours(mask, contours, hierarchy, RETR_CCOMP, CHAIN_APPROX_SIMPLE);
56.  drawContours(coloredRoi, contours, -1, color, 5, LINE_8, hierarchy, 100);
57.  pyTo(frame(box), mask);
58.
59. }
60. // For each frame, extract the bounding box and mask for each detected object
61.
62. void postprocess(Mat& frame, const vector<Mat>& outs)
63. {
64.  Mat outDetections = outs[0];
65.  Mat outMasks = outs[1];
66.
67.  // Output size of masks is NxCxHxW where
68.  // N - number of detected boxes
69.  // C - number of classes (excluding background)
70.  // HxW - segmentation shape
71.  const int numDetections = outDetections.size[2];
72.  const int numClasses = outMasks.size[1];
73.
74.  outDetections = shape(1, al() / 7);
75.  for (int i = 0; i < numDetections; ++i)
76.  {
77.    float score = outDetections.at<float>(i, 2);
78.    if (score > confThreshold)
79.    {
80.      // Extract the bounding box
81.      int classId = static_cast<int>(outDetections.at<float>(i, 1));
82.      int left = static_cast<int>(ls * outDetections.at<float>(i, 3));
83.      int top = static_cast<int>(ws * outDetections.at<float>(i, 4));
84.      int right = static_cast<int>(ls * outDetections.at<float>(i, 5));
85.      int bottom = static_cast<int>(ws * outDetections.at<float>(i, 6));
86.
87.      left = max(0, min(left, ls - 1));
88.      top = max(0, min(top, ws - 1));
89.      right = max(0, min(right, ls - 1));
90.      bottom = max(0, min(bottom, ws - 1));
91.      Rect box = Rect(left, top, right - left + 1, bottom - top + 1);
tensorflow入门教程92.
93.      // Extract the mask for the object
94.      Mat objectMask(outMasks.size[2], outMasks.size[3], CV_32F, outMasks.ptr<float>(i, classId));
94.      Mat objectMask(outMasks.size[2], outMasks.size[3], CV_32F, outMasks.ptr<float>(i, classId));
95.
96.      // Draw bounding box, colorize and show the mask on the image
97.      drawBox(frame, classId, score, box, objectMask);
98.
99.    }
100.  }
101. }
102.
103.
104. /***************Image Test****************/
105. int main()
106. {
107.  // Load names of classes
108.  string classesFile = './model2/label.names';
109.  ifstream ifs(classesFile.c_str());
110.  string line;
111.  while (getline(ifs, line)) classes.push_back(line);
112.
113.  // Load the colors
114.  string colorsFile = './';
115.  ifstream colorFptr(colorsFile.c_str());
116.  while (getline(colorFptr, line))
117.  {
118.    char* pEnd;
119.    double r, g, b;
120.    r = strtod(line.c_str(), &pEnd);
121.    g = strtod(pEnd, NULL);
122.    b = strtod(pEnd, NULL);
123.    Scalar color = Scalar(r, g, b, 255.0);
124.    colors.push_back(Scalar(r, g, b, 255.0));
125.  }
126.
127.  // Give the configuration and weight files for the model
128.  String textGraph = './model2/defect_label.pbtxt';
129.  String modelWeights = './model2/frozen_inference_graph.pb';
130.
131.  // Load the network
132.  Net net = readNetFromTensorflow(modelWeights, textGraph);
133.
134.  // Open a video file or an image file or a camera stream.
135.  string str, outputFile;
136.  Mat frame, blob;
137.
138.  // Create a window
139.  static const string kWinName = 'OpenCV DNN Mask-RCNN Demo';
140.
141.  // Process frames.
142.  frame = imread('./imgs/4.jpg');
143.  // Create a 4D blob from a frame.
144.  //blobFromImage(frame, blob, 1.0, ls, ws), Scalar(), true, false);
145.  //blobFromImage(frame, blob, 1.0, Size(1012, 800), Scalar(), true, false);
146.  blobFromImage(frame, blob, 1.0, Size(800, 800), Scalar(), true, false);
147.  //blobFromImage(frame, blob);
148.
149.  //Sets the input to the network
150.  net.setInput(blob);
151.
152.  // Runs the forward pass to get output from the output layers
153.  std::vector<String> outNames(2);
154.  cout << outNames[0] << endl;
155.  cout << outNames[1] << endl;
156.  outNames[0] = 'detection_out_final';
156.  outNames[0] = 'detection_out_final';
157.  outNames[1] = 'detection_masks';
158.  vector<Mat> outs;
159.  net.forward(outs, outNames);
160.
161.  // Extract the bounding box and mask for each of the detected objects
162.  postprocess(frame, outs);
163.
164.  // Put efficiency information. The function getPerfProfile returns the overall time for inference(t) and the timings for each of the layers(in layersTimes) 165.  vector<double> layersTimes;
166.  double freq = getTickFrequency() / 1000;
167.  double t = PerfProfile(layersTimes) / freq;
168.  string label = format('test use time: %0.0f ms', t);
169.  putText(frame, label, Point(10, 20), FONT_HERSHEY_SIMPLEX, 0.8, Scalar(0, 0, 255), 2);
170.
171.  // Write the frame with the detection boxes
172.  Mat detectedFrame;
173.  vertTo(detectedFrame, CV_8U);
174.  imwrite('result.jpg', frame);
175.  //resize(frame, frame, ls / 3, ws / 3));
176.  imshow(kWinName, frame);
177.  waitKey(0);
178.  return 0;
179. }
测试图像:
运⾏结果:
更多OpenCV/Halcon等相关资讯请关注:OpenCV与AI深度学习

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。