forked from tengesser/motld
/
camExample.cpp
434 lines (394 loc) · 11.8 KB
/
camExample.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
/* Copyright (C) 2012 Christian Lutz, Thorsten Engesser
*
* This file is part of motld
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
This is a live demo invoking a webcam.
It makes use of OpenCV to capture the frames and highgui to display the output.
Use your mouse to draw bounding boxes for tracking.
There are some keys to customize which components are displayed:
D - dis/enable drawing of detections (green boxes)
P - dis/enable drawing of learned patches
T - dis/enable drawing of tracked points
L - dis/enable learning
S - save current classifier to CLASSIFIERFILENAME
O - load classifier from CLASSIFIERFILENAME (not implemented)
R - reset classifier (not implemented)
ESC - exit
*/
#include <stdlib.h>
#include <stdio.h>
#include <chrono>
#include <ctime>
#include <iostream>
#include <fstream>
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include "motld/MultiObjectTLD.h"
#define LOADCLASSIFIERATSTART 0
#define CLASSIFIERFILENAME "test.moctld"
//uncomment if you have a high resolution camera and want to speed up tracking
#define FORCE_RESIZING
#define RESOLUTION_X 320
#define RESOLUTION_Y 240
#define MOUSE_MODE_MARKER 0
#define MOUSE_MODE_ADD_BOX 1
#define MOUSE_MODE_IDLE 2
#define MOUSE_MODE_ADD_GATE 4
// #define _GLIBCXX_USE_CXX11_ABI 0
cv::Mat curImage;
bool ivQuit = false;
int ivWidth, ivHeight;
ObjectBox mouseBox = {0,0,0,0,0,boost::circular_buffer<CvPoint>(CB_LEN)};
//int gate[2][2];
cv::Point gate[2];
int mouseMode = MOUSE_MODE_IDLE;
int drawMode = 255;
bool learningEnabled = true, save = false, load = false, reset = false, cascadeDetect = false, drawPath = true, drawGateEnabled = false;
std::string cascadePath = "../haarcascades/haarcascade_frontalface_alt.xml";
std::clock_t c_start;
std::clock_t c_end;
std::clock_t c_start1;
std::clock_t c_end1;
std::clock_t c_start2;
std::clock_t c_end2;
std::clock_t c_start3;
std::clock_t c_end3;
// auto t_start;
// auto t_end;
int Ndetections = 0;
typedef struct DebugInfo
{
int NObjects;
int side0Cnt;
int side1Cnt;
} DebugInfo;
void Init(cv::VideoCapture& capture);
void* Run(cv::VideoCapture& capture);
void HandleInput(int interval = 1);
void MouseHandler(int event, int x, int y, int flags, void* param);
void BGR2RGB(Matrix& maRed, Matrix& maGreen, Matrix& maBlue);
void drawMouseBox();
void writeDebug(DebugInfo dbgInfo);
void drawGate();
int main(int argc, char *argv[])
{
cv::VideoCapture capture(0);
Init(capture);
Run(capture);
cv::destroyAllWindows();
return 0;
}
void Init(cv::VideoCapture& capture)
{
if(!capture.isOpened()){
std::cout << "error starting video capture" << std::endl;
exit(0);
}
//propose a resolution
capture.set(CV_CAP_PROP_FRAME_WIDTH, RESOLUTION_X);
capture.set(CV_CAP_PROP_FRAME_HEIGHT, RESOLUTION_Y);
//get the actual (supported) resolution
ivWidth = capture.get(CV_CAP_PROP_FRAME_WIDTH);
ivHeight = capture.get(CV_CAP_PROP_FRAME_HEIGHT);
std::cout << "camera/video resolution: " << ivWidth << "x" << ivHeight << std::endl;
cv::namedWindow("MOCTLD", 0); //CV_WINDOW_AUTOSIZE );
// cv::resizeWindow("MOCTLD", ivWidth, ivHeight);
cv::setMouseCallback("MOCTLD", MouseHandler);
}
void* Run(cv::VideoCapture& capture)
{
int size = ivWidth*ivHeight;
int count = 1;
DebugInfo dbgInfo;
cv::CascadeClassifier cascade;
std::vector<cv::Rect> detectedFaces;
std::vector<ObjectBox> trackBoxes;
ObjectBox detectBox;
// Initialize MultiObjectTLD
#if LOADCLASSIFIERATSTART
MultiObjectTLD p = MultiObjectTLD::loadClassifier((char*)CLASSIFIERFILENAME);
#else
MOTLDSettings settings(COLOR_MODE_RGB);
settings.useColor = false;
MultiObjectTLD p(ivWidth, ivHeight, settings);
#endif
if(cascadePath != "")
cascade.load( cascadePath );
Matrix maRed;
Matrix maGreen;
Matrix maBlue;
unsigned char img[size*3];
while(!ivQuit)
{
/*
if(reset){
p = *(new MultiObjectTLD(ivWidth, ivHeight, COLOR_MODE_RGB));
reset = false;
}
if(load){
p = MultiObjectTLD::loadClassifier(CLASSIFIERFILENAME);
load = false;
}
*/
#if TIMING
c_end = std::clock();
std::cout << "Total: " << (c_end-c_start) << std::endl;
c_start = std::clock();
#endif
// Grab an image
if(!capture.grab()){
std::cout << "error grabbing frame" << std::endl;
break;
}
cv::Mat frame;
capture.retrieve(frame);
frame.copyTo(curImage);
//BGR to RGB
// for(int j = 0; j<size; j++){
// img[j] = frame.at<cv::Vec3b>(j).val[2];
// img[j+size] = frame.at<cv::Vec3b>(j).val[1];
// img[j+2*size] = frame.at<cv::Vec3b>(j).val[0];
// }
#if TIMING
c_start1 = std::clock();
#endif
for(int i = 0; i < ivHeight; ++i){
for(int j = 0; j < ivWidth; ++j){
img[i*ivWidth+j] = curImage.at<cv::Vec3b>(i,j).val[2];
img[i*ivWidth+j+size] = curImage.at<cv::Vec3b>(i,j).val[1];
img[i*ivWidth+j+2*size] = curImage.at<cv::Vec3b>(i,j).val[0];
}
}
#if TIMING
c_end1 = std::clock();
std::cout << "time1: " << (c_end1-c_start1) << std::endl;
#endif
// for(int i = 0; i < ivHeight; ++i){
// for(int j = 0; j < ivWidth; ++j){
// curImage.at<cv::Vec3b>(i,j).val[2] = 0;
// curImage.at<cv::Vec3b>(i,j).val[1] = 0;
// curImage.at<cv::Vec3b>(i,j).val[0] = 0;
// }
// }
// cv::imshow("MOCTLD", curImage);
#if TIMING
c_start2 = std::clock();
#endif
// Process it with motld
p.processFrame(img);
#if TIMING
c_end2 = std::clock();
std::cout << "time2: " << (c_end2-c_start2) << std::endl;
#endif
// Add new box
if(mouseMode == MOUSE_MODE_ADD_BOX){
p.addObject(mouseBox);
mouseMode = MOUSE_MODE_IDLE;
}
if(mouseMode == MOUSE_MODE_ADD_GATE){
p.addGate(gate);
mouseMode = MOUSE_MODE_IDLE;
}
if(((count%20)==0) && cascadeDetect)
{
cascade.detectMultiScale( frame, detectedFaces,
1.1, 2, 0
//|CASCADE_FIND_BIGGEST_OBJECT
//|CASCADE_DO_ROUGH_SEARCH
|cv::CASCADE_SCALE_IMAGE,
cv::Size(30, 30) );
Ndetections = detectedFaces.size();
for( std::vector<cv::Rect>::const_iterator r = detectedFaces.begin(); r != detectedFaces.end(); r++ )
{
detectBox.x = r->x;
detectBox.y = r->y;
detectBox.width = r->width;
detectBox.height = r->height;
if(p.isNewObject(detectBox))
p.addObject(detectBox);
}
//printf("size detectedFaces: %i\n", detectedFaces.size());
}
count++;
// Display result
HandleInput();
p.getDebugImage(img, maRed, maGreen, maBlue, drawMode);
#if TIMING
c_start3 = std::clock();
#endif
BGR2RGB(maRed, maGreen, maBlue);
#if TIMING
c_end3 = std::clock();
std::cout << "time3: " << (c_end3-c_start3) << std::endl;
#endif
drawGate();
drawMouseBox();
dbgInfo.NObjects = p.getObjectTotal();
dbgInfo.side0Cnt = p.getSide0Cnt();
dbgInfo.side1Cnt = p.getSide1Cnt();
writeDebug(dbgInfo);
cv::imshow("MOCTLD", curImage);
p.enableLearning(learningEnabled);
if(save){
p.saveClassifier((char*)CLASSIFIERFILENAME);
save = false;
}
}
//delete[] img;
capture.release();
return 0;
}
void HandleInput(int interval)
{
int key = cvWaitKey(interval);
if(key >= 0)
{
switch (key)
{
case 'd': drawMode ^= DEBUG_DRAW_DETECTIONS; break;
case 't': drawMode ^= DEBUG_DRAW_CROSSES; break;
case 'p': drawMode ^= DEBUG_DRAW_PATCHES; break;
case 'h': drawMode ^= DEBUG_DRAW_PATH; break;
case 'c': cascadeDetect = !cascadeDetect; break;
case 'g': drawGateEnabled = true; break;
case 'l':
learningEnabled = !learningEnabled;
std::cout << "learning " << (learningEnabled? "en" : "dis") << "abled" << std::endl;
break;
case 'r': reset = true; break;
case 's': save = true; break;
case 'o': load = true; break;
case 27: ivQuit = true; break; //ESC
default:
//std::cout << "unhandled key-code: " << key << std::endl;
break;
}
}
}
void MouseHandler(int event, int x, int y, int flags, void* param)
{
if(drawGateEnabled)
{
switch(event){
case CV_EVENT_LBUTTONDOWN:
gate[0].x = x;
gate[0].y = y;
//gate[0][0] = x;
//gate[0][1] = y;
mouseMode = MOUSE_MODE_MARKER;
break;
case CV_EVENT_MOUSEMOVE:
if(mouseMode == MOUSE_MODE_MARKER){
gate[1].x = x;
gate[1].y = y;
//gate[1][0] = x;
//gate[1][1] = y;
}
break;
case CV_EVENT_LBUTTONUP:
if(mouseMode != MOUSE_MODE_MARKER)
break;
mouseMode = MOUSE_MODE_ADD_GATE;
break;
case CV_EVENT_RBUTTONDOWN:
mouseMode = MOUSE_MODE_IDLE;
break;
}
}
else
{
switch(event){
case CV_EVENT_LBUTTONDOWN:
mouseBox.x = x;
mouseBox.y = y;
mouseBox.width = mouseBox.height = 0;
mouseMode = MOUSE_MODE_MARKER;
break;
case CV_EVENT_MOUSEMOVE:
if(mouseMode == MOUSE_MODE_MARKER){
mouseBox.width = x - mouseBox.x;
mouseBox.height = y - mouseBox.y;
}
break;
case CV_EVENT_LBUTTONUP:
if(mouseMode != MOUSE_MODE_MARKER)
break;
if(mouseBox.width < 0){
mouseBox.x += mouseBox.width;
mouseBox.width *= -1;
}
if(mouseBox.height < 0){
mouseBox.y += mouseBox.height;
mouseBox.height *= -1;
}
if(mouseBox.width < 4 || mouseBox.height < 4){
std::cout << "bounding box too small!" << std::endl;
mouseMode = MOUSE_MODE_IDLE;
}else
mouseMode = MOUSE_MODE_ADD_BOX;
break;
case CV_EVENT_RBUTTONDOWN:
mouseMode = MOUSE_MODE_IDLE;
break;
}
}
}
void BGR2RGB(Matrix& maRed, Matrix& maGreen, Matrix& maBlue)
{
for(int i = 0; i < ivHeight; ++i){
for(int j = 0; j < ivWidth; ++j){
curImage.at<cv::Vec3b>(i,j).val[2] = maRed.data()[i*ivWidth+j];
curImage.at<cv::Vec3b>(i,j).val[1] = maGreen.data()[i*ivWidth+j];
curImage.at<cv::Vec3b>(i,j).val[0] = maBlue.data()[i*ivWidth+j];
}
}
//at this place you could save the images using
//cvSaveImage(filename, curImage);
}
void drawGate()
{
if(drawGateEnabled)
{
cv::line(curImage,gate[0],gate[1],cv::Scalar(0,0,255));
}
}
void writeDebug(DebugInfo dbgInfo)
{
std::ofstream dbgFile;
char strSide0[25];
char strSide1[25];
char strNObj[25];
cv::Point midPt;
sprintf(strSide0, "Side0: %i", dbgInfo.side0Cnt);
sprintf(strSide1, "Side1: %i", dbgInfo.side1Cnt);
sprintf(strNObj, "#objects: %i", dbgInfo.NObjects);
dbgFile.open ("dbg.dat",std::ios::ate);
dbgFile << dbgInfo.NObjects << ' ' << Ndetections << std::endl;
dbgFile.close();
cv::putText(curImage, strSide0, cv::Point(0, ivHeight/10), CV_FONT_HERSHEY_SIMPLEX, 0.5, 0);
cv::putText(curImage, strSide1, cv::Point(ivWidth-ivHeight/3, ivHeight/10), CV_FONT_HERSHEY_SIMPLEX, 0.5, 0);
cv::putText(curImage, strNObj, cv::Point(ivWidth-3*ivHeight/4, ivHeight-ivHeight/4), CV_FONT_HERSHEY_SIMPLEX, 0.5, 0);
}
void drawMouseBox()
{
if(mouseMode == MOUSE_MODE_MARKER)
{
cv::Point pt1; pt1.x = mouseBox.x; pt1.y = mouseBox.y;
cv::Point pt2; pt2.x = mouseBox.x + mouseBox.width; pt2.y = mouseBox.y + mouseBox.height;
cv::rectangle(curImage, pt1, pt2, CV_RGB(0,0,255));
}
}