1. 程式人生 > >微信小程式(利用百度雲後臺進行人臉識別)

微信小程式(利用百度雲後臺進行人臉識別)

  takePhoto() {
    const ctx = wx.createCameraContext()
    ctx.takePhoto({
      quality: 'high',
      success: (res) => {
        this.setData({
          src: res.tempImagePath

        })
        wx.uploadFile({
          url: "http://lichenlu.top/server/index.php/home/index/login",
          filePath: this.data.src,
          name: 'file',
          success: function (res) {
            console.log(res.data);
            wx.showToast({
              title: res.data,
              icon: 'none',
              duration: 2000
            })
          }
        })
        
      }
    })
  },
 
  error(e) {
    console.log(e.detail)
  },
  chooseImage: function () {
    var that = this
    wx.chooseImage({
      // // sourceType: sourceType[this.data.sourceTypeIndex],
      // sizeType: sizeType[this.data.sizeTypeIndex],
      // count: this.data.count[this.data.countIndex],
      count: 1, // 預設9
      sizeType: ['original', 'compressed'], // 可以指定是原圖還是壓縮圖,預設二者都有
      sourceType: ['album', 'camera'], // 可以指定來源是相簿還是相機,預設二者都有
      success: function (res) {
        console.log(res)
        that.setData({
          imageList: res.tempFilePaths
        })
      }
    })
  },
  //預覽圖片
  previewImage: function (e) {
    var current = e.target.dataset.src

    wx.previewImage({
      current: current,
      urls: this.data.imageList
    })
  }
前臺呼叫介面開啟手機攝像頭,後臺設定介面中的內容 上傳拍照內容到阿里雲伺服器的uploads資料夾中,此外還要建立百度雲人臉頭像小組  

以下為上傳到百度雲後臺

	private function face_group(){
		//組名
		$groupname = 'pingjiao';

		$client = $this->init_face();
		$ret = $client->getGroupList();
		if($ret['eror_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;
		}
	}

上傳照片時檢查人臉可靠性等判斷

		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();

					$faces = $client->addUser($image,'BASE64',$group);
					// echo '人臉檢測完成,並已入庫';
					if($faces['error_code']>0){
						$client->addUser($image,'BASE6',$group,$no);
					
					}else{
						// die('可靠性為:'. $face_probability);
						$client -> updateUser($image,'BASE64',$group,$no);
					}
					return true;
				}else{
					// die('人臉數量大於1');
					die('圖片數量僅為:' . $face_probability . ',上傳失敗!');
				}
			}
	}