-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample_bbf_face.html
executable file
·216 lines (188 loc) · 6.69 KB
/
sample_bbf_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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="description" content="A JavaScript Computer Vision Library" />
<meta name="author" content="Eugene Zatepyakin" />
<title>JSFeat - JavaScript Computer Vision Library.</title>
<link
rel="stylesheet"
href="https://fonts.googleapis.com/css?family=Droid+Sans:regular,bold|Inconsolata|PT+Sans:400,700"
/>
<link rel="stylesheet" href="css/bootstrap.css" />
<link rel="stylesheet" href="css/jsfeat.css" />
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(["_setAccount", "UA-36419199-1"]);
_gaq.push(["_trackPageview"]);
(function() {
var ga = document.createElement("script");
ga.type = "text/javascript";
ga.async = true;
ga.src =
("https:" == document.location.protocol
? "https://ssl"
: "http://www") + ".google-analytics.com/ga.js";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(ga, s);
})();
</script>
</head>
<body>
<video id="webcam" width="640" height="480" style="display:none;"></video>
<div style=" width:640px;height:480px;margin: 10px auto;">
<canvas id="canvas" width="640" height="480"></canvas>
<div id="no_rtc" class="alert alert-error" style="display:none;"></div>
<div id="log" class="alert alert-info"></div>
</div>
<script
type="text/javascript"
src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"
></script>
<script type="text/javascript" src="js/jsfeat-min.js"></script>
<script type="text/javascript" src="js/bbf_face.js"></script>
<script type="text/javascript" src="js/compatibility.js"></script>
<script type="text/javascript" src="js/profiler.js"></script>
<script type="text/javascript">
$(window).load(function() {
"use strict";
// lets do some fun
var video = document.getElementById("webcam");
var canvas = document.getElementById("canvas");
try {
var attempts = 0;
var readyListener = function(event) {
findVideoSize();
};
var findVideoSize = function() {
if (video.videoWidth > 0 && video.videoHeight > 0) {
video.removeEventListener("loadeddata", readyListener);
onDimensionsReady(video.videoWidth, video.videoHeight);
} else {
if (attempts < 10) {
attempts++;
setTimeout(findVideoSize, 200);
} else {
onDimensionsReady(640, 480);
}
}
};
var onDimensionsReady = function(width, height) {
demo_app(width, height);
compatibility.requestAnimationFrame(tick);
};
video.addEventListener("loadeddata", readyListener);
compatibility.getUserMedia(
{ video: true },
function(stream) {
try {
video.src = compatibility.URL.createObjectURL(stream);
} catch (error) {
video.srcObject = stream;
}
setTimeout(function() {
video.play();
}, 500);
},
function(error) {
$("#canvas").hide();
$("#log").hide();
$("#no_rtc").html("<h4>WebRTC not available.</h4>");
$("#no_rtc").show();
}
);
} catch (error) {
$("#canvas").hide();
$("#log").hide();
$("#no_rtc").html("<h4>Something goes wrong...</h4>");
$("#no_rtc").show();
}
var stat = new profiler();
var ctx, canvasWidth, canvasHeight;
var img_u8, work_canvas, work_ctx;
var max_work_size = 160;
function demo_app(videoWidth, videoHeight) {
canvasWidth = canvas.width;
canvasHeight = canvas.height;
ctx = canvas.getContext("2d");
ctx.fillStyle = "rgb(0,255,0)";
ctx.strokeStyle = "rgb(0,255,0)";
var scale = Math.min(
max_work_size / videoWidth,
max_work_size / videoHeight
);
var w = (videoWidth * scale) | 0;
var h = (videoHeight * scale) | 0;
img_u8 = new jsfeat.matrix_t(w, h, jsfeat.U8_t | jsfeat.C1_t);
work_canvas = document.createElement("canvas");
work_canvas.width = w;
work_canvas.height = h;
work_ctx = work_canvas.getContext("2d");
jsfeat.bbf.prepare_cascade(jsfeat.bbf.face_cascade);
stat.add("bbf detector");
}
function tick() {
compatibility.requestAnimationFrame(tick);
stat.new_frame();
if (video.readyState === video.HAVE_ENOUGH_DATA) {
ctx.drawImage(video, 0, 0, canvasWidth, canvasHeight);
work_ctx.drawImage(
video,
0,
0,
work_canvas.width,
work_canvas.height
);
var imageData = work_ctx.getImageData(
0,
0,
work_canvas.width,
work_canvas.height
);
stat.start("bbf detector");
jsfeat.imgproc.grayscale(
imageData.data,
work_canvas.width,
work_canvas.height,
img_u8
);
// possible options
//jsfeat.imgproc.equalize_histogram(img_u8, img_u8);
var pyr = jsfeat.bbf.build_pyramid(img_u8, 24 * 2, 24 * 2, 4);
var rects = jsfeat.bbf.detect(pyr, jsfeat.bbf.face_cascade);
rects = jsfeat.bbf.group_rectangles(rects, 1);
stat.stop("bbf detector");
// draw only most confident one
draw_faces(ctx, rects, canvasWidth / img_u8.cols, 1);
$("#log").html(stat.log());
}
}
function draw_faces(ctx, rects, sc, max) {
var on = rects.length;
if (on && max) {
jsfeat.math.qsort(rects, 0, on - 1, function(a, b) {
return b.confidence < a.confidence;
});
}
var n = max || on;
n = Math.min(n, on);
var r;
for (var i = 0; i < n; ++i) {
r = rects[i];
ctx.strokeRect(
(r.x * sc) | 0,
(r.y * sc) | 0,
(r.width * sc) | 0,
(r.height * sc) | 0
);
}
}
$(window).unload(function() {
video.pause();
video.src = null;
});
});
</script>
</body>
</html>