加入收藏 | 设为首页 | 会员中心 | 我要投稿 应用网_阳江站长网 (https://www.0662zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 创业 > 模式 > 正文

PC人脸识别登录,出乎意料的简单

发布时间:2020-07-30 13:18:48 所属栏目:模式 来源:51cto
导读:之前不是做了个开源项目嘛,在做完GitHub登录后,想着再显得有逼格一点,说要再加个人脸识别登录,就我这佛系的开发进度,过了一周总算是抽时间安排上了。 源码在文末 其实最近对写文章有点小抵触,写的东西没人看,总有点小失落,好在有同行大佬们的开导

之前不是做了个开源项目嘛,在做完GitHub登录后,想着再显得有逼格一点,说要再加个人脸识别登录,就我这佛系的开发进度,过了一周总算是抽时间安排上了。

源码在文末

其实最近对写文章有点小抵触,写的东西没人看,总有点小失落,好在有同行大佬们的开导让我重拾了信心。调整了自己的心态,只要我分享的东西对大家有帮助就好,至于多少人看那就随缘吧!

废话不多说先看人脸识别效果动态,马赛克有点重哈,没办法长相实在是拿不出手。

PC人脸识别登录,出乎意料的简单

实现原理

我们看一下实现人脸识别登录的大致流程,三个主要步骤:

PC人脸识别登录,出乎意料的简单

前端登录页打开摄像头,进行人脸识别,注意:只识别画面中是不是有人脸

识别到人脸后,拍照上传当前画面图片

后端接受图片并调用人脸库SDK,对人像进行比对,通过则登录成功,并将人像信息注册到人脸库和本地mysql。

前端实现

上边说过要在前端识别到人脸,所以这里就不得不借助工具了,我使用的 tracking.js,一款轻量级的前端人脸识别框架。

前端 Vue 代码实现逻辑比较简单,tracking.js 打开摄像头识别到人脸信息后,对视频图像拍照,将图片信息上传到后台,等待图片对比的结果就可以了。

data() {          return {              showContainer: true,   // 显示              tracker: null,              tipFlag: false,         // 提示用户已经检测到              flag: false,            // 判断是否已经拍照              context: null,          // canvas上下文              removePhotoID: null,    // 停止转换图片              scanTip: '人脸识别中...',// 提示文字              imgUrl: '',              // base64格式图片              canvas: null          }      },      mounted() {          this.playVideo()      },      methods: {          playVideo() {              var video = document.getElementById('video');              this.canvas = document.getElementById('canvas');              thisthis.context = this.canvas.getContext('2d');              this.tracker = new tracking.ObjectTracker('face');              this.tracker.setInitialScale(4);              this.tracker.setStepSize(2);              this.tracker.setEdgesDensity(0.1);              tracking.track('#video', this.tracker, {camera: true});              this.tracker.on('track', this.handleTracked);          },          handleTracked(event) {                  this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);                  if (event.data.length === 0) {                      this.scanTip = '未识别到人脸'                  } else {                      if (!this.tipFlag) {                          this.scanTip = '识别成功,正在拍照,请勿乱动~'                      }                      // 1秒后拍照,仅拍一次                      if (!this.flag) {                          this.scanTip = '拍照中...'                          this.flag = true                          this.removePhotoID = setTimeout(() => {                                  this.tackPhoto()                                  this.tipFlag = true                              },                              2000                          )                      }                      event.data.forEach(this.plot);                  }         },          plot(rect){              this.context.strokeStyle = '#eb652e';              this.context.strokeRect(rect.x, rect.y, rect.width, rect.height);              this.context.font = '11px Helvetica';              this.context.fillStyle = "#fff";              this.context.fillText('x: ' + rect.x + 'px', rect.x + rect.width + 5, rect.y + 11);              this.context.fillText('y: ' + rect.y + 'px', rect.x + rect.width + 5, rect.y + 22);          },          // 拍照          tackPhoto() {              this.context.drawImage(this.$refs.refVideo, 0, 0, 500, 500)              // 保存为base64格式              thisthis.imgUrl = this.saveAsPNG(this.$refs.refCanvas)              var formData = new FormData();              formData.append("file", this.imgUrl);              this.scanTip = '登录中,请稍等~'              axios({                  method: 'post',                  url: '/faceDiscern',                 data: formData,              }).then(function (response) {                  alert(response.data.data);                  window.location.href="http://127.0.0.1:8081/home";              }).catch(function (error) {                  console.log(error);              });              this.close()          },          // 保存为png,base64格式图片          saveAsPNG(c) {              return c.toDataURL('image/png', 0.3)          },          // 关闭并清理资源          close() {              this.flag = false              this.tipFlag = false              this.showContainer = false              this.tracker && this.tracker.removeListener('track', this.handleTracked) && tracking.track('#video', this.tracker, {camera: false});              this.tracker = null              this.context = null              this.scanTip = ''              clearTimeout(this.removePhotoID)          }      } 

人脸识别

之前也搞过一个人脸识别案例 《基于 Java 实现的人脸识别功能(附源码)》 ,不过调用SDK的方式太过繁琐,而且代码量巨大。所以这次为了简化实现,改用了百度的人脸识别API,没想到出乎意料的简单。

别抬杠问我为啥不自己写人脸识别工具,别问,问就是不会

(编辑:应用网_阳江站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

推荐文章
    热点阅读