安装 npm install tracking -s
复制代码
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<template> <div class="face" v-show="isShowFace"> <div class="faceBack"></div> <div class="faceDataDlo"> <div class="titleDialog" > <p>人脸识别</p><i></i> <em class="el-icon-close detIcon" @click="isOpenShow()"></em> </div> <div class="faceDiv" v-if="isSuccessful === ''"> <p>请衣着整齐,坪石摄像头,并正对光源</p> <div class="faceData"> <div class="leftTop"></div> <div class="rightTop"></div> <div class="leftButton"></div> <div class="rightButton"></div> <i class="faceDataLine"></i> <video autoplay playsinline id="video" width="300" height="300"></video> <canvas id="canvas" width="300" height="300"></canvas> </div> <el-button class="refer-btn" type="primary">人脸识别中...</el-button> </div> <!-- 审核中--> <div class="faceDivSuccessful" v-if="isSuccessful === 'audit'"> <img class="auditImg" src="../assets/images/auditImg.png" alt=""> <p>人工正在识别中请耐心等待...</p> <p style="width: 400px"><el-progress :percentage="waitingTme" :format="format"></el-progress></p> </div> <!-- 成功--> <div class="faceDivSuccessful" v-if="isSuccessful === 'successful'"> <img src="../assets/images/faceSuccessful.png" alt=""> <p>已识别成功<br> 点击下方按钮就开始考试</p> <el-button class="refer-btn" type="primary" @click="isRouter()">{{isOnline? '继续考试' : '开始考试'}}</el-button> </div> <!-- 失败--> <div class="faceDivSuccessful" v-if="isSuccessful === 'failure'"> <img src="../assets/images/faceErr.png" alt=""> <p>识别失败<br> 请拨打下方电话,联系管理员<br> 400-038-8978 </p> <el-button class="refer-btn" type="primary">申请人工审核</el-button> <el-button class="refer-btn" type="primary" @click="setSuccessful()">重新识别</el-button> </div> </div> </div> </template> <script> require('tracking/build/tracking-min.js'); require('tracking/build/data/face-min.js'); import { faceServe } from '@/api/home' export default { name: "face", props: { isOnline: { type: Boolean, default: false, } }, data() { return { isShowFace: true, trackerTask: null, mediaStreamTrack: null, start: true, isSuccessful: '', waitingTme: 10, } }, mounted() { this.openUserMedia() this.openCamera() }, methods: { format(percentage) { return percentage? '' : `${percentage}%`; }, setTime() { const isTime = setInterval(() => { this.waitingTme += 1 if(this.waitingTme === 99) { clearInterval(isTime) } }, 200) }, isRouter() { this.$emit('isRouter') }, isOpenShow() { this.$emit('isValidationFace') this.trackerTask.stop(); }, setSuccessful() { this.isSuccessful = '' }, success(stream) { const than = this; let video = document.getElementById("video"); than.mediaStreamTrack = null; than.mediaStreamTrack = stream; try { video.srcObject = stream; } catch (e) { console.log("访问用户媒体设备失败:", e.name, e.message); } video.play();//播放视频 }, error(error) { console.log('访问用户媒体失败:', error.name, error.message); }, getUserMediaToPhoto(constraints, success, error) { if (navigator.mediaDevices.getUserMedia) { navigator.mediaDevices.getUserMedia(constraints).then(success).catch(error); } else if (navigator.webkitGetUserMedia) { navigator.webkitGetUserMedia(constraints, success, error); } else if (navigator.mozGetUserMedia) { navigator.mozGetUserMedia(constraints, success, error); } else if (navigator.getUserMedia) { navigator.getUserMedia(constraints, success, error); } }, openCamera() { const than = this; let tracker = new window.tracking.ObjectTracker('face'); tracker.setInitialScale(4); tracker.setStepSize(2); tracker.setEdgesDensity(0.1); than.trackerTask = tracking.track('#video', tracker, {camera: true}) tracker.on('track', (event) => { if (event.data.length > 0) { event.data.forEach( () => { console.log('识别成功') if(than.start) { than.setTime() this.isSuccessful = 'audit' than.isOpen() } }); } }) }, openUserMedia() { if (navigator.mediaDevices.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.getUserMedia) { this.getUserMediaToPhoto({ video: { width: 300, height: 300, facingMode: "user" } }, this.success, this.error); } else { alert('你的浏览器不支持访问用户媒体设备'); } }, async isOpen() { let video = document.getElementById('video'), canvas = document.getElementById('canvas'); // vendorUrl = window.URL || window.webkitURL; navigator.getMedia = navigator.getUserMedia || navagator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia; navigator.getMedia({ video: true, //使用摄像头对象 audio: false //不适用音频 }, function(){ video.play(); }, function(error) { //error.code console.log(error); }); canvas.getContext('2d').drawImage(video, 0, 0, 300, 300); const imgData = canvas.toDataURL("image/png") const [ , imgDataTwo] = imgData.split(',') let formData = new FormData() formData.append('file', imgDataTwo); this.start = false; // 接口请求 const data = await faceServe.personVerify(formData); if(data.data.code !== 0) { this.isSuccessful = 'failure' }else { this.isSuccessful = 'successful' } this.trackerTask.stop() }, }, } </script> <style scoped lang="scss"> .face{width: 100%;height: 100%;position: fixed;left: 0;top: 0;z-index: 111;display: flex;justify-content: center;background: rgba(0,0,0,0.5)} .face .faceDataDlo{width: 665px;height: 600px;background: #fff;margin-top: 10%} .titleDialog{display: flex;flex-direction: column;align-items: center;padding-top: 20px;position: relative} .titleDialog p{font-size: 20px;color: #333;} .titleDialog i{width: 105px;height: 13px;@include bgColor('primary');margin-top: -12px;opacity: 0.37} .faceDiv{display: flex;flex-direction: column;align-items: center} .faceDiv p{font-size: 15px;color: #333;margin: 50px 0 42px} .faceDiv .faceData{width: 300px;height: 300px;overflow: hidden;position: relative;margin-bottom: 50px} .faceDiv .leftTop{width: 50px;height: 50px;border-left: 10px solid #2E58FF;border-top: 10px solid #2E58FF;position: absolute;left: 0;top: 0} .faceDiv .rightTop{width: 50px;height: 50px;border-right: 10px solid #2E58FF;border-top: 10px solid #2E58FF;position: absolute;right: 0;top: 0} .faceDiv .leftButton{width: 50px;height: 50px;border-left: 10px solid #2E58FF;border-bottom: 10px solid #2E58FF;position: absolute;left: 0;bottom: 0} .faceDiv .rightButton{width: 50px;height: 50px;border-right: 10px solid #2E58FF;border-bottom: 10px solid #2E58FF;position: absolute;right: 0;bottom: 0} .faceDiv .faceDataLine{width: 300px;height: 2px;background: #2E58FF;position: absolute;top: 0; animation: keyLine 1.5s infinite } .faceDivSuccessful{display: flex;flex-direction: column;align-items: center} .faceDivSuccessful p{font-size: 15px;color: #999;text-align: center;margin-bottom: 45px} .faceDivSuccessful img{margin: 55px 0 25px} .faceDivSuccessful .auditImg{margin: 150px 0 25px} .refer-btn {width: 233px;border-radius: 50px;} .detIcon{font-size: 30px;position: absolute;top: 10px;right: 10px;cursor: pointer} .detIcon:hover{color: #2E58FF } @keyframes keyLine { 0%{top: 0} 50%{top: 300px} 100%{top: 0} } </style>
最后
以上就是糟糕丝袜最近收集整理的关于vue使用tracking人脸识别实现拍照并上传给后台安装 npm install tracking -s的全部内容,更多相关vue使用tracking人脸识别实现拍照并上传给后台安装内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复