微信小程序—人脸识别

1.首先你要有一个可以就行人脸识别的服务器,然后就是上传到百度云,百度云人脸识别的API接口全面升级到V3版本,并进行开放测试


2.wxml代码

  1. <camera device-position="{{sxt}}" flash="off" binderror="error" style="width: 100%; height: 300px;"></camera>  
  2. <view class="weui-cells weui-cells_after-title">  
  3.                 <view class="weui-cell weui-cell_switch">  
  4.                     <view class="weui-cell__bd"></view>  
  5.                     <!-- <view class="weui-cell__ft"> -->  
  6.                         <input name='sex' value='{{sxt}}'>{{sxt}}</input>  
  7.                         <switch checked bindchange='switch1Change'/>  
  8.                     <!-- </view> -->  
  9.                 </view>  
  10.             </view>   
  11. <button type="primary" bindtap="takePhoto">拍照</button>  

3.js代码

  1. data: {  
  2.     sxt: '前置'  
  3.   },  
  4.   switch1Change: function (e) {  
  5.     console.log(e)  
  6.     var front = '前置'  
  7.     var back = '后置'  
  8.     if (this.data.sxt == '前置') {  
  9.       this.setData({ sxt: back })  
  10.     } else if (this.data.sxt == '后置') {  
  11.       this.setData({ sxt: front })  
  12.     }  
  13.   },  
  14.   takePhoto() {  
  15.     const ctx = wx.createCameraContext()  
  16.     ctx.takePhoto({  
  17.       quality: 'high',  
  18.       success: (res) => {  
  19.         // console.log(res);  
  20.         this.setData({  
  21.           src: res.tempImagePath  
  22.         })  
  23.         wx.showLoading({  
  24.             title:'正在核验身份...',  
  25.         })  
  26.         this.setData({logindisabled:true});  
  27.   
  28.         wx.uploadFile({  
  29.           url: 'http://www.swinder.top/server/index.php/home/index/login',  
  30.             filePath:res.tempImagePath,  
  31.             name:'file',  
  32.   
  33.             success:(res)=>{  
  34.                 wx.hideLoading();  
  35.                 this.setData({logindisabled:false});  
  36.   
  37.                 var data = res.data  
  38.                 console.log(data);  
  39.                 wx.showModal({  
  40.                     title:'提示',  
  41.                     content:data,  
  42.                     showCancel:false  
  43.                 })  
  44.             }  
  45.         })  
  46.       }  
  47.     })  
  48.   }  

4.后台php代码

  1. private function init_face(){  
  2.       $APP_ID=''//在百度云上查看自己的信息  
  3.       $API_KEY='';  
  4.       $SECRET_KEY='';  
  5.       $dir = APP_PATH . '/face-sdk/';  
  6.       require_once $dir . 'AipFace.php';  
  7.       return new \AipFace($APP_ID,$API_KEY,$SECRET_KEY);  
  8.   }  
  9.   
  10. public function login(){  
  11.         //上传文件路径  
  12.         $dir="./Uploads/temp/";  
  13.         if(!file_exists($dir)){  
  14.             mkdir($dir,0777,true);  
  15.         }  
  16.         $upload = new \Think\Upload();//实例化上传类  
  17.         $upload->maxSize = 2048000;//设置附件上传大小2m  
  18.         $upload->exts=array('jpg','gif','png','jpeg');//设置上传类型  
  19.         $upload->rootPath=$dir;//设置附件上传根目录  
  20.         $upload->savePath='';//设置附件上传(子)目录  
  21.         $upload->autoSub=false;  
  22.   
  23.         //上传文件  
  24.         $info = $upload->uploadOne($_FILES['file']);  
  25.         if(!$info){  
  26.             //上传错误提示信息  
  27.             echo json_encode(array('error'=>true,'msg'=>$upload->getError()),JSON_UNESCAPED_UNICODE);  
  28.         }else{  
  29.             //上传成功  
  30.             $file = $dir . $info['savepath'] . $info['savename'];  
  31.             $image = base64_encode(file_get_contents($file));  
  32.             $client = $this->init_face();  
  33.             $options['liveness_control']='NORMAL';  
  34.             $options['max_user_num']='1';  
  35.             $ret = $client->search($image,'BASE64',$this->face_group(),$options);  
  36.   
  37.             if($ret['error_code']==0){  
  38.                 $user = $ret['result']['user_list'][0];  
  39.                 $no = $user['user_id'];  
  40.                 $score = $user['score'];  
  41.   
  42.                 if(!empty($no)){  
  43.                     $data = M('student')->field('no,name,sex')->where("no='{$no}'")->find();  
  44.   
  45.                     if($data){  
  46.                         //查到此学号  
  47.                         $data['score'] = $score;  
  48.                         echo json_encode($data,JSON_UNESCAPED_UNICODE);  
  49.                     }else{  
  50.                         //本地库不存在此学号  
  51.                         echo "本地数据库没有该学生,百度云库信息:个人信息:{$no},分值:{$score}";  
  52.                     }  
  53.   
  54.                 }  
  55.   
  56.             }else{  
  57.                 echo "活体检测失败,".json_encode($ret,JSON_UNESCAPED_UNICODE);  
  58.             }  
  59.         }  
  60.     }  

猜你喜欢

转载自blog.csdn.net/hello_xiang/article/details/80492503