1. 程式人生 > >微信小程式--人臉識別入庫以及匹配人臉

微信小程式--人臉識別入庫以及匹配人臉

首先,建立Camera目錄和page

camera.xml的程式碼如下

<camera device-position="{{show}}" flash="off" binderror="error" style="width: 100%; height: 400px;"></camera>
 <view class='weui-cell__ft'>
            <switch checked bindchange='switch1Change'></switch>
  </view>
 <view class="weui-btn-area">
          <button class="weui-btn" type="primary" bindtap="takePhoto">拍照                   </button>
</view> 

camera.js程式碼

拍照和錄影的方法如下

// 拍照
  takePhoto() {
    this.ctx.takePhoto({
      quality: 'high',
      success: (res) => {
        this.setData({
          src: res.tempImagePath
        })

        wx.uploadFile({
          url: '', //僅為示例,非真實的介面地址
          filePath: this.data.src,

          name: 'file',
          formData: {

          },
          success: function (res) {
            // var data = res.data
            // console.log(res.data);
            //do something
            wx.showModal({
              title: '提示',
              content: res.data,
            })
          }
        })

      }
    })

  },

  //開始錄影
  startRecord() {
    this.ctx.startRecord({
      success: (res) => {
        console.log('startRecord')
      }
    })
  },
  //結束錄影
  stopRecord() {
    this.ctx.stopRecord({
      success: (res) => {
        this.setData({
          src: res.tempThumbPath,
          videoSrc: res.tempVideoPath
        })
      }
    })
  },

後臺的程式碼如下:

 public function upload($id=''){
        if(empty($id)){
            return false;
        }

        $no=M('student')->where("id=$id")->getField('no');

        // echo $no;
        // exit;
        $dir="./Uploads/studentface/";//上傳檔案路徑
        if(!file_exists($dir)){
            mkdir($dir,0777,true);
        }

         $upload = new \Think\Upload();// 例項化上傳類
        $upload->maxSize = 2048000;// 設定附件上傳大小2m
        $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) {// 上傳錯誤提示錯誤資訊
    // $this->error($upload->getError());
      // echo json_encode(array('error'=>true,'msg'=>$upload->getError()),JSON_UNESCAPED_UNICODE); 
      $this->ajaxReturn(array('error'=>true,'msg'=>$upload->getError()));
    }else{// 上傳成功
            // $this->success('上傳成功!');

            $file = $dir.$info['savepath'].$info['savename'];
            // echo $file;

            $image = base64_encode(file_get_contents($file));
            // echo  $no;
            // echo $image;
            $this->facevalid($no,$image);
           
          
            $m=M('head');
    
            $data=$m->where("no='{$no}'")->find();
            // print_r($data);

            if($data){
                //有資料就更新
                $m->where("no='{$no}'")->save(array('base64'=>$image));
            }else{
                //無資料就新增
                $m->add(array('no'=>$no,'base64'=>$image,'path'=>$file));

            }

            return '採集照片成功';
      // return $this->ajaxReturn(array('error'=>false,'msg'=>'上傳成功'));
    }
這就是主要的程式碼了,你要沒有get到呢?如果對你有幫助給個贊吧!