-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
138 lines (121 loc) · 4.36 KB
/
Copy pathindex.html
File metadata and controls
138 lines (121 loc) · 4.36 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>人脸识别</title>
<!--
face-api可以放在自己的服务器上使用url引入加载,例如:
<script src="http://127.0.0.1:8081/face-api.min.js"></script>
-->
<script src="./dist/face-api.js"></script>
<style>
#image {
width: 500px;
height: auto;
position: absolute;
left: 100px;
top: 100px;
}
#btn {
width: 90px;
height: 40px;
border-radius: 5px;
background: #40a3ef;
color: #fff;
cursor: pointer;
font-size: 16px;
}
.btn-area{
margin: 5px 10px;
border: 2px solid #ccc;
}
.btn-area > button{
padding: 10px;
border-radius: 5px;
background: #40a3ef;
color: #fff;
cursor: pointer;
font-size: 16px;
border-radius: 8px;
margin-right: 5px;
}
#canvas,
#video {
position: absolute;
left: 100px;
top: 100px;
}
</style>
</head>
<body>
<button id="btn">开始检测</button>
<canvas id="canvas" width="1200" height="800" style="border:1px solid #d3d3d3;">
您的浏览器不支持canvas
</canvas>
<div class="btn-area">
<span style="color: orange;"><b>其他示例:</b></span>
<button><a href="./input-face.html">自定义图片检测示例</a></button>
<button><a href="./video-face.html">摄像头人脸识别检测示例</a></button>
<button><a href="./faceapi-draw.html">faceapi内置方法绘制识别框示例</a></button>
</div>
<script>
const image = document.getElementById('image');
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const btn = document.getElementById('btn');
// 窗口加载触发
window.onload = function () {
// 将图片绘制到canvas
drawImage()
btn.onclick = function () {
loadModel()
}
}
// 将图片绘制到canvas
function drawImage(params) {
var img = new Image();
img.onload = function () {
ctx.drawImage(img, 0, 0);
}
img.src = './MR.jpg'; // 替换为你的图片源
}
// 绘制识别框的方法
function drawRect(x, y, width, height, color, weight) {
ctx.strokeStyle = color
ctx.lineWidth = weight
ctx.strokeRect(x, y, width, height)
}
// 绘制文本
function drawText(text, x, y, fontSize) {
ctx.font = fontSize + " " + "Arial";
ctx.strokeText(text, x, y)
}
// 置信度处理
function financial(x) {
return Number.parseFloat(x).toFixed(4);
}
async function loadModel(params) {
// 加载模型,模型的Json和shard文件必须位于同一目录
await faceapi.loadSsdMobilenetv1Model('./weights/')
// 全局神经网络实例
console.log(faceapi.nets)
// 检测图像中的所有人脸
const detections = await faceapi.detectAllFaces(canvas)
console.log("detections", detections);
for (let index = 0; index < detections.length; index++) {
const item = detections[index];
console.log("item", item);
drawRect(item._box._x, item._box._y, item._box._width, item._box._height, "orange", 2)
drawText(`置信度:${financial(item._score)}`, item._box._x + 1, item._box._y - 5, "20px")
}
// 检测图像中置信度得分最高的人脸
const detection = await faceapi.detectSingleFace(canvas)
console.log("detection", detection);
console.log(`得分:${detection._score}`);
drawRect(detection._box._x, detection._box._y, detection._box._width, detection._box._height, "red", 2)
drawText(`置信度:${financial(detection._score)}`, detection._box._x + 1, detection._box._y - 5, "20px")
}
</script>
</body>
</html>