微信小程序> 微信小程序之人脸识别,并入库

微信小程序之人脸识别,并入库

浏览量:1345 时间: 来源:gqycrush

现在人脸识别可以说是非常火热,支付宝可以人脸识别登录,公司需要人脸识别上下班,还有有的地方取钱也可以刷脸取钱,所以我们要根据大众的需求,做一款可以刷脸的小程序。

在此我们需要有一个百度云账号,下载上面的sdk文件它里面存了好多接口,可以直接调用。

新建一个home模块,home模块新建一个IndexController.php控制器。

?phpnamespace HomeController;use ThinkController;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 ThinkUpload();// 实例化上传类      $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,以便调用,

?phpfunction 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;}}
//生成变种guidfunction 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;    }}

这样小程序就可以进行一个简单的刷脸入库了,大家可以去试一下,接下来还需要检测是否是人脸和活体检测,大家敬请期待。

版权声明

即速应用倡导尊重与保护知识产权。如发现本站文章存在版权问题,烦请提供版权疑问、身份证明、版权证明、联系方式等发邮件至197452366@qq.com ,我们将及时处理。本站文章仅作分享交流用途,作者观点不等同于即速应用观点。用户与作者的任何交易与本站无关,请知悉。

产品经理

手机 : 13312967497

擅长 : 小程序流量变现

扫码领取礼包

热门模板

  • 头条
  • 搜狐
  • 微博
  • 百家
  • 一点资讯
  • 知乎