Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
154 lines (124 loc) Β· 4.62 KB
/
Copy pathapp.js
File metadata and controls
154 lines (124 loc) Β· 4.62 KB
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
let faceDetectionSupported = false;
let animationTimer = null;
let numberOfFaces = 0;
let emojiBoxes = [];
// Our <video> element
const video = document.getElementById('webcam');
// Mapping expression to emoji
const emojiMap = {
happy: 'π',
sad: 'π',
surprised: 'π―',
angry: 'π‘',
fearful: 'π±',
neutral: 'π',
disgusted: 'π€’'
}
// Instantiate our preferred face detector
const faceDetector = new faceapi.TinyFaceDetectorOptions();
function showEmoji(index, expression, x = 0, y = 0, height, width) {
// Get emoji Unicode for our expression
const emoji = emojiMap[expression] || emojiMap.neutral;
// Detected face is roughly 15% of height lower than we want
const topAdjustment = height * .15;
// Emoji size is roughly 13% of the height
const fontSize = height * .13;
// Look up an existing emojiBox...
let emojiBox = emojiBoxes[index];
// ...or create a new one...
if (!emojiBox) {
emojiBox = document.createElement('span');
emojiBox.classList.add('emojibox');
document.body.appendChild(emojiBox);
// ...and remember it
emojiBoxes[index] = emojiBox;
}
// Render the emojiBox
emojiBox.innerHTML = emoji;
emojiBox.style.top = `${y - topAdjustment}px`;
emojiBox.style.left = `${x}px`;
emojiBox.style.width = `${width}px`;
emojiBox.style.height = `${height}px`;
emojiBox.style.fontSize = `${fontSize}vh`;
}
function cleanupBoxes(numberOfBoxes) {
// If we're storing more boxes than number of detection boxes...
if (emojiBoxes.length > numberOfBoxes) {
// ...loop through the difference...
for (let i = numberOfBoxes; i < emojiBoxes.length; i++) {
const emojiBox = emojiBoxes[i];
// ...and remove them from the DOM
emojiBox.remove();
}
// Forget extra boxes from storage
emojiBoxes = emojiBoxes.slice(0, numberOfBoxes);
}
}
function animateFace() {
// Animation loop
animationTimer = requestAnimationFrame(async () => {
if (faceDetectionSupported) {
// Detect all possible faces
const rawFaces = await faceapi.detectAllFaces(video, faceDetector).withFaceLandmarks().withFaceExpressions();
// Clean-up extra emoji boxes if people left
numberOfFaces = rawFaces.length;
cleanupBoxes(numberOfFaces);
if (numberOfFaces) {
const videoSize = video.getBoundingClientRect();
// face-api can resize detection boxes for our <video> size
const faces = faceapi.resizeResults(rawFaces, {
height: videoSize.height,
width: videoSize.width
});
// Loop through our faces and...
faces.forEach((face, index) => {
// Pick the most likely expression
const emotion = face.expressions.asSortedArray()[0];
// Only use that emotion if it's pretty likely accurate
const expression = emotion.probability > .5 ? emotion.expression : '';
// Get dimensions of the face detection
const box = face.detection.box;
// Show the Emoji box
showEmoji(index, expression, box.left + videoSize.left, box.top, box.height, box.width);
});
}
}
// Do it again
animateFace();
});
}
function startVideo() {
if (navigator.mediaDevices) {
navigator.mediaDevices.getUserMedia({
// We don't need audio
audio: false,
// Prefer user-facing camera
video: { facingMode: 'user' }
})
// Pass the stream to our <video>
.then(stream => video.srcObject = stream)
.catch(err => console.error(err));
}
}
function everythingWorked() {
faceDetectionSupported = true;
startVideo();
}
function somethingFailed(reason) {
console.error(reason);
faceDetectionSupported = false;
startVideo();
}
// <video> event listeners
video.addEventListener('play', animateFace);
video.addEventListener('pause', () => cancelAnimationFrame(animationTimer));
video.addEventListener('error', () => cancelAnimationFrame(animationTimer));
// Load all our ML models to kick things off
Promise.all([
faceapi.nets.tinyFaceDetector.loadFromUri('./models'),
faceapi.nets.faceRecognitionNet.loadFromUri('./models'),
faceapi.nets.faceLandmark68Net.loadFromUri('./models'),
faceapi.nets.faceExpressionNet.loadFromUri('./models')
])
.then(everythingWorked)
.catch(somethingFailed);