-
Notifications
You must be signed in to change notification settings - Fork 0
/
fvsa.cpp
292 lines (237 loc) · 7.23 KB
/
fvsa.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
#include <opencv2/opencv.hpp>
#include "opencv2/highgui.hpp"
#include <iostream>
#include <vector>
#include <string>
#include "opencv2/videoio.hpp"
#include <opencv2/imgcodecs.hpp>
#include <opencv2/videoio/videoio.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/opencv_modules.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include <opencv2/opencv.hpp>
#include <opencv2/tracking.hpp>
#include <opencv2/core/ocl.hpp>
#include <ctime>
#include <time.h>
#include <cstdio>
#include <sys/stat.h>
#include <math.h>
#include <iomanip>
#include <chrono>
using namespace cv;
using namespace std;
#define VIDEO_FILE_NAME "fvsa.mp4"
#define CASCADE_FILE_NAME "cascade.xml"
#define thresholdpercentage 15
#define usedecision 1
#define numberofthresholdframes 30
#define splitdisplay 0
#define biggest_bbox 0
//=======================================================variable declaration=================================================
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
double initialwidth, thresholdwidth = 0;
VideoCapture cap;
Mat mFrame, mGray, imageROI, carROI;
CascadeClassifier cars;
vector<Rect> cars_found;
Mat frame;
bool isGetInitialWidth = true;
bool isStart = false;
bool isUsingDetector = true;
int thresholdcount = 0;
Ptr<Tracker> tracker;
vector<Rect2d> v2;
Rect detected_vehicle;
int y_carROI;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//==========================================================================n=================================================
//=======================================================functions declaration=================================================
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void getVideoData()
{
cap.open(VIDEO_FILE_NAME);
}
void loadDetector()
{
cars.load(CASCADE_FILE_NAME);
}
void framePreprocessing()
{
resize(mFrame, mFrame, Size(704, 480));
imageROI = mFrame(Rect(mFrame.cols*0.25, mFrame.rows * 0, mFrame.cols*0.5, mFrame.rows));
cvtColor(imageROI, mGray, COLOR_BGR2GRAY);
}
bool compare_rect(const cv::Rect & a, const cv::Rect &b)
{
return a.width > b.width;
}
double getDistance(Point a, Point b)
{
double x_diff = a.x - b.x;
double y_diff = a.y - b.y;
return sqrt(x_diff*x_diff + y_diff*y_diff);
}
int get_index_bbox_centre(Mat ROI, vector<Rect> bbox)
{
int index;
Point ROI_centre;
Point bbox_temp_centre;
double min_distance = 10000;
ROI_centre.x = 0 + ROI.cols / 2;
ROI_centre.y = 0 + ROI.rows / 2;
for (int i = 0; i < bbox.size(); i++)
{
bbox_temp_centre.x = bbox[i].x + bbox[i].width / 2;
bbox_temp_centre.y = bbox[i].y + bbox[i].height / 2;
double dist = getDistance(ROI_centre, bbox_temp_centre);
if (dist < min_distance)
{
min_distance = dist;
index = i;
}
}
return index;
}
void detectVehicle()
{
cars.detectMultiScale(mGray, cars_found, 1.05, 3, 0 | 2, Size(60, 60));
cout << cars_found.size() << endl;
if (cars_found.size() > 0)
{
isUsingDetector = false;
if (biggest_bbox == 1)
{
sort(cars_found.begin(), cars_found.end(), compare_rect);
detected_vehicle = cars_found[0];
}
else
{
int index = get_index_bbox_centre(mGray, cars_found);
detected_vehicle = cars_found[index];
}
//make new ROI for tracking car
if (detected_vehicle.y - 50 >= 0)
y_carROI = detected_vehicle.y - 50;
else
y_carROI = 0;
mGray(Rect(0, y_carROI, mGray.cols, detected_vehicle.height + (detected_vehicle.y - y_carROI))).copyTo(carROI);
cout << "detector box: " << detected_vehicle << " ";
cv::rectangle(mGray, detected_vehicle, Scalar(0, 0, 225));
v2.push_back(Rect(detected_vehicle.x, (detected_vehicle.y - y_carROI), detected_vehicle.width, detected_vehicle.height));
tracker->init(carROI, v2[0]);
}
}
void trackVehicle()
{
mGray(Rect(0, y_carROI, mGray.cols, detected_vehicle.height + (detected_vehicle.y - y_carROI))).copyTo(carROI);
cv::rectangle(mGray, detected_vehicle, Scalar(0, 0, 225)); //draw the detector box
bool ok = tracker->update(carROI, v2[0]);
if (ok)
{
// Tracking success : Draw the tracked object
cout << "tracker box: " << v2[0] << " ";
rectangle(carROI, v2[0], Scalar(255, 0, 0), 2, 1);
rectangle(mGray, Rect(v2[0].x, v2[0].y + y_carROI, v2[0].width, v2[0].height), Scalar(255, 0, 0), 2, 1);
#if usedecision //=================================================================================================================
if (isGetInitialWidth == true)
{
initialwidth = v2[0].width;
thresholdwidth = initialwidth*(100 - thresholdpercentage) / 100;
isGetInitialWidth = false;
}
//frame by frame checking whether tracked vehicle has moved away from the camera; indicated by bbox size reduced by thresholdvalue
else
{
if (v2[0].width <= thresholdwidth)
{
thresholdcount++;
if (thresholdcount > numberofthresholdframes)
{
putText(mGray, "!!!Warning!!! ", Point(100, 80), FONT_HERSHEY_SIMPLEX, 0.75, Scalar(0, 0, 255), 2);
putText(carROI, "!!!Warning!!! ", Point(100, 80), FONT_HERSHEY_SIMPLEX, 0.75, Scalar(0, 0, 255), 2);
}
}
}
#endif//===========================================================================================================================
}
else
{
// Tracking failure detected.
putText(mGray, "Tracking failure detected", Point(100, 80), FONT_HERSHEY_SIMPLEX, 0.75, Scalar(0, 0, 255), 2);
putText(carROI, "Tracking failure detected", Point(100, 80), FONT_HERSHEY_SIMPLEX, 0.75, Scalar(0, 0, 255), 2);
}
}
void StartFVSA()
{
// reset all flags to start FVSA
isStart = true;
isUsingDetector = true;
isGetInitialWidth = true;
thresholdcount = 0;
tracker = TrackerCSRT::create();
}
void EndFVSA()
{
isStart = false;
if (v2.empty() == false)
v2.pop_back();
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//============================================================================================================================
int main()
{
int framecount = 0;
loadDetector();
getVideoData();
while (cap.read(mFrame))
{
auto start_time = std::chrono::high_resolution_clock::now();
framePreprocessing();
if (isStart == true)
{
if (isUsingDetector == true)
{
detectVehicle();
}
else if (isUsingDetector == false)
{
trackVehicle();
}
}
framecount++;
auto end_time = std::chrono::high_resolution_clock::now();
auto time = end_time - start_time;
std::cout << "Time: " <<
time / std::chrono::milliseconds(1) << "ms.\n";
if (isUsingDetector == true)
cv::imshow("detection", mGray);
else
{
cv::imshow("detection", mGray);
cv::imshow("tracking", carROI);
}
int c = waitKey(1);
if ((char)c == 27)
{ break; }
else if ((char)c == 'q') //starts FVSA
{
if (isStart==false) // check if it is already starting or not
{
StartFVSA();
}
}
else if ((char)c == 'w') //ends FVSA
{
if (isStart==true) // check if it is already starting or not
{
EndFVSA();
}
}
}
if (v2.empty() == false)
v2.pop_back();
return 0;
}