现在人脸识别可以说是非常火热,支付宝可以人脸识别登录,公司需要人脸识别上下班,还有有的地方取钱也可以刷脸取钱,所以我们要根据大众的需求,做一款可以刷脸的小程序。
在此我们需要有一个百度云账号,下载上面的sdk文件它里面存了好多接口,可以直接调用。
新建一个home模块,home模块新建一个IndexController.php控制器。
<?php namespace Home\Controller; use Think\Controller; class IndexController extends Controller { const HOST="https://www.xx/facevalid"; //初始化人脸识别 private function init_face(){ $APP_ID=''; $API_KEY=''; $SECRET_KEY=''; $dir=APP_PATH.'/face-sdk/'; require_once $dir.'AipFace.php'; return new \AipFace($APP_ID,$API_KEY,$SECRET_KEY); } //学生列表 public function index($condition=''){ $where=array(); if(!empty($condition)){ $where=['s.no|s.name']=$condition; } $pagesize=10; $data=M('student') ->alias('s') ->join('__HEAD_ h on s.no=h.no','LEFT') ->field('s.id,s.no,s.name,s.sex,s.age,h.path,h.base64') ->order('id') ->where($where) ->page($_GET['page'],$pagesize) ->select(); foreach ($data as &$row) { $path=$row['path']; if(!empty($path)){ $dir=dirname($path); mkdir($dir,0777,true); file_put_contents($path, base64_decode($row['base64'])); } unset($data['base64']); $this->ajaxReturn($data); } } //学生列表 public function add($no,$name,$sex,$age){ if(empty($no) || empty($name)){ return $this->ajaxReturn(array('error'=>true,'msg'=>'学号或姓名必填')); } $data['no']=$no; $data['name']=$name; $data['sex']=$sex; $data['age']=$age; // $data['head']=$head; $stu=M('student'); if($stu->where("no='{$no}'")->find()){ return $this->ajaxReturn(array('error'=>true,'msg'=>'学号重复')); }else{ $id=$stu->add($data); if($id){ return $this->ajaxReturn(array('error'=>false,'id'=>$id)); } return $this->ajaxReturn(array('error'=>true,'msg'=>'添加出错')); } } //上传照片 public function upload($id=''){ $upload = new \Think\Upload();// 实例化上传类 $upload->maxSize = 3145728 ;// 设置附件上传大小 $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型 $upload->rootPath = './Uploads/'; // 设置附件上传根目录 $upload->savePath = ''; // 设置附件上传(子)目录 // 上传文件 $info = $upload->uploadOne($_FILES['file']); // $id=M('student')->where('id'=$id)->setField('head',$info); if(!$info) {// 上传错误提示错误信息 $this->ajaxReturn(array('error'=>true,'msg'=>$upload->getError())); }else{// 上传成功 $this->ajaxReturn(array('error'=>false,'msg'=>$info['savePath'].$info['savename'],'id'=>$id)); } } //获取小组 private function face_group(){ //组名 $groupname='pingjiao'; $client=$this->init_face(); $ret=$client->getGroupList(); if($ret['error_code']==0){ $grouplist=$ret['result']['group_id_list']; if(in_array($groupname,$grouplist)){ return $groupname; }else{ $ret=$client->groupAdd($groupname); if($ret['error_code']==0){ return $groupname; }else{ return false; } } }else{ return false; } } private function del_facegroup(){ $client=$this->init_face(); $ret=$client->groupDelete('pingjiao'); print($ret); } public function facevalid(){ $file='./Uploads/1.jpg'; if(!file_exists($file)){ die('文件不存在'); } $image= base64_encode(file_get_contents($file)); $options=array(); $options['max_face_num']=2; $client=$this->init_face(); $ret=$client->detect($image,'BASE64',$options); // echo $ret; // exit; if($ret['error_code']==0){ // echo $ret['error_code']; //有人脸 $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){ //可靠性为1 $guid=myguid(); $group=$this->face_group(); $client->addUser($image,'BASE64',$group,$guid); echo '人脸检测完成,并已入库'; }else{ die('人脸性为'.$face_probability); } }else{ die('人脸数量大于1'); } }else{ die('没有人脸'); } } public function guid(){ echo myguid(); } }
在common下新建一个function,以便调用,
<?php function guid(){ if(function_exists('com_create_guid')){ return com_create_guid(); }else{ mt_srand((double)microtime()*10000); $charid=strtoupper(md5(uniqid(rand(),true))); $hyphen=chr(45); $uuid=chr(123) .substr($charid,0,8).$hyphen .substr($charid,8,4).$hyphen .substr($charid,12,4).$hyphen .substr($charid,16,4).$hyphen .substr($charid,20,12) .chr(125); return $uuid; } }
//生成变种guid function myguid(){ $guid=guid(); $guid=trim($guid,'{}'); $guid=str_replace('-', '_',$guid); return $guid; } function https_get($url,$param){ } function https_post($url,$param){ if(empty($url)|| empty($param)){ return false; } }
这样小程序就可以进行一个简单的刷脸入库了,大家可以去试一下,接下来还需要检测是否是人脸和活体检测,大家敬请期待。