IObjectDetector.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #pragma once
  2. #include "vector"
  3. #include <memory>
  4. #include "string"
  5. #include "opencv2/opencv.hpp"
  6. #ifndef mymax
  7. #define mymin(x,y) ((x) < (y) ? (x) : (y))
  8. #define mymax(x,y) ((x) > (y) ? (x) : (y))
  9. #endif // mymax
  10. #ifndef _DetectorResult
  11. #define _DetectorResult
  12. struct DetectorResult
  13. {
  14. int image_id, label; //label 1 开始 0 背景
  15. char name[16];
  16. float score;
  17. int left, top, height, width;
  18. bool overlap;
  19. bool hasMask;
  20. int area;
  21. cv::Mat mask;
  22. DetectorResult()
  23. {
  24. memset(name, 0, sizeof(name));
  25. }
  26. DetectorResult(int label, float left, float top, float width, float height, float score)
  27. {
  28. this->image_id = 0;
  29. this->label = label;
  30. this->left = left;
  31. this->width = width;
  32. this->top = top;
  33. this->height = height;
  34. this->score = score;
  35. this->area = width * height;
  36. }
  37. DetectorResult(const DetectorResult& detectorResult)
  38. {
  39. this->image_id = detectorResult.image_id;
  40. this->label = detectorResult.label;
  41. strcpy_s(this->name, detectorResult.name);
  42. this->left = detectorResult.left;
  43. this->width = detectorResult.width;
  44. this->top = detectorResult.top;
  45. this->height = detectorResult.height;
  46. this->score = detectorResult.score;
  47. this->area = detectorResult.area;
  48. this->overlap = detectorResult.overlap;
  49. this->hasMask = detectorResult.hasMask;
  50. if (!detectorResult.mask.empty())
  51. this->mask = detectorResult.mask.clone();
  52. }
  53. void Offset(int offsetX, int offsetY)
  54. {
  55. left += offsetX;
  56. top += offsetY;
  57. }
  58. void Scale(float scaleX, float scaleY)
  59. {
  60. left *= scaleX;
  61. top *= scaleY;
  62. width *= scaleX;
  63. height *= scaleY;
  64. }
  65. void ScaleOffset(float scaleX, float scaleY, int offsetX, int offsetY)
  66. {
  67. Scale(scaleX, scaleY);
  68. Offset(offsetX, offsetY);
  69. }
  70. void OffsetScale(int offsetX, int offsetY, float scaleX, float scaleY)
  71. {
  72. Offset(offsetX, offsetY);
  73. Scale(scaleX, scaleY);
  74. }
  75. void Clip(int iMinX, int iMaxX, int iMinY, int iMaxY)
  76. {
  77. int right = left + width;
  78. int bottom = top + height;
  79. left = mymax(iMinX, left);
  80. top = mymax(iMinY, top);
  81. right = mymin(right, iMaxX);
  82. bottom = mymin(bottom, iMaxY);
  83. width = right - left;
  84. height = bottom - top;
  85. }
  86. cv::Rect GetRect()
  87. {
  88. cv::Rect cvRect(left, top, width, height);
  89. return cvRect;
  90. }
  91. };
  92. #endif
  93. #ifndef _FaultDetectResult
  94. #define _FaultDetectResult
  95. struct FaultDetectResult
  96. {
  97. int image_id, label; //label 1 开始 0 背景
  98. char name[16];
  99. float score;
  100. float value;
  101. int left, top, height, width;
  102. bool overlap;
  103. bool hasMask;
  104. cv::Mat mask;
  105. FaultDetectResult()
  106. {
  107. }
  108. FaultDetectResult(int label, float left, float top, float width, float height, float score)
  109. {
  110. this->image_id = 0;
  111. this->label = label;
  112. this->left = left;
  113. this->width = width;
  114. this->top = top;
  115. this->height = height;
  116. this->score = score;
  117. }
  118. void Offset(int offsetX, int offsetY)
  119. {
  120. left += offsetX;
  121. top += offsetY;
  122. }
  123. void Scale(float scaleX, float scaleY)
  124. {
  125. left *= scaleX;
  126. top *= scaleY;
  127. width *= scaleX;
  128. height *= scaleY;
  129. }
  130. void ScaleOffset(float scaleX, float scaleY, int offsetX, int offsetY)
  131. {
  132. Scale(scaleX, scaleY);
  133. Offset(offsetX, offsetY);
  134. }
  135. void OffsetScale(int offsetX, int offsetY, float scaleX, float scaleY)
  136. {
  137. Offset(offsetX, offsetY);
  138. Scale(scaleX, scaleY);
  139. }
  140. cv::Rect GetRect()
  141. {
  142. cv::Rect cvRect(left, top, width, height);
  143. return cvRect;
  144. }
  145. };
  146. #endif
  147. #ifndef _SInferenceParams
  148. #define _SInferenceParams
  149. struct SInferenceParams
  150. {
  151. std::string init_net_pb, predict_net_pb;
  152. int batchSize = 1;
  153. int minSize = 800;
  154. int maxSize = 1333;
  155. float threshold = 0.5;
  156. bool hasMask = true;
  157. };
  158. #endif
  159. struct IObjectDetector
  160. {
  161. public:
  162. virtual ~IObjectDetector() = default;
  163. //初始化对象
  164. virtual bool Init() = 0;
  165. //释放对象
  166. virtual bool UnInit() = 0;
  167. //获取对象名
  168. virtual const char* GetType() = 0;
  169. //执行infer
  170. virtual bool Execute(cv::Mat& image) = 0;
  171. //获取结果
  172. virtual bool GetResults(std::vector<DetectorResult>& vecDetectorResults) = 0;
  173. };
  174. float Iou(cv::Rect box1, cv::Rect box2);