IDetection.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #pragma once
  2. #include<iostream>
  3. #include <vector>
  4. #include<opencv2/opencv.hpp>
  5. #include "YunDaISASImageRecognitionService.h"
  6. using namespace std;
  7. class IDetection {
  8. public:
  9. struct Output {
  10. Output(int id, float confidence, cv::Rect box)
  11. {
  12. m_id = id;
  13. m_confidence = confidence;
  14. m_box = box;
  15. }
  16. /*Output()
  17. {
  18. }*/
  19. public:
  20. int m_id; //结果类别id
  21. float m_confidence; //结果置信度
  22. cv::Rect m_box; //矩形框
  23. };
  24. public:
  25. struct DectectResult {
  26. // DectectResult(float confidence, float dValue, string sValue/*, string sUnit,string sName*/ )
  27. // {
  28. // m_confidence = confidence;
  29. // m_dValue = dValue;
  30. // m_sValue = sValue;
  31. ///* m_sUnit = sUnit;
  32. // m_sName = sName;*/
  33. // }
  34. DectectResult(float confidence, float dValue, string sValue)
  35. {
  36. m_confidence = confidence;
  37. m_dValue = dValue;
  38. m_sValue = sValue;
  39. }
  40. DectectResult()
  41. {
  42. }
  43. float m_confidence =0; //结果置信度
  44. float m_dValue =0;
  45. string m_sValue="";
  46. //string m_sUnit=""; //单位 例如℃
  47. //string m_sName=""; //名称
  48. };
  49. //public:
  50. // virtual vector<DectectResult> GetStateResult(cv::Mat img,std::vector<cv::Rect> rects) = 0;
  51. // virtual vector<DectectResult> GetDigitResult(cv::Mat img,std::vector<cv::Rect> rects) = 0;
  52. public:
  53. #if(defined YOLO_P6 && YOLO_P6==true)
  54. const float netAnchors[4][6] = { { 19,27, 44,40, 38,94 },{ 96,68, 86,152, 180,137 },{ 140,301, 303,264, 238,542 },{ 436,615, 739,380, 925,792 } };
  55. const int netWidth = 1280; //ONNX图片输入宽度
  56. const int netHeight = 1280; //ONNX图片输入高度
  57. const int strideSize = 4; //stride size
  58. #else
  59. const float netAnchors[3][6] = { { 10,13, 16,30, 33,23 },{ 30,61, 62,45, 59,119 },{ 116,90, 156,198, 373,326 } };
  60. const int netWidth = 640; //ONNX图片输入宽度
  61. const int netHeight = 640; //ONNX图片输入高度
  62. const int strideSize = 3; //stride size
  63. #endif // YOLO_P6
  64. const float netStride[4] = { 8, 16.0,32,64 };
  65. float boxThreshold = 0.25;
  66. float classThreshold = 0.25;
  67. float nmsThreshold = 0.45;
  68. float nmsScoreThreshold = boxThreshold * classThreshold;
  69. public:
  70. static void DrawPred(cv::Mat& img, vector<Output> result,vector<std::string> className)
  71. {
  72. for (int i = 0; i < result.size(); i++) {
  73. try
  74. {
  75. int left = result[i].m_box.x;
  76. int top = result[i].m_box.y;
  77. int width = result[i].m_box.width;
  78. int height = result[i].m_box.height;
  79. int baseLine;
  80. //2022.10.17
  81. cv::Rect box = result[i].m_box;
  82. cv::Mat input_image_copy = img.clone();
  83. cv::Mat cutMat = input_image_copy(box);
  84. string label = className[result[i].m_id] + ": " + to_string(result[i].m_confidence);
  85. cv::Size labelSize = getTextSize(label, cv::FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
  86. top = max(top, labelSize.height);
  87. cv::rectangle(img, cv::Point(left, top - int(2 * labelSize.height)), cv::Point(left + int(2 * labelSize.width), top + baseLine), cv::Scalar(0, 0, 255), cv::FILLED);
  88. cv::rectangle(img, cv::Point(left, top), cv::Point(left + width, top + height), cv::Scalar(255, 255, 0), 1);
  89. cv::putText(img, label, cv::Point(left, top), cv::FONT_HERSHEY_SIMPLEX, 1, cv::Scalar(255, 255, 0), 1);
  90. }
  91. catch (const std::exception& ex)
  92. {
  93. YunDaISASImageRecognitionService::ConsoleLog(ex.what());
  94. }
  95. }
  96. }
  97. };