微信小程序——如何做文件上传和检测人脸

首先,用thinkphp框架在控制器中写一个uploud()方法:

public function upload(){
      $upload = new \Think\Upload();// 实例化上传类
      $upload->maxSize = 3145728 ;// 设置附件上传大小
      $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
      $upload->rootPath = './Uploads/'; // 设置附件上传根目录
      $upload->savePath = ''; // 设置附件上传(子)目录
      //上传文件
      $info = $upload->uploadOne($_FILES['file']);
      if(!$info) {// 上传错误提示错误信息
      return $this->ajaxReturn(array('error'=>true,'msg'=>$upload->getError()));
      }else{// 上传成功 获取上传文件信息
     return $this->ajaxReturn(array('error'=>false,'msg'=>$info['savepath'].$info['savename']));
      }
    }

在微信小程序中的.js文件中写上接口。

下面是检测人脸的代码:

在php文件中写sdk()方法:

public function sdk(){
      $file='./Uploads/3.jpg';
      if(!file_exists($file)){
        die('文件不存在');
      }
      $dir=APP_PATH . '/face-sdk/';

      require_once $dir . 'AipFace.php';

     // 你的 APPID AK SK
       $APP_ID = '你的id';
       $API_KEY = '你的key';
       $SECRET_KEY = '你的sk';

       $client=new \AipFace($APP_ID,$API_KEY,$SECRET_KEY);

       $image=file_get_contents($file);
       $image=base64_encode($image);

       // echo $image;
       // exit;
       $imageType="BASE64";

       // 如果有可选参数
        $options = array();
        // $options["face_field"] = "age";
        $options["max_face_num"] = 10;
        // $options["face_type"] = "LIVE";

       $ret=$client->detect($image,$imageType,$options);
       print_r($ret);
       $result=$ret['result'];
       $list=$result['face_list'];

       $face_num=$result['face_num'];
       // echo $face_num;
       foreach ($list as $key => $value) {
         $face_probability =$value['face_probability'];

          //旋转角为90度
       $yaw=$value['angle']['yaw'];
       // print_r($yaw);
       $pitch=$value['angle']['pitch'];
       // print_r($pitch);
       $roll=$value['angle']['roll'];

       }

       if($face_num===1 && $face_probability===1 && $yaw<20 && $pitch<20 && $roll<20){
        $userid=guid();
        $userid=ltrim($userid,'{');
        $userid=rtrim($userid,'}');
        $userid=str_replace('-','_',$userid);
        $groupid='face_01';
        // echo $userid;
        $rett=$client->addUser($image,$imageType,$groupid,$userid);

       }else{
        echo '照片不符合要求';
       }
       if($ret){
        echo '创建成功';
       }else{
        echo "创建失败";
       }
}

猜你喜欢

转载自blog.csdn.net/lixiaotong_123/article/details/80385664