-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracking-face.html
149 lines (109 loc) · 4.77 KB
/
tracking-face.html
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
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>tracking.js - face with camera</title>
<script src="../build/tracking-min.js"></script>
<script src="../build/data/face-min.js"></script>
<style>
video, canvas {
margin: 0, 0, 0, 0 ;
position: absolute;
}
#extracted-canvas {
display: none !important;
margin-top: 252px;
}
img[src="#"] {
display: none;
}
#extracted-face {
margin-top: 246px;
}
</style>
</head>
<body>
<div class="demo-title">
<p><a href="http://trackingjs.com" target="_parent">tracking.js</a> - get user's webcam and detect faces</p>
</div>
<video id="video" width="320" height="240" preload autoplay loop muted></video>
<canvas id="canvas" width="320" height="240"></canvas>
<div><canvas id="extracted-canvas" width="400" height="500"></canvas></div>
<a href="/#" id="png-dl"><img src="#" id="extracted-face"></a>
<script>
window.onload = function() {
var video = document.getElementById('video');
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
var tracker = new tracking.ObjectTracker('face');
//canvas to put extracted face into
var extracted_canvas = document.getElementById('extracted-canvas');
var extracted_context = extracted_canvas.getContext('2d');
const ext_width = extracted_canvas.width;
const ext_height = extracted_canvas.height;
tracker.setInitialScale(4);
tracker.setStepSize(2);
tracker.setEdgesDensity(0.1);
tracking.track('#video', tracker, { camera: true });
tracker.on('track', function(event) {
context.clearRect(0, 0, canvas.width, canvas.height);
extracted_context.clearRect(0, 0, extracted_canvas.width, extracted_canvas.height);
event.data.forEach(function(rect) {
context.strokeStyle = '#a64ceb';
context.strokeRect(rect.x, rect.y, rect.width, rect.height);
context.font = '11px Helvetica';
context.fillStyle = "#fff";
context.fillText('x: ' + rect.x + 'px', rect.x + rect.width + 5, rect.y + 11);
context.fillText('y: ' + rect.y + 'px', rect.x + rect.width + 5, rect.y + 22);
//cropping
var myrect_centx = (rect.x + rect.width/2);
var myrect_centy = (rect.y + rect.height/2);
//for some reason the units don't align, i think one var is in pixels and another is in pts or something
//this bit helps calculate compensation.
var centx = myrect_centx/ext_width;
var centy = myrect_centy/ext_height;
//take an area and put it into the new canvas, scaling to fit the canvas
//(should've used a var to create and set canvas height but it's too late for that)
extracted_context.drawImage(image=video,
rect.x+(centx*rect.width*2.4), rect.y+(centy*rect.height*1.5),
rect.width*2, rect.height*2.5,
0, 0,
400, 500
);
//get the img and the hyperlink tag as js objects
var extracted_face = document.getElementById('extracted-face');
var png_dl_link = document.getElementById('png-dl');
//making the png
//jpg works too but i think jpg data is harder for tensorflow to work with, needs preprocessing
dataURL = extracted_canvas.toDataURL('image/png;base64');
//set the image source and hyperlink to the png
extracted_face.src = dataURL
png_dl_link.href = dataURL
//fun but useless:
/*extracted_canvas.toBlob(function(blob) {
var newImg = document.createElement('img'),
url = URL.createObjectURL(blob);
newImg.onload = function() {
// no longer need to read the blob so it's revoked
URL.revokeObjectURL(url);
};
newImg.src = url;
document.body.appendChild(newImg);
});*/
// console.log(centx);
// console.log(centy);
// console.log("centers");
// console.log(rect.x);
// console.log(rect.width);
// console.log("x");
// console.log(rect.y);
// console.log(rect.height);
// console.log("y");
// console.log(rect.width + " rec.width");
// console.log(rect.height + " rect.height");
});
});
};
</script>
</body>
</html>