人工智能——人脸登录

本文介绍了如何通过微信小程序结合PHP实现人脸识别登录。首先在微信小程序中选择照片并上传到服务器,接着使用百度云的人脸识别SDK进行人脸检测。在后台PHP接口中,将上传的图片进行处理并调用人脸识别接口进行匹配,根据识别得分决定登录是否成功。
摘要由CSDN通过智能技术生成

在百度云中存入照片

[php]  view plain  copy
  1. chooseImage:function(){  
  2.     var that=this  
  3.     wx.chooseImage({  
  4.       count: 1, // 默认9  
  5.       sizeType: ['original''compressed'], // 可以指定是原图还是压缩图,默认二者都有  
  6.       sourceType: ['album''camera'], // 可以指定来源是相册还是相机,默认二者都有  
  7.       success: function (res) {  
  8.         // 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片  
  9.         var imageList = res.tempFilePaths;  
  10.         that.setData({  
  11.           imageList: res.tempFilePaths  
  12.         })  
  13.         console.log(imageList);  
  14.       }  
  15.     })  
  16.   },  
  17.   previewImage: function (e) {  
  18.     var current = e.target.dataset.src  
  19.     wx.previewImage({  
  20.       current: current,  
  21.       urls: this.data.imageList  
  22.     })  
  23.   },  
  24.  upload:function(){  
  25.    wx.uploadFile({  
  26.      url: 'http://bestwangyuan.top/face/index.php/home/index/upload', //仅为示例,非真实的接口地址  
  27.      filePath: this.data.imageList[0],  
  28.      name: 'file',  
  29.      formData: {  
  30.        'user''test'  
  31.      },  
  32.      success: function (res) {  
  33.        var data = res.data  
  34.        console.log(data);  
  35.        var json=JSON.parse(res.data);  
  36.       //  console.log(json);  
  37.        wx.showToast({  
  38.          title: json.msg,  
  39.          icon: 'none',  
  40.          duration: 3000,  
  41.        })  
  42.      }  
  43.    })  
  44.  }  

在用php把照片上传到照片库里面

方法如下

[php]  view plain  copy
  1. public function sdk(){  
  2.      $file='./Uploads/111.jpg';  
  3.      if(!file_exists($file)){  
  4.        die('文件不存在');  
  5.      }  
  6.      $dir=APP_PATH .'/face-sdk/';  
  7.      require_once $dir .'AipFace.php';  
  8.      $APP_ID='';  
  9.      $API_KEY='';  
  10.      $SECRET_KEY='';  
  11.      $client=new \AipFace($APP_ID,$API_KEY,$SECRET_KEY);  
  12.   
  13.      $image=file_get_contents($file);  
  14.      $image=base64_encode($image);  
  15.   
  16.      $imageType='BASE64';  
  17.      $options=array();  
  18.      $options["max_face_num"]=10;  
  19.      $ret=$client->detect($image,$imageType,$options);  
  20.      print_r($ret);  
  21.   
  22.   
  23.   
  24.    }  

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

[php]  view plain  copy
  1. public function login(){  
  2.       // 上传文件路径  
  3.       $dir = "./Uploads/temp/";  
  4.       if(!file_exists($dir)){  
  5.         mkdir($dir,0777,true);  
  6.       }  
  7.       $upload = new \Think\Upload();  
  8.       $upload->maxSize = 2048000 ;// 设置附件上传大小  
  9.       $upload->exts = array('jpg''gif''png''jpeg');// 设置附件上传类型  
  10.       $upload->savepath = '';  
  11.       $upload->autoSub = false;  
  12.       $upload->rootPath = $dir// 设置附件上传根目录  
  13.       // 上传单个文件  
  14.       $info = $upload->uploadOne($_FILES['file']);  
  15.       if(!$info) {// 上传错误提示错误信息  
  16.           echo json_encode(array('error'=>true,'msg'=>$upload->getError()),JSON_UNESCAPED_UNICODE);  
  17.       }else{// 上传成功 获取上传文件信息  
  18.         $file = $dir . $info['savepath'].$info['savename'];  
  19.         $image = base64_encode(file_get_contents($file));  
  20.         $client = $this->init_face();  
  21.         $options['liveness_control'] = 'NORMAL';  
  22.         $options['max_user_num']  = '1';  
  23.         $ret = $client->search($image,'BASE64','student',$options);  
  24.         // echo json_encode($ret,JSON_UNESCAPED_UNICODE);  
  25.         // exit;  
  26.         if($ret['error_code']==0){  
  27.           $user = $ret['result']['user_list'][0];  
  28.           $no = $user['user_id'];  
  29.           $score = $user['score'];  
  30.           if($score>=95){  
  31.             $data = M('student')->where("no = '{$no}'")->find();  
  32.             $data['score'] = $score;  
  33.             // $data['name'] = json_decode($data['name'],true);  
  34.             // $data['sex'] = json_decode($data['sex'],true);  
  35.             echo '识别成功' . json_encode($data,JSON_UNESCAPED_UNICODE);  
  36.           }else{  
  37.             echo '识别失败' . $data['score'];  
  38.           }  
  39.         }  
  40.       }  
  41.     }  

在js里面调用接口

[html]  view plain  copy
  1. takePhoto() {  
  2.      const ctx = wx.createCameraContext()  
  3.      ctx.takePhoto({  
  4.        quality: 'high',  
  5.        success: (res) => {  
  6.          this.setData({  
  7.            src: res.tempImagePath  
  8.          })  
  9.          console.log(res)  
  10.          wx.uploadFile({  
  11.            url: '', //仅为示例,非真实的接口地址  
  12.            filePath: this.data.src,  
  13.            name: 'file',  
  14.            formData: {  
  15.            },  
  16.            success: function (res) {  
  17.              // var data = res.data  
  18.              // var json = JSON.parse(data)  
  19.              console.log(res)  
  20.              wx.showModal({  
  21.                title: "提示",  
  22.                content: res.data,  
  23.                showCancel: false,  
  24.                confirmText: "确定"  
  25.              })  
  26.            }  
  27.          })  
  28.        }  
  29.      })  
  30.    },  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值