# 配套vue界面
# 下载
npm install tracking --save
1
# 代码示例
<template>
<div class="testTracking">
<video
id="video"
width="418"
height="270"
preload
autoplay
loop
muted
></video>
<canvas id="canvas" width="418" height="270"></canvas>
<div class="buttonDiv">
<button type="button" @click="submit">登陆认证</button>
<button type="button" @click="popup.open = true">拍照注册</button>
</div>
<el-dialog title="请输入您的信息,请不要移动" :visible.sync="popup.open">
<el-form ref="form" label-width="80px" :model="popup" :rules="rules">
<el-form-item label="用户名" prop="username">
<el-input v-model="popup.username"></el-input>
</el-form-item>
<el-form-item label="手机号" prop="phone">
<el-input v-model="popup.phone"></el-input>
</el-form-item>
<el-form-item label="邮箱" prop="mail">
<el-input v-model="popup.mail"></el-input>
</el-form-item>
</el-form>
<span slot="footer" class="dialog-footer">
<el-button @click="popup.open = false">取 消</el-button>
<el-button type="primary" @click="zhuce('form')">确 定</el-button>
</span>
</el-dialog>
</div>
</template>
<script>
require("tracking/build/tracking-min.js");
require("tracking/build/data/face-min.js");
require("tracking/build/data/eye-min.js");
require("tracking/build/data/mouth-min.js");
require("tracking/examples/assets/stats.min.js");
export default {
name: "Home",
data() {
return {
recty: 0,
rectx: 0,
trackerTask: undefined,
popup: {
open: false,
username: undefined,
phone: undefined,
mail: undefined,
},
rules: {
username: [
{
required: true, //是否必填
message: "用户名不能为空", //规则
trigger: "blur", //何事件触发
},
],
phone: [
{
required: true, //是否必填
message: "手机号不能为空", //规则
trigger: "blur", //何事件触发
},
],
mail: [
{
required: true, //是否必填
message: "邮箱不能为空", //规则
trigger: "blur", //何事件触发
},
],
},
};
},
mounted() {
this.openCamera();
},
methods: {
openCamera() {
var video = document.getElementById("video");
navigator.mediaDevices
.getUserMedia({ video: { facingMode: "user" } })
.then(function (stream) {
document.getElementById("video").srcObject = stream;
})
.catch(function (error) {
alert(error);
});
var canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");
var tracker = new tracking.ObjectTracker("face");
tracker.setInitialScale(4);
tracker.setStepSize(2);
tracker.setEdgesDensity(0.1);
this.trackerTask = tracking.track("#video", tracker, { camera: true });
tracker.on("track", (event) => {
context.clearRect(0, 0, canvas.width, canvas.height);
event.data.forEach((rect) => {
context.font = "11px Helvetica";
context.fillText(
"已识别到人脸,请点击拍照",
rect.x - 30,
rect.y - 40
);
context.strokeStyle = "#a64ceb";
context.strokeRect(
rect.x - 20,
rect.y - 20,
rect.width + 20,
rect.height + 20
);
this.rectx = rect.x - 110;
this.recty = rect.y - 10;
});
});
},
zhuce(formName) {
var formData = new FormData();
let canvas = document.getElementById("canvas");
let context = canvas.getContext("2d");
let video = document.getElementById("video");
context.drawImage(video, -this.rectx, -this.recty, 500, 400);
let imgUrl = this.saveAsPNG(canvas);
this.$refs[formName].validate((valid) => {
if (valid) {
console.log(imgUrl);
formData.append("file", imgUrl);
formData.append("mail", this.popup.mail);
formData.append("username", this.popup.username);
formData.append("phone", this.popup.phone);
this.axios({
method: "post",
url: "https://face-890657-1305567820.ap-shanghai.run.tcloudbase.com/face/registration",
// url: "http://192.168.0.106:5001/face/registration",
data: formData,
headers: {
"Content-Type": "multipart/form-data",
},
}).then((response) => {
console.log(response);
if (response.data.code == "200") {
this.$message({
type: "success",
message: "注册成功",
});
} else {
this.$message({
type: "error",
message: "失败",
});
}
});
} else {
return false;
}
});
},
submit() {
var formData = new FormData();
let canvas = document.getElementById("canvas");
let context = canvas.getContext("2d");
let video = document.getElementById("video");
context.drawImage(video, -this.rectx, -this.recty, 500, 400);
let imgUrl = this.saveAsPNG(canvas);
console.log(imgUrl);
formData.append("file", imgUrl);
this.$prompt("请不要移动", "请输入邮箱", {
confirmButtonText: "确定",
cancelButtonText: "取消",
inputPattern:
/[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?/,
inputErrorMessage: "邮箱格式不正确",
})
.then(({ value }) => {
formData.append("mail", value);
this.axios({
method: "post",
url: "https://face-890657-1305567820.ap-shanghai.run.tcloudbase.com/face/verification",
// url: "http://192.168.0.106:5001/face/verification",
data: formData,
headers: {
"Content-Type": "multipart/form-data",
},
})
.then((response) => {
console.log(response);
if (response.data.is_picture_of_obama) {
this.$message({
type: "success",
message: "欢迎登陆: " + response.data.username,
});
} else {
this.$message({
type: "error",
message: "失败",
});
}
})
.catch(() => {
this.$message({
type: "info",
message: "服务器异常",
});
});
})
.catch(() => {
this.$message({
type: "info",
message: "取消输入",
});
});
},
// 保存为png,base64格式图片
saveAsPNG(c) {
return c.toDataURL("image/jpeg").split(";base64,")[1];
},
},
destroyed() {
// 停止侦测
this.trackerTask.stop();
// 关闭摄像头
this.trackerTask.closeCamera();
},
};
</script>
<style lang="less" scoped>
.testTracking {
height: 100vh;
width: 100%;
position: relative;
> * {
position: absolute;
left: 0;
right: 0;
margin: auto;
}
video,
canvas {
top: 0;
}
.buttonDiv {
bottom: 10px;
}
}
</style>
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263