首页 > 其他分享 >vue PC端调用摄像头实现人脸识别

vue PC端调用摄像头实现人脸识别

时间:2022-08-30 08:44:38浏览次数:67  
标签:vue text image height PC 人脸识别 video getUserMedia navigator

一、思路

vue打开摄像头获取视频流数据->截取视频流通过canvas绘制图片->图片传到服务器识别(后端使用的是阿里的人脸识别api)

二、直接上代码<template>

  <div class="face-area">
      
    <div class="area-left">
        <p>请将脸部正对摄像头并填充满取景框</p>
      <frame-1>
        <video id="video" autoplay></video>
      </frame-1>
      <button v-if="reFace" class="upload-btn" @click="openVideo">重新识别</button>
    </div>
    <div class="area-right">
        <p>{{text}}</p>
        <canvas  id="canvas" width="300" height="424"></canvas>
    </div>
  </div>
</template>
<script>
import frame1 from '../components/frame1.vue';
export default {
  components: {
    frame1
  },
  props: {
      account:{
          type:String
      },
      type:{
          type:Number
      }
  },
  data() {
    return {
      context: {},
      text:"人脸识别中...",
      mediaStreamTrack:{},
      count:0,
      reFace:false,
    }
  },
  methods: {
//关闭摄像头 closeMedia() { var _this=this; this.mediaStreamTrack.getVideoTracks().forEach(function (track) { track.stop(); _this.context.clearRect(0, 0, _this.context.width, _this.context.height); //清除画布 }); },
//打开摄像头 openVideo() { this.text="人脸识别中..." var canvas = document.getElementById("canvas"); this.context = canvas.getContext("2d"); this.video = document.getElementById("video"); if (navigator.mediaDevices === undefined) { navigator.mediaDevices = {} } //缺少getUserMedia属性 if (navigator.mediaDevices.getUserMedia === undefined) { navigator.mediaDevices.getUserMedia = function (constraints) { // 首先获取现存的getUserMedia(如果存在) var getUserMedia = navigator.webkitGetUserMedia || navigator.mozGetUserMedia; // 浏览器不支持,返回错误信息 if (!getUserMedia) { return Promise.reject(new Error('getUserMedia is not implemented in this browser')); } //使用Promise将调用包装到navigator.getUserMedia return new Promise(function (resolve, reject) { getUserMedia.call(navigator, constraints, resolve, reject); }); } } var constraints = { audio: false, video: { width: 600, height: 848, } } var _this=this; navigator.mediaDevices.getUserMedia(constraints) .then(function (stream) { var video = document.querySelector('video'); _this.mediaStreamTrack = stream; // 旧的浏览器可能没有srcObject if ("srcObject" in video) { video.srcObject = stream; } else { //避免在新的浏览器中使用它,因为它正在被弃用。 video.src = window.URL.createObjectURL(stream); } video.onloadedmetadata = function (e) { video.play(); //开始识别 if (_this.type == 1) { setTimeout(_this.getImg(), 500); } }; }) .catch(function (err) { console.log(err.name + ": " + err.message); _this.$confirm('是否前往查看设置教程?', '打开摄像头失败请检查浏览器设置或域名是否安全', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' }).then(() => { window.open("https://www.cnblogs.com/tuofei13/p/16636145.html", "_blank"); }) }); }, //截取人脸图片 getImg() { var date = new Date().getTime(); // 点击,canvas画图 this.context.drawImage(this.video, 0, 0, 300, 424); // 获取图片base64链接 var image = canvas.toDataURL('image/png'); // 定义一个img var img = new Image(); //设置属性和src img.id = "imgBox"; img.src = image; // console.log(image); if(this.type==0){ this.addFace(image.split(",")[1]); } else{ this.recognitionFace(image.split(",")[1]); } }, //上传到服务器比对结果 recognitionFace(image){ let params={ account:this.account, imageDataB:image, }; this.$http.post(this.$rconfig.basic("登录-人脸识别"), params) .then(res => { // console.log(res); if (res.code == 200) { this.text = "人脸匹配成功,登录中..." this.$message({ message: '人脸匹配成功', type: 'success' }); this.closeMedia() this.login(res); } else{ //递归识别 this.count++; if(this.count<=10){ this.getImg(); } else{ this.$message({ message: '识别失败次数过多,请稍后重试', type: 'warning', center: true }); this.count=0; this.text="识别失败次数过多,请稍后重试" this.reFace=true; this.closeMedia(); } } }) ; },// base64转文件 dataURLtoFile(dataurl, filename) { var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1], bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n); while (n--) { u8arr[n] = bstr.charCodeAt(n); } return new File([u8arr], filename, { type: mime }); }, //处理返回信息(菜单、缓存等)并登录 login(res) { //登录逻辑 }, }, mounted() { //当type为1时循环请求识别接口 setTimeout(this.openVideo, 600); }, destroyed(){ this.closeMedia(); } } </script> <style scoped> .face-area{ width: 100%; height: 100%; text-align: center; float: left; } .area-left{ width: 360px; height: 630px; float: left; text-align: center; } .area-right{ width: 640px; height: 632px; float: left; } .area-left p{ margin-top: 60px; font-size: 16px; color: #33dafd; height: 30px; line-height: 30px; } .area-right p { margin-top: 60px; font-size: 24px; height: 30px; line-height: 30px; text-align: center; background-image: -webkit-linear-gradient(left, #053744, #33dafd 25%, #147B96 50%, #33dafd 75%, #053744); -webkit-text-fill-color: transparent; -webkit-background-clip: text; background-clip: text; -webkit-background-size: 200% 100%; background-size: 200% 100%; animation: slideShine 2s infinite linear; } @keyframes slideShine { 0%{ background-position: 0 0;} 100% { background-position: -100% 0;} } #video{ width: 296px; height: 420px; margin: 2px; transform: rotateY(180deg);/* 摄像头呈镜像用样式左右翻转让人像动起来正常方便识别 */ } #canvas{ margin-top: 20px; border-radius: 10px; /* opacity:0 */ } .upload-btn{ border: none; height: 40px; width: 180px; line-height: 40px; background-color: #33dafd; color: #FFF; margin-top: 30px; } </style>

 

标签:vue,text,image,height,PC,人脸识别,video,getUserMedia,navigator
From: https://www.cnblogs.com/tuofei13/p/16638058.html

相关文章

  • vue3项目-小兔鲜儿笔记-首页03
    1.面板封装提取首页的公用面板进行复用头部标题和副标题由props传入右侧内容由具名插槽right传入查看更多封装成全局组件主体由默认插槽传入......
  • 【Vue面试题】谈谈你对Vue的diff算法的理解
    1diff算法到底是什么?diff算法是一种通过同层的树节点进行比较的高效算法,它可以不用频繁操作DOM,而是选用虚拟DOM节点操作,说人话就是专门用来处理虚拟DOM节点的。2操作......
  • vue学习之------vue-router【路由生命周期(也叫“导航守卫”)】
    1、全局导航守卫2、路由规则自己的守卫3、组件内部的导航守卫 ......
  • vue3 基础-事件绑定 & 修饰符
    无非就是js的一些事件,按键,鼠标等的一些绑定在vue的实现而已,很好理解.先来看一个基础例子.事件初体验<!DOCTYPEhtml><htmllang="en"><head><title>事......
  • 第46届ICPC EC final游记
    前言因为在昆明区域赛中获得银首,高于csy,wh,zxh队和hzl,syr,tyy队,我队获得了EC名额。此时我两个队友都决定退役了,所以这场比赛被当作了他们的最后一场比赛(然而后来又决定......
  • Vue面试题之如何解决vue第一次加载的时候 页面上使用的数据会闪烁?
    引起数据闪烁的原因:界面加载的时候会把节点直接挂载到文档树中,导致{{msg+"666"}}这个字符串会显示一下,vue对象生成data数据时候回去刷新界面把{{msg+"666"}}字符串替换成......
  • Vue的基础指令
    1.普通插值表达式插入数据<body> <divid="app"> <div>{{text1}}</div> <div>{{text2}}</div> </div></body><script> newVue({ el:"#app", data:{ tex......
  • 什么是vue⽣命周期?
    什么是vue⽣命周期?vue⽣命周期都有哪些钩⼦函数?这些钩⼦函数如何触发? 项⽬开发过程中,在⽣命周期⾥⾯都分别做过什么功能? ⻚⾯第⼀次加载时⽗⼦组件⽣命周期执⾏的顺......
  • Python3处理grpc接口返回包含中文编码的protobuf数据时的显示问题
    [本文出自天外归云的博客园]当你用python调用grpc接口的时候,返回的protobuf数据中如果含有中文,会显示成编码模式,类似“\345\214\227\344\272\254”,如何显示成中文呢?这里有......
  • PCA降维
     以下内容来自:https://blog.csdn.net/qq_58535145/article/details/122598783 一、主成分分析1、动机:多个变量之间往往存在一定的相关性,可以通过线性组合的方式从中提......