# Web SDK: Face tracking

`SonaTrack`: webcam face tracking, MediaPipe's face landmarker in, an avatar's face and head out ([guide](../../guides/face-tracking.md)).

## FaceTracker properties

| Field | Type | Meaning |
|---|---|---|
| `options` | `Record<string, unknown>` | The settings in use: the defaults with the ones createFaceTracker() was given. A copy. Read only. |

## tracker.feed()

```ts
interface FaceTracker {
  feed(frame: FaceFrame): void;
}
```

Takes one frame.

| Parameter | Type | Meaning |
|---|---|---|
| `frame` | [`FaceFrame`](#faceframe) | One frame of blendshape weights and head rotation; frameFromLandmarker() makes one. |

## tracker.state()

```ts
interface FaceTracker {
  state(): FaceTrackerState;
}
```

What the tracker makes of the frames so far.

Returns `FaceTrackerState`.

## tracker.reset()

```ts
interface FaceTracker {
  reset(): void;
}
```

Forgets every frame and hands the face back to the clip; a calibration is kept.

## tracker.calibrateBegin()

```ts
interface FaceTracker {
  calibrateBegin(): void;
}
```

The frames from calibrateBegin to calibrateEnd are the neutral face.

## tracker.calibrateEnd()

```ts
interface FaceTracker {
  calibrateEnd(): {
    frames: number;
    neutral: number[];
    head: number[];
  } | null;
}
```

Ends the calibration and uses it: the neutral face's 52 weights and the head's rest rotation [x, y, z, w]. Returns null when no frame came in.

Returns `{ frames: number; neutral: number[]; head: number[] } | null`.

## tracker.setCalibration()

```ts
interface FaceTracker {
  setCalibration(
    c: { frames: number; neutral: number[]; head: number[] } | null,
  ): void;
}
```

Uses a calibration saved earlier.

| Parameter | Type | Meaning |
|---|---|---|
| `c` | `{ frames: number; neutral: number[]; head: number[] } \| null` | A calibration calibrateEnd() returned, or null to drop it. |

## SonaTrack

Webcam face tracking: the page brings MediaPipe's face landmarker and a `<video>` with the camera; nothing leaves the browser.

| Field | Type | Meaning |
|---|---|---|
| `createFaceTracker` | `createFaceTracker(options?: Record<string, unknown>): FaceTracker` | options: mirror, noHead, noFace, noGaze; faceMinCutoff, faceBeta, headMinCutoff, headBeta, headGain, headLimitDeg, neckShare, sensitivity, lostSeconds, returnSeconds. |
| `startFaceTracking` | `startFaceTracking(opts: { video: HTMLVideoElement; landmarker: unknown; tracker: FaceTracker; avatar?: SonaAvatar \| null; calibrateSeconds?: number; onFrame?: (state: FaceTrackerState, frame: FaceFrame) => void }): () => void` | Runs the landmarker on each new video frame, feeds the tracker and, with an avatar, drives its face and head. Returns stop(). |
| `driveAvatar` | `driveAvatar(avatar: SonaAvatar, tracker: FaceTracker): () => void` | Puts the tracker's face and head on an avatar until the returned function is called; the avatar poses every frame meanwhile. |
| `frameFromLandmarker` | `frameFromLandmarker(result: unknown, timeSeconds: number): FaceFrame` | One FaceLandmarker result (detectForVideo) as a tracker frame. |

## FaceFrame

One frame for a face tracker: MediaPipe's 52 blendshapes (or ARKit's) and the head's rotation.

| Field | Type | Meaning |
|---|---|---|
| `time` | `number` | The frame's time in seconds, rising from frame to frame. |
| `found` | `boolean` | Whether a face was found in it; false lets the face go back to the clip after a moment. |
| `weights` (optional) | `number[] \| { categoryName: string; score: number }[]` | The 52 blendshape weights, 0 to 1, in the order layout names; or MediaPipe's categories, in any order. Needed when found. |
| `layout` (optional) | `number` | The order of a list of 52 numbers: 0 ARKit's, 1 MediaPipe's. Default 0. |
| `headRotation` (optional) | `[number, number, number, number]` | The head's rotation as a quaternion [x, y, z, w], in the camera's space. Default: none (the head keeps its rest turn). |

## FaceTrackerState

What a face tracker makes of the frames so far.

| Field | Type | Meaning |
|---|---|---|
| `tracking` | `boolean` | True once a face has been found, until it has been lost long enough to hand back to the clip. |
| `time` | `number` | The time of the last frame fed, in seconds. |
| `weight` | `number` | How much the tracked face and head show over the clip, 0 to 1: it eases in when a face is found and out when it is lost. |
| `expression` | `number[]` | Five shape indices: mouth, left brow, right brow, left eye, right eye. 255 leaves that part to the clip. |
| `mood` | `number` | The mood read from the face: 0 neutral, 1 sad, 2 angry, 3 confused, 4 laughing, 5 shocked, 6 happy. |
| `head` | `[number, number, number, number]` | The head's turn as a quaternion [x, y, z, w] in the body's space, smoothed and limited. |
| `weights` | `number[]` | The 52 blendshape weights after smoothing and calibration, 0 to 1, in ARKit's order. |
