-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
350 lines (283 loc) · 8.91 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
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
var count_temp=0;
onblur = function() {
setTimeout('self.focus()',1);
}
//prakhar's code
var switch_1 = false;
var switch_2 = false;
document.getElementsByClassName("switch1")[0].addEventListener("click",function(){
if(switch_1==false)
switch_1=true;
else
switch_1=false;
})
document.getElementsByClassName("switch2")[0].addEventListener("click",function(){
if(switch_2==false)
switch_2=true;
else
switch_2=false;
})
document.getElementById("i_button").addEventListener('mouseover',function(){
document.getElementById("glass").style.visibility="visible";
})
document.getElementById("i_button").addEventListener('mouseout',function(){
document.getElementById("glass").style.visibility="hidden";
})
// FOR LIGHT MODE
document.getElementById("color_button").addEventListener('click',function(){
// console.log('hello');
document.body.classList.toggle("light-mode");
// document.getElementsByClassName("cursor")[0].classList.toggle("cursor-light-mode");
document.getElementsByTagName("*")[0].classList.toggle("cursor-blue");
document.getElementsByTagName("*")[0].classList.toggle("cursor-yellow");
if(document.getElementsByTagName("*")[0].classList.contains("cursor-blue"))
{
document.getElementsByClassName("moon")[0].src="sun.png";
}
else
{
document.getElementsByClassName("moon")[0].src="moon.png";
}
})
// ARYAN's code
//looping audio with 0 volume to keep this tab in focus, else chaning tabs will stop the audio
var audio = document.getElementById('notif_audio');
// landmarks of the tips of fingers
finger_tip_landmarks_list = [8, 12, 16, 20, 4];
finger_tip_coords = []
// for the bounding box of the face for area calculation
face_mesh_bbox = [10, 454, 152, 234];
face_mesh_bbox_coords = []
// the lips landmarks
FACEMESH_LIPS = [61, 291, 0, 17]; // [x_start, x_end, y_start, y_end]
facesmash_lips_coords = []
//one arm distance area of bounding box of face
var ideal_area;
//var threshold_area = 0.02;
var bbox_counter_temp = 0;
var face_detected = false;
var hand_detected = false;
var startapp=false;
var count = 0;
//for count-up timer
// var timerVariable = setInterval(countUpTimer, 1000);
var totalSeconds = 0;
function countUpTimer() {
++totalSeconds;
var hour = Math.floor(totalSeconds / 3600);
var minute = Math.floor((totalSeconds - hour * 3600) / 60);
var seconds = totalSeconds - (hour * 3600 + minute * 60);
document.getElementById("count_up_timer").innerHTML = hour + ":" + minute + ":" + seconds;
// accroding to the 20-20-20 rule after 20 minutes look at something 20 feet away for 20 seconds.
if(totalSeconds % (20*60) == 0)
{
audio.play();
}
if(totalSeconds >= 15)
document.getElementById("loading_circle").classList.add('removed');
if(totalSeconds == 15)
document.getElementsByClassName("ready")[0].classList.remove('removed');
if(totalSeconds == 17)
document.getElementsByClassName("ready")[0].classList.add('removed');
}
// calculating area of polygon given the vertices
function calcPolygonArea(v) {
var total = 0;
for (var i = 0, l = v.length; i < l; i++) {
var x = v[i].x;
var y = v[i == v.length - 1 ? 0 : i + 1].y;
var sub_X = v[i == v.length - 1 ? 0 : i + 1].x;
var sub_Y = v[i].y;
total += (x * y * 0.5);
total -= (sub_X * sub_Y * 0.5);
}
return Math.abs(total);
}
var which_finger_inside_quad = [0,0,0,0,0, 0, 0, 0, 0, 0];
function check_points_in_quadrilateral(quadrilateral, points, number_of_points)
{
var finger_in_mouth_bool = false;
for(i=0; i<number_of_points; i++)
{
//console.log();
// points [{x:0.2, y:0.4, z:0.4}]
try
{
if(((points[i]['x']>= quadrilateral[0]['x']) && (points[i]['x']<= quadrilateral[1]['x'])) && ((points[i]['y']>= quadrilateral[2]['y']) && (points[i]['y']<= quadrilateral[3]['y'])) && Math.abs(Math.abs(points[i]['z']) - Math.abs(quadrilateral[1]['z']))<=0.03)
{
which_finger_inside_quad[i] = 1;
}
else
{
which_finger_inside_quad[i] = 0;
}
}
catch(err) {
console.log(err.message);
}
}
if(which_finger_inside_quad.reduce((a,b) => a+b, 0) > 0)
{
which_finger_inside_quad = [0,0,0,0,0, 0, 0, 0, 0, 0];
return true;
}
}
function face_mesh_coords_func(results)
{
facesmash_lips_coords = []
for(i = 0; i<4; i++)
{
facesmash_lips_coords.push(results.multiFaceLandmarks[0][FACEMESH_LIPS[i]]);
}
// for the bounding box of the face for area calculation
face_mesh_bbox_coords = []
for(i = 0; i<4; i++)
{
face_mesh_bbox_coords.push(results.multiFaceLandmarks[0][face_mesh_bbox[i]]);
}
//console.log(facesmash_lips_coords);
}
function finger_in_mouth(results) //finger_tip_landmarks : [{x:___, y:___, x:___},...] for all 21 landmarks
{
var temp;
finger_tip_coords = []
for (i = 0; i<5; i++) //for first hand
{
finger_tip_coords.push(results.multiHandLandmarks[0][finger_tip_landmarks_list[i]]);
}
for (i = 0; i<5; i++) //for second hand
{
if(results.multiHandLandmarks.length > 1)
finger_tip_coords.push(results.multiHandLandmarks[1][finger_tip_landmarks_list[i]]);
}
// if(check_point_in_quadrilateral(facesmash_lips_coords, finger_tip_coords, 4))
// {
// }
//console.log(finger_tip_coords);
temp = check_points_in_quadrilateral(facesmash_lips_coords, finger_tip_coords, results.multiHandLandmarks.length*5);
console.log(hand_detected);
if(temp == true)
{
//prakhar nail biting
if(switch_2==true && hand_detected == true && face_detected == true)
{
audio.volume = 1;
audio.play();
count_temp++;
console.log(count_temp);
document.getElementsByClassName('status_2')[0].innerHTML="DETECTED!";
}
}
else
{
document.getElementsByClassName('status_2')[0].innerHTML="Not detected";
}
}
const videoElement = document.getElementsByClassName('input_video')[0];
var close_to_screen = false;
function onResults(results)
{
if (results.multiFaceLandmarks.length != 0)
{
face_detected = true;
face_mesh_coords_func(results);
if (bbox_counter_temp == 0) // first frame bbox area will be taken as ideal area
{
ideal_area = (calcPolygonArea(face_mesh_bbox_coords));
console.log((calcPolygonArea(face_mesh_bbox_coords)));
}
bbox_counter_temp = 1;
threshold_area = 1.2 * ideal_area;
// if face is close to the screen then area value becomes greater than threshold_area + ideal_area
if(calcPolygonArea(face_mesh_bbox_coords) > threshold_area + ideal_area)
{
//prakhar safe distance
if(switch_1==true && face_detected == true)
{
audio.volume = 1;
audio.play();
count_temp++;
console.log(count_temp);
document.getElementsByClassName('status_1')[0].innerHTML="Unsafe!";
}
//console.log("very close to the screen");
}
else
{
document.getElementsByClassName('status_1')[0].innerHTML="Safe";
}
}
else
{
face_detected = false;
}
}
function onResults1(results)
{
if ((results.multiHandLandmarks.length != 0) && (face_detected == true))
{
hand_detected = true;
finger_in_mouth(results);
}
else
{
document.getElementsByClassName('status_2')[0].innerHTML="Not detected";
hand_detected = false;
}
}
//open-source code taken from mediapipe website
const faceMesh = new FaceMesh({locateFile: (file) => {
return `https://cdn.jsdelivr.net/npm/@mediapipe/face_mesh/${file}`;
}});
faceMesh.setOptions({
maxNumFaces: 1,
refineLandmarks: true,
minDetectionConfidence: 0.8,
minTrackingConfidence: 0.85
});
//open-source code taken from mediapipe website
const hands = new Hands({locateFile: (file) => {
return `https://cdn.jsdelivr.net/npm/@mediapipe/hands/${file}`;
}});
hands.setOptions({
maxNumHands: 2,
modelComplexity: 1,
minDetectionConfidence: 0.8,
minTrackingConfidence: 0.8
});
const camera = new Camera(videoElement, {
onFrame: async () => {
await faceMesh.send({image: videoElement});
await hands.send({image: videoElement});
},
});
function together()
{
faceMesh.onResults(onResults);
hands.onResults(onResults1);
window.requestAnimationFrame(together);
}
document.getElementsByClassName("switch0")[0].addEventListener("click",function(){
if(startapp==false)
{
startapp=true;
}
else
{
startapp=false;
}
if(startapp==true)
{
document.getElementById("loading_circle").classList.remove('removed');
document.getElementsByClassName("ready")[0].classList.add('removed');
setInterval(countUpTimer, 1000);
count++;
camera.start();
}
else if(startapp==false && count!=0)
{
location.reload();
}
})
together();
//</script>