-
Notifications
You must be signed in to change notification settings - Fork 1
/
EyeTracker.cpp
149 lines (119 loc) · 4.15 KB
/
EyeTracker.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
#include "pch.h"
#include "EyeTracker.h"
#include <ppltasks.h>
#include <iostream>
using namespace concurrency;
using namespace Windows::Devices::Enumeration;
using namespace Windows::UI::Xaml;
using namespace Windows::Media::MediaProperties;
using namespace Windows::Graphics::Imaging;
using namespace Windows::Foundation::Collections;
EyeTracker::EyeTracker(CaptureElement^ previewControl)
: mediaCapture(ref new MediaCapture())
, previewControl(previewControl) // ref new PreviewControl()
, faceDetector(nullptr)
, isPreviewing(false)
, isInitialized(false)
, newEyePosition(false)
, currEyePositionIndex(0)
, eyePositions(eyePBufSiz)
{
}
void EyeTracker::InitializeAsync(void)
{
create_task(mediaCapture->InitializeAsync()).then([t = this]()
{
t->previewControl->Source = t->mediaCapture.Get();
t->previewControl->FlowDirection = FlowDirection::RightToLeft;
return t->mediaCapture->StartPreviewAsync();
}).then([t = this]() -> IAsyncOperation<FaceDetector^>^
{
t->isPreviewing = true;
// Get information about the preview
// IMediaEncodingProperties^ previewProperties = mediaCapture->VideoDeviceController->GetMediaStreamProperties(MediaStreamType::VideoPreview);
// auto previewProperties = (VideoEncodingProperties^)mediaCapture->VideoDeviceController->GetMediaStreamProperties(MediaStreamType::VideoPreview);
// Create a video frame in the desired format for the preview frame
t->videoFrame = ref new VideoFrame(BitmapPixelFormat::Gray8, 1920, 1080);// (int)previewProperties->Width, (int)previewProperties->Height);
// Create a face detector
return FaceDetector::CreateAsync();
}).then([t = this](FaceDetector^ detector)
{
t->faceDetector = detector;
t->UpdateEyePosition();
});
}
Vector3 * EyeTracker::GetEyePosition(void)
{
if (newEyePosition)
{
newEyePosition = false;
return &eyePositions[currEyePositionIndex];
}
return nullptr;
}
inline int positive_modulo(int i, int n) {
return (i % n + n) % n;
}
Vector3 * EyeTracker::GetEyePosition(int offset)
{
int index = positive_modulo((int)currEyePositionIndex + offset, (int)eyePositions.size());
return &eyePositions[index];
}
Vector3 EyeTracker::EstimateEyePosition(int depth)
{
Vector3 estimation;
Vector3 currentPosition = *GetEyePosition(0);
for (int i = 1; i <= depth; i++)
{
estimation += (currentPosition - *GetEyePosition(-i)) / (float)i;
}
return currentPosition + (Vector3) (estimation / (float)depth);
}
void EyeTracker::UpdateEyePosition(void)
{
create_task(mediaCapture->GetPreviewFrameAsync(videoFrame)).then([t = this](VideoFrame^ previewFrame)
{
SoftwareBitmap^ bitmap = previewFrame->SoftwareBitmap->GetReadOnlyView();
create_task(t->faceDetector->DetectFacesAsync(bitmap)).then([t, bitmap](IVector<DetectedFace^>^ faces)
{
const float distanceFactor = 500.0f;
const float imagePlaneDistance = 45.0f;
if (faces->Size > 0)
{
BitmapBounds bounds = faces->GetAt(0)->FaceBox;
float xScale = 1.0f / (float)bitmap->DpiX;
float yScale = 1.0f / (float)bitmap->DpiY;
float x = bounds.X * xScale;
float y = bounds.Y * yScale;
float width = bounds.Width * xScale;
float height = bounds.Height * yScale;
float distance = distanceFactor / (width > height ? width : height);
float imageWidth = bitmap->PixelWidth * xScale;
float imageHeight = bitmap->PixelHeight * yScale;
x += width * 0.5f;
y += height * 0.35f;
Vector3 newEye (
(imageWidth * 0.5f - x) / imagePlaneDistance * distance,
(imageHeight * 0.5f - y) / imagePlaneDistance * distance,
distance
);
Vector3 estimatedNewEye = t->EstimateEyePosition(8);
t->currEyePositionIndex = (t->currEyePositionIndex + 1) % t->eyePositions.size();
*t->GetEyePosition(0) =
0.1f * newEye +
0.08f * *t->GetEyePosition(-1) +
0.07f * *t->GetEyePosition(-2) +
0.06f * *t->GetEyePosition(-3) +
0.05f * *t->GetEyePosition(-4) +
0.04f * *t->GetEyePosition(-5) +
0.6f * estimatedNewEye;
// 0.4f * t->EstimateEyePosition(1) + 0.6f * newEye;
// (2 * *t->GetEyePosition(0) - *t->GetEyePosition(-1) + newEye) / 2;
t->newEyePosition = true;
}
}).then([t](void)
{
t->UpdateEyePosition();
});
});
}