# Hand detection

VisionKit from the base library Version 2.28.0 provides hand detection capabilities. from Weixin>=8.1.0 The version begins to provide manual 3D keypoint detection as an extended capability interface for Hand detection.

# Definition of method

There are two ways to use hand detection, one is to input a static picture for detection, the other is through the camera real-time detection.

# 1. Static picture detection

adopt [VKSession.detectHand interface](https://developers.weixin.qq.com/miniprogram/dev/api/ai/Visionkit /VKSession.detectHand.html) Input an image, the algorithm detects the gesture in the image, and then passes [VKSession.on interface](https://developers.weixin.qq.com/miniprogram/dev/api/ai/Visionkit /VKSession.on.html) Output the acquired gesture key point information.

Sample code:

const session = wx.createVKSession({
  track: {
    hand: { mode: 2 } // mode: 1 - Use the camera2 - Manual input image
  },
})

// In static image detection mode, each tone detectHand The interface will trigger once updateAnchors event
session.on('updateAnchors', anchors => {
    this.data.anchor2DList = []
    this.setData({
        anchor2DList:  anchors.map(anchor => ({
            points: anchor.points, // Key point coordinates
            origin: anchor.origin, // Identifying Box Start Point Coordinates
            size: anchor.size, // The size of the identification box
            gesture: anchor.gesture // Gesture classification
        })),
    }) 
})

// Need to call once start To initiate
session.start(errno => {
  if (errno) {
    // If it fails, it will return. errno
  } else {
    // Otherwise, return null, indicating success
    session.detectHand({
      frameBuffer, // picture ArrayBuffer Data. Pixel data of the image to be detected, each of four terms representing a RGBA
      width, // Image width
      height, // Image height
      scoreThreshold: 0.5, // Scoring threshold
      algoMode: 2 //Model: 0, Detect Modes, Output Boxes and DotsGesture mode, output box and gesture classification2, both 0 and 1, output box, point, gesture classification
    })
  }
})

# 2. Real-time detection via camera

Algorithm to detect gesture gestures in the camera in real time by [VKSession.on interface](https://developers.weixin.qq.com/miniprogram/dev/api/ai/Visionkit /VKSession.on.html) Output the detected gesture key points in real time.

Sample code:

const session = wx.createVKSession({
  track: {
    hand: { mode: 1 } // mode: 1 - Use the camera2 - Manual input image
  },
})

// When gestures are detected, updateAnchors are updateAnchors Events are triggered continuously (Triggered once per frame)
session.on('updateAnchors', anchors => {
    this.data.anchor2DList = []
    this.data.anchor2DList = this.data.anchor2DList.concat(anchors.map(anchor => ({
        points: anchor.points,
        origin: anchor.origin,
        size: anchor.size
    })))
})

// When the gesture is removed from the camera, it triggers removeAnchors event
session.on('removeAnchors',  () => {
  console.log('removeAnchors')
})

// Need to call once start To initiate
session.start(errno => {
  if (errno) {
    // If it fails, it will return. errno
  } else {
    // Otherwise, return null, indicating success
  }
})

# 3. Turn on 3D keypoint detection

To enable manual 3D keypoint detection, static picture mode only needs to be added on top of the 2D call.open3dFields, as follows

// Static picture mode call
session.detectHand({
      ...,           // Same as 2D call parameters
      open3d: true,  // Turn on manual 3D keypoint detection capability, defaults to false
    })

The real-time mode adds a 3D switch update function to the 2D call, as follows

// Camera Real Time Mode Call
session.on('updateAnchors', anchors => {
  this.session.update3DMode({open3d: true})  // Turn on manual 3D keypoint detection capability, defaults to false
  ...,  // Same as 2D call parameters
})

# Output Dxplaination

# Point explaination

The manual 2D keypoint definition is the same as OpenPose, using a 21-bit bit definition, as shown in the figure below.

The 3D key points of the human hand are defined using the MANO-16 point joint, as shown in the figure below.

# Manual testing

Manual Inspection Output Fields include

struct anchor
{
  points,     // Hand 2D Key Points in the Image(x,y)coordinate
  origin,     // The upper left corner of the manual detection box(x,y)coordinate
  size,       // Width and height of the manual detection frame(w,h)
  score,      // Confidence level of manual detection frame
  confidence, // Confidence Level of Manpower Key Points
  gesture     // Hand Gesture Category
}

# Manpower 3D Key Points

After turning on the manpower 3D key point detection capability, the manpower 2D and 3D key point information can be obtained, wherein the manpower 3D key point output field includes

struct anchor
{ 
  ...,               // Hand detection 2D output information
  points3d,          // Manpower 3D key points(x,y,z)3D coordinates
  camExtArray,       // The camera outer parameter matrix, defined as[R, T  03 , 1], 3D points can be projected back to the image using the camera's internal and external parameters matrix
  camIntArray        // Camera internal parameter matrix, with reference to glm:: perspective(fov, width / height, near, far)
}

# Application Scenario Examples

  1. Smart home.
  2. AR Interaction.
  3. Smart car.

# Program Examples

  1. [Real Time Camera Hand Detection Capability Using Reference](https://github.com/WeChat mini-program /miniprogram-demo/tree/master/miniprogram/packageAPI/pages/ar/hand-detect)
  2. [Static image hand detection capabilities to use reference](https://github.com/WeChat mini-program /miniprogram-demo/tree/master/miniprogram/packageAPI/pages/ar/photo-hand-detect)