-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEngine.cpp
More file actions
133 lines (94 loc) · 3.52 KB
/
Engine.cpp
File metadata and controls
133 lines (94 loc) · 3.52 KB
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
#include "Engine.h"
void Engine::initCapture(std::string file_name) {
cap.open(file_name);
if (!cap.isOpened()) {
return;
}
flow.init();
frame_count = 0;
resetTrackers();
namedWindow(rawWindow, CV_WINDOW_AUTOSIZE);
}
void Engine::analyzeDetection(int det_i, Point2f &curr_center) {
if (!test_tracker[det_i].isLost()) {
float score_ratio = test_tracker[det_i].getScore().y / abs(test_tracker[det_i].getScore().x);
cout << det_i << " score: " << test_tracker[det_i].getScore().x << " " << test_tracker[det_i].getScore().y << " " << score_ratio << endl;
//first, check final position vector, otherwice check scores
if (initial_tracker_positions[det_i].x > 0.6*rgbFrames.cols &&
initial_tracker_positions[det_i].x < test_tracker[det_i].getDetCenter().x) {}
else if (score_ratio > -0.15 && score_ratio < 0.15 && abs(test_tracker[det_i].getScore().x) > 30) {
cout << endl << "SUCCES!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
success_detection = true;
}
}
if (curr_center.x < 0.2*rgbFrames.cols || curr_center.x > 0.85*rgbFrames.cols ||
curr_center.y < 0.2*rgbFrames.rows || curr_center.y > 0.85*rgbFrames.rows) {
test_tracker[det_i].deactivate();
} else {
test_tracker[det_i].init(rgbFrames, curr_center);
}
}
void Engine::update() {
frame_count++;
//analyze each 3 frame because of high video framerate
cap >> rgbFrames;
cap >> rgbFrames;
cap >> rgbFrames;
//calc flow points and filter it
flow.calcFlow(rgbFrames);
flow.filterResults();
vector <Point2f> &filtered_points = flow.getResult();
//clasterize results with using k-means
seg.makeSegmentation(filtered_points);
auto_ptr<Mat> centers = seg.getCenters();
map<int, int > points_count = seg.getPointsCount();
if (centers.get() == NULL) {
return;
}
if (frame_count % tact_lenght == 0) {
resetTrackers();
}
//init meanshift
if (init_tracker) {
success_detection = false;
cout << "Tracker scores:" << endl;
for (int i = 0; i < TRACKERS_N; ++i) {
Point2f curr_center(centers->at<float>(i, 0), centers->at<float>(i, 1));
analyzeDetection(i, curr_center);
initial_tracker_positions[i] = curr_center;
}
init_tracker = false;
}
//update trackers
for (int i = 0; i < TRACKERS_N; ++i) {
test_tracker[i].update(rgbFrames);
}
if (success_detection) {
line(rgbFrames, Point(0, rgbFrames.rows - 5), Point(rgbFrames.cols - 5, rgbFrames.rows - 5),
Scalar(0, 255, 255), 10);
}
for (int i = 0; i < centers->rows; i++) {
circle(rgbFrames, Point(centers->at<float>(i, 0), centers->at<float>(i, 1)), 10, Scalar(10, 100, 230), 2);
}
}
void Engine::draw() {
//auto_ptr<Mat> labels = seg.getLabels();
//for (int i = 0; i < filtered_points.size(); i++) {
// circle(rgbFrames, filtered_points[i], 7, Scalar(labels->at<int>(i, 0) * 20, labels->at<int>(i, 0) * 10, 0), 10, 1,
// 0);
//}
line(rgbFrames, Point(0, 0.2 * rgbFrames.rows), Point(rgbFrames.cols, 0.2 * rgbFrames.rows), Scalar(255, 0, 0), 5);
line(rgbFrames, Point(0, rgbFrames.rows - 0.2 * rgbFrames.rows), Point(rgbFrames.cols, rgbFrames.rows - 0.2 * rgbFrames.rows), Scalar(255, 0, 0), 5);
line(rgbFrames, Point(0.2*rgbFrames.cols, 0), Point(0.2*rgbFrames.cols, rgbFrames.rows), Scalar(255, 0, 0), 5);
line(rgbFrames, Point(0.85*rgbFrames.cols, 0), Point(0.85*rgbFrames.cols, rgbFrames.rows), Scalar(255, 0, 0), 5);
cv::imshow(rawWindow, rgbFrames);
}
void Engine::drawTrackers() {
}
void Engine::resetTrackers() {
init_tracker = true;
for (int i = 0; i < TRACKERS_N; ++i) {
test_tracker[i].setId(i);
}
cout << "LOG: Trackers reseted" << endl;
}