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

网友投稿 670 2022-09-04

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

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

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

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

下面我们来看看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']; 

     } 

    } 

   } 

  }

然后进行前台设计

 

    开关 

     

       

        切换摄像头 

         

           

         

       

     

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

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: "确定"        }) 

      } 

     }) 

    } 

   }) 

  },

刷脸登录就成功了

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:287. Find the Duplicate Number
下一篇:70. Climbing Stairs
相关文章

 发表评论

暂时没有评论,来抢沙发吧~