基于百度AI接口的微信小程序-人臉?biāo)阉?/h1>
      <view class="container">
        <!-- 相機(jī)組件 -->
        <camera
          class="camera"
          device-position="front"
          flash="off"
          binderror="error"
        ></camera>
      
        <view class="button_container">
          <!-- 按鈕組件,點(diǎn)擊上傳人臉庫(kù) -->
          <button class="button"
                  bindtap="takePhoto">上傳人臉庫(kù) </button>
          <!-- 按鈕組件香伴,進(jìn)行人臉識(shí)別 -->
          <button class="button"
                  bindtap="check">人臉庫(kù)識(shí)別 </button>
        </view>
      </view>

2慰枕、Wxss:

    page {
      background: white;
    }
    
    .container {
      display: flex;
      flex-direction: column;
      align-items: center;
    }
    
    .button_container {
      margin-top: 600rpx;
      display: flex;
      flex-direction: column;
    }
    
    .camera {
      width: 300rpx;
      height: 300rpx;
      border-radius: 50%;
      position: fixed;
      top: 100rpx;
    }
    
    .button {
      margin-top: 20rpx;
      width: 100rpx;
      height: 60rpx;
      background: forestgreen;
      color: white;
    }

3、Javascript:

    // camera.js
    Page({
      data: {
        src: "",
        token: "",
        base64: "",
        msg: "",
      },
      //上傳至人臉庫(kù)
      upload(){
        this.takePhoto();
        this.faceUpload();
      },
      //進(jìn)行人臉識(shí)別
      check(){
        this.takePhoto();
        this.faceRecognition();
      },
    
    //拍照
      takePhoto() {
        var that = this;
        //創(chuàng)建拍照上下文對(duì)象
        const ctx = wx.createCameraContext()
        ctx.takePhoto({
          quality: 'high',
          //拍照成功
          success: (res) => {
            console.log(res)
            wx.getFileSystemManager().readFile({
              filePath: res.tempImagePath,
              encoding: 'base64',
              success: res => {
                console.log(res)
                this.setData({
                  base64: res.data
                })
              },
               //拍照失敗
              fail: err => {
                console.log(err)
                this.showToast("調(diào)用失敗,請(qǐng)稍后重試");
              }
            })
          },
          fail: err => {
            this.showToast("調(diào)用失敗,請(qǐng)稍后重試");
          }
        })
      },
    //人臉上傳
      faceUpload(group_id, user_id) {
        //調(diào)用接口即纲,獲取token
        wx.request({
          url: 'https://aip.baidubce.com/oauth/2.0/token', //百度智能云相關(guān)的接口地址
          data: {
            grant_type: 'client_credentials',
            client_id: 'xxxxxxxxxxxxxxxx',//用你創(chuàng)建的應(yīng)用的API Key
            client_secret: 'xxxxxxxxxxxxxxxx'//用你創(chuàng)建的應(yīng)用的Secret Key
          },
          header: {
            'Content-Type': 'application/json' // 默認(rèn)值
          },
          //獲取到之后具帮,請(qǐng)求接口,向人臉庫(kù)添加照片
          success: res => {
            console.log(res)
            wx.request({
              url: 'https://aip.baidubce.com/rest/2.0/face/v3/faceset/user/add?access_token=' + res.data.access_token,
              method: 'POST',
              data: {
                image: this.data.base64,
                image_type: 'BASE64',
                group_id:'xxx',//用戶組的id
                user_id:"xxxx"http://給用戶指定的id
              },
              header: {
                'Content-Type': 'application/json'
              },
              success: res => {
                // console.log(res)
                if (res.data.error_msg == 'SUCCESS') {
                  wx.hideLoading();
                  this.showToast("上傳成功");
                } else {
                  this.showToast("上傳失敗低斋,請(qǐng)稍后重試");
                }
              },
              fail(err) {
                this.showToast("上傳失敗蜂厅,請(qǐng)稍后重試");
              }
            })
          },
          fail(err) {
            // console.log(err)
            this.showToast("上傳失敗,請(qǐng)稍后重試");
          }
        })
      },
    
      //人臉識(shí)別
      faceRecognition( group_id_list) {
        var that = this;
        wx.showLoading({
          title: '識(shí)別中',
        })
        //獲取token
        wx.request({
          url: 'https://aip.baidubce.com/oauth/2.0/token', //真實(shí)的接口地址
          data: {
            grant_type: 'client_credentials',
            client_id: 'xxxxxxxxxxxxxx',
            client_secret: 'xxxxxxxxxxxxxxxx'
          },
          header: {
            'Content-Type': 'application/json'
          },
          //有了token之后膊畴,進(jìn)行人臉識(shí)別
          success: res => {
            wx.request({
              url: 'https://aip.baidubce.com/rest/2.0/face/v3/search?access_token=' + res.data.access_token,
              method: 'POST',
              data: {
                image: that.data.base64,
                image_type: 'BASE64',
                group_id_list: 'xxxx'//你創(chuàng)建的應(yīng)用的用戶組id
              },
              header: {
                'Content-Type': 'application/json'
              },
              success: res => {
                wx.hideLoading();
                console.log(res)
                that.setData({
                  msg: parseInt(res.data.result.user_list[0].score)
                })
                if (that.data.msg > 80) {
                  let title = "識(shí)別通過,匹配率:" + this.data.msg + "%";
                  this.showToast(title)
                } else {
                  let title = "識(shí)別未通過,匹配率:" + this.data.msg + "%";
                  this.showToast(title)
                }
              },
              fail: err => {
                wx.hideLoading();
                this.showToast("調(diào)用失敗,請(qǐng)稍后重試")
              }
            });
          }
        })
      },
    //封裝的wx.showToast
      showToast(title) {
        wx.showToast({
          title: title,
          icon: 'none',
          duration: 3000
        })
      }
    })

請(qǐng)求中body中相關(guān)的參數(shù)這里就不一一說明了掘猿,詳情請(qǐng)閱讀官方文檔
本文只介紹了簡(jiǎn)單的人臉?biāo)阉鲗?shí)現(xiàn)方法巴比,由于功力有限术奖,還望多多指教。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者

  • 序言:七十年代末轻绞,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子佣耐,更是在濱河造成了極大的恐慌政勃,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,548評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件兼砖,死亡現(xiàn)場(chǎng)離奇詭異奸远,居然都是意外死亡既棺,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,497評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門懒叛,熙熙樓的掌柜王于貴愁眉苦臉地迎上來丸冕,“玉大人,你說我怎么就攤上這事薛窥∨种颍” “怎么了?”我有些...
    開封第一講書人閱讀 167,990評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵诅迷,是天一觀的道長(zhǎng)佩番。 經(jīng)常有香客問我,道長(zhǎng)罢杉,這世上最難降的妖魔是什么趟畏? 我笑而不...
    開封第一講書人閱讀 59,618評(píng)論 1 296
  • 正文 為了忘掉前任,我火速辦了婚禮滩租,結(jié)果婚禮上赋秀,老公的妹妹穿的比我還像新娘。我一直安慰自己律想,他們只是感情好猎莲,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,618評(píng)論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著蜘欲,像睡著了一般益眉。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上姥份,一...
    開封第一講書人閱讀 52,246評(píng)論 1 308
  • 那天郭脂,我揣著相機(jī)與錄音,去河邊找鬼澈歉。 笑死展鸡,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的埃难。 我是一名探鬼主播莹弊,決...
    沈念sama閱讀 40,819評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼涡尘!你這毒婦竟也來了忍弛?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,725評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤考抄,失蹤者是張志新(化名)和其女友劉穎细疚,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體川梅,經(jīng)...
    沈念sama閱讀 46,268評(píng)論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡疯兼,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,356評(píng)論 3 340
  • 正文 我和宋清朗相戀三年然遏,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片吧彪。...
    茶點(diǎn)故事閱讀 40,488評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡待侵,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出姨裸,到底是詐尸還是另有隱情秧倾,我是刑警寧澤,帶...
    沈念sama閱讀 36,181評(píng)論 5 350
  • 正文 年R本政府宣布啦扬,位于F島的核電站中狂,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏扑毡。R本人自食惡果不足惜胃榕,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,862評(píng)論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望瞄摊。 院中可真熱鬧勋又,春花似錦、人聲如沸换帜。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,331評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽惯驼。三九已至蹲嚣,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間祟牲,已是汗流浹背隙畜。 一陣腳步聲響...
    開封第一講書人閱讀 33,445評(píng)論 1 272
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留说贝,地道東北人议惰。 一個(gè)月前我還...
    沈念sama閱讀 48,897評(píng)論 3 376
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像乡恕,于是被迫代替她去往敵國(guó)和親言询。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,500評(píng)論 2 359