-
Notifications
You must be signed in to change notification settings - Fork 0
/
labeler.cpp
520 lines (480 loc) · 16.8 KB
/
labeler.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
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
#include <stdlib.h>
#include <sys/time.h>
#include <csignal>
#include <cstdint>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <boost/program_options.hpp>
#include <opencv2/opencv.hpp>
namespace po = boost::program_options;
// Constants.
constexpr int HEIGHT = 480;
constexpr int WARNING_OVERLAY_DURATION = 5000;
constexpr int FRAMESKIP_MAX = 900;
constexpr int SEEK_INTERVAL_MAX = 900;
// Global variables. Why, Thomas?!?!?!
std::ofstream os;
std::vector<std::pair<int64_t, int64_t>> uncertain_intervals;
std::pair<int64_t, int64_t> cur_uncertain_interval;
std::vector<std::pair<int64_t, int64_t>> event_intervals;
std::pair<int64_t, int64_t> cur_event_interval;
std::unordered_map<int64_t, std::vector<std::string>> labels;
int64_t target_frame_number = 0;
int frameskip = 0;
int seek_interval = 30;
double prev_microseconds = 0;
int64_t prev_frame_number = 0;
int did_seek = 0;
void display_status_text(int64_t frame_id) {
std::string status_bar_text = "";
if (cur_uncertain_interval.first >= 0) {
status_bar_text +=
"Uncertain tag start: " + std::to_string(cur_uncertain_interval.first) +
".";
}
if (cur_event_interval.first >= 0) {
status_bar_text +=
" Event tag start: " + std::to_string(cur_event_interval.first) + ".";
}
if (status_bar_text == "") {
status_bar_text = "No tags open";
}
auto labels_for_frame = labels[frame_id];
for (decltype(labels_for_frame.size()) i = 0; i < labels_for_frame.size();
++i) {
status_bar_text += " " + labels_for_frame.at(i);
}
cv::displayStatusBar("video", status_bar_text, 0);
}
void delete_cur_uncertain(int state, void* data) {
(void)state;
int64_t frame_number = *(int64_t*)data;
if (cur_uncertain_interval.first == -1) {
cv::displayOverlay("video",
"Unable to delete start uncertain event if current "
"uncertain event is closed.",
WARNING_OVERLAY_DURATION);
} else {
labels[cur_uncertain_interval.first].pop_back();
cur_uncertain_interval.first = -1;
}
display_status_text(frame_number);
}
void delete_cur_event(int state, void* data) {
(void)state;
int64_t frame_number = *(int64_t*)data;
if (cur_uncertain_interval.first == -1) {
cv::displayOverlay(
"video", "Unable to delete start event if current event is closed.",
WARNING_OVERLAY_DURATION);
} else {
labels[cur_event_interval.first].pop_back();
cur_event_interval.first = -1;
}
display_status_text(frame_number);
}
void uncertain_interval_start(int state, void* data) {
(void)state;
int64_t frame_number = *(int64_t*)data;
// Do verification that we're not in a bad state
if (cur_uncertain_interval.first >= 0) {
cv::displayOverlay("video",
"Please close the existing uncertain interval before "
"starting a new one",
WARNING_OVERLAY_DURATION);
} else {
cur_uncertain_interval.first = frame_number;
labels[frame_number].push_back("Uncertain interval #" +
std::to_string(uncertain_intervals.size()) +
" start.");
}
display_status_text(frame_number);
}
void uncertain_interval_end(int state, void* data) {
(void)state;
int64_t frame_number = *(int64_t*)data;
if (cur_uncertain_interval.first < 0) {
cv::displayOverlay("video",
"Cannot end uncertain interval: start not specified",
WARNING_OVERLAY_DURATION);
} else {
cur_uncertain_interval.second = frame_number;
uncertain_intervals.push_back(cur_uncertain_interval);
labels[frame_number].push_back(
"Uncertain interval #" +
std::to_string(uncertain_intervals.size() - 1) + " end.");
cur_uncertain_interval.first = -1;
cur_uncertain_interval.second = -1;
}
display_status_text(frame_number);
}
void event_interval_start(int state, void* data) {
(void)state;
int64_t frame_number = *(int64_t*)data;
// Do verification that we're not in a bad state
if (cur_event_interval.first >= 0) {
cv::displayOverlay(
"video",
"Please close the existing event interval before starting a new one",
WARNING_OVERLAY_DURATION);
} else {
cur_event_interval.first = frame_number;
labels[frame_number].push_back("Event interval #" +
std::to_string(uncertain_intervals.size()) +
" start.");
}
display_status_text(frame_number);
}
void event_interval_end(int state, void* data) {
(void)state;
int64_t frame_number = *(int64_t*)data;
if (cur_event_interval.first < 0) {
cv::displayOverlay("video",
"Cannot end event interval: start not specified",
WARNING_OVERLAY_DURATION);
} else {
cur_event_interval.second = frame_number;
event_intervals.push_back(cur_event_interval);
labels[frame_number].push_back("Event interval #" +
std::to_string(event_intervals.size() - 1) +
" end.");
cur_event_interval.first = -1;
cur_event_interval.second = -1;
}
display_status_text(frame_number);
}
// TODO: this loop should probably be backwards for efficiency
void goto_prev_event(int state, void* data) {
(void)state;
int64_t frame_number = *(int64_t*)data;
int64_t min_dist = INT64_MAX;
if (event_intervals.size() == 0) {
cv::displayOverlay("video", "No events.", WARNING_OVERLAY_DURATION);
return;
}
for (decltype(event_intervals.size()) i = 0; i < event_intervals.size();
++i) {
if (frame_number < event_intervals.at(i).first) {
continue;
}
if (frame_number - event_intervals.at(i).first < min_dist) {
target_frame_number = event_intervals.at(i).first;
}
}
if (min_dist == INT64_MAX) {
cv::displayOverlay("video", "You are at the first event.",
WARNING_OVERLAY_DURATION);
}
}
void goto_prev_uncertainty(int state, void* data) {
(void)state;
int64_t frame_number = *(int64_t*)data;
int64_t min_dist = INT64_MAX;
if (uncertain_intervals.size() == 0) {
cv::displayOverlay("video", "No uncertainties.", WARNING_OVERLAY_DURATION);
return;
}
for (decltype(uncertain_intervals.size()) i = 0;
i < uncertain_intervals.size(); ++i) {
if (frame_number < uncertain_intervals.at(i).first) {
continue;
}
if (frame_number - uncertain_intervals.at(i).first < min_dist) {
target_frame_number = uncertain_intervals.at(i).first;
}
}
if (min_dist == INT64_MAX) {
cv::displayOverlay("video", "You are at the first uncertainty.",
WARNING_OVERLAY_DURATION);
}
}
void goto_next_event(int state, void* data) {
(void)state;
int64_t frame_number = *(int64_t*)data;
int64_t min_dist = INT64_MAX;
if (event_intervals.size() == 0) {
cv::displayOverlay("video", "No events.", WARNING_OVERLAY_DURATION);
return;
}
for (decltype(event_intervals.size()) i = 0; i < event_intervals.size();
++i) {
if (frame_number > event_intervals.at(i).first) {
continue;
}
if (event_intervals.at(i).first - frame_number < min_dist) {
target_frame_number = event_intervals.at(i).first;
}
}
if (min_dist == INT64_MAX) {
cv::displayOverlay("video", "You are at the last event.",
WARNING_OVERLAY_DURATION);
}
}
void goto_next_uncertainty(int state, void* data) {
(void)state;
int64_t frame_number = *(int64_t*)data;
int64_t min_dist = INT64_MAX;
if (uncertain_intervals.size() == 0) {
cv::displayOverlay("video", "No uncertainties.", WARNING_OVERLAY_DURATION);
return;
}
for (decltype(uncertain_intervals.size()) i = 0;
i < uncertain_intervals.size(); ++i) {
if (frame_number > uncertain_intervals.at(i).first) {
continue;
}
if (uncertain_intervals.at(i).first - frame_number < min_dist) {
target_frame_number = uncertain_intervals.at(i).first;
}
}
if (min_dist == INT64_MAX) {
cv::displayOverlay("video", "You are at the last uncertainty.",
WARNING_OVERLAY_DURATION);
}
}
void play_button_callback(int state, void* data) {
(void)state;
(void)data;
target_frame_number = INT64_MAX;
}
void pause_button_callback(int state, void* data) {
(void)state;
int64_t frame_number = *(int64_t*)data;
target_frame_number = frame_number;
}
void seek_forward(int state, void* data) {
(void)state;
int64_t frame_number = *(int64_t*)data;
target_frame_number = frame_number + seek_interval;
did_seek = 1;
}
void seek_backward(int state, void* data) {
(void)state;
int64_t frame_number = *(int64_t*)data;
target_frame_number = frame_number - seek_interval;
}
// TODO: merge the lists
void print_events(std::ostream& stream) {
for (decltype(event_intervals.size()) i = 0; i < event_intervals.size();
i++) {
stream << "(" << event_intervals.at(i).first << ", "
<< event_intervals.at(i).second << ") - Event " << i << std::endl;
}
for (decltype(uncertain_intervals.size()) i = 0;
i < uncertain_intervals.size(); i++) {
stream << "(" << uncertain_intervals.at(i).first << ", "
<< uncertain_intervals.at(i).second << ") - Uncertain" << std::endl;
}
}
// Gracefully handle termination
void handler(int signal) {
(void)signal;
print_events(std::cout);
}
// Calculate fps
double get_fps(int64_t frame_number) {
int64_t elapsed_frames = frame_number - prev_frame_number;
if (elapsed_frames < 0) {
elapsed_frames *= -1;
}
prev_frame_number = frame_number;
struct timeval time;
gettimeofday(&time, NULL);
double cur_microseconds =
double(time.tv_sec * 1000000) + double(time.tv_usec);
double elapsed = cur_microseconds - prev_microseconds;
prev_microseconds = cur_microseconds;
return double(elapsed_frames) / elapsed * 1000000;
}
void print_controls() {
std::cout << "Commands" << std::endl;
std::cout << "Spacebar - play really fast" << std::endl;
std::cout << "p - pause immediately" << std::endl;
std::cout << "h - step back 1 frame" << std::endl;
std::cout << "l - step forward 1 frame" << std::endl;
std::cout << "n - fast forward K frames" << std::endl;
std::cout << "b - fast backward N frames" << std::endl;
std::cout << "a - mark the current frame as the start of a partial event"
<< std::endl;
std::cout << "s - mark the current frame as the start of a full event"
<< std::endl;
std::cout << "d - mark the current frame as the end of a full event"
<< std::endl;
std::cout << "f - mark the current frame as the end of a partial event"
<< std::endl;
std::cout << "q - quit and output all events" << std::endl;
}
cv::Mat preprocess_frame(cv::Mat frame, int64_t frame_number) {
cv::Mat modified_frame;
// 1. Do a resize to the target resolution
double scale_factor = double(HEIGHT) / frame.rows;
cv::resize(frame, modified_frame, cv::Size(0, 0), scale_factor, scale_factor,
cv::INTER_LANCZOS4);
// Font-related constants
double font_size = 1;
cv::Scalar font_color(200, 200, 250);
int thickness = 1;
// 2. Write fps
cv::Point fps_point(2, 40);
double fps = get_fps(frame_number);
std::stringstream fps_string;
fps_string << fps << " fps";
cv::putText(modified_frame, fps_string.str(), fps_point,
CV_FONT_HERSHEY_SIMPLEX, font_size, font_color, thickness, CV_AA);
// 2. Write frame number
cv::Point frame_number_point(2, 80);
std::stringstream frame_number_string;
frame_number_string << "Frame " << frame_number;
cv::putText(modified_frame, frame_number_string.str(), frame_number_point,
CV_FONT_HERSHEY_SIMPLEX, font_size, font_color, thickness, CV_AA);
return modified_frame;
}
int main(int argc, char* argv[]) {
po::options_description desc("Labels event intervals.");
desc.add_options()("help,h", "Print the help message.");
desc.add_options()("video,v", po::value<std::string>()->required(),
"Path to the video to label.");
desc.add_options()("start-frame,s", po::value<int>()->default_value(0),
"Frame at which to start labeling.");
desc.add_options()("out,o", po::value<std::string>()->required(),
"Path to output file.");
// Parse the command line arguments.
po::variables_map args;
try {
po::store(po::parse_command_line(argc, argv, desc), args);
if (args.count("help")) {
std::cout << desc << std::endl;
return 0;
}
po::notify(args);
} catch (const po::error& e) {
std::cerr << e.what() << std::endl;
std::cout << desc << std::endl;
exit(EXIT_FAILURE);
}
auto in_filepath = args["video"].as<std::string>();
auto start_frame = args["start-frame"].as<int>();
if (start_frame < 0) {
std::cerr << "\"--start-frame\" cannot be negative, but is: "
<< std::to_string(start_frame) << std::endl;
return 1;
}
auto out_filepath = args["out"].as<std::string>();
print_controls();
std::signal(SIGINT, handler);
// Parse arguments.
cv::VideoCapture vc(in_filepath);
int64_t frame_id = (int64_t)start_frame;
vc.set(cv::CAP_PROP_POS_FRAMES, frame_id);
os = std::ofstream(out_filepath);
cv::namedWindow("video", cv::WINDOW_AUTOSIZE | CV_GUI_EXPANDED);
cv::createButton("uncertainty start (a)", uncertain_interval_start,
&frame_id);
cv::createButton("event start (s)", event_interval_start, &frame_id);
cv::createButton("event end (d)", event_interval_end, &frame_id);
cv::createButton("uncertainty end (f)", uncertain_interval_end, &frame_id);
cv::createTrackbar("Skip this many frames per 30 played", nullptr, &frameskip,
FRAMESKIP_MAX, nullptr);
cv::createButton("Delete uncertainty start", delete_cur_uncertain, &frame_id);
cv::createButton("Delete event start", delete_cur_event, &frame_id);
cv::createTrackbar("Seek interval", nullptr, &seek_interval,
SEEK_INTERVAL_MAX, nullptr);
cv::createButton("Play", play_button_callback);
cv::createButton("Pause", pause_button_callback, &frame_id);
cv::createButton("Seek forward", seek_forward, &frame_id);
cv::createButton("Seek backward", seek_backward, &frame_id);
int dummy = 0;
cv::createTrackbar("Separator", nullptr, &dummy, 1, nullptr);
cv::createButton("Go to previous event", goto_prev_event, &frame_id);
cv::createButton("Go to previous uncertainty", goto_prev_uncertainty,
&frame_id);
cv::createButton("Go to next event", goto_next_event, &frame_id);
cv::createButton("Go to next uncertainty", goto_next_uncertainty, &frame_id);
cv::Mat frame;
double frames_to_skip = 0;
target_frame_number = frame_id;
cur_uncertain_interval.first = -1;
cur_uncertain_interval.second = -1;
cur_event_interval.first = -1;
cur_event_interval.second = -1;
while (true) {
display_status_text(frame_id);
if (frame_id < target_frame_number) {
if (frameskip == 0 && did_seek == 0) {
vc >> frame;
frame_id += 1;
if (frame.empty()) {
print_events(os);
print_events(std::cout);
}
} else {
did_seek = 0;
frame_id = target_frame_number;
vc.set(cv::CAP_PROP_POS_FRAMES, frame_id);
vc >> frame;
if (frame.empty()) {
print_events(os);
print_events(std::cout);
}
}
} else if (frame_id > target_frame_number) {
frame_id = target_frame_number;
vc.set(cv::CAP_PROP_POS_FRAMES, frame_id);
vc >> frame;
if (frame.empty()) {
print_events(os);
print_events(std::cout);
}
} else {
if (frame.empty()) {
vc >> frame;
if (frame.empty()) {
print_events(os);
print_events(std::cout);
}
}
}
if (frame.empty()) {
target_frame_number = frame_id - 1;
}
cv::imshow("video", preprocess_frame(frame, frame_id));
auto command = cv::waitKey(1);
if (command == -1) {
// continue
} else if (command == 'p') {
target_frame_number = frame_id;
} else if (command == 'h') {
target_frame_number -= 1;
} else if (command == 'l') {
target_frame_number += 1;
} else if (command == 'n') {
seek_forward(-1, &frame_id);
} else if (command == 'b') {
seek_backward(-1, &frame_id);
} else if (command == 'q') {
break;
} else if (command == 's') {
event_interval_start(-1, &frame_id);
} else if (command == 'd') {
event_interval_end(-1, &frame_id);
} else if (command == 'a') {
uncertain_interval_start(-1, &frame_id);
} else if (command == 'f') {
uncertain_interval_end(-1, &frame_id);
} else if (command == ' ') {
if (target_frame_number == INT64_MAX) {
pause_button_callback(-1, &frame_id);
} else {
play_button_callback(-1, nullptr);
}
} else {
// do nothing
}
}
print_events(os);
print_events(std::cout);
return 0;
}