forked from opengazer/OpenGazer
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Calibrator.cpp
123 lines (101 loc) · 3 KB
/
Calibrator.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
#include "Calibrator.h"
Calibrator::~Calibrator() {
#ifdef DEBUG
cout << "Destroying calibrator" << endl;
#endif
}
FrameFunction::~FrameFunction() {
#ifdef DEBUG
cout << "Destroying framefunction" << endl;
#endif
}
MovingTarget::MovingTarget(const int &frameno,
const vector<Point>& points,
const shared_ptr<WindowPointer> &pointer,
int dwelltime):
FrameFunction(frameno),
points(points), dwelltime(dwelltime), pointer(pointer)
{
};
MovingTarget::~MovingTarget() {
int id = getFrame() / dwelltime;
}
void MovingTarget::process() {
if (active()) {
int id = getPointNo();
if (getPointFrame() == 1)
pointer->setPosition((int)points[id].x, (int)points[id].y);
}
else
detach();
}
bool MovingTarget::active() {
return getPointNo() < (int) points.size();
}
int MovingTarget::getPointNo() {
return getFrame() / dwelltime;
}
int MovingTarget::getPointFrame() {
return getFrame() % dwelltime;
}
Calibrator::Calibrator(const int &framecount,
const shared_ptr<TrackingSystem> &trackingsystem,
const vector<Point>& points,
const shared_ptr<WindowPointer> &pointer,
int dwelltime):
MovingTarget(framecount, points, pointer, dwelltime),
trackingsystem(trackingsystem)
{
trackingsystem->gazetracker.clear();
// todo: remove all calibration points
}
void Calibrator::process() {
if (active()) {
int id = getPointNo();
int frame = getPointFrame();
if (frame == 1) // start
averageeye.reset(new FeatureDetector(EyeExtractor::eyesize));
if (frame >= dwelltime/2) // middle
averageeye->addSample(trackingsystem->eyex.eyefloat.get());
if (frame == dwelltime-1) { // end
trackingsystem->gazetracker.
addExemplar(points[id], averageeye->getMean().get(),
trackingsystem->eyex.eyegrey.get());
}
}
MovingTarget::process();
}
const Point Calibrator::defaultpointarr[] = {Point(0.5, 0.5),
Point(0.1, 0.5), Point(0.9, 0.5),
Point(0.5, 0.1), Point(0.5, 0.9),
Point(0.1, 0.1), Point(0.1, 0.9),
Point(0.9, 0.9), Point(0.9, 0.1),
Point(0.3, 0.3), Point(0.3, 0.7),
Point(0.7, 0.7), Point(0.7, 0.3)};
vector<Point>
Calibrator::defaultpoints(Calibrator::defaultpointarr,
Calibrator::defaultpointarr+
(sizeof(Calibrator::defaultpointarr) /
sizeof(Calibrator::defaultpointarr[0])));
vector<Point> Calibrator::loadpoints(istream& in) {
vector<Point> result;
for(;;) {
double x, y;
in >> x >> y;
if (in.rdstate()) break; // break if any error
result.push_back(Point(x, y));
}
return result;
}
vector<Point> Calibrator::scaled(const vector<Point> &points,
double x, double y)
{
// double dx = x > y ? (x-y)/2 : 0.0;
// double dy = y > x ? (y-x)/2 : 0.0;
// double scale = x > y ? y : x;
vector<Point> result;
xforeach(iter, points)
result.push_back(Point(iter->x * x, iter->y * y));
// result.push_back(Point(iter->x * scale + dx, iter->y * scale + dy));
return result;
}