-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
214 lines (184 loc) · 7.09 KB
/
Copy pathscript.js
File metadata and controls
214 lines (184 loc) · 7.09 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
const video = document.getElementById('video')
let nowBlinking = false;
let vThreshold = 1.3;
Promise.all([
faceapi.nets.tinyFaceDetector.loadFromUri('./facemodels'),
faceapi.nets.faceLandmark68Net.loadFromUri('./facemodels'),
faceapi.nets.faceRecognitionNet.loadFromUri('./facemodels'),
faceapi.nets.faceExpressionNet.loadFromUri('./facemodels')
]).then(startVideo)
function startVideo() {
console.log("PC");
navigator.getUserMedia(
{ video: {} },
stream => video.srcObject = stream,
err => console.error(err)
)
}
console.log("video:"+[video.width, video.height]);
// let div = document.createElement('div')
// div.innerText = 'video size:'+video.width+', '+video.height
// console.log(div.innerText);
// document.body.appendChild(div)
video.addEventListener('play', () => {
var canvas_bg = document.createElement("canvas");
canvas_bg.setAttribute("id", "irrelevantCanvas");
canvas_bg.width = video.width;
canvas_bg.height = video.height;
document.body.append(canvas_bg)
var ctx_bg = canvas_bg.getContext('2d');
// ctx_bg.fillStyle = "rgb(0,0,0)";
// ctx_bg.fillRect(0, 0, video.width, video.height/2);
var canvas_face = document.createElement("canvas");
canvas_face.setAttribute("id", "irrelevantCanvas");
canvas_face.width = video.width;
canvas_face.height = video.height;
var ctx_face = canvas_face.getContext('2d');
const canvas = faceapi.createCanvasFromMedia(video)
canvas.setAttribute("id", "irrelevantCanvas")
document.body.append(canvas)
const displaySize = { width: video.width, height: video.height }
faceapi.matchDimensions(canvas, displaySize)
var t1 = performance.now();
var irisC = [];
let blinkCount = 0;
setInterval(async () => {
//const detections = await faceapi.detectAllFaces(video, new faceapi.TinyFaceDetectorOptions()).withFaceLandmarks().withFaceExpressions()
const detections = await faceapi.detectAllFaces(video, new faceapi.TinyFaceDetectorOptions()).withFaceLandmarks()
const resizedDetections = faceapi.resizeResults(detections, displaySize)
canvas.getContext('2d').clearRect(0, 0, canvas.width, canvas.height)
//faceapi.draw.drawDetections(canvas, resizedDetections)
faceapi.draw.drawFaceLandmarks(canvas, resizedDetections)
//faceapi.draw.drawFaceExpressions(canvas, resizedDetections)
//console.log(resizedDetections);
const landmarks = resizedDetections[0].landmarks;
//console.log(landmarks);
const landmarkPositions = landmarks.positions;
//--- Iric mark ---//
ctx_bg.clearRect(0, 0, canvas_bg.width, canvas_bg.height)
var x_ = landmarkPositions[38-1].x
var y_ = landmarkPositions[38-1].y
var w_ = landmarkPositions[39-1].x - landmarkPositions[38-1].x
var h_ = landmarkPositions[42-1].y - landmarkPositions[38-1].y
ctx_bg.fillStyle = "rgb(255,0,0)";
ctx_bg.fillRect(x_, y_, w_, h_)
x_ = landmarkPositions[44-1].x
y_ = landmarkPositions[44-1].y
w_ = landmarkPositions[45-1].x - landmarkPositions[44-1].x
h_ = landmarkPositions[48-1].y - landmarkPositions[44-1].y
ctx_bg.fillRect(x_, y_, w_, h_)
//--- Face mask ---//
ctx_bg.fillStyle = 'rgb(0,200,0)';
ctx_bg.beginPath();
ctx_bg.moveTo(landmarkPositions[0].x, landmarkPositions[0].y);
for(var i=1;i<17;i++){
ctx_bg.lineTo(landmarkPositions[i].x, landmarkPositions[i].y);
}
ctx_bg.fill();
ctx_bg.moveTo(landmarkPositions[0].x, landmarkPositions[0].y);
ctx_bg.lineTo(landmarkPositions[17].x, landmarkPositions[17].y);
ctx_bg.lineTo(landmarkPositions[27].x, landmarkPositions[17].y);
ctx_bg.lineTo(landmarkPositions[27].x, landmarkPositions[0].y);
//ctx_bg.lineTo(landmarkPositions[26].x, landmarkPositions[26].y);
ctx_bg.lineTo(landmarkPositions[16].x, landmarkPositions[16].y);
ctx_bg.lineTo(landmarkPositions[16].x, landmarkPositions[16].y-200);
ctx_bg.lineTo(landmarkPositions[0].x, landmarkPositions[0].y-200);
ctx_bg.lineTo(landmarkPositions[0].x, landmarkPositions[0].y);
ctx_bg.fill();
//--- Iris value ---//
ctx_face.clearRect(0, 0, canvas_face.width, canvas_face.height)
ctx_face.drawImage(video, 0, 0, video.width, video.height);
var frame = ctx_face.getImageData(0, 0, video.width, video.height);
var p_ = Math.floor(x_+w_/2) + Math.floor(y_+h_/2) * video.width
//console.log("eye_RGB:"+[frame.data[p_*4+0], frame.data[p_*4+1], frame.data[p_*4+2]]);
var v_ = Math.floor( (frame.data[p_*4+0] + frame.data[p_*4+1] + frame.data[p_*4+2])/3 );
//console.log("irisC:"+v_);
irisC.push(v_);
if(irisC.length>100){
irisC.shift();
}//
let meanIrisC = irisC.reduce(function(sum, element){
return sum + element;
}, 0);
meanIrisC = meanIrisC / irisC.length;
let currentIrisC = irisC[irisC.length-1];
if(irisC.length==100){
if(nowBlinking==false){
if(currentIrisC>=meanIrisC*vThreshold){
nowBlinking = true;
}//
}//
else{
if(currentIrisC<meanIrisC*vThreshold){
nowBlinking = false;
blinkCount += 1;
}//
}//
}//
// //--- Iris position ---// 36 -> 39
// let horizontal_eye = [];
// var x_s = Math.floor( landmarkPositions[36].x )
// var x_e = Math.floor( landmarkPositions[39].x )
// var py = Math.floor( landmarkPositions[36].y )
// for(var x=x_s;x<=x_e;x++){
// p_ = x + py * video.width
// v_ = (frame.data[p_*4+0] + frame.data[p_*4+1] + frame.data[p_*4+2])/3
// horizontal_eye.push(v_)
// }
//--- Graph ---//
ctx_bg.strokeStyle = 'red';
ctx_bg.lineWidth = 5;
var Ox = 0;
var Oy = canvas_bg.height/2;
var Lx = canvas_bg.width;
var Ly = canvas_bg.height/2;
var vx = 0/irisC.length * Lx;
var vy = irisC[0]/255 * Ly;
ctx_bg.beginPath();
ctx_bg.moveTo(Ox+vx, Oy-vy);
for(var i=1;i<irisC.length;i++){
vx = i/irisC.length * Lx;
vy = irisC[i]/255 * Ly;
ctx_bg.lineTo(Ox+vx, Oy-vy);
}
ctx_bg.stroke();
//--- mean value x threshold(1.X)
ctx_bg.strokeStyle = 'rgb(0,255,0)';
ctx_bg.lineWidth = 2;
ctx_bg.beginPath();
vx = 0 * Lx;
vy = meanIrisC*vThreshold/255 * Ly;
ctx_bg.moveTo(Ox+vx, Oy-vy);
vx = 1 * Lx;
ctx_bg.lineTo(Ox+vx, Oy-vy);
ctx_bg.stroke();
// //--- Graph ---//
// Ox = 0;
// Oy = canvas_bg.height;
// Lx = canvas_bg.width/2;
// Ly = canvas_bg.height/2;
// vx = 0;
// vy = horizontal_eye[0]/255 * Ly;
// ctx_bg.beginPath();
// ctx_bg.moveTo(Ox+vx, Oy-vy);
// for(var i=1;i<horizontal_eye.length;i++){
// vx = i/horizontal_eye.length * Lx;
// vy = horizontal_eye[i]/255 * Ly;
// ctx_bg.lineTo(Ox+vx, Oy-vy);
// }
// ctx_bg.stroke();
var ctx = canvas.getContext('2d');
var t2 = performance.now();//ms
ctx.font = "48px serif";
ctx.fillText("FPS:"+ Math.floor(1000.0/(t2-t1)), 10, 50);
ctx.fillText("Count:"+blinkCount, 10, 100);
if(!nowBlinking){
console.log(blinkCount);
}
if(nowBlinking){
console.log("Blinking");
}
//ctx.fillText("FPS:"+ (t2-t1), 10, 50);
t1 = t2;
}, 75)
})