Scripting API
BodyTrackingModule
BodyTrackingModule Overview

BodyTrackingModule

The BodyTrackingModule class allows you to track the body and get Body details.

Example

//============================================================================
// Scales a rectangle to the bounding box of a detected body.
//
// Project setup:
// - Insert a rectangle to the scene
//============================================================================

// Load in the required modules
const Diagnostics = require('Diagnostics');
const Scene = require('Scene');
const BodyTracking = require('BodyTracking');


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

  // Locate the rectangle and canvas in the Scene
  const [rectangle, canvas] = await Promise.all([
    Scene.root.findFirst('rectangle0'),
    Scene.root.findFirst('canvas0')
  ]);

  // Get the rectangle's transform
  const rectangleTransform = rectangle.transform;

  // Get the canvas width and height
  const canvasBounds = canvas.bounds;
  const canvasBoundsWidth = canvasBounds.width;
  const canvasBoundsHeight = canvasBounds.height;

  // Create a reference to a detected body
  const body = bodyTracking.body(0);
  
  // Get the center point of the body's bounding box 
  const bodyBoundingBox = body0.boundingBox;
  const bodyBoundingBoxCenter = bodyBoundingBox0.center;
  
  // Get whether the body is currently being tracked
  const tracked = body.isTracked;

  // Convert the normalized screen space value for x and y by multiplying by the
  // width and height of the canvas
  const scaledX = bodyBoundingBoxCenter.x.mul(canvasBoundsWidth);
  const scaledY = bodyBoundingBoxCenter.y.mul(canvasBoundsHeight);

  // Subtract the half the width/height to center the plane correctly and
  // multiply the y position by -1
  // This is required because 0,0 in the canvas is the bottom left of the device
  const rectangleX  = scaledX.sub(canvasBoundsWidth.div(2));
  const rectangleY = scaledY.sub(canvasBoundsHeight.div(2)).mul(1);

  // Bind the new values to the rectangle's x and y axis positions
  rectangleTransform.x = rectangleX;
  rectangleTransform.y = rectangleY;

  // Bind the new values to the rectangle's x and y axis scale
  rectangleTransform.scaleX = bodyBoundingBox.width;
  rectangleTransform.scaleY = bodyBoundingBox.height;

  // Add the values to the watch view
  Diagnostics.watch("Count: ", bodyTracking.count);
  Diagnostics.watch("Tracked: ", body.isTracked);
  Diagnostics.watch("X position: ", bodyBoundingBox.x);
  Diagnostics.watch("Y position: ", bodyBoundingBox.y);
  Diagnostics.watch("Width: ", bodyBoundingBox.width);
  Diagnostics.watch("Height: ", bodyBoundingBox.height);

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

Properties

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


Returns a ScalarSignal representing the count of tracked bodies.

Methods

MethodDescription
body
body(index: number): Body


Returns a Body object.

Classes

ClassDescription
BodyThe Body class exposes details of a tracked body.
Body2DArmRepresents a tracked arm in the body.
Body2DLegRepresents a tracked leg in the body.
Body2DPoseThe Body2DPose class exposes details of a tracked body's 2d key points.
Body2DTorsoRepresents a tracked torso in the body.
KeyPoint2DRepresents a single tracked 2d key point in the scene.