-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.html
85 lines (76 loc) · 2.39 KB
/
index.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
<html>
<head>
<title>Face Detection Test</title>
<style>
.app-container {
display: flex;
flex-direction: column;
text-align: center;
}
#detect {
margin: 20px auto;
width: 80%;
}
</style>
</head>
<body>
<div class="app-container">
<div class="image">
<img id="image" hidden src="./faces.png">
<canvas id="canvas" width="493px" height="329px"></canvas>
</div>
<button id="detect" onclick="detect()" class="btn btn-lg btn-primary">检测</button>
</div>
<footer></footer>
<script>
var image = document.getElementById('image');
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext("2d");
var scale = 1;
image.onload = function () {
ctx.drawImage(image,
0, 0, image.width, image.height,
0, 0, canvas.width, canvas.height);
scale = canvas.width / image.width;
};
function detect() {
if (window.FaceDetector == undefined) {
console.error('Face Detection not supported');
return;
}
var faceDetector = new FaceDetector();
console.time('detect');
return faceDetector.detect(image)
.then(faces => {
console.log(faces);
// Draw the faces on the <canvas>.
var ctx = canvas.getContext("2d");
ctx.lineWidth = 2;
ctx.strokeStyle = "red";
for (var i = 0; i < faces.length; i++) {
var item = faces[i].boundingBox;
ctx.rect(Math.floor(item.x * scale),
Math.floor(item.y * scale),
Math.floor(item.width * scale),
Math.floor(item.height * scale));
ctx.stroke();
}
// Add the faces as strings to the <footer>
// var footer = document.getElementsByTagName('footer')[0];
// footer.innerHTML =
// '<p>Detected ' + faces.length + ' faces</p><ul>';
// for (var i = 0; i < faces.length; i++) {
// footer.innerHTML +=
// '<li>@ (' + faces[i].boundingBox.x + ',' + faces[i].boundingBox.y + '), size ' +
// faces[i].boundingBox.width + 'x' + faces[i].boundingBox.height + '</li>';
// }
// footer.innerHTML += '</ul>';
console.timeEnd('detect');
})
.catch((e) => {
console.error("Boo, Face Detection failed: " + e);
});
}
</script>
</body>
</html>