pansen
2018-12-19 6495701d221972e7c780415ab1ba4c092f669dfa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#ifndef TRACKINGTRIGGER_H
#define TRACKINGTRIGGER_H
 
#include <vector>
#include <opencv2/opencv.hpp>
#include <functional>
#include <atomic>
 
using namespace std;
 
struct ScoredRect {
    ScoredRect() : id(-1), isMask(false) {}
 
    bool isMask;
    float score;
    cv::Rect rect;
    long id;
    map<string, string> properties;
};
 
class TrackingTrigger {
public:
    TrackingTrigger(float threshold) :
        threshold(threshold), faceTrackingId(0) {}
 
 
    bool triggerOnce(ScoredRect &rect, long faceId = -1) {
        if (faceId < 0) {
            return triggerOnce(rect, false);
        } else {
            bool found = false;
            for (auto lastRect: lastScoreRects) {
                if (lastRect.id >= 0 && lastRect.id == faceId) {
                    found = true;
                    rect.id = faceId;
                    rect.properties = lastRect.properties;
                    tempScoreRects.push_back(rect);
                    break;
                }
            }
            if (!found) {
                if (rect.score < threshold) {
//                tempScoreRects.push_back(rect);
                    return false;
                } else {
                    rect.id = faceId;
                    tempScoreRects.push_back(rect);
                    return true;
                }
            }
            return false;
        }
    }
 
    void triggerLine() {
        lastScoreRects.swap(tempScoreRects);// = tempScoreRects;
        tempScoreRects.clear();
    }
 
    ScoredRect &getLastRect() {
        return tempScoreRects[tempScoreRects.size() - 1];
    }
 
    std::vector<ScoredRect> getLastScoreRects() const {
        return lastScoreRects;
    }
 
private:
    bool triggerOnce(ScoredRect &rect, bool) {
        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;
    }
 
private:
    float threshold;
    std::vector<ScoredRect> lastScoreRects;
    std::vector<ScoredRect> tempScoreRects;
    std::vector<int> lastScoreInts;
    std::vector<int> tempScoreInts;
    std::atomic<long> faceTrackingId;
};
 
#endif // TRACKINGTRIGGER_H