-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
index.js
143 lines (118 loc) · 3.98 KB
/
index.js
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
import * as faceLandmarksDetection from "@tensorflow-models/face-landmarks-detection";
import * as tf from "@tensorflow/tfjs-core";
import "@tensorflow/tfjs-backend-webgl";
let model, video, rafID;
let amountStraightEvents = 0;
const VIDEO_SIZE = 500;
let positionXLeftIris;
let positionYLeftIris;
let event;
const normalize = (val, max, min) =>
Math.max(0, Math.min(1, (val - min) / (max - min)));
const isFaceRotated = (landmarks) => {
const leftCheek = landmarks.leftCheek;
const rightCheek = landmarks.rightCheek;
const midwayBetweenEyes = landmarks.midwayBetweenEyes;
const xPositionLeftCheek = video.width - leftCheek[0][0];
const xPositionRightCheek = video.width - rightCheek[0][0];
const xPositionMidwayBetweenEyes = video.width - midwayBetweenEyes[0][0];
const widthLeftSideFace = xPositionMidwayBetweenEyes - xPositionLeftCheek;
const widthRightSideFace = xPositionRightCheek - xPositionMidwayBetweenEyes;
const difference = widthRightSideFace - widthLeftSideFace;
if (widthLeftSideFace < widthRightSideFace && Math.abs(difference) > 5) {
return true;
} else if (
widthLeftSideFace > widthRightSideFace &&
Math.abs(difference) > 5
) {
return true;
}
return false;
};
async function renderPrediction() {
const predictions = await model.estimateFaces({
input: video,
returnTensors: false,
flipHorizontal: false,
predictIrises: true,
});
if (predictions.length > 0) {
predictions.forEach((prediction) => {
positionXLeftIris = prediction.annotations.leftEyeIris[0][0];
positionYLeftIris = prediction.annotations.leftEyeIris[0][1];
const faceBottomLeftX =
video.width - prediction.boundingBox.bottomRight[0]; // face is flipped horizontally so bottom right is actually bottom left.
const faceBottomLeftY = prediction.boundingBox.bottomRight[1];
const faceTopRightX = video.width - prediction.boundingBox.topLeft[0]; // face is flipped horizontally so top left is actually top right.
const faceTopRightY = prediction.boundingBox.topLeft[1];
if (faceBottomLeftX > 0 && !isFaceRotated(prediction.annotations)) {
const positionLeftIrisX = video.width - positionXLeftIris;
const normalizedXIrisPosition = normalize(
positionLeftIrisX,
faceTopRightX,
faceBottomLeftX
);
if (normalizedXIrisPosition > 0.355) {
event = "RIGHT";
} else if (normalizedXIrisPosition < 0.315) {
event = "LEFT";
} else {
amountStraightEvents++;
if (amountStraightEvents > 8) {
event = "STRAIGHT";
amountStraightEvents = 0;
}
}
const normalizedYIrisPosition = normalize(
positionYLeftIris,
faceTopRightY,
faceBottomLeftY
);
if (normalizedYIrisPosition > 0.62) {
event = "TOP";
}
}
});
}
return event;
}
const loadModel = async () => {
await tf.setBackend("webgl");
model = await faceLandmarksDetection.load(
faceLandmarksDetection.SupportedPackages.mediapipeFacemesh,
{ maxFaces: 1 }
);
};
const setUpCamera = async (videoElement, webcamId = undefined) => {
video = videoElement;
const mediaDevices = await navigator.mediaDevices.enumerateDevices();
const defaultWebcam = mediaDevices.find(
(device) =>
device.kind === "videoinput" && device.label.includes("Built-in")
);
const cameraId = defaultWebcam ? defaultWebcam.deviceId : webcamId;
const stream = await navigator.mediaDevices.getUserMedia({
audio: false,
video: {
facingMode: "user",
deviceId: cameraId,
width: VIDEO_SIZE,
height: VIDEO_SIZE,
},
});
video.srcObject = stream;
video.play();
video.width = 500;
video.height = 500;
return new Promise((resolve) => {
video.onloadedmetadata = () => {
resolve(video);
};
});
};
const gaze = {
loadModel: loadModel,
setUpCamera: setUpCamera,
getGazePrediction: renderPrediction,
};
export default gaze;