0.1.5 • Published 10 months ago

react-hand-tracking v0.1.5

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

Hand tracking component for React

This component used Google's Gesture recognition API to predict gestures and location of hand landmarks from the user's webcam

Features

  • Recognized user's hand gestures
  • Track 21 different hand landmarks
  • Easy integration with any React project

Installation

npm i react-hand-tracking # using npm

Basic usage

import React from 'react'
import { ReactHandTracking } from 'react-hand-tracking'

export default function App() {
  return (
    <main>
      <ReactHandTracking callback={(results) => console.log(results)} />
    </main>
  )
}

In this example, the results will be printed out to the console every frame

Results object

The ReactHandTracking component will run your callback function passing the results array as the only argument. It is important to understand the structure of this object.

The results array will contain one entry for every hand present in the frame. Make sure to read the configuration section to learn how to configure the component to detect a custom number of hands.

results: [
  { // Each object represents one hand. If the numHands prop is set to 1, the maximum length of the array will be 1.
    hand: { // information about the hand
      handType: "Right" | "Left",
      confidence: number (0-1) // how confident model is on the handType prediction. From 0 to 1
    },
    gesture: { // Information about the gesture being performed
      gestureType: // The gesture provided will be one of the following:
        | 'None'
        | 'Closed_Fist'
        | 'Open_Palm'
        | 'Pointing_Up'
        | 'Thumb_Down'
        | 'Thumb_Up'
        | 'Victory'
        | 'ILoveYou',
      confidence: number (0-1)
    },
    landmarks: [ // Array with lenght 21. Each entry represents a different hand landmark
                 // Refer to the next section on information about what element in the array represents which landmark
      {x: number, y: number, z: number},
      ...
    ]
  },
  ...
]

Landmakrs:

Refer to the following image for information about how the hand landmarks are provided: For example, if we wanted to access the location of the index finger, we would access the 8th element in the landmarks array.

const callback = (results) => {
  const indexPosition = results[0].landmarks[8]
  console.log(`The index finger is located at coordinates (${indexPosition.x}, ${indexPosition.y})`)
}

Configuration

The element provides a number of configuration options that can be set through props.

Prop nameDescription
callbackThe function specified by the user which takes the results object as an argument default value: (result: ReactHandTrackingResult[]) => console.log(result)
videoResolutionAn object of the form { width: number, height: number }. It specified the resolution of video to be processed by the model. A highier resolution can give a highier a better accuracy, but it will also affect performance. default value: { width: 1280, height: 720 },
recognizerOptionsAn object of the form { numHands: number detectionThreshold: number }. The numHands entry specifies the maximum number of hands to be tracked by the model. The detectionThreshold is a number from 0 to 1 which specifies the confidence threshold for the model to start tracking a hand. default value: { numHands: 1, detectionThreshold: 0.5 }
predictionTimeoutA number. Specifies the number of miliseconds to wait in between each prediction. A larger value will mean you have to wait more to receive an new results object, but will require consume less computation resources. default value: 0

Typescript

The package exports a typescript type for the result array given as an argument to the callback function.

import React, { useState } from 'react'
import { ReactHandTracking, ReactHandTrackingResultType } from 'react-hand-tracking'

export default function App() {
  const [firstHandData, setFirstHandData] = useState<ReactHandTrackingResultType>(undefined)

  const handTrackingCallback = (result: ReactHandTrackingResultType[]) => setFirstHandData(result[0])

  return (
    <main>
      <ReactHandTracking callback={handTrackingCallback} />
    </main>
  )
}
0.1.5

10 months ago

0.1.4

10 months ago

0.1.3

11 months ago

0.1.2

11 months ago

0.1.1

11 months ago

0.1.0

11 months ago