FaceTracking2DModule Overview
FaceTracking2DModule Overview
FaceTracking2DModule Overview


The FaceTracking2DModule class enables tracking faces in 2D.


// The following example demonstrates how to bind the position of a rectangle
// to a 2D face.
// The rectangle is also scaled to match the dimensions of the detected
// face's bounding box.
// Project setup:
// - Insert a rectangle

// Load in the required modules
const Scene = require('Scene');
const CameraInfo = require('CameraInfo');
const FaceTracking = require('FaceTracking2D');

(async function () { // Enable async/await in JS [part 1]

    // Find the rectangle in the scene
    const rectangle = await Scene.root.findFirst("rectangle0");

    // Create a reference to the detected face
    const face = FaceTracking.face(0);

    // Calculate the width and height of the preview,
    // correcting for the pixel resolution
    const previewSizeWidth  = CameraInfo.previewSize.width

    const previewSizeHeight = CameraInfo.previewSize.height

    // Set the position of the rectangle to the position of the face,
    // multiplied by the preview size
    rectangle.transform.x = face.boundingBox.x.mul(previewSizeWidth);
    rectangle.transform.y = face.boundingBox.y.mul(previewSizeHeight);

    // Set the size of the rectangle to the size of the face,
    // multiplied by the preview size
    rectangle.width  = face.boundingBox.width.mul(previewSizeWidth);
    rectangle.height = face.boundingBox.height.mul(previewSizeHeight);

})(); // Enable async/await in JS [part 2]


(get) count: ScalarSignal
(set) (Not Available)

Returns a ScalarSignal representing the number of faces tracked in the scene.


face(index: number): Face2D

Returns the Face2D object from the detected face array at the specified index.


Face2DThe Face2D class exposes details of a tracked face.

Was this article helpful?