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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
|
#include "opencv2/core/utility.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/tracking.hpp"
#include "opencv2/videoio.hpp"
#include "opencv2/plot.hpp"
#include <fstream>
#include <iomanip>
#include <iostream>
using namespace std;
using namespace cv;
// TODO: do normalization ala Kalal's assessment protocol for TLD
static const Scalar gtColor = Scalar(0, 255, 0);
static Scalar getNextColor()
{
const int num = 6;
static Scalar colors[num] = {Scalar(160, 0, 0), Scalar(0, 0, 160), Scalar(0, 160, 160),
Scalar(160, 160, 0), Scalar(160, 0, 160), Scalar(20, 50, 160)};
static int id = 0;
return colors[id < num ? id++ : num - 1];
}
inline vector<Rect2d> readGT(const string &filename, const string &omitname)
{
vector<Rect2d> res;
{
ifstream input(filename.c_str());
if (!input.is_open())
CV_Error(Error::StsError, "Failed to open file");
while (input)
{
Rect2d one;
input >> one.x;
input.ignore(numeric_limits<std::streamsize>::max(), ',');
input >> one.y;
input.ignore(numeric_limits<std::streamsize>::max(), ',');
input >> one.width;
input.ignore(numeric_limits<std::streamsize>::max(), ',');
input >> one.height;
input.ignore(numeric_limits<std::streamsize>::max(), '\n');
if (input.good())
res.push_back(one);
}
}
if (!omitname.empty())
{
ifstream input(omitname.c_str());
if (!input.is_open())
CV_Error(Error::StsError, "Failed to open file");
while (input)
{
unsigned int a = 0, b = 0;
input >> a >> b;
input.ignore(numeric_limits<std::streamsize>::max(), '\n');
if (a > 0 && b > 0 && a < res.size() && b < res.size())
{
if (a > b)
swap(a, b);
for (vector<Rect2d>::iterator i = res.begin() + a; i != res.begin() + b; ++i)
{
*i = Rect2d();
}
}
}
}
return res;
}
inline bool isGoodBox(const Rect2d &box) { return box.width > 0. && box.height > 0.; }
const int LTRC_COUNT = 100;
struct AlgoWrap
{
AlgoWrap(const string &name_)
: tracker(Tracker::create(name_)), lastState(NotFound), name(name_), color(getNextColor()),
numTotal(0), numResponse(0), numPresent(0), numCorrect_0(0), numCorrect_0_5(0),
timeTotal(0), auc(LTRC_COUNT + 1, 0)
{
}
enum State
{
NotFound,
Overlap_None,
Overlap_0,
Overlap_0_5,
};
Ptr<Tracker> tracker;
bool lastRes;
Rect2d lastBox;
State lastState;
// visual
string name;
Scalar color;
// results
int numTotal; // frames passed to tracker
int numResponse; // frames where tracker had response
int numPresent; // frames where ground truth result present
int numCorrect_0; // frames where overlap with GT > 0
int numCorrect_0_5; // frames where overlap with GT > 0.5
int64 timeTotal; // ticks
vector<int> auc; // number of frames for each overlap percent
void eval(const Mat &frame, const Rect2d >Box, bool isVerbose)
{
// RUN
lastBox = Rect2d();
int64 frameTime = getTickCount();
lastRes = tracker->update(frame, lastBox);
frameTime = getTickCount() - frameTime;
// RESULTS
double intersectArea = (gtBox & lastBox).area();
double unionArea = (gtBox | lastBox).area();
numTotal++;
numResponse += (lastRes && isGoodBox(lastBox)) ? 1 : 0;
numPresent += isGoodBox(gtBox) ? 1 : 0;
double overlap = unionArea > 0. ? intersectArea / unionArea : 0.;
numCorrect_0 += overlap > 0. ? 1 : 0;
numCorrect_0_5 += overlap > 0.5 ? 1 : 0;
auc[std::min(std::max((size_t)(overlap * LTRC_COUNT), (size_t)0), (size_t)LTRC_COUNT)]++;
timeTotal += frameTime;
if (isVerbose)
cout << name << " - " << overlap << endl;
if (isGoodBox(gtBox) != isGoodBox(lastBox)) lastState = NotFound;
else if (overlap > 0.5) lastState = Overlap_0_5;
else if (overlap > 0.0001) lastState = Overlap_0;
else lastState = Overlap_None;
}
void draw(Mat &image, const Point &textPoint) const
{
if (lastRes)
rectangle(image, lastBox, color, 2, LINE_8);
string suf;
switch (lastState)
{
case AlgoWrap::NotFound: suf = " X"; break;
case AlgoWrap::Overlap_None: suf = " ~"; break;
case AlgoWrap::Overlap_0: suf = " +"; break;
case AlgoWrap::Overlap_0_5: suf = " ++"; break;
}
putText(image, name + suf, textPoint, FONT_HERSHEY_PLAIN, 1, color, 1, LINE_AA);
}
// calculates "lost track ratio" curve - row of values growing from 0 to 1
// number of elements is LTRC_COUNT + 2
Mat getLTRC() const
{
Mat t, res;
Mat(auc).convertTo(t, CV_64F); // integral does not support CV_32S input
integral(t.t(), res, CV_64F); // t is a column of values
return res.row(1) / (double)numTotal;
}
void plotLTRC(Mat &img) const
{
Ptr<plot::Plot2d> p_ = plot::createPlot2d(getLTRC());
p_->render(img);
}
double calcAUC() const
{
return cv::sum(getLTRC())[0] / (double)LTRC_COUNT;
}
void stat(ostream &out) const
{
out << name << endl;
out << setw(20) << "Overlap > 0 " << setw(20) << (double)numCorrect_0 / numTotal * 100
<< "%" << setw(20) << numCorrect_0 << endl;
out << setw(20) << "Overlap > 0.5" << setw(20) << (double)numCorrect_0_5 / numTotal * 100
<< "%" << setw(20) << numCorrect_0_5 << endl;
double p = (double)numCorrect_0_5 / numResponse;
double r = (double)numCorrect_0_5 / numPresent;
double f = 2 * p * r / (p + r);
out << setw(20) << "Precision" << setw(20) << p * 100 << "%" << endl;
out << setw(20) << "Recall " << setw(20) << r * 100 << "%" << endl;
out << setw(20) << "f-measure" << setw(20) << f * 100 << "%" << endl;
out << setw(20) << "AUC" << setw(20) << calcAUC() << endl;
double s = (timeTotal / getTickFrequency()) / numTotal;
out << setw(20) << "Performance" << setw(20) << s * 1000 << " ms/frame" << setw(20) << 1 / s
<< " fps" << endl;
}
};
inline ostream &operator<<(ostream &out, const AlgoWrap &w) { w.stat(out); return out; }
inline vector<AlgoWrap> initAlgorithms(const string &algList)
{
vector<AlgoWrap> res;
istringstream input(algList);
for (;;)
{
char one[30];
input.getline(one, 30, ',');
if (!input)
break;
cout << " " << one << " - ";
AlgoWrap a(one);
if (a.tracker)
{
res.push_back(a);
cout << "OK";
}
else
{
cout << "FAILED";
}
cout << endl;
}
return res;
}
static const string &window = "Tracking API";
int main(int argc, char **argv)
{
const string keys =
"{help h||show help}"
"{video||video file to process}"
"{gt||ground truth file (each line describes rectangle in format: '<x>,<y>,<w>,<h>')}"
"{start|0|starting frame}"
"{num|0|frame number (0 for all)}"
"{omit||file with omit ranges (each line describes occluded frames: '<start> <end>')}"
"{plot|false|plot LTR curves at the end}"
"{v|false|print each frame info}"
"{@algos||comma-separated algorithm names}";
CommandLineParser p(argc, argv, keys);
if (p.has("help"))
{
p.printMessage();
return 0;
}
int startFrame = p.get<int>("start");
int frameCount = p.get<int>("num");
string videoFile = p.get<string>("video");
string gtFile = p.get<string>("gt");
string omitFile = p.get<string>("omit");
string algList = p.get<string>("@algos");
bool doPlot = p.get<bool>("plot");
bool isVerbose = p.get<bool>("v");
if (!p.check())
{
p.printErrors();
return 0;
}
cout << "Reading GT from " << gtFile << " ... ";
vector<Rect2d> gt = readGT(gtFile, omitFile);
if (gt.empty())
CV_Error(Error::StsError, "Failed to read GT file");
cout << gt.size() << " boxes" << endl;
cout << "Opening video " << videoFile << " ... ";
VideoCapture cap;
cap.open(videoFile);
if (!cap.isOpened())
CV_Error(Error::StsError, "Failed to open video file");
cap.set(CAP_PROP_POS_FRAMES, startFrame);
cout << "at frame " << startFrame << endl;
// INIT
vector<AlgoWrap> algos = initAlgorithms(algList);
Mat frame, image;
cap >> frame;
for (vector<AlgoWrap>::iterator i = algos.begin(); i != algos.end(); ++i)
i->tracker->init(frame, gt[0]);
// DRAW
{
namedWindow(window, WINDOW_AUTOSIZE);
frame.copyTo(image);
rectangle(image, gt[0], gtColor, 2, LINE_8);
imshow(window, image);
}
bool paused = false;
int frameId = 0;
cout << "Hot keys:" << endl << " q - exit" << endl << " p - pause" << endl;
for (;;)
{
if (!paused)
{
cap >> frame;
if (frame.empty())
{
cout << "Done - video end" << endl;
break;
}
frameId++;
if (isVerbose)
cout << endl << "Frame " << frameId << endl;
// EVAL
for (vector<AlgoWrap>::iterator i = algos.begin(); i != algos.end(); ++i)
i->eval(frame, gt[frameId], isVerbose);
// DRAW
{
Point textPoint(1, 16);
frame.copyTo(image);
rectangle(image, gt[frameId], gtColor, 2, LINE_8);
putText(image, "GROUND TRUTH", textPoint, FONT_HERSHEY_PLAIN, 1, gtColor, 1, LINE_AA);
for (vector<AlgoWrap>::iterator i = algos.begin(); i != algos.end(); ++i)
{
textPoint.y += 14;
i->draw(image, textPoint);
}
imshow(window, image);
}
}
char c = (char)waitKey(1);
if (c == 'q')
{
cout << "Done - manual exit" << endl;
break;
}
else if (c == 'p')
{
paused = !paused;
}
if (frameCount && frameId >= frameCount)
{
cout << "Done - max frame count" << endl;
break;
}
}
// STAT
for (vector<AlgoWrap>::iterator i = algos.begin(); i != algos.end(); ++i)
cout << "==========" << endl << *i << endl;
if (doPlot)
{
Mat img(300, 300, CV_8UC3);
for (vector<AlgoWrap>::iterator i = algos.begin(); i != algos.end(); ++i)
{
i->plotLTRC(img);
imshow("LTR curve for " + i->name, img);
}
waitKey(0);
}
return 0;
}
|