123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- #pragma once
- #include "vector"
- #include <memory>
- #include "string"
- #include "opencv2/opencv.hpp"
- #ifndef mymax
- #define mymin(x,y) ((x) < (y) ? (x) : (y))
- #define mymax(x,y) ((x) > (y) ? (x) : (y))
- #endif // mymax
- #ifndef _DetectorResult
- #define _DetectorResult
- struct DetectorResult
- {
- int image_id, label; //label 1 开始 0 背景
- char name[16];
- float score;
- int left, top, height, width;
- bool overlap;
- bool hasMask;
- int area;
- cv::Mat mask;
- DetectorResult()
- {
- memset(name, 0, sizeof(name));
- }
- DetectorResult(int label, float left, float top, float width, float height, float score)
- {
- this->image_id = 0;
- this->label = label;
- this->left = left;
- this->width = width;
- this->top = top;
- this->height = height;
- this->score = score;
- this->area = width * height;
- }
- DetectorResult(const DetectorResult& detectorResult)
- {
- this->image_id = detectorResult.image_id;
- this->label = detectorResult.label;
- strcpy_s(this->name, detectorResult.name);
- this->left = detectorResult.left;
- this->width = detectorResult.width;
- this->top = detectorResult.top;
- this->height = detectorResult.height;
- this->score = detectorResult.score;
- this->area = detectorResult.area;
- this->overlap = detectorResult.overlap;
- this->hasMask = detectorResult.hasMask;
- if (!detectorResult.mask.empty())
- this->mask = detectorResult.mask.clone();
- }
- void Offset(int offsetX, int offsetY)
- {
- left += offsetX;
- top += offsetY;
- }
- void Scale(float scaleX, float scaleY)
- {
- left *= scaleX;
- top *= scaleY;
- width *= scaleX;
- height *= scaleY;
- }
- void ScaleOffset(float scaleX, float scaleY, int offsetX, int offsetY)
- {
- Scale(scaleX, scaleY);
- Offset(offsetX, offsetY);
- }
- void OffsetScale(int offsetX, int offsetY, float scaleX, float scaleY)
- {
- Offset(offsetX, offsetY);
- Scale(scaleX, scaleY);
- }
- void Clip(int iMinX, int iMaxX, int iMinY, int iMaxY)
- {
- int right = left + width;
- int bottom = top + height;
- left = mymax(iMinX, left);
- top = mymax(iMinY, top);
- right = mymin(right, iMaxX);
- bottom = mymin(bottom, iMaxY);
- width = right - left;
- height = bottom - top;
- }
- cv::Rect GetRect()
- {
- cv::Rect cvRect(left, top, width, height);
- return cvRect;
- }
- };
- #endif
- #ifndef _FaultDetectResult
- #define _FaultDetectResult
- struct FaultDetectResult
- {
- int image_id, label; //label 1 开始 0 背景
- char name[16];
- float score;
- float value;
- int left, top, height, width;
- bool overlap;
- bool hasMask;
- cv::Mat mask;
- FaultDetectResult()
- {
- }
- FaultDetectResult(int label, float left, float top, float width, float height, float score)
- {
- this->image_id = 0;
- this->label = label;
- this->left = left;
- this->width = width;
- this->top = top;
- this->height = height;
- this->score = score;
- }
- void Offset(int offsetX, int offsetY)
- {
- left += offsetX;
- top += offsetY;
- }
- void Scale(float scaleX, float scaleY)
- {
- left *= scaleX;
- top *= scaleY;
- width *= scaleX;
- height *= scaleY;
- }
- void ScaleOffset(float scaleX, float scaleY, int offsetX, int offsetY)
- {
- Scale(scaleX, scaleY);
- Offset(offsetX, offsetY);
- }
- void OffsetScale(int offsetX, int offsetY, float scaleX, float scaleY)
- {
- Offset(offsetX, offsetY);
- Scale(scaleX, scaleY);
- }
- cv::Rect GetRect()
- {
- cv::Rect cvRect(left, top, width, height);
- return cvRect;
- }
- };
- #endif
- #ifndef _SInferenceParams
- #define _SInferenceParams
- struct SInferenceParams
- {
- std::string init_net_pb, predict_net_pb;
- int batchSize = 1;
- int minSize = 800;
- int maxSize = 1333;
- float threshold = 0.5;
- bool hasMask = true;
- };
- #endif
- struct IObjectDetector
- {
- public:
- virtual ~IObjectDetector() = default;
- //初始化对象
- virtual bool Init() = 0;
- //释放对象
- virtual bool UnInit() = 0;
- //获取对象名
- virtual const char* GetType() = 0;
- //执行infer
- virtual bool Execute(cv::Mat& image) = 0;
- //获取结果
- virtual bool GetResults(std::vector<DetectorResult>& vecDetectorResults) = 0;
- };
- float Iou(cv::Rect box1, cv::Rect box2);
|