-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathTracker.cpp
223 lines (179 loc) · 6.5 KB
/
Tracker.cpp
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include "Tracker.h"
#include "FaceDetector.h"
#include "opencv2/highgui/highgui.hpp"
using namespace cvip;
/**
* Destructor
* Release memory. Delete detector and all trackItems
*/
Tracker::~Tracker()
{
delete detector;
typedef std::map<uint, TrackItem*>::iterator TiIter;
for (TiIter it = trackItems.begin(); it != trackItems.end(); ++it)
drop(it->first);
}
/**
* Sample function, run tracker on video
*
* @todo erase this function !
* @return void
*/
void Tracker::onVideo()
{
using namespace cv;
Mat frame;
VideoCapture cap(0);
if (!cap.isOpened())
return;
cap.set(CV_CAP_PROP_FRAME_WIDTH, 640);
cap.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
while (1)
{
double t = (double) cv::getTickCount();
cap >> frame;
cv::Mat frame2 = frame.clone();
std::vector<Image*> images = Image::create_scale_space(frame,detector);
std::vector<cvip::DetectionRect> detections;
detections = detector->detect(images, true);
for (uint i=0; i<images.size(); i++)
delete images[i];
for (uint i=0; i<detections.size(); ++i)
{
cvip::DetectionRect& d = detections[i];
cv::Rect r(d.x1, d.y1, d.width, d.height);
cv::rectangle(frame2, r, CV_RGB(255,0,0),3);
}
updateWith(detections);
typedef std::map<uint, TrackItem*>::const_iterator TiIter;
for (TiIter it=trackItems.begin();
it != trackItems.end(); ++it)
{
cvip::DetectionRect& d = it->second->dRect;
cv::Rect r(d.x1, d.y1, d.width, d.height);
// draw bold if item is being tracked
if (it->second->isActive())
{
cv::rectangle(frame, r, cv::Scalar::all(255),3);
std::stringstream ss1, ss2, ss3;
std::string faceTracked("Face Tracked!");
ss1 << "id: " << it->first;
ss2 << "Uptime: " << it->second->uptime();
ss3 << "# of dead frames: " << it->second->numInactiveFrames;
cv::putText(frame, ss1.str().c_str(), Point(d.x1,d.y2), FONT_HERSHEY_SIMPLEX, 0.5, CV_RGB(255,0,0), 2);
cv::putText(frame, ss2.str().c_str(), Point(d.x1,d.y2+20), FONT_HERSHEY_SIMPLEX, 0.5, CV_RGB(255,0,0), 2);
if (it->second->numInactiveFrames >0)
cv::putText(frame, faceTracked.c_str(), Point(d.x1,d.y2+40), FONT_HERSHEY_SIMPLEX, 0.5, CV_RGB(255,0,0), 2);
}
else // draw thin otherwise
{
//cv::rectangle(frame, r, cv::Scalar::all(255),1);
}
}
//std::cout << numItems() << std::endl;
cv::imshow("DETECTION", frame2);
cv::imshow("TRACKING", frame);
if(waitKey(10) >= 0)
break;
}
}
/**
* Take new detections and update the whole trackItems list.
* Processes are distributed to some internal methods.
*
* @param vector<DetectionRect>& freshDetects - incoming detections
* @return void
*/
void Tracker::updateWith(std::vector<DetectionRect>& freshDetects)
{
// a flag map keeping the state of each item: updated or not
std::map<uint, bool> flagActive;
// 1) update whatever you matchs
flagActive = updateActiveItems(freshDetects);
// 2) add remaining rectangles ass new items
this->addNewItems(freshDetects);
// 3) update unmatched items, drop them if necessary
this->updateInactiveItems(flagActive);
}
/**
* Take new detections and update the ones matched with the
* existing items.
* WARNING! the detections which are not matched are removed
* from the vector.
*
* @param vector<DetectionRect>& freshDetects - incoming detections
* @return std::map<uint,bool> - a flag for each item stating whether item is updated or not
*/
std::map<uint,bool> Tracker::updateActiveItems(std::vector<DetectionRect>& freshDetects)
{
std::map<uint, bool> flagActive;
typedef std::map<uint, TrackItem*>::iterator TiIter;
for (TiIter it=trackItems.begin(); it != trackItems.end(); ++it)
flagActive[it->first] = false;
// associate rects to items
for (int i=freshDetects.size()-1; i>=0; --i)
{
int maxIdx = -1;
double maxArea = 0.;
for (TiIter it=trackItems.begin(); it != trackItems.end(); ++it)
{
if (flagActive[it->first]) // update an item only once
continue;
uint area = Rect::intersect(freshDetects[i], it->second->dRect);
// check if they intersect
if (area > 0)
{
double ratio1 = (double)area/(freshDetects[i].width*freshDetects[i].height);
double ratio2 = (double)area/(it->second->dRect.width*it->second->dRect.height);
// try to find the best/largest intersection between rects
if (max<double>(ratio1,ratio2) > maxArea)
{
maxArea = max<double>(ratio1,ratio2);
maxIdx = it->first;
}
}
}
// is the best good enough?
if (maxArea > 0.20)
{
// if it is, freshDetects[i] is assumed to stand for the
// TrackedItem with id = maxIdx
flagActive[maxIdx] = true;
trackItems[maxIdx]->update(freshDetects[i]);
freshDetects.erase(freshDetects.begin()+i);
}
}
// this will be piped to updateInactiveItems()
return flagActive;
}
/**
* Take a list including a flag for each TrackItem: updated or not.
* Update each inactive item
*
* @param map<uint,bool>& flagActive
* @return void
*/
void Tracker::updateInactiveItems(const std::map<uint,bool>& flagActive)
{
typedef std::map<uint, bool>::const_iterator FlagIter;
for (FlagIter it = flagActive.begin(); it != flagActive.end(); ++it )
{
// skip if item is active at this frame
if (it->second)
continue;
// drop item if it's inactive for long
if (!trackItems[it->first]->update())
drop(it->first);
}
}
/**
* Add remaining unmatched freshRects as new TrackItems
*
* @param vector<DetectionRect>
* @return void
*/
void Tracker::addNewItems(std::vector<DetectionRect>& freshDetects)
{
for (uint i=0; i<freshDetects.size(); ++i)
add(new TrackItem(freshDetects[i]));
}