-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminno_mesh.js
239 lines (179 loc) · 9.55 KB
/
minno_mesh.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
function init_minno_mesh(global) {
global.container = document.createElement('container');
global.canvas_div = document.createElement('div');
global.canvas_div.style["margin-left"] = 'auto';
global.canvas_div.style["margin-right"] = 'auto';
global.canvas_div.style.position = 'absolute';
global.canvas_div.style.bottom = "0";
global.full_video = document.createElement('video');
global.full_video.className = "input_video";
global.full_video.width = "1280px";
global.full_video.height = "720px";
global.full_canvas = document.createElement('canvas');
global.full_canvas.className = "output_canvas";
global.full_canvas.width = 256;
global.full_canvas.height = 144;
global.full_canvas.style.display = 'block';
global.full_canvas.style["margin"] = 'auto';
// global.full_canvas.style["border-style"] = "dotted";
global.canvas_div.appendChild(global.full_video);
global.container.appendChild(global.canvas_div);
global.container.appendChild(global.full_canvas);
document.body.appendChild(global.container);
global.videoElement = document.getElementsByClassName('input_video')[0];
global.eye_data = [];
global.start_recording = start_recording;
global.stop_recording = stop_recording;
global.stopVideo = stop_video;
global.get_all = get_all;
global.detectionNeeded = false;
global.faceMesh = new FaceMesh({locateFile: (file) => {
return `https://cdn.jsdelivr.net/npm/@mediapipe/face_mesh/${file}`;
}});
global.faceMesh.setOptions({
maxNumFaces: 1,
selfieMode: true,
refineLandmarks: true,
minDetectionConfidence: 0.5,
minTrackingConfidence: 0.5
});
global.faceMesh.onResults(parse_data, global);
global.camera = new Camera(global.videoElement, {
onFrame: async () => {
if (global.stop_me)
return stop_video(global);
await global.faceMesh.send({image: global.videoElement});
// console.log({val:global.stop_me});
},
width: 1280,
height: 720
});
global.camera.start();
global.stop_me = false;
function parse_data(results) {
const canvasElement = document.getElementsByClassName('output_canvas')[0];
const canvasCtx = canvasElement.getContext('2d');
// canvasCtx.font = "30px Comic Sans MS";
// canvasCtx.fillStyle = "yellow";
canvasCtx.save();
canvasCtx.clearRect(0, 0, canvasElement.width, canvasElement.height);
canvasCtx.drawImage(results.image, 0, 0, canvasElement.width, canvasElement.height);
canvasCtx.strokeStyle = "#00ff00";
canvasCtx.lineWidth = 5;
canvasCtx.strokeRect(0.3*global.full_canvas.width, 0.1*global.full_canvas.height, 0.4*global.full_canvas.width, 0.8*global.full_canvas.height);
if (global.detectionNeeded && results.multiFaceLandmarks) {
if(results.multiFaceLandmarks.length==0){
canvasCtx.lineWidth = 5;
canvasCtx.strokeStyle = "#ff0000";
canvasCtx.strokeRect(0.3*global.full_canvas.width, 0.1*global.full_canvas.height, 0.4*global.full_canvas.width, 0.8*global.full_canvas.height);
}
for (const landmarks of results.multiFaceLandmarks) {
const face_height = landmarks[152].y-landmarks[10].y;
// drawConnectors(canvasCtx, landmarks, FACEMESH_FACE_OVAL, {color: '#30FF30'});
// drawConnectors(canvasCtx, landmarks, FACEMESH_TESSELATION,
// {color: '#30FF30', lineWidth: 1});
const vertices = FACEMESH_FACE_OVAL.flatMap(num => num);
const oval_size = calcPolygonArea(landmarks, vertices);
// canvasCtx.fillText(oval_size, global.full_canvas.width/2, global.full_canvas.height/4);
const left_eye_vertices = FACEMESH_LEFT_EYE.flatMap(num => num);
const right_eye_vertices = FACEMESH_RIGHT_EYE.flatMap(num => num);
const eye_sizes = (calcPolygonArea(landmarks, left_eye_vertices) + calcPolygonArea(landmarks, right_eye_vertices))/2;
const ratio = 100*100*(eye_sizes/oval_size);
const ratio2 = Math.round(100*calcratio2(landmarks));
// console.log(ratio2);
canvasCtx.fillStyle = "#ffffff";
canvasCtx.font = "30px Verdana";
// canvasCtx.fillText(ratio2, global.full_canvas.width/2, global.full_canvas.height/2);
global.eye_data.push({t: new Date().getTime(), ratio2});
if (landmarks[33].x<0.31 || landmarks[263].x>0.69 ||
landmarks[33].y<0.21 || landmarks[263].y<0.21 ||
landmarks[33].x>0.79 || landmarks[263].x>0.79){
canvasCtx.strokeStyle = "#ff0000";
canvasCtx.strokeRect(0.3*global.full_canvas.width, 0.1*global.full_canvas.height, 0.4*global.full_canvas.width, 0.8*global.full_canvas.height);
}
// canvasCtx.fillStyle = "#ffffff";
// canvasCtx.font = "20px";
// // left (385-380) + (387-373)/ (362-263)
// // right (160-144) + (158-153) / (33-133)
// console.log(right_eye_vertices);
// const vals = [33, 263];
// for (let i = 0; i < right_eye_vertices.length-1; i++) {
// var point0 = landmarks[right_eye_vertices[i]];
// var x = point0.x;
// var y = point0.y;
// canvasCtx.fillText(right_eye_vertices[i], x*global.full_canvas.width, y*global.full_canvas.height);
// }
// for (let i = 0; i < left_eye_vertices.length-1; i++) {
// var point0 = landmarks[left_eye_vertices[i]];
// var x = point0.x;
// var y = point0.y;
// canvasCtx.fillText(left_eye_vertices[i], x*global.full_canvas.width, y*global.full_canvas.height);
// }
// const vals = [385, 380, 387, 373, 362, 263, 160, 144, 158, 153, 33, 133];
// for (let i = 0; i < vals.length; i++) {
// var point0 = landmarks[vals[i]];
// var x = point0.x;
// var y = point0.y;
// canvasCtx.fillText(vals[i], x*global.full_canvas.width, y*global.full_canvas.height);
// }
// canvasCtx.fillText("eyes: " + Math.round(1000*eye_sizes) + ", oval: " +Math.round(1000*oval_size), global.full_canvas.width/3, global.full_canvas.height/5);
// if (face_height>0.4){
// drawConnectors(canvasCtx, landmarks, FACEMESH_FACE_OVAL, {color: '#FF3030'});
// drawConnectors(canvasCtx, landmarks, FACEMESH_TESSELATION,
// {color: '#FF3030', lineWidth: 1});
// }
// if (ratio<10){
// // canvasCtx.fillText("Blink! " /*+ 100*100*(eye_sizes/oval_size)*/, global.full_canvas.width/2, global.full_canvas.height/6);
// drawConnectors(canvasCtx, landmarks, FACEMESH_FACE_OVAL, {color: '#FFFFFF'});
// // drawConnectors(canvasCtx, landmarks, FACEMESH_TESSELATION, {color: '#FFFFFF', lineWidth: 1});
// }
// drawConnectors(canvasCtx, landmarks, FACEMESH_RIGHT_EYE, {color: '#FF3030'});
// drawConnectors(canvasCtx, landmarks, FACEMESH_LEFT_EYE, {color: '#FF3030'});
}
}
canvasCtx.restore();
}
}
function pythagoras(pointA, pointB){
return Math.sqrt(Math.pow(pointA.x-pointB.x, 2) + Math.pow(pointA.y-pointB.y, 2));
}
function calcratio2(landmarks) {
var left = (pythagoras(landmarks[385], landmarks[380])+pythagoras(landmarks[387], landmarks[373]))/(2*pythagoras(landmarks[362], landmarks[263]));
var right = (pythagoras(landmarks[160], landmarks[144])+pythagoras(landmarks[158], landmarks[153]))/(2*pythagoras(landmarks[33], landmarks[133]));
return 0.5*(left+right);
// left (385-380) + (387-373)/ (362-263)
// right (160-144) + (158-153) / (33-133)
}
function calcPolygonArea(landmarks, vertices) {
var total = 0;
for (var i = 0, l = vertices.length; i < l; i++) {
var addX = landmarks[vertices[i]].x;
var addY = landmarks[vertices[i == vertices.length - 1 ? 0 : i + 1]].y;
var subX = landmarks[vertices[i == vertices.length - 1 ? 0 : i + 1]].x;
var subY = landmarks[vertices[i]].y;
total += (addX * addY * 0.5);
total -= (subX * subY * 0.5);
}
return 1000*Math.abs(total);
}
function get_all(global) {
return global.eye_data;
}
function start_recording(global) {
global.detectionNeeded = true;
global.eye_data = [];
}
function stop_recording(global) {
global.detectionNeeded = false;
}
function stop_video(global) {
global.stop_me = true;
// console.log('bang!');
global.detectionNeeded = false;
document.body.removeChild(global.container);
global.camera.stop();
global.camera = false;
// global.facemesh = false;
// global.facemesh2 = false;
global.minno_mesh = false;
}