当前位置: 移动技术网 > IT编程>开发语言>PHP > PHP实现微信小程序人脸识别刷脸登录功能

PHP实现微信小程序人脸识别刷脸登录功能

2018年06月16日  | 移动技术网IT编程  | 我要评论

首先我们先确认我们的百度云人脸库里已经上传了我们的个人信息照片

然后我们在后台写刷脸登陆的接口login我们要把拍照获取的照片存储到服务器

public function login(){ 
   // 上传文件路径 
   $dir = "./uploads/temp/"; 
   if(!file_exists($dir)){ 
    mkdir($dir,0777,true); 
   } 
   $upload = new \think\upload(); 
   $upload->maxsize = 2048000 ;// 设置附件上传大小 
   $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型 
   $upload->savepath = ''; 
   $upload->autosub = false; 
   $upload->rootpath = $dir; // 设置附件上传根目录 
   // 上传单个文件 
   $info = $upload->uploadone($_files['file']); 
   if(!$info) {// 上传错误提示错误信息 
     echo json_encode(array('error'=>true,'msg'=>$upload->geterror()),json_unescaped_unicode); 
   }else{// 上传成功 获取上传文件信息 
    $file = $dir . $info['savepath'].$info['savename']; 
    $image = base64_encode(file_get_contents($file)); 
    $client = $this->init_face(); 
    $options['liveness_control'] = 'normal'; 
    $options['max_user_num'] = '1'; 
    $ret = $client->search($image,'base64','student',$options); 
    // echo json_encode($ret,json_unescaped_unicode); 
    // exit; 
    if($ret['error_code']==0){ 
     $user = $ret['result']['user_list'][0]; 
     $no = $user['user_id']; 
     $score = $user['score']; 
     if($score>=95){ 
      $data = m('student')->where("no = '{$no}'")->find(); 
      $data['score'] = $score; 
      // $data['name'] = json_decode($data['name'],true); 
      // $data['sex'] = json_decode($data['sex'],true); 
      echo '识别成功' . json_encode($data,json_unescaped_unicode); 
     }else{ 
      echo '识别失败' . $data['score']; 
     } 
    } 
   } 
  } 

然后进行前台设计

<camera device-position="{{device?'back':'front'}}" flash="off" binderror="error" style="width: 100%; height: 300px;"></camera> 
    <view class="weui-cells__title" >开关</view> 
    <view class="weui-cells weui-cells_after-title"> 
      <view class="weui-cell weui-cell_switch"> 
        <view class="weui-cell__bd">切换摄像头</view> 
        <view class="weui-cell__ft" > 
          <switch bindtap="deviceposition" /> 
        </view> 
      </view> 
    </view> 
<button type="primary" bindtap="takephoto">刷脸登录</button> 

我们还可以控制相机的前后镜头

deviceposition() { 
this.setdata({ 
 device: !this.data.device, 
}) 
console.log("当前相机摄像头为:", this.data.device ? "后置" : "前置"); 
camera() { 
 let { ctx, type, startrecord } = this.data; }, 
data: { 
 src: null, 
}, 

在js里面调用接口

takephoto() { 
   const ctx = wx.createcameracontext() 
   ctx.takephoto({ 
    quality: 'high', 
    success: (res) => { 
     this.setdata({ 
      src: res.tempimagepath 
     }) 
     console.log(res) 
     wx.uploadfile({ 
      url: '', //仅为示例,非真实的接口地址 
      filepath: this.data.src, 
      name: 'file', 
      formdata: { 
      }, 
      success: function (res) { 
       // var data = res.data 
       // var json = json.parse(data) 
       console.log(res) 
       wx.showmodal({ 
        title: "提示", 
        content: res.data, 
        showcancel: false, 
        confirmtext: "确定" 
       }) 
      } 
     }) 
    } 
   }) 
  }, 

刷脸登录就成功了

总结

以上所述是小编给大家介绍的php实现微信小程序人脸识别刷脸登录,希望对大家有所帮助

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网