Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
196 lines (155 loc) · 5.77 KB
/
Copy pathscript.js
File metadata and controls
196 lines (155 loc) · 5.77 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
const video = document.getElementById('video');
const $body = $('body');
const $message = $('#message');
const $sentiment = $('#sentiment');
const $ai_talk = $('#ai');
const $happy = $('#happy');
const $sad = $('#sad');
const $surprised = $('#surprised');
const $ai = document.querySelector('#ai');
const fadeout_duration = 300;
const opacity_init = 0.1;
const opacity_step = 0.1;
const same_expression_skip = 10;
let hide = false;
let inputSize = 224;
let scoreThreshold = 0.5;
let opacity = 0.1;
let same_expression_count = 0;
const ai_feedback_baseon_face_expression = {
neutral: ["좀 웃지 그래?", "웃으라고", "뭐가 그리 심각해?", "얼굴 좀 펴", "웃으면 복이 오는데...넌 왜..."],
happy: ["웃으니깐 좋잖아^^", "오 미소 좋은데?", "웃으니깐 좀 낫네", "거봐 웃으니 얼마나 좋니^^"],
surprised: ["내가 그렇게 놀랍니?", "놀라지 마 21세기잖아", "귀신이라도 본거야?", "눈이 크네~^^"],
sad: ["우울해 하지마", "슬프니?", "울지마....", "혹시... 무슨일이라도..."]
};
var before_expression = null;
Promise.all([
faceapi.nets.tinyFaceDetector.loadFromUri('./models'),
faceapi.nets.ageGenderNet.loadFromUri('./models'),
faceapi.nets.faceLandmark68Net.loadFromUri('./models'),
faceapi.nets.faceRecognitionNet.loadFromUri('./models'),
faceapi.nets.faceExpressionNet.loadFromUri('./models')
]).then(startVideo)
function startVideo() {
navigator.getUserMedia(
{video: {}},
stream => video.srcObject = stream,
err => console.error(err)
)
}
function getFaceDetectorOptions() {
return new faceapi.TinyFaceDetectorOptions({inputSize, scoreThreshold})
}
async function onPlay() {
const videoEl = $('#video').get(0);
if (videoEl.paused || videoEl.ended)
return setTimeout(() => onPlay())
const options = getFaceDetectorOptions()
const detections = await faceapi.detectSingleFace(videoEl, options).withFaceLandmarks().withFaceExpressions().withAgeAndGender()
const canvas = $("#overlayCanvas").get(0)
if (detections) {
const dims = faceapi.matchDimensions(canvas, videoEl, true)
const resizedResult = faceapi.resizeResults(detections, dims)
const minConfidence = 0.05
try {
console.log(resizedResult);
const expression = get_top_expression(resizedResult.expressions);
console.log(expression);
ai_talk(expression)
} catch (e) {
//얼굴 인식이 안되는 상황일때
console.error(e.message)
}
faceapi.draw.drawDetections(canvas, resizedResult)
faceapi.draw.drawFaceLandmarks(canvas, resizedResult)
faceapi.draw.drawFaceExpressions(canvas, resizedResult, minConfidence)
const {age, gender, genderProbability} = resizedResult
new faceapi.draw.DrawTextField(
[
`${faceapi.round(age, 0)} years`,
`${gender} (${faceapi.round(genderProbability)})`
],
resizedResult.detection.box.bottomRight
).draw(canvas)
} else {
$ai.textContent = "얼굴 인식 안됨";
canvas.getContext('2d').clearRect(0, 0, canvas.width, canvas.height)
}
setTimeout(() => onPlay())
}
function reset() {
var $before_expression = $("#" +before_expression);
$before_expression.css({
opacity:opacity_init
});
$before_expression.hide();
opacity = 0;
same_expression_count = 0;
}
function ai_talk(expression) {
if (before_expression != expression) {
reset();
if (expression == 'neutral') {
$ai.textContent = ai_feedback_baseon_face_expression.neutral[_.random(0, ai_feedback_baseon_face_expression.neutral.length - 1)]
}
if (expression == 'happy') {
$happy.show();
$ai.textContent = ai_feedback_baseon_face_expression.happy[_.random(0, ai_feedback_baseon_face_expression.happy.length - 1)]
}
if (expression == 'surprised') {
$surprised.show();
$ai.textContent = ai_feedback_baseon_face_expression.surprised[_.random(0, ai_feedback_baseon_face_expression.surprised.length - 1)]
}
if (expression == 'sad') {
$sad.show();
$ai.textContent = ai_feedback_baseon_face_expression.sad[_.random(0, ai_feedback_baseon_face_expression.sad.length - 1)]
}
before_expression = expression;
} else {
console.log(before_expression);
if(same_expression_count % same_expression_skip == 0){
$ai.textContent = ai_feedback_baseon_face_expression[before_expression][_.random(0, ai_feedback_baseon_face_expression[before_expression].length - 1)]
}
same_expression_count++;
let $before_expression = $('#' + before_expression);
if (0 <= opacity && opacity < 1) {
console.log(opacity);
opacity = opacity + opacity_step;
$before_expression.animate({
opacity:opacity
}, 0, ()=>{
console.log('animation complete');
})
} else {
console.log("--------")
console.log(opacity)
}
}
}
function get_top_expression(obj) {
let default_value = 0;
let final_expression;
_.mapObject(obj, (v, k) => {
if (default_value < v) {
default_value = v;
final_expression = k;
}
});
return final_expression;
}
video.addEventListener('play', async () => {
setTimeout(() => onPlay())
})
$body.click(() => {
if (hide) {
hide = false;
$sentiment.show();
$message.show();
$ai_talk.show();
} else {
hide = true;
$sentiment.hide();
$message.hide();
$ai_talk.hide();
}
})