
微信小程序实现人脸识别认证
发布日期:2021-05-10 03:43:05
浏览次数:6
分类:技术文章
本文共 7521 字,大约阅读时间需要 25 分钟。
微信小程序实现人脸识别认证
这个项目中使用的是阿里云的实人认证接口,需要录制视频上传
先说一下逻辑:
1、获取摄像头和录音权限;
2、调起摄像头用于录制视频,录制5s; 3、录制结束后上传视频; 4、取上传返回的视频地址,调用后端提供的人脸识别接口,判断视频中是否含有人脸; 5、通过人脸识别后会返回一个图片地址,调用后端提供的实人认证接口 6、若这个过程中出现错误,则重新录制上传,本项目中失败三次则提示认证失败,可重新识别上代码:
wxml:wxss
.identification { width: 480rpx; height: 480rpx; margin-top: 10%; margin-left: 50%; transform: translate(-50%, 0);}.identification .camera { width: 430rpx; height: 430rpx; position: absolute; top: 20rpx; left: 20rpx;}.identification .camera .zz { width: 100%; height: 100%; position: absolute; top: 0; overflow: hidden;}.identification .camera camera { width: 100%; height: 100%;}.message { text-align: center; margin-top: 60rpx;}.message text { font: 24rpx/50rpx ""; color: #333; display: flex; justify-content: center;}.message view { font: 46rpx/50rpx ""; color: #4877D1; display: flex; justify-content: center; margin-top: 40rpx;}.message view.msgTitle { font: 32rpx/50rpx ""; color: #333; display: flex; justify-content: center; margin-top: 20rpx;}.message view.msgContent { font: 24rpx/50rpx ""; color: #999; display: flex; justify-content: center; margin-top: 20rpx;}.message image { width: 70rpx; height: 70rpx;}.message .nextBtn { width: 400rpx; margin: 40rpx auto 0; background: #4877D1; color: #fff; font: 32rpx/80rpx ""; border-radius: 50rpx;}.crl-dot { width: 10rpx; height: 10rpx; background: red; position: absolute; top: -10rpx; left: -10rpx;}
js
const app = getApp()Page({ /** * 页面的初始数据 */ data: { countDownNum: 5, // 录制视频时间 msgStatus: 0, // 识别状态,0为未识别或识别中,1为识别成功,-1为识别失败 i: '', recordNum: 0, // 记录识别次数 showCamera: false, // 是否显示相机 recordStatus: 0 // 录制状态,0为未开始录制, 1为开始录制,2为结束录制 }, onLoad() { this.getAuthorize('scope.camera', 1) }, getAuthorize: function(value, num) { // 判断是否授权摄像头和录音,num表示判断的序号,1为摄像头,2为录音 let _this = this wx.getSetting({ success: res => { console.log(res.authSetting) if (res.authSetting[value] != true) { wx.authorize({ scope: value, success () { _this.successNext(num) }, fail() { console.log('未授权'); wx.showModal({ title: '开启' + (num == 1 ? '摄像机' : '录音') + '失败', showCancel: false, content: '点击确定重试!', success: function (res) { if (res.confirm) { _this.openSetting(value, num) } } }); } }) } else { // 权限开启成功 _this.successNext(num) } } }) }, openSetting: function(value, num) { // 打开设置页 var _this =this; wx.openSetting({ success(res) { if (res.authSetting[value] != true){ wx.showModal({ title: '开启' + (num == 1 ? '摄像机' : '录音') + '失败', showCancel: false, content: '点击确定重试!', success: function (res) { if (res.confirm) { _this.openSetting(value, num) } } }); } else { _this.successNext(num) } }, fail(res) { console.log(res) } }) }, successNext: function(num) { if (num == 1) { // 摄像头权限获取成功,去获取录音权限 this.getAuthorize('scope.record', 2) } else if (num == 2) { // 录音权限获取成功,开始录像 this.openCamera(); console.log('已授权'); } }, openCamera: function() { // 打开相机 this.setData({ showCamera: true }) this.ctx = wx.createCameraContext(); this.countDown(); }, countDown: function (e) { // 视频录制三秒 let that = this; let countDownNum = 5; // 获取倒计时初始值 let i = this.data.i; if (!i) { i = 2 } that.data.recordNum++; that.setData({ recordStatus: 1, timer: setInterval(function () { if (countDownNum == 5) { that.startRecord(); // 开始录制 } countDownNum--; that.setData({ countDownNum: countDownNum, i: i }) if (countDownNum == 0) { clearInterval(that.data.timer); // 清空定时器 that.stopRecord(); // 结束录制 } }, 1000) }) }, startRecord() { // 开始录制 var _this = this; _this.ctx.startRecord({ success: (res) => { console.log('录像开始') },fail(res){ console.log('录像未开始') } }) }, stopRecord() { var that = this; console.log('停止录像'); that.ctx.stopRecord({ success: (res) => { that.setData({ recordStatus: 2 }) that.uploadFile(res) },fail(res){ console.log(res); that.judgeStatus() } }) }, judgeStatus: function() { // 失败时判断是否继续识别 var that = this; if (that.data.recordNum < 3 && that.data.msgStatus != 1) { setTimeout(function () { that.setData({ countDownNum: 5 }) that.countDown(); }, 1000) } else if (that.data.recordNum == 3 && that.data.msgStatus != 1) { that.setData({ countDownNum: 5, msgStatus: -1 }) } }, uploadFile: function(file) { // 上传视频 var that = this; wx.showLoading({ title: '视频上传中', }) wx.uploadFile({ url: 'http://图片上传接口地址', filePath: file.tempThumbPath, name: 'img', // 上传的视频字段名 success: function (res) { console.log(res); wx.hideLoading() if (res.code == '00000') { // 上传成功 that.identify(res.data.img) } else { that.judgeStatus() } }, fail(res) { wx.hideLoading() wx.showToast({ title: '视频上传失败', }) that.judgeStatus() } }) }, identify: function(img) { // 请求后端人脸识别接口 wx.showLoading({ title: '正在识别', }) wx.request({ url: 'http://人脸识别接口地址', // 判断是否有人脸 data: { image: img }, method: "POST", success: function (res) { console.log(res) if (res.data.code == '00000') { console.log('人脸识别成功'); var image_url = res.data.data.image_url; wx.request({ url: 'http://实人认证接口地址', // 实人认证 data: { image_url: res.data.data.image_url }, method: "POST", success: function (res) { console.log('实人认证成功'); wx.hideLoading(); if (res.data.code == '00000') { that.setData({ msgStatus: 1 }) // 实人认证成功后的操作 } else { wx.hideLoading(); console.log('实人认证失败'); that.judgeStatus() } } }) } else { console.log('人脸识别失败' + that.data.recordNum); wx.hideLoading(); that.judgeStatus() } } }) }, // 重新扫描 rescan: function () { this.onLoad(); this.setData({ msgStatus: 0, recordNum: 0, recordStatus: 0 }) }, onUnload: function () { var that = this; clearInterval(that.data.timer); },})
转载地址:https://blog.csdn.net/qsj0606/article/details/114403466 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
哈哈,博客排版真的漂亮呢~
[***.90.31.176]2023年11月12日 21时13分47秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
科研神器(高效开发工具)
2019-03-28
悬臂式连接环的应力与变形分析——ansys
2019-03-28
超静定桁架的有限元建模与分析--ansys
2019-03-28
Python实现kMeans(k均值聚类)
2019-03-28
PCL实现区域增长算法(代码详细解析)
2019-03-28
C/C++常用函数总结以及在ubuntu和vs中运行
2019-03-28
基于tensorflow的一元线性回归详细讲解
2019-03-28
tensorflow简单二维分类 simple_classification
2019-03-28
tensorflow的CNN
2019-03-28
tensorflow的regress(超详细教程)
2019-03-28
tensorflow的dropout 防止过拟合
2019-03-28
PCL包围盒(详细介绍)
2019-03-28
c++调用函数,返回字符串数组
2019-03-28
点云常见几种算法(详细教程)
2019-03-28
UG10.0快速配置环境
2019-03-28
OBB包围盒碰撞检测算法验证
2019-03-28
tensorflow的RNN(递归神经网络)
2019-03-28
Python-Tkinter图形化界面设计(详细教程 )
2019-03-28
CSDN页面内跳转测试
2019-03-28
MDK查找替换
2019-03-28