Picture recognition, face recognition, gesture recognition, sign-in applet, with Baidu AI intelligent recognition function

Second, preparation

2-1, register a Baidu developer account

We use Baidu's image recognition technology here, so before using it, we need to register a Baidu developer account now, the official address: https
://ai.baidu.comRegistration address: https
://login.bce.baidu.comAs for How to register, I don't need to teach here, everyone can register by themselves.
We mainly use face recognition technology

2-2, remember to register with your real name

Now to use Baidu image recognition, you must be authenticated by real name.

Choose the certification method according to your own situation. If you

study, you only need personal certification.

2-3, create a face recognition application

What we mainly use here is Baidu's face comparison function, which is to save the photo of the person to be identified in the Baidu database in advance, and then when the card is punched, let the corresponding person take a good photo and compare it with the face picture stored in the database. If the degree reaches a certain value, such as 90% similarity, it can be considered that the recognition is successful, and you can sign in.
insert image description here
It is estimated that Baidu is preparing for later charges, but there are still free resources to receive. Since you can white piao, get the free one first. Even if it is charged later, it is estimated that it will not take much for us to learn to use it. It is estimated that a few cents are enough for us to learn and use.

Official document of face recognition: https://cloud.baidu.com/product/face

Click to receive resources for

free Since it is free, of course, all of them are received.

After receiving it, wait patiently for it to take effect. If you can't get it for free when you are studying, you can pay for it with a few cents. Basically, a few cents are enough for us to learn and use.

After all the above should be collected, let's create an application.

By default, all face recognition related items have been checked, and

you can select an individual.

After creation, the following two things will be used later, and you can know where they are.

3. Access face recognition

3-1, official documentation

When we access face recognition in our own applet, we must read Baidu's official technical documentation.
Document address: https://ai.baidu.com/ai-doc/FACE/Lk37c1tpf
We mainly look at this face comparison document here.

The official documentation does not give the code how to call in the applet.

Therefore, the next study should follow in the footsteps of Brother Stone, who will take you hand in hand to realize the call of the applet.

3-2, face registration

If we want to realize face recognition, we need to register the face in Baidu's visual face library at the beginning. The interface to be called is as follows.


Before calling this, we need to get the corresponding access_token, so the first step we need to do is to get the access_token

3-2-1, get access_token

All the operations we do later basically have to get this.

So I posted the source code for everyone, remember to replace the client_id and client_secret with your own.

wx.request({
      url: 'https://aip.baidubce.com/oauth/2.0/token',
      data: {
        grant_type: 'client_credentials',
        client_id:, //应用的API Key
        client_secret:  //应用的Secret Key
      },
      header: {
        'Content-Type': 'application/json' // 默认值
      },
      success: res => {
        this.setData({
          token: res.data.access_token //获取到token
        })
        console.log('获取到的token', this.data.token)
      }
    })

3-2-2, take a face photo

The second and third steps need to be done at the same time, so I put the code in the third step below

3-2-3, register face to Baidu face database

After we take a picture, we get the picture and convert the picture to base64 through the wx.getFileSystemManager().readFile() method, because Baidu needs data in this format. The

corresponding code is as follows:

    var that = this;
    //拍照
    const ctx = wx.createCameraContext()
    ctx.takePhoto({
      quality: 'high',
      success: (res) => {
        that.setData({
          src: res.tempImagePath //获取图片
        })
        //图片base64编码
        wx.getFileSystemManager().readFile({
          filePath: that.data.src, //选择图片返回的相对路径
          encoding: 'base64', //编码格式
          success: res => { //成功的回调
            that.setData({
              base64: res.data
            })
            //第三步:上传人脸进行注册
            wx.request({
                url: 'https://aip.baidubce.com/rest/2.0/face/v3/faceset/user/add?access_token=' + that.data.token,
                method: 'POST',
                data: {
                  image: that.data.base64,
                  image_type: 'BASE64',
                  group_id: 'users', //自己建的用户组id
                  user_id: app.globalData.userInfo.phone, //学号
                  user_info: app.globalData.userInfo.name //存储学生姓名
                },
                header: {
                  'Content-Type': 'application/json' // 默认值
                },
                success(res) {
                  that.setData({
                    msg: res.data.error_msg
                  })
                  console.log("人脸注册返回结果", res)
                  //做成功判断
                  if (that.data.msg == 'SUCCESS') { //微信js字符串使用单引号
                    wx.showToast({
                      title: '注册成功',
                      icon: 'success',
                      duration: 2000
                    })
                    // that.registerFace()
                  }
                }
              }),
              //失败尝试
              wx.showToast({
                title: '请重试',
                icon: 'loading',
                duration: 500
              })
          }
        })
      } //拍照成功结束
    }) //调用相机结束

Remember to replace these parameters with your own

 	group_id: 'users', //自己建的用户组id
    user_id: app.globalData.userInfo.phone, //学号或者用户电话能唯一标识用户的
    user_info: app.globalData.userInfo.name //存储用户姓名

After we register, we can see this data in the Baidu face database, and we can see the users table we created.

3-3, view face database

We enter Baidu's console, find the following categories,

and then click on user 123 to see his registered face photos.

Guess you like

Origin blog.csdn.net/qiushi_1990/article/details/124270648