-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
354 lines (314 loc) · 10.4 KB
/
index.html
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
<!-- Copyright 2020 Google LLC. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================-->
<head>
<style>
.canvas-wrapper, #scatter-gl-container {
display: inline-block;
vertical-align: top;
}
.p5Canvas{
position: absolute;
right: 1vw;
top: 30vh;
border: fuchsia 3px solid;
background-color: Lavender;
}
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r118/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/mrdoob/three.js@r92/examples/js/loaders/GLTFLoader.js"></script>
<script src="https://cdn.jsdelivr.net/npm/p5@1.1.9/lib/p5.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.1.9/addons/p5.sound.js" integrity="sha512-KxzVm+IqxNNq0+SzT/zzd5PHxY4LPrN+v5gZJ6+JKqjeU3Cr4y/djAg5eNlKDWurn1SeKZpql/yeOMWblMSzOg==" crossorigin="anonymous"></script>
<title>Virtual Avatar</title>
</head>
<body>
<div id="main">
<div id="smile" style="position: absolute;
left: 25vw;
top: 45vh;"
hidden>
<img src="smile.png" height="200px" width="200px">
</div>
<div id="pog" style="position: absolute;
left: 65vw;
top: 45vh;"
hidden>
<img src="pog.png" height="200px" width="200px">
</div>
<div class="container">
<div class="canvas-wrapper">
<canvas id="output" style="
border: 5px black solid;
margin-top: 1vh;
border: fuchsia 3px solid;
">
</canvas>
<video id="video" playsinline style="
-webkit-transform: scaleX(-1);
transform: scaleX(-1);
visibility: hidden;
width: auto;
height: auto;
">
</video>
</div>
<div id="debug" style="
float: right;
padding: 1%;
margin-right: 1vw;
margin-top: 1vh;
font-size: 14px;
background-color: Lavender;
text-align: center;
font-family: Verdana, Geneva, Tahoma, sans-serif;
border: fuchsia 3px solid;">Loading.. </div>
<canvas id="mouth-display" width="400" height="200" style="border: fuchsia 3px solid; position: absolute; top: 50vh; left: .5vw;
background-color: Lavender;">
Your browser does not support the HTML canvas tag.
</canvas>
<div id="jsond" style="position: absolute; bottom: 5vh" hidden>
<button onclick="t();">Save</button>
<select name="" id="fer_c">
<option value="0">Neutral</option>
<option value="1">Smile</option>
<option value="2">Shock</option>
</select>
<button onclick="s();">DL</button>
</div>
<div id="jsond-a" style="position: absolute; bottom: 5vh" hidden>
<button onclick="t_3();">Save</button>
<select name="" id="pho_c">
<option value="0">N</option>
<option value="1">A1</option>
<option value="2">A2</option>
<option value="3">O1</option>
<option value="4">O2</option>
</select>
<button onclick="s_3();">DL</button>
</div>
<div id="audiod" style="position: absolute; bottom: 5vh" hidden>
<button onclick="star();">Start</button>
<span id="dssize"></span>
<button onclick="s2();">DL</button>
</div>
<div id="testout" style="
border: fuchsia 3px solid;
background-color: Lavender;
position: absolute;
top: 75vh;
left: .5vw;
width: 400px;
height: 100px;
font-size: 28px;
text-align: center;
padding-top: 25px;">
(>-^.^-)>
</div>
<div id="scatter-gl-container"></div>
</div>
</div>
<canvas id="facerig" style="
position: absolute;
background-color: lightblue;
top: 0;
left: 0;
width: 100vw;
height: 100vh;
z-index: -1;">
</canvas>
<script>
let scene, camera, renderer, face, bones, frav, x_face, y_face, x_jaw, mouth_ex, pho_p, eans;
let tft, mic;
//hosted version, need to fix...
const face_location = "http://127.0.0.1:8887/face.gltf";
const fps = 25;
const x_angle = .7;
const y_angle = .7;
const tilt_angle = 0.02;
const micThreshold = 0.003;
var c = document.getElementById("mouth-display")
ctxt = c.getContext("2d");
ctxt.strokeStyle = "orange"
ctxt.lineWidth = 4.5;
var record = false;
var xs_ = [];
var ys_ = [];
var xs = [];
var ys = [];
var mid = [400/2 , 200/2];
// controls the strength of rotation
function initalise(){
//set up the scene, load in the model, set some variables
scene = new THREE.Scene();
scene.background = new THREE.Color("#008080");
camera = new THREE.PerspectiveCamera(40, window.innerWidth/window.innerHeight, 1, 5000);
camera.position.z = 100;
camera.position.y = 20;
light = new THREE.PointLight(0xc4c4c4, 3);
light.position.set(0,300,500);
scene.add(light);
renderer = new THREE.WebGLRenderer({ canvas: facerig, antialias: true});
renderer.setSize(window.innerWidth,window.innerHeight);
//document.body.appendChild(renderer.domElement);
let loader = new THREE.GLTFLoader();
loader.load(face_location, function(gltf){
gltf.scene.name = "Face";
scene.add(gltf.scene);
face = scene.getObjectByName("Face");
bones = face.children[0].children[1];
bones.head = bones.children[0];
bones.jaw = bones.head.children[0];
x_jaw = bones.jaw.rotation.x;
x_face = bones.head.rotation.y;
y_face = bones.head.rotation.x;
animation();
})
mouth_ex_move();
}
function star(){ record=!record;}
function setup(){
createCanvas(400, 250);
noFill();
mic = new p5.AudioIn();
mic.start();
fft = new p5.FFT();
fft.setInput(mic);
}
function t(){
xs.push(mouth_ex);
ys.push(document.getElementById("fer_c").value)
}
function t_3(){
xs.push(mouth_ex);
ys.push(document.getElementById("pho_c").value)
}
function s(){
var data = {
x: xs,
y: ys
}
var json = JSON.stringify(data);
var blob = new Blob([json], {type: "application/json"});
var url = URL.createObjectURL(blob);
var a = document.createElement('a');
a.download = "dataset_fer.json";
a.href = url;
a.textContent = "Download";
document.getElementById('jsond').appendChild(a);
}
function s_3(){
var data = {
x: xs,
y: ys
}
var json = JSON.stringify(data);
var blob = new Blob([json], {type: "application/json"});
var url = URL.createObjectURL(blob);
var a = document.createElement('a');
a.download = "dataset_pho.json";
a.href = url;
a.textContent = "Download";
document.getElementById('jsond-a').appendChild(a);
}
function s2(){
var data = {
x: xs_,
y: ys_
}
var json = JSON.stringify(data);
var blob = new Blob([json], {type: "application/json"});
var url = URL.createObjectURL(blob);
var a = document.createElement('a');
a.download = "dataset_audio.json";
a.href = url;
a.textContent = "Download";
document.getElementById('audiod').appendChild(a);
}
function mouth_ex_move(){
if(mouth_ex){
ctxt.clearRect(0,0,400,200);
ctxt.beginPath();
ctxt.arc(mid[0], mid[1], 2, 0, 2 * Math.PI);
for (let x = 0; x < mouth_ex.length; x++) {
ctxt.arc(mid[0]-(3*mouth_ex[x][0]), mid[1]-(3*mouth_ex[x][1]), 2, 0, 2 * Math.PI);
}
ctxt.arc(mid[0], mid[1], 2, 0, 2 * Math.PI);
ctxt.stroke();
}
requestAnimationFrame(mouth_ex_move);
}
function animation(){
//animation loop
updateFace();
renderer.render(scene, camera);
setTimeout(()=>{
requestAnimationFrame(animation);
}, 1000 / fps
)
}
function touchStarted() {
if (getAudioContext().state !== 'running') {
getAudioContext().resume();
console.log('Audio Resumed!');
}
}
function soundClassification(){
if(record){
xs_.push(fft.analyze());
ys_.push(mouth_ex);
document.getElementById("dssize").innerHTML = xs_.length;
}
}
function draw(){
background(255);
let spectrum = fft.analyze();
strokeWeight(5);
let c = color("orange");
stroke(c);
beginShape();
for (i = 0; i < spectrum.length; i++) {
vertex(i, map(spectrum[i], 0, 255, height, 0));
}
endShape();
soundClassification();
}
function updateFace(){
// if frav is being updated from the webcam feed, update face
if(typeof frav !== 'undefined'){
bones.head.rotation.z = (frav.tilt * tilt_angle);
let x_roll = ( frav.roll_x / 50 ) - 1;
bones.head.rotation.y = x_face - (x_roll * x_angle);
let y_roll = ( frav.roll_y / 50) - 1;
bones.head.rotation.x = y_face + (y_roll * y_angle);
//bones.head.rotation.x = 0 + (fr.roll_y-50)/1000;
}
if(mouth_ex){
bones.jaw.rotation.x = x_jaw + (mouth_ex[4][1] * -.03);
if (eans == 1) {
document.getElementById('smile').hidden = false;
}else{
document.getElementById('smile').hidden = true;
}
if (eans == 2) {
document.getElementById('pog').hidden = false;
}else{
document.getElementById('pog').hidden = true;
}
}
//console.log(fr);
//updates facial position and bones based on webcam feed
//bones.jaw.position.z -= .00001
}
initalise();
</script>
</body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.7.6/dat.gui.min.js"></script>
<script src="index.js"></script>