-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
125 lines (102 loc) · 3.19 KB
/
sketch.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
const MODEL_URL = 'models/';
let canvas;
let vid;
let results;
let landmarks;
let btn;
let div;
let loaded = false;
let points = 0;
let laugh;
let neutral;
let size = 0.5;
let canvas_width = 560;
let canvas_height = 315;
let resizeFactor = 1;
function preload() {
laugh = loadImage("laughing-smiley.png");
neutral = loadImage("neutral-smiley.png");
}
function setup() {
//div = createDiv('<br>face-api models are loading...');
canvas = createCanvas(canvas_width, canvas_height);
/*.parent('myCanvas');*/
// use an async callback to load in the models and run the getResults() function
vid = createCapture(VIDEO, async () => {
await faceapi.loadSsdMobilenetv1Model(MODEL_URL);
await faceapi.loadFaceLandmarkModel(MODEL_URL);
await faceapi.loadFaceRecognitionModel(MODEL_URL);
await faceapi.loadFaceExpressionModel(MODEL_URL);
//div.elt.innerHTML = '<br>model loaded!';
loaded = true;
document.getElementById("points").innerText = "Press Start";
getResults(); // init once
})
vid.size(canvas_width, canvas_height);
vid.hide();
}
async function getResults() {
results = await faceapi.detectSingleFace(vid.elt).withFaceExpressions();
getResults();
}
/*function resize() {
canvas_width = document.getElementById("myCanvas").clientWidth;
canvas_height = document.getElementById("myCanvas").clientHeight;
resizeFactor = 560 / canvas_width;
resizeCanvas(canvas_width, canvas_height);
vid.size(canvas_width, canvas_height);
}*/
function draw() {
//resize();
background(255);
if (points > 0) {
/*imageMode(CORNERS);
image(vid, 0, 0);*/
if (loaded) {
// results
if (results) {
// draw bounding box
let x = results.detection.box.x / resizeFactor;
let y = results.detection.box.y / resizeFactor;
let w = results.detection.box.width / resizeFactor;
let h = results.detection.box.height / resizeFactor;
// expressions
let expressions = [];
for (var expr in results.expressions) {
expressions.push([expr, results.expressions[expr]]);
}
let label;
let confidence;
let emotion;
for (let i = 0; i < expressions.length - 1; i++) {
label = expressions[i][0];
confidence = expressions[i][1];
if (confidence > 0.8) {
emotion = label;
}
}
console.log(emotion);
imageMode(CENTER);
if (emotion == "happy") {
points--;
document.getElementById("smiley").src = "laughing-smiley.png";
//image(laugh, x+w/3, y+h/2, w*2, w*0.72*2);
} else {
document.getElementById("smiley").src = "neutral-smiley.png";
//image(neutral, x+w/3, y+h/2, w*1.5, w*1.5);
}
}
document.getElementById("points").innerText = "Points: " + points;
}
} else {
background(0);
textSize(30);
textAlign(CENTER, CENTER);
fill(255);
stroke(255);
if (loaded) {
document.getElementById("points").innerHTML = "Press Start";
}
stopVideo();
}
}