E企盈营销工具技术服务商 热线:4006-838-530

微观塘区E企盈小程序信小程序刷脸认证(人脸识别入百度云人脸库)

创建一个thinkphp3的框架,创建一个小程序。在tp3框架里创建公共文件,function.php,生成guid//生成guidfunction 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;}

然后创建indexcontroller.class.php初始化人脸识别//初始化人脸识别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($no,$name,$sex,$age){开通小白鞋小程序电话:4006-838-530$data['no']=$no;$data['name']=$name;$data['sex']=$sex;$data['age']=$age;$data['guid'] = myguid();$id=M('student')->add($data);if($id){return $this->ajaxReturn(array('error'=>false,'msg'=>'添加成功!','guid'=>$student['guid']));}else{return $this->ajaxReturn(array('error'=>true,'msg'=>'添加出错'));}}

获取小组private function face_group(){   //组名   $groupname='1002';   $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('1001');   print_r($ret);   }

人脸识别//人脸识别public function facevalid(){$file = './Uploads/1.jpg';if(!file_exists($file)){die('文件不存在');}$image = base64_encode(file_get_contents($file));//如果有可选参数$options =array();$options["开通待产包小程序电话:4006-838-530max_face_num"]=2;$client=$this->init_face();$ret=$client->detect($image,'BASE64',$options);print_r($ret);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){//可靠性为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 upload($id=''){if(empty($id)){return false;}$no = M("student")->where("id={$id}")->getField("no");$dir = "./Uploads/studentface";//上传文件路径if(!file_exists($dir)){mkdir($dir,0777,true);}$upload = new ThinkUpload();// 实例化上传类$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) {// 上传错误提示错误信息echo json_encode(array('error'=>true,'msg'=>$upload->getError()),JSON_UNESCAPED_UNICODE);}else{// 上传成功$file = $dir . $info['savePath'].$info['savename'];$image = base64_encode(file_get_contents($file));$this->facevalid($no,$image);$m=M('head');$data = $m->where("no='{$no}'")->find();if($data){//有数据,更新$m->where("no='{$no}'")->save(array('base64'=>$image,'path'=>$file));}else{//无数据,添加$m->add(array('no'=>$no,'base64'=>$image,'path'=>$file));}echo "采集照片成功";}}

小程序首页<form bindsubmit="formSubmit" bindtap="locationViewTap">                <view class="weui-panel__hd">请输入您的信息</view>             <view class="weui-cell weui-cell_input">                 <view class="weui-cell__hd">                    <view class="weui-label">学号</view>                </view>                <view class="weui-cell__bd">                    <input class="weui-input" placeholder="请输入学号" name='no'                           />                </view>             </view>             <view class="weui-cell weui-cell_input">                <view class="weui-cell__hd">                    <view class="weui-label">姓名</view>                </view>                <view class="weui-cell__bd">                    <input class="weui-input" placeholder="请输入姓名" name='name'                           />                </view>            </view>            <view class="weui-cell weui-cell_input">              <view class="weui-label">性别</view>              <input class="weui-input" name='sex'  value='{{sex}}'/>              <view class='weui-cell_ft'>                  <switch checked bindchange='switch1Change'></switch>              </view>      </view>            <view class="weui-cell weui-cell_input">                <view class="weui-cell__hd">                    <view class="weui-label">年龄</view>                </view>                <view class="weui-cell__bd">                    <input class="weui-input" placeholder="请输入年龄" name='age'                           />                </view>            </view>  <view class="button">   <button class="weui-btn" type="primary" formType="submit">确定</button>  </view></form>

上传照片页<form bindsubmit="formSubmit" ><view class="page__hd">        <view class="page__title">采集照片</view>        <view class="page__desc">2</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}}/1</view>                </view>                <view class="weui-uploader__bd">                  <view class="weui-uploader__files">                    <block wx:for="{{imageList}}" wx:for-item="image">                      <view class="weui-uploader__file">                        &开通八角帽小程序电话:4006-838-530lt;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="button">  <button class="weui-btn" type="primary" formType="submit">确定</button>  <button class="weui-btn" type="default" bindtap="onProductsItemTap">取消</button></view></form>

赞(0) 打赏
未经允许不得转载:E企盈小程序开发-热线:4006-838-530 » 微观塘区E企盈小程序信小程序刷脸认证(人脸识别入百度云人脸库)
分享到:更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

模王系统开发

联系我们升级日志

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏