#ifndef TRACKINGTRIGGER_H
|
#define TRACKINGTRIGGER_H
|
#include <vector>
|
#include <opencv2/opencv.hpp>
|
#include <functional>
|
#include <atomic>
|
using namespace std;
|
struct ScoredRect{
|
//ScoredRect():id(-1){}
|
float score;
|
cv::Rect rect;
|
long id;
|
map<string,string> properties;
|
};
|
|
class TrackingTrigger
|
{
|
public:
|
TrackingTrigger(float threshold)://threshold:触发阈值,满足要求阈值的人脸才会触发
|
threshold(threshold),faceTrackingId(0)
|
{}
|
|
bool triggerOnce(ScoredRect& rect){//检测一次人脸后,for循环内每个人脸封装成ScoredRect结构体进行调用,返回值是该人脸是否被触发
|
bool found = false;
|
for(auto lastRect: lastScoreRects){
|
if(lastRect.id>=0&&(rect.rect&lastRect.rect).area()>lastRect.rect.area()*0.4){
|
found = true;
|
rect.id = lastRect.id;
|
rect.properties = lastRect.properties;
|
tempScoreRects.push_back(rect);
|
break;
|
}
|
}
|
if(!found){
|
if(rect.score<threshold){
|
tempScoreRects.push_back(rect);
|
return false;
|
}else{
|
rect.id = faceTrackingId++;
|
tempScoreRects.push_back(rect);
|
return true;
|
}
|
}
|
return false;
|
}
|
void triggerLine(){//for 循环遍历后调用,结束一组触发判断
|
lastScoreRects = tempScoreRects;
|
tempScoreRects.clear();
|
}
|
|
ScoredRect& getLastRect(){
|
return tempScoreRects[tempScoreRects.size() -1];
|
}
|
|
std::vector<ScoredRect> getLastScoreRects() const
|
{
|
return lastScoreRects;
|
}
|
|
private:
|
float threshold;
|
std::vector<ScoredRect> lastScoreRects;
|
std::vector<ScoredRect> tempScoreRects;
|
std::atomic<long> faceTrackingId;
|
};
|
|
#endif // TRACKINGTRIGGER_H
|