forked from opengazer/OpenGazer
-
Notifications
You must be signed in to change notification settings - Fork 3
/
opengazerHighGui.cpp
138 lines (114 loc) · 3.49 KB
/
opengazerHighGui.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
#include <opencv/highgui.h>
#include "utils.h"
#include "OutputMethods.h"
#include "MainGazeTracker.h"
#include "WindowStore.h"
#define MAIN_WINDOW_NAME "OpenGazer"
MainGazeTracker* gazeTracker;
static vector<shared_ptr<AbstractStore> > getStores() {
vector<shared_ptr<AbstractStore> > stores;
stores.push_back(shared_ptr<AbstractStore>(new SocketStore()));
stores.push_back(shared_ptr<AbstractStore>(new StreamStore(cout)));
stores.push_back(shared_ptr<AbstractStore>
( new WindowStore( WindowPointer::PointerSpec(60, 60, 0, 0, 255),
WindowPointer::PointerSpec(60, 60, 250, 0, 250) ) ) );
return stores;
}
// Redundant work to wrap the buttons
// TODO: Abstract this using macros
void calibrateCallbackWrapper(int state, void*) {
if(state == -1) { // for push buttons
gazeTracker->startCalibration();
}
}
void testCallbackWrapper(int state, void*) {
if(state == -1) { // for push buttons
gazeTracker->startTesting();
}
}
void savePointsCallbackWrapper(int state, void*) {
if(state == -1) { // for push buttons
gazeTracker->savepoints();
}
}
void loadPointsCallbackWrapper(int state, void*) {
if(state == -1) { // for push buttons
gazeTracker->loadpoints();
}
}
void clearPointsCallbackWrapper(int state, void*) {
if(state == -1) { // for push buttons
gazeTracker->clearpoints();
}
}
void mouseClick(int event, int x, int y, int flags, void* param) {
if(event == CV_EVENT_LBUTTONDOWN || event == CV_EVENT_LBUTTONDBLCLK) {
Point point(x, y);
PointTracker &tracker = gazeTracker->tracking->tracker;
int closest = tracker.getClosestTracker(point);
int lastPointId;
if(closest >= 0 && point.distance(tracker.currentpoints[closest]) <= 10) lastPointId = closest;
else
lastPointId = -1;
if(event == CV_EVENT_LBUTTONDOWN) {
if(lastPointId >= 0) tracker.updatetracker(lastPointId, point);
else {
tracker.addtracker(point);
}
}
if(event == CV_EVENT_LBUTTONDBLCLK) {
if(lastPointId >= 0) tracker.removetracker(lastPointId);
}
}
}
void createButtons() {
//Create the buttons
//These don't work - they fail to connect the signal
//See https://code.ros.org/trac/opencv/ticket/786
cvCreateButton("Calibrate", calibrateCallbackWrapper);
cvCreateButton("Test", testCallbackWrapper);
cvCreateButton("Save Points", savePointsCallbackWrapper);
cvCreateButton("Load Points", loadPointsCallbackWrapper);
cvCreateButton("Clear Points", clearPointsCallbackWrapper);
}
void registerMouseCallbacks() {
cvSetMouseCallback(MAIN_WINDOW_NAME, mouseClick, NULL);
}
void drawFrame() {
cvShowImage(MAIN_WINDOW_NAME, gazeTracker->canvas.get());
}
int main(int argc, char **argv) {
gazeTracker = new MainGazeTracker(argc, argv, getStores());
cvNamedWindow(MAIN_WINDOW_NAME, CV_GUI_EXPANDED);
cvResizeWindow(MAIN_WINDOW_NAME, 640, 480);
createButtons();
registerMouseCallbacks();
while(1) {
gazeTracker->doprocessing();
drawFrame();
char c = cvWaitKey(33);
switch(c) {
case 'c':
gazeTracker->startCalibration();
break;
case 't':
gazeTracker->startTesting();
break;
case 's':
gazeTracker->savepoints();
break;
case 'l':
gazeTracker->loadpoints();
break;
case 'x':
gazeTracker->clearpoints();
break;
default:
break;
}
if(c == 27) break;
}
cvDestroyWindow(MAIN_WINDOW_NAME);
delete gazeTracker;
return 0;
}