-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
214 lines (173 loc) · 6.61 KB
/
index.html
File metadata and controls
214 lines (173 loc) · 6.61 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Face Detection & Expression Detection with face-api.js</title>
<style>
* {
box-sizing: border-box;
}
/* hidden but available for debugging */
video, canvas, #expression {
opacity:0;
}
.shown {
opacity:.5
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
transition:background 150ms;
background:#000;
transition: background-color 350ms;
}
body:after {
position: absolute;
top:50%;
left:50%;
transform: translate3d(-50%, -50%, 0);
font-size:132px;
}
/*
Possible expressions to detect:
'neutral'
'happy'
'sad'
'angry'
'fearful'
'disgusted'
'surprised'
*/
body.neutral {
background:#aaa;
}
body.neutral:after {
content: "😐"
}
body.happy {
background:yellow;
}
body.happy:after {
content: "😃"
}
body.surprised {
background:#a0a;
}
body.surprised:after {
content: "😮"
}
body.disgusted {
background:greenyellow;
}
body.disgusted:after {
content: "🤢"
}
body.sad {
background:dodgerblue;
}
body.sad:after {
content: "😔"
}
body.angry {
background:firebrick;
}
body.angry:after {
content: "😡"
}
body.fearful {
background:#fff;
}
body.fearful:after {
content: "😱"
}
</style>
</head>
<body>
<!--
These items are necessary but rendered
with `opacity:0` to be invisible. Pressing
the Space key will reveal them to help test/debug
-->
<h1 id='expression'> </h1>
<video onplay="onPlay(this)" id="inputVideo" autoplay muted></video>
<canvas id="overlay" />
<!--
This is the audio file it plays when a "sad"
expression is detected. Found in the public domain
on Archive.org:
https://archive.org/details/78_what-can-i-do-to-make-you-happy_charles-dickson-gordon-eddy_gbia0086082b
If you're testing things locally it's probably better
to download it once and serve locally.
-->
<audio id="cheerUp" src="https://ia800707.us.archive.org/6/items/78_what-can-i-do-to-make-you-happy_charles-dickson-gordon-eddy_gbia0086082b/What%20Can%20I%20Do%20to%20Make%20You%20Happy%20-%20Charles%20Dickson.mp3"></audio>
<script src="dist/face-api.min.js"></script>
<script>
const video = document.getElementById('inputVideo')
const cheerUp = document.getElementById('cheerUp')
async function run() {
// load the models
console.log("loading models...")
await faceapi.nets.ssdMobilenetv1.load('weights')
await faceapi.nets.faceExpressionNet.load('weights')
await faceapi.loadFaceRecognitionModel('weights')
const stream = await navigator.mediaDevices.getUserMedia({ video: {} })
video.srcObject = stream
}
async function onPlay() {
// Wait until the models are loaded
if (!isFaceDetectionModelLoaded()) return setTimeout(() => onPlay())
const options = new faceapi.SsdMobilenetv1Options({ minConfidence: .5 }) //mino confidence
const ts = Date.now()
const result = await faceapi.detectSingleFace(video, options).withFaceExpressions()
if (result) {
result.expressions.forEach((expression) => {
if (Math.round(expression.probability) == 1) {
console.log(expression.expression, Math.round(expression.probability))
document.querySelector('body').className = expression.expression
document.querySelector('#expression').textContent = expression.expression
if (expression.expression === 'sad' && cheerUp.paused === true) {
cheerUp.play();
}
if (expression.expression === 'angry' && cheerUp.paused === false) {
cheerUp.pause();
cheerUp.currentTime = 0;
}
}
})
}
window.requestAnimationFrame(onPlay);
}
function isFaceDetectionModelLoaded() {
return !!faceapi.nets.ssdMobilenetv1.params
}
// Press space to reveal video stream and currently
// perceived expression for debugging/testing. Also
// press Escape to cancel audio in case demo is going
// off the rails.
document.addEventListener('keypress', (event) => {
switch (event.code) {
case "Space":
if (video.classList.contains('shown')) {
video.classList.remove('shown')
document.querySelector('#expression').classList.remove('shown')
}else{
video.classList.add('shown')
document.querySelector('#expression').classList.add('shown')
}
break;
case "Escape":
if (cheerUp.paused === false) {
cheerUp.pause();
cheerUp.currentTime = 0;
}
break;
}
event.preventDefault();
})
// note: on mobile a user interaction
// is required to initiate getUserMedia().
run();
</script>
</body>
</html>