现在我们的科技是越来越发达了,我们现在有的手机有一个功能就是刷脸登录,那这是什么原理呢?那就是人脸识别。首先你要把你的照片(包括整张人脸)传给服务器,他将你的照片传入到数据库当中。在你要登录的时候,将你刷脸是的照片与传入服务器数据库里面的照片进行对比,从而实现刷脸登录。下面是我写的一个微信小程序的关于学生的一个人脸识别功能,下面的代码是将照片传入数据库中
1.首先写一个上传图片的方法在后台
//初始化人脸识别
private function init_face(){
//自己百度云上面的信息
$APP_ID='***';
$APP_KEY='****';
$SECRET_KEY='*****';
//给face-sdk定义路径,方便引用
$dir = APP_PATH . '/face-sdk/';
require_once $dir .'AipFace.php';//引用AipFace.php
return new \AipFace($APP_ID,$APP_KEY,$SECRET_KEY);
}
上传到阿里云的方法
public function upload($id=''){
$no = M('student')->where("id = {$id}")->getField('no');
$dir = "./Upload/studentface/";
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->saveName = $no;
//覆盖
$upload->replace = true;
//阻止建文件夹
$upload->autoSub = false;
// 上传文件
$info = $upload->uploadOne($_FILES['file']);
if(!$info) {// 上传错误提示错误信息
return $this->ajaxReturn(array('error'=>true,'msg'=>$upload->getError()),JSON_UNESCAPED_UNICODESON);
}else{// 上传成功 获取上传文件信息
// return $this->ajaxReturn(array('error'=>false,'msg'=>$info['savepath'].$info['savename'],'id'=>$id));
$path = $dir.$info['savepath'].$info['savename'];
$file = file_get_contents($path);
$img = base64_encode($file);
// echo $no;
//调用facevalid方法检测人脸并入库
$this->facevalid($no,$img);
$m = M('head');
$model = $m->where("no = $no")->find();
if($model){
// $m->where("no = {$no}")->save(array('base64'=>$img,'path'=>$path));
$data['base64'] = $img;
$data['path'] = $path;
$ret = M('head')->where("no = {$no}")->save($data);
}else{
// $ret = $m->add(array('no'=>$no,'base64'=>$img,'path'=>$path));
$data['no'] = $no;
$data['base64'] = $img;
$data['path'] = $path;
$ret = M('head')->add($data);
}
}
}
这个方法是将照片传到阿里云服务器,它是根据前台传过来信息的id,在与数据库中的信息id对比,
2.再写一个方法facevalid,他是将你的照片传到百度云,它的功能是判断你传过来的图片是否是人脸,还是有几张人脸,如果是没有人脸或有多张人脸,它将不会传到百度云上的库里面。否则将照片传到库里。
public function facevalid($no,$img){//$no是传过来的学号,$img是传过来的照片
//如果有可选参数
$options=array();
// $options['face_field']="age";
$options["max_face_num"]=1;
// $options['face_type']='LIVE';
$client=$this->init_face();//调用初始化人脸识别方法
$ret =$client->detect($img,'BASE64',$options);
if($ret['error_code']==0){
//有人脸
$result=$ret['result'];
$face_num=$result['face_num'];
if(1==$face_num){//人脸数量为1
$face_probability=$result['face_list'][0]['face_probability'];
if(1==$face_probability){
$group=$this->face_group();//获取你在百度云上面建的组
$client->addUser($img,'BASE64',$group,$no);
echo "人脸检测完成,并已入库";
}else{
die('可靠性:'.$face_probability);
}
}else{
die('人脸数量大于1');
}
}else{
die('没有人脸');
}
}
3.前台页面
<form bindsubmit='submit'>
<view class='page'>
<view class='page_title spe'>采集照片</view>
<view class='page_dsec spe spe1' wx:for="{{student}}">{{item.no}} {{item.name}} {{item.sex}}</view>
</view>
<view class="weui-cell">
<view class="weui-cell__bd">
<view class="weui-uploader">
<view class="weui-uploader__hd">
<view class="weui-uploader__title">个人照片</view>
<view class="weui-uploader__info">{{imageList.length}}/{{count[countIndex]}}</view>
</view>
<view class="weui-uploader__bd">
<view class="weui-uploader__files">
<block wx:for="{{imageList}}" wx:for-item="image"><!--wx:for-item 用来定义一个循环过程中每个元素的变量的 -->
<view class="weui-uploader__file">
<image class="weui-uploader__img" src="{{image}}" data-src="{{image}}" bindtap="previewImage"></image>
</view>
</block>
</view>
<view class="weui-uploader__input-box">
<view class="weui-uploader__input" bindtap="chooseImage"></view>
</view>
</view>
</view>
</view>
</view>
<view class="weui-btn-area">
<button class="weui-btn" type="primary" form-type='submit'>提交</button>
<button class="weui-btn" form-type='reset'>取消</button>
</view>
</form>
4.js
const app = getApp();
function upload(that, id) {
if (that.data.imageList.length == 0) {
return;
}
wx.uploadFile({
url: '你自己写的后台接口',
filePath: that.data.imageList[0],
name: 'file',
formData: {
'id': id
},
success: (res) => {
var data = res.data
console.log(data);
}
})
}
Page({
/**
* 页面的初始数据
*/
data: {
imageList: [],
},
submit: function () {
//上传照片
var id = wx.getStorageSync("id");
upload(this, id);
wx.showToast({
title: '上传成功',
icon: 'success',
duration: 2000,
success: function () {
setTimeout(function () {
wx.navigateTo({
url: '../headimg/headimg',
})
}, 2000)
}
})
},