微信小程序-人脸识别(2)实现人脸识别功能

上一篇的博客已经将图片存入到百度云与自己的服务器阿里云上面了,接下来就是在写一个页面,是刷脸页面,通过这个页面你将自己的脸拍照,传入到自己的服务器上,去与存入百度云上面的照片进行对比。下面是代码。

前台代码:

<camera device-position="front" flash="off" binderror="error" style="width: 100%; height: 300px;"></camera>
<view class="weui-cell weui-cell_switch">
                <view class="weui-cell__ft">
                     <switch checked bindchange="switch1Change" /> 
                </view>
            </view>
<button type="primary" bindtap="takePhoto">刷脸登录</button>
js代码:
 data: {
    // switch1Change:true
    path:null,
    status:'front'
  },


  switch1Change: function (e) {
     if(e.detail.value){
          this.setData({status:'back'})
     }else{
          this.setData({status:'front'})
     }

  },

  takePhoto() {
    const ctx = wx.createCameraContext()
    ctx.takePhoto({
      quality: 'high',
      success: (res) => {
        this.setData({
          src: res.tempImagePath
        })

        wx.uploadFile({
          url: 'http://www.anweimin.top/miniprgram-php/server/index.php/home/index/login',
          filePath: this.data.src,
          name: 'file',
          success: (res) => {
            var data = res.data;
            console.log(data);

          }
        })
      }
    })
  },

后台代码,这个方法是将你刷脸是拍的照片上传到服务器上与百度云对比,并返回数据,返回的数据中,其中一个是两张照片的相似度,相似度在百分之九十五以上,则是本人,

public function login(){
        //上传文件路径
       $dir ="./Upload/temp/";
       if(!file_exists($dir)){
           mkdir($dir,0777,true);
       }

        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 2048000 ;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = $dir; // 设置附件上传根目录
        $upload->savePath = ''; // 设置附件上传(子)目录
        //阻止建文件夹
        $upload->autoSub = false;

        // 上传文件
        $info =$upload->uploadOne($_FILES['file']);

        if(!$info){
           //上传错误提示信息
             echo json_encode(array('error'=>true,'msg'=>$uplaod->getError()),JSON_UNESCAPED_UNICODE);
        }else{//上传成功
             // $this->success('上传成功');
             $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','pingjiao',$options);

             echo json_encode($ret,JSON_UNESCAPED_UNICODE);

             if($ret['error_code']==0){  
                    $user=$ret['result']['user_list']['0'];
                    $no=$user['user_id'];
                    $score=$user['score'];
                    if(!empty($no)){
                         $data=M('student')->field('no,name,sex')->where("no={$no}")->find();
                         if($data){
                             $data['score']=$score;
                         }
                    }
             }
        }

  }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值