-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
279 lines (244 loc) · 9.83 KB
/
main.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
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
//
// Created by Patrick Werner (boonto) on 10.10.17.
//
#include <iostream>
#include <opencv2/core/utility.hpp>
#include <opencv2/tracking.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#include <chrono>
#include <fstream>
#include <memory>
#include <thread>
#include "MeanshiftTracker.h"
#include "LucasKanadeTracker.h"
// Handle input arguments to avoid rebuilding for parameter changes
// TODO: Pretty bad
int parseArguments(int argc, char *argv[], std::string &videoPath, std::string &groundTruthFileName,
std::string &errorFileName, bool &bUseGroundTruth, bool &bWriteErrorToFile, int ¤tTracker) {
if (2 == argc) {
auto argumentsFile = std::ifstream(argv[1]);
if (argumentsFile.fail()) {
std::cerr << "Error opening arguments file: " << std::strerror(errno) << "\n";
std::cerr << "Press a button to quit.\n";
std::cin.get();
return 1;
}
auto line = std::string();
while (std::getline(argumentsFile, line)) {
if (line.front() != '#') {
auto argName = line.substr(0, line.find('='));
auto argValue = line.substr(line.find('=') + 1);
if (argName == "videoPath") {
videoPath = argValue;
} else if (argName == "groundTruthFileName") {
groundTruthFileName = argValue;
} else if (argName == "errorFileName") {
errorFileName = argValue;
} else if (argName == "bUseGroundTruth") {
bUseGroundTruth = (argValue == "true");
} else if (argName == "bWriteErrorToFile") {
bWriteErrorToFile = (argValue == "true");
} else if (argName == "currentTracker") {
currentTracker = std::stoi(argValue);
}
}
}
} else if (7 == argc) {
videoPath = argv[1];
groundTruthFileName = argv[2];
errorFileName = argv[3];
bUseGroundTruth = (std::string(argv[4]) == "true");
bWriteErrorToFile = (std::string(argv[5]) == "true");
currentTracker = std::stoi(argv[6]);
} else {
std::cerr << "Please specify a arguments file!\n";
std::cerr << "Press a button to quit.\n";
std::cin.get();
return 1;
}
return 0;
}
// Converts a line in a text file to a rect
cv::Rect2f lineToRect(std::ifstream &stream, int offset) {
auto line = std::string();
std::getline(stream, line);
auto lineStream = std::stringstream(line);
auto item = std::string();
auto points = std::vector<int>();
auto prev = std::size_t();
decltype(prev) pos;
while ((pos = line.find_first_of(",\t", prev)) != std::string::npos) {
if (pos > prev) {
points.push_back(std::stoi(line.substr(prev, pos - prev)));
}
prev = pos + 1;
}
if (prev < line.length()) {
points.push_back(std::stoi(line.substr(prev, std::string::npos)));
}
if (!points.empty()) {
return cv::Rect2f(points[0] - offset, points[1] - offset, points[2] + offset * 2, points[3] + offset * 2);
} else {
return cv::Rect2f();
}
}
int main(int argc, char *argv[]) {
// Various arguments
auto videoPath = std::string();
auto groundTruthFileName = std::string();
auto errorFileName = std::string();
auto bUseGroundTruth = false;
auto bWriteErrorToFile = false;
// Starting tracker 0 = LK, 1 = MS
auto currentTracker = 0;
if (parseArguments(argc, argv, videoPath, groundTruthFileName, errorFileName,
bUseGroundTruth, bWriteErrorToFile, currentTracker) != 0) {
return EXIT_FAILURE;
}
// Timing
auto t0 = std::chrono::high_resolution_clock::now();
// Initialize video
auto vidCap = cv::VideoCapture();
if (videoPath.empty()) {
// Use camera
vidCap.open(0);
// Discard the first few frames to let the camera adjust to surroundings
for (int i = 0; i < 15; ++i) {
vidCap.grab();
}
} else {
// Use video
vidCap.open(videoPath);
}
if (!vidCap.isOpened()) {
std::cerr << "Failed to open video\n";
return EXIT_FAILURE;
}
// Load ground truth file
auto groundTruthFile = std::ifstream();
auto lastPos = videoPath.find_last_of('/');
auto secondToLastPos = videoPath.substr(0, lastPos).find_last_of('/');
groundTruthFile.open(videoPath.substr(0, secondToLastPos + 1) + groundTruthFileName);
if (groundTruthFile.fail()) {
std::cerr << "Error opening ground truth file: " << std::strerror(errno) << "\n"
<< "Ignoring ground truth data\n";
bUseGroundTruth = false;
}
auto resolution = cv::Point(static_cast<int>(vidCap.get(cv::CAP_PROP_FRAME_WIDTH)),
static_cast<int>(vidCap.get(cv::CAP_PROP_FRAME_HEIGHT)));
std::cout << "Video frame size is " << resolution << "\n";
std::string windowName = "Tracking";
cv::namedWindow(windowName);
// Initialize overarching variables
auto frame = cv::Mat();
auto display = cv::Mat();
auto roi = cv::Rect2f(cv::Point2f(), resolution);
auto bRoiInitialized = false;
auto trackers = std::vector<std::unique_ptr<Tracker>>();
trackers.push_back(std::unique_ptr<Tracker>(new LucasKanadeTracker(LucasKanadeTracker::Parameters())));
trackers.push_back(std::unique_ptr<Tracker>(new MeanshiftTracker(MeanshiftTracker::Parameters())));
// Load error writing file
errorFileName += trackers[currentTracker]->classname();
auto errorFile = std::ofstream();
if (bUseGroundTruth && bWriteErrorToFile) {
errorFile.open(videoPath.substr(0, secondToLastPos + 1) + errorFileName);
if (errorFile.fail()) {
std::cerr << "Error opening error file" << std::strerror(errno) << "\n";
bWriteErrorToFile = false;
}
}
auto t1 = std::chrono::high_resolution_clock::now();
std::cout << "Init: " << std::chrono::duration_cast<std::chrono::milliseconds>(t1 - t0).count() << " ms";
while (true) {
// Get next frame
vidCap >> frame;
// Restart video when it is over
if (frame.empty()) {
vidCap.set(cv::CAP_PROP_POS_FRAMES, 0);
vidCap >> frame;
trackers[currentTracker]->reset();
bRoiInitialized = false;
// Reset ground truth file
if (bUseGroundTruth) {
groundTruthFile.clear();
groundTruthFile.seekg(0, std::ios::beg);
// Stop recording errors
if (bWriteErrorToFile) {
bWriteErrorToFile = false;
}
}
}
// Mat for displaying in the window
display = frame.clone();
// Initialize roi
if (!bRoiInitialized) {
// Use ground truth roi if available
if (bUseGroundTruth) {
roi = lineToRect(groundTruthFile, 0);
std::cout << "\nStarting roi " << roi << "\n";
// To avoid 1 line difference
groundTruthFile.clear();
groundTruthFile.seekg(0, std::ios::beg);
} else {
roi = cv::selectROI(frame);
std::cout << "\nSelected roi " << roi << "\n";
if (roi.width == 0 || roi.height == 0) {
std::cerr << "No ROI selected\n Ignoring roi\n";
}
trackers[currentTracker]->reset();
}
bRoiInitialized = true;
}
auto t2 = std::chrono::high_resolution_clock::now();
// Use the current tracker to track the region of interest
trackers[currentTracker]->track(frame, roi);
// Display tracking points if its the Lucas Kanade tracker
if (dynamic_cast<LucasKanadeTracker *>(trackers[currentTracker].get())) {
dynamic_cast<LucasKanadeTracker *>(trackers[currentTracker].get())->display(display);
}
auto t3 = std::chrono::high_resolution_clock::now();
std::cout << "\r" << "Tracking: " << std::chrono::duration_cast<std::chrono::milliseconds>(t3 - t2).count()
<< " ms";
std::cout.flush();
// Show roi
cv::rectangle(display, roi, cv::Scalar(0, 255, 255));
// Compare roi with ground truth roi
if(bUseGroundTruth) {
auto groundTruthRoi = lineToRect(groundTruthFile, 0);
if (bWriteErrorToFile) {
errorFile << trackers[currentTracker]->evaluate(roi, groundTruthRoi) << "\n";
} /*else {
std::cout << trackers[currentTracker]->classname() << " "
<< trackers[currentTracker]->evaluate(roi, groundTruthRoi) << "\n";
}*/
// Show ground truth roi
cv::rectangle(display, groundTruthRoi, cv::Scalar(0, 0, 255));
}
// Display mat in window
cv::imshow(windowName, display);
// ~ 30 fps
int keyPressed = cv::waitKey(33);
if (keyPressed != -1) {
// Only the least-significant 16 bits contain the actual key code. The other bits contain modifier key states.
keyPressed &= 0xFFFF;
std::cout << "\nKey pressed: " << keyPressed << "\n";
// Quit the loop when the Esc key is pressed.
if (keyPressed == 27)
break;
// Space
if (keyPressed == 32) {
currentTracker = (currentTracker + 1) % static_cast<int>(trackers.size());
trackers[currentTracker]->reset();
}
// F
if (keyPressed == 102)
bRoiInitialized = false;
// G
// if (keyPressed == 103)
}
}
groundTruthFile.close();
errorFile.close();
return EXIT_SUCCESS;
}