-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
79 lines (73 loc) · 2.21 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
// switch at upload to git
// const gitPagesPath = "";
const gitPagesPath = "/serious-face-detector";
const startVideo = async (video) => {
try {
const constraints = { audio: false, video: {} };
const stream = await navigator.mediaDevices.getUserMedia(constraints);
video.srcObject = stream;
} catch (error) {
console.error(error);
}
};
const loadModels = async () => {
await Promise.all([
faceapi.nets.tinyFaceDetector.loadFromUri(`${gitPagesPath}/js/lib/models`),
faceapi.nets.faceExpressionNet.loadFromUri(`${gitPagesPath}/js/lib/models`),
]);
};
const calcExpressionPoint = (expression) => {
switch (expression) {
case "neutral":
return 1;
case "angry":
case "disgusted":
return 2;
case "happy":
return -5;
default:
return 0;
}
};
(async () => {
const video = document.querySelector("video");
await loadModels();
await startVideo(video);
video.addEventListener("play", () => {
const displaySize = { width: video.width, height: video.height };
const tinyFaceDetectorOption = {
// default 416
inputSize: 224,
// default 0.5
scoreThreshold: 0.5,
};
// 真顔ポイント
let seriousPoint = 0;
let seriousCount = 0;
const pictureCanvas = document.querySelector("canvas");
const timestampLabel = document.querySelector("#timestamp");
setInterval(async () => {
const results = await faceapi
.detectAllFaces(
video,
new faceapi.TinyFaceDetectorOptions(tinyFaceDetectorOption)
)
.withFaceExpressions();
if (results.length <= 0) return;
const resizedResults = faceapi.resizeResults(results, displaySize);
resizedResults.forEach((result) => {
const expression = result.expressions.asSortedArray()[0].expression;
seriousPoint += calcExpressionPoint(expression);
if (seriousPoint >= 100) {
// 画像出力
pictureCanvas
.getContext("2d")
.drawImage(video, 0, 0, pictureCanvas.width, pictureCanvas.height);
seriousPoint = 0;
seriousCount++;
timestampLabel.textContent = `${seriousCount}回目 ${new Date()}`;
}
});
}, 100);
});
})();