美文网首页
微信小程序-人脸识别(2)实现人脸识别功能

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

作者: 你的Yang | 来源:发表于2020-07-22 17:09 被阅读0次

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

前台代码:wxml

<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;

                        }

                    }

            }

        }

  }

相关文章

网友评论

      本文标题:微信小程序-人脸识别(2)实现人脸识别功能

      本文链接:https://www.haomeiwen.com/subject/flstlktx.html