HandTracker
Detects hands in an image.
public class HandTracker : IDisposable
Implements System.IDisposable interface.
Constructors
HandTracker()
Creates a new HandTracker object.
Properties
Version
string
Read-Only. The API version.
Copyright
string
Read-Only. The API copyright.
Methods
Load(UnityEngine.Color32[], int, int)
List<Hand>
Detects the hands from the specified color array, width and height.
Load(byte[], int, int, int)
List<Hand>
Detects hands for the specified image data, width, height and channels.
Dispose()
void
Releases unmanaged resources
Example
The example shows how to detect hands with the HandTracker
class.
Create a HandTracker
HandTracker handTracker = new HandTracker();
Detect hands in Texture2D
To detect hands in a Texture2D, create a Texture2D and load an image. There are multiple ways to load an image. You could load it from a webcam using the WebcamSource, from a file in the Assets folder or from a file on your disc.
// Create an empty Texture2D.
Texture2D texture = new Texture2D(0, 0);
// Load a file to a byte array.
string filename = "path/to/image";
byte[] rawData = System.IO.File.ReadAllBytes(filename);
// Load an byte array to the Texture2D.
texture.LoadImage(rawData);
After loading the image to the texture there are 3 different ways to detect hands in that image:
The simplest is to detect hands for the specified Texture2D.
List<Hand> hands = handTracker.Load(texture);
If you need to run the detection in another thread, for example, you can get the texture pixels and detect hands for the specified color array.
// Get the texture as a Color32 array.
Color32[] colors = texture.GetPixels32();
// Detect hands for the specified color array.
List<Hand> hands = handTracker.Load(colors, texture.width, texture.height);
Alternatively, you can get the texture raw data and detect hands for the specified byte array.
// Get the texture as a byte array.
byte[] rawData = texture.GetRawTextureData();
// Detect hands for the specified byte array, width, height and channels.
List<Hand> hands = handTracker.Load(rawData, texture.width, texture.height, 3);
Detect hands in an image file
To detect hands in an image file, load the file and detect hands for the specified byte array.
// Load byte array from file.
string filename = "Assets/Images/test.png";
byte[] rawData = File.ReadAllBytes(filename);
// Detect hands for the specified byte array, width, height and channels.
List<Hand> hands = handTracker.Load(rawData, 500, 500, 3);
Dispose Handtracker
Once tracking is no longer needed, it's recommended to dispose of the HandTracker
. You can do that either in the OnApplicationQuit()
or the OnDestroy()
methods.
handTracker.Dispose();
Last updated