-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathObjectTracker.cpp
173 lines (151 loc) · 5.35 KB
/
ObjectTracker.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
#include "ObjectTracker.hpp"
#include "PointTracker.hpp"
#include <time.h>
ObjectTracker::ObjectTracker(unsigned int distanceThreshold,
double contour_threshold, cv::Rect interest_area) {
this->distance_threshold = distanceThreshold;
this->object_locator = new ObjectLocator(contour_threshold);
this->object_counter = new ObjectCounter(interest_area);
}
void ObjectTracker::IterateTracker(cv::Mat &frame, cv::Mat &fore) {
this->CreateNewTracker();
this->AddRectanglesToTracker(frame, fore);
this->AccountNewObjects(frame);
this->RenewTrackers();
}
void ObjectTracker::SetInterestArea(cv::Rect interest_area) {
this->object_counter->SetReferenceBox(interest_area);
}
void ObjectTracker::CreateNewTracker() {
if (this->old_tracker) {
this->new_tracker = new RectangleTracker(this->distance_threshold,
*(this->old_tracker));
}
else {
this->new_tracker = new RectangleTracker(this->distance_threshold);
}
}
void ObjectTracker::AddRectanglesToTracker(cv::Mat &frame, cv::Mat &fore) {
auto rects_from_countour = object_locator->DetectObjects(fore);
for (auto rec: rects_from_countour) {
this->new_tracker->AddRectangle(rec);
cv::rectangle(frame, rec, cv::Scalar(0, 0, 255), 1);
}
}
void ObjectTracker::AddDate(cv::Mat &frame) {
time_t time_now = time(nullptr);
char buff[50];
int yPos = 15;
strftime(buff, sizeof(buff), "%d/%m/%Y", localtime(&time_now));
cv::putText(frame, std::string(buff), cv::Point(2, yPos),
CV_FONT_HERSHEY_PLAIN, 1, cv::Scalar(255, 255, 255), 2, CV_AA);
cv::putText(frame, std::string(buff), cv::Point(2, yPos),
CV_FONT_HERSHEY_PLAIN, 1, cv::Scalar(0, 0, 0), 1, CV_AA);
strftime(buff, sizeof(buff), "%X", localtime(&time_now));
cv::Size sz = cv::getTextSize(std::string(buff), CV_FONT_HERSHEY_PLAIN, 1,
2, nullptr);
cv::putText(frame,
std::string(buff),
cv::Point(frame.size().width - sz.width - 2, yPos),
CV_FONT_HERSHEY_PLAIN,
1,
cv::Scalar(255, 255, 255),
2,
CV_AA);
cv::putText(frame,
std::string(buff),
cv::Point(frame.size().width - sz.width - 2, yPos),
CV_FONT_HERSHEY_PLAIN,
1,
cv::Scalar(0, 0, 0),
1,
CV_AA);
}
void ObjectTracker::PersistImage(cv::Mat &frame) {
char date[50];
time_t time_now = time(nullptr);
strftime(date, sizeof(date), "%Y%m%d", localtime(&time_now));
char fileName[20];
sprintf(fileName, "tmp/bike%s%d.jpg", date, this->image_counter++);
cv::imwrite( fileName, frame );
}
void ObjectTracker::AccountNewObjects(cv::Mat &frame) {
if (this->old_tracker && this->new_tracker) {
for(auto trackedRect: this->new_tracker->points) {
auto rectCenter = trackedRect.second;
if(object_counter->AccountPoint(rectCenter)) {
this->PersistImage(frame);
}
char id[11];
sprintf(id, "%02d", rectCenter.id);
cv::putText(frame,
std::string(id),
rectCenter.pt,
CV_FONT_HERSHEY_PLAIN,
1,
cv::Scalar(0, 255, 0));
}
delete old_tracker;
old_tracker = nullptr;
}
}
void ObjectTracker::RenewTrackers() {
if (new_tracker) {
old_tracker = new_tracker;
}
}
void ObjectTracker::PrintCounters(cv::Mat &frame) {
char id[20];
sprintf(id, "Total: %02d", object_counter->GetTotalPoints());
cv::putText(frame, std::string(id), cv::Point(2, 30),
CV_FONT_HERSHEY_PLAIN, 1, cv::Scalar(255, 255, 255), 2, CV_AA);
cv::putText(frame, std::string(id), cv::Point(2, 30),
CV_FONT_HERSHEY_PLAIN, 1, cv::Scalar(0, 0, 0), 1, CV_AA);
sprintf(id, ">%02d", object_counter->GetLTRPoints());
cv::putText(frame, std::string(id), cv::Point(2, 60),
CV_FONT_HERSHEY_PLAIN, 1, cv::Scalar(255, 255, 255), 2, CV_AA);
cv::putText(frame, std::string(id), cv::Point(2, 60),
CV_FONT_HERSHEY_PLAIN, 1, cv::Scalar(0, 0, 0), 1, CV_AA);
sprintf(id, "<%02d", object_counter->GetRTLPoints());
cv::putText(frame, std::string(id), cv::Point(2, 45),
CV_FONT_HERSHEY_PLAIN, 1, cv::Scalar(255, 255, 255), 2, CV_AA);
cv::putText(frame, std::string(id), cv::Point(2, 45),
CV_FONT_HERSHEY_PLAIN, 1, cv::Scalar(0, 0, 0), 1, CV_AA);
}
void ObjectTracker::PrintTotal(cv::Mat &frame) {
char id[20];
cv::Point pt(10, 50);
sprintf(id, "Total: %02d", object_counter->GetTotalPoints());
cv::putText(frame, std::string(id), pt, CV_FONT_HERSHEY_PLAIN, 3,
cv::Scalar(0, 0, 0), 5, CV_AA);
cv::putText(frame, std::string(id), pt, CV_FONT_HERSHEY_PLAIN, 3,
cv::Scalar(0, 255, 255), 3, CV_AA);
}
void ObjectTracker::PrintRightPartial(cv::Mat &frame, cv::Point pt) {
char id[10];
sprintf(id, ">%02d", object_counter->GetLTRPoints());
cv::putText(frame, std::string(id), pt, CV_FONT_HERSHEY_PLAIN, 3,
cv::Scalar(0, 0, 0), 5, CV_AA);
cv::putText(frame, std::string(id), pt, CV_FONT_HERSHEY_PLAIN, 3,
cv::Scalar(0, 255, 255), 3, CV_AA);
}
void ObjectTracker::PrintLeftPartial(cv::Mat &frame, cv::Point pt) {
char id[10];
sprintf(id, "<%02d", object_counter->GetRTLPoints());
cv::putText(frame, std::string(id), pt, CV_FONT_HERSHEY_PLAIN, 3,
cv::Scalar(0, 0, 0), 5, CV_AA);
cv::putText(frame, std::string(id), pt, CV_FONT_HERSHEY_PLAIN, 3,
cv::Scalar(0, 255, 255), 3, CV_AA);
}
unsigned int ObjectTracker::GetTotal() {
return object_counter->GetTotalPoints();
}
unsigned int ObjectTracker::GetRightPartial() {
return object_counter->GetLTRPoints();
}
unsigned int ObjectTracker::GetLeftPartial() {
return object_counter->GetRTLPoints();
}
void ObjectTracker::ZeroCounters() {
object_counter->ZeroCounters();
}