-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcapture.js
More file actions
295 lines (270 loc) · 9.78 KB
/
capture.js
File metadata and controls
295 lines (270 loc) · 9.78 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
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
let exp
let expressions = ["neutral", "happy","sad","angry","fearful","disgusted","surprised"]
let ready = false
let emoji = []
let canvas = document.querySelector("#canvas");
let theme = "Emoji"
let img = 0
let time = 3
let content = document.querySelector("#content")
let loading = document.querySelector("#loading")
let startBtn = document.querySelector(".play-btn")
let video = document.querySelector("#video")
let webCam = document.querySelector(".webcam")
let emojiCam = document.querySelector("#emoji-capture")
let randomBtn = document.querySelector("#random-btn")
let downloadBtn = document.querySelector("#download-btn")
let timerCam = document.querySelector("#timer")
let imgRes = document.querySelector("#result-img")
let yourExpression = document.querySelector("#exp")
let first = document.querySelector("#first")
let second = document.querySelector("#second")
let third = document.querySelector("#third")
let retakeImg = new Image;
retakeImg.src = "src/assets/Restart.svg"
// Load all expression image
var expList = {}
for(i of expressions){
var newImg = new Image;
newImg.src = `src/svg/${i}.svg`
expList[i] = newImg
}
// add background
var background = new Image();
background.src = `src/assets/${theme}.svg`;
background.onload = function(){
canvas.getContext('2d').drawImage(background, 0, 0, canvas.width, canvas.height);
}
Promise.all([
faceapi.nets.tinyFaceDetector.loadFromUri('/models'),
faceapi.nets.faceLandmark68Net.loadFromUri('/models'),
faceapi.nets.faceRecognitionNet.loadFromUri('/models'),
faceapi.nets.faceExpressionNet.loadFromUri('/models')
]).then(() => {
// kalau sudah ready
loading.style.display = "none"
content.style.display = "block"
video.addEventListener('play', () => {
const canvasVideo = faceapi.createCanvasFromMedia(video)
document.body.append(canvasVideo)
const displaySize = { width: video.width, height: video.height }
faceapi.matchDimensions(canvasVideo, displaySize)
let detections, resizedDetections
setInterval(async () => {
if(ready){
detections = await faceapi.detectAllFaces(video, new faceapi.TinyFaceDetectorOptions()).withFaceLandmarks().withFaceExpressions()
resizedDetections = faceapi.resizeResults(detections, displaySize)
canvasVideo.getContext('2d').clearRect(0, 0, canvasVideo.width, canvasVideo.height)
// faceapi.draw.drawDetections(canvasVideo, resizedDetections)
// faceapi.draw.drawFaceLandmarks(canvasVideo, resizedDetections)
// faceapi.draw.drawFaceExpressions(canvasVideo, resizedDetections)
if(resizedDetections[0]){
exp = getMaxValueKey(resizedDetections[0]["expressions"])
}
if(exp && ready){
yourExpression.innerHTML = exp
if(exp == emoji[img]){
if(img < 3){
if(time == 0){
timerCam.innerHTML = ""
capture()
}else if(time > 0){
time -= 1
timerCam.innerHTML = time + 1
}
}
}else{
time = 3
timerCam.innerHTML = ""
}
}
}
}, 1000)
})
})
function startVideo() {
navigator.getUserMedia(
{ video: {width: 500, height: 500} },
stream => {
window.localStream = stream;
video.srcObject = stream
},
err => console.error(err)
)
}
function getMaxValueKey(obj){
return Object.keys(obj).reduce(function(a, b){ return obj[a] > obj[b] ? a : b });
}
function getRandom(arr, n) {
var result = new Array(n),
len = arr.length,
taken = new Array(len);
if (n > len)
throw new RangeError("getRandom: more elements taken than available");
while (n--) {
var x = Math.floor(Math.random() * len);
result[n] = arr[x in taken ? taken[x] : x];
taken[x] = --len in taken ? taken[len] : len;
}
return result;
}
function setEmojiCamera(){
// Set emoji on camera
if(img < 3){
emojiCam.src = expList[emoji[img]].src
}
}
function capture(){
if(img < 3){
var emojiPic = new Image();
emojiPic.src = expList[emoji[img]].src
var position = (400*img+100)+700*img
// Add photo to canvas
canvas.getContext('2d').drawImage(video, 100, position, 1000, 1000);
// Add emoji to canvas
canvas.getContext('2d').drawImage(emojiPic, 150, position+750, 200, 200);
// Url hasil
image_data_url = canvas.toDataURL('image/jpeg',3);
// Add photo and emoji to result img
var imgCap = document.createElement("canvas")
imgCap.width = video.width*5
imgCap.height = video.height*5
imgCap.getContext('2d').drawImage(video, 0, 0, video.width*5, video.height*5);
imgCap.getContext('2d').drawImage(emojiPic, 20, 375, 100, 100);
var imgEl = document.createElement("img")
imgEl.style.width = "30%"
imgEl.style.margin = "0 10px"
imgEl.style.cursor = "pointer"
imgEl.onclick = downloadImg
imgEl.src = imgCap.toDataURL('image/jpeg')
imgRes.appendChild(imgEl)
}
img += 1
setEmojiCamera()
if(img >= 3) {
// Stop
exp = "undefined"
ready = false
localStream.getVideoTracks()[0].stop();
video.src = '';
randomBtn.removeAttribute('disabled')
yourExpression.parentElement.style.display = "none"
video.style.display = "none"
emojiCam.style.display = "none"
startBtn.firstChild.src = retakeImg.src
startBtn.style.display = "block"
randomBtn.style.display = "block"
downloadBtn.style.display = "block"
}
}
let image_data_url
document.querySelector("#capture-btn").addEventListener('click', function() {
capture()
});
document.querySelector("#download-btn").addEventListener('click', function() {
var anchor = document.createElement("a");
anchor.href = image_data_url
anchor.download = "facemoji.png";
anchor.click();
});
startBtn.addEventListener("click", function(){
yourExpression.parentElement.style.display = "block"
imgRes.innerHTML = ""
ready = true
img = 0
setEmojiCamera()
randomBtn.disabled = "true"
downloadBtn.style.display = "none"
randomBtn.style.display = "none"
this.style.display = "none"
emojiCam.style.display = "block"
video.style.display = "block"
startVideo()
})
// Start random
var firstRand, secondRand, thirdRand, randomInt
randomBtn.addEventListener("click", function(){
startBtn.disabled = "true"
randomBtn.disabled = "true"
emoji = getRandom(expressions, 3)
// Random untuk emoji pertama setiap 0.1 detik
firstRand = setInterval(() => {
randomInt = Math.floor(Math.random() * 7)
first.src = expList[expressions[randomInt]].src
document.querySelector("#first-text").innerHTML = expressions[randomInt]
},100)
setTimeout(() => {
// dalam waktu 0.5 detik sudahi random dan pilih emoji
clearInterval(firstRand)
first.setAttribute("src", expList[emoji[0]].src)
document.querySelector("#first-text").innerHTML = emoji[0]
// Random untuk emoji kedua setiap 0.1 detik
secondRand = setInterval(() => {
randomInt = Math.floor(Math.random() * 7)
second.src = expList[expressions[randomInt]].src
document.querySelector("#second-text").innerHTML = expressions[randomInt]
},100)
}, 500);
setTimeout(() => {
// dalam waktu 1 detik sudahi random dan pilih emoji
clearInterval(secondRand)
second.setAttribute("src", expList[emoji[1]].src)
document.querySelector("#second-text").innerHTML = emoji[1]
// Random untuk emoji pertama setiap 0.1 detik
thirdRand = setInterval(() => {
randomInt = Math.floor(Math.random() * 7)
third.src = expList[expressions[randomInt]].src
document.querySelector("#third-text").innerHTML = expressions[randomInt]
},100)
}, 1000);
setTimeout(() => {
// dalam waktu 1.5 detik sudahi random dan pilih emoji
clearInterval(thirdRand)
third.setAttribute("src", expList[emoji[2]].src)
document.querySelector("#third-text").innerHTML = emoji[2]
randomBtn.removeAttribute('disabled')
startBtn.removeAttribute('disabled')
}, 1500);
})
// Download every image
function downloadImg(){
var anchor = document.createElement("a");
anchor.href = this.src
anchor.download = "expression.png";
anchor.click();
}
// Fungsi untuk mendapatkan sibling
function getSiblings(n, skipMe){
var parent = n.parentElement
var children = parent.children
var sibling = []
for(c of children){
if(c != n){
sibling.push(c)
}
}
return sibling
};
// Select theme
function select(n){
// Jika sudah ready take tidak bisa ganti background
if(ready){
return false
}
var sibiling = getSiblings(n)
for(s of sibiling){
s.removeAttribute("class")
}
n.setAttribute("class","selected")
theme = n.getAttribute("alt")
var themeColor = {"Emoji":"#2051CF","Animal":"#F52E12","Food":"#008E39","Sport":"#1C1919"}
for(let key in themeColor){
if(theme == key){
document.querySelector("#theme-text").style.color = themeColor[theme]
}
}
background.src = `src/assets/${theme}.svg`;
background.onload = function(){
canvas.getContext('2d').drawImage(background, 0, 0, canvas.width, canvas.height);
}
}