herrDeng網內搜尋

自訂搜尋

Ads

2021年12月15日 星期三

C++ realsense opencv::dnn同時辨識多個小比例模型

  1. //程式簡化,不用太新的C++ 語法 本版本修改Size參數(原300x300),可辨識更小比例模型
  2. #include <opencv2/opencv.hpp>
  3. #include <opencv2/dnn.hpp>
  4. #include <librealsense2/rs.hpp>
  5. #include "cv-helpers.hpp"
  6. #include <iostream>
  7. #include <string>
  8. using namespace cv::dnn;
  9. using namespace cv;
  10. using namespace rs2;
  11. using namespace std;
  12.  
  13. int main(int argc, char** argv)
  14. try
  15. {
  16. cout << "本版本修改Size參數,簡化非必要參數,可辨識同時多個小比例模型\n";
  17. string classNames[] = {
  18. "background", "aeroplane", "bicycle", "bird", "boat","bottle", "bus", "car", "cat", "chair","cow",
  19. "diningtable", "dog", "horse","motorbike", "person", "pottedplant","sheep", "sofa", "train", "tvmonitor"
  20. };
  21. Net net = readNetFromCaffe("MobileNetSSD_deploy.prototxt",
  22. "MobileNetSSD_deploy.caffemodel");
  23.  
  24. // Start streaming from Intel RealSense Camera
  25. pipeline pipe;
  26. config cfg;
  27. const int w = 1280, h = 720, FPS = 30;
  28. cfg.enable_stream(RS2_STREAM_COLOR, w, h, RS2_FORMAT_BGR8, FPS);
  29. cfg.enable_stream(RS2_STREAM_DEPTH, w, h, RS2_FORMAT_Z16, FPS);
  30. pipe.start(cfg);
  31. rs2::align align_to(RS2_STREAM_COLOR);
  32.  
  33. string wname = "1280*720顯示辨識";
  34. namedWindow(wname);
  35. while (getWindowProperty(wname, WND_PROP_AUTOSIZE) >= 0)
  36. {
  37. frameset data = pipe.wait_for_frames();
  38. data = align_to.process(data);
  39.  
  40. frame color_frame = data.get_color_frame();
  41. frame depth_frame = data.get_depth_frame();
  42.  
  43. // Convert RealSense frame to OpenCV matrix:
  44. Mat color_mat(h, w, CV_8UC3, (void*)color_frame.get_data());;
  45. Mat depth_mat = depth_frame_to_meters(depth_frame);
  46. //Convert Mat to batch of images Size參數原Size(300,300)
  47. Mat inputBlob = blobFromImage(color_mat, 1/127.5, Size( w, h), 127.5);
  48.  
  49. net.setInput(inputBlob, "data"); //set the network input
  50. Mat detection = net.forward(); //compute output
  51.  
  52. Mat detectionMat(detection.size[2], detection.size[3], CV_32F, (float*)detection.data);
  53. float confidenceThreshold = 0.8;
  54. Vec3b color[] = {Vec3b(255,255,0), Vec3b(0, 255, 0), Vec3b(0,255, 255), Vec3b(255, 255, 127)};
  55. for (int i = 0; i < detectionMat.rows; i++)
  56. {
  57. putText(color_mat, "Max size of Detections: "+ to_string(detection.size[2]),
  58. Size(30, 30), FONT_HERSHEY_SIMPLEX, 1, Scalar(0, 0, 255));
  59. float confidence = detectionMat.at<float>(i, 2);
  60.  
  61. if (confidence > confidenceThreshold)
  62. {
  63. size_t objectClass = (size_t)(detectionMat.at<float>(i, 1));
  64. cout <<"i="<<i<<", #"<<objectClass << "\t";
  65. int xLeftBottom = (int)(detectionMat.at<float>(i, 3) * color_mat.cols);
  66. int yLeftBottom = (int)(detectionMat.at<float>(i, 4) * color_mat.rows);
  67. int xRightTop = (int)(detectionMat.at<float>(i, 5) * color_mat.cols);
  68. int yRightTop = (int)(detectionMat.at<float>(i, 6) * color_mat.rows);
  69.  
  70. Rect object((int)xLeftBottom, (int)yLeftBottom,
  71. (int)(xRightTop - xLeftBottom), (int)(yRightTop - yLeftBottom));
  72. object = object & Rect(0, 0, depth_mat.cols, depth_mat.rows);
  73. cout << object;
  74. // Calculate mean depth inside the detection region
  75. // This is a very naive way to estimate objects depth
  76. Scalar m = mean(depth_mat(object));
  77.  
  78. string ss = classNames[objectClass];
  79. ss += " Prob=" + to_string(confidence);
  80. ss += " Distance= " + to_string(100 * m[0]) + " cm ";
  81. cout << ss << endl;
  82.  
  83. rectangle(color_mat, object, color[i%4], 2);
  84. int baseLine = 0;
  85. Size labelSize = getTextSize(ss, FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
  86.  
  87. auto center = (object.br() + object.tl()) * 0.5;
  88. center.x = center.x - labelSize.width / 2;
  89.  
  90. rectangle(color_mat, Rect(Point(center.x, center.y - labelSize.height),
  91. Size(labelSize.width, labelSize.height + baseLine)), color[i%4], -1);
  92. putText(color_mat, ss, center, FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 0));
  93. }
  94. }
  95. imshow(wname, color_mat);
  96. int k=waitKey(1);
  97. if (k == 27) break;
  98. else if (k == (int)'+') imwrite("photo.png", color_mat);
  99. }
  100. destroyAllWindows();
  101. pipe.stop();
  102. return 0;
  103. }
  104. catch (exception& e){
  105. cerr << e.what();
  106. return 1;
  107. }

沒有留言:

Related Posts Plugin for WordPress, Blogger...

熱門文章