The wrapper class for MediaPipe segmentation masks.
Masks are stored as Uint8Array
, Float32Array
or WebGLTexture
objects. You can convert the underlying type to any other type by passing the desired type to getAs...()
. As type conversions can be expensive, it is recommended to limit these conversions. You can verify what underlying types are already available by invoking has...()
.
Masks that are returned from a MediaPipe Tasks are owned by by the underlying C++ Task. If you need to extend the lifetime of these objects, you can invoke the clone()
method. To free up the resources obtained during any clone or type conversion operation, it is important to invoke close()
on the MPMask
instance.
Signature:
export declare class MPMask
Properties
Property | Modifiers | Type | Description |
---|---|---|---|
canvas | HTMLCanvasElement | OffscreenCanvas | undefined | Returns the canvas element that the mask is bound to. | |
height | number | Returns the height of the mask. | |
interpolateValues | boolean | ||
width | number | Returns the width of the mask. |
Methods
Method | Modifiers | Description |
---|---|---|
clone() | Creates a copy of the resources stored in this MPMask . You can invoke this method to extend the lifetime of a mask returned by a MediaPipe Task. Note that performance critical applications should aim to only use the MPMask within the MediaPipe Task callback so that copies can be avoided. |
|
close() | Frees up any resources owned by this MPMask instance.Note that this method does not free masks that are owned by the C++ Task, as these are freed automatically once you leave the MediaPipe callback. Additionally, some shared state is freed only once you invoke the Task's close() method. |
|
getAsFloat32Array() | Returns the underlying mask as a single channel Float32Array . Note that this involves an expensive GPU to CPU transfer if the current mask is only available as a WebGLTexture . The current mask as a Float32Array. |
|
getAsUint8Array() | Returns the underlying mask as a Uint8Array. Note that this involves an expensive GPU to CPU transfer if the current mask is only available as a WebGLTexture`. The current data as a Uint8Array. |
|
getAsWebGLTexture() | Returns the underlying mask as a WebGLTexture object. Note that this involves a CPU to GPU transfer if the current mask is only available as a CPU array. The returned texture is bound to the current canvas (see .canvas ). The current mask as a WebGLTexture. |
|
hasFloat32Array() | Returns whether this MPMask contains a mask of type Float32Array . |
|
hasUint8Array() | Returns whether this MPMask contains a mask of type Uint8Array . |
|
hasWebGLTexture() | Returns whether this MPMask contains a mask of type WebGLTexture . |
MPMask.canvas
Returns the canvas element that the mask is bound to.
Signature:
readonly canvas: HTMLCanvasElement | OffscreenCanvas | undefined;
MPMask.height
Returns the height of the mask.
Signature:
readonly height: number;
MPMask.interpolateValues
Signature:
readonly interpolateValues: boolean;
MPMask.width
Returns the width of the mask.
Signature:
readonly width: number;
MPMask.clone()
Creates a copy of the resources stored in this MPMask
. You can invoke this method to extend the lifetime of a mask returned by a MediaPipe Task. Note that performance critical applications should aim to only use the MPMask
within the MediaPipe Task callback so that copies can be avoided.
Signature:
clone(): MPMask;
Returns:
MPMask.close()
Frees up any resources owned by this MPMask
instance.
Note that this method does not free masks that are owned by the C++ Task, as these are freed automatically once you leave the MediaPipe callback. Additionally, some shared state is freed only once you invoke the Task's close()
method.
Signature:
close(): void;
Returns:
void
MPMask.getAsFloat32Array()
Returns the underlying mask as a single channel Float32Array
. Note that this involves an expensive GPU to CPU transfer if the current mask is only available as a WebGLTexture
.
The current mask as a Float32Array.
Signature:
getAsFloat32Array(): Float32Array;
Returns:
Float32Array
MPMask.getAsUint8Array()
Returns the underlying mask as a Uint8Array. Note that this involves an expensive GPU to CPU transfer if the current mask is only available as a
WebGLTexture`.
The current data as a Uint8Array.
Signature:
getAsUint8Array(): Uint8Array;
Returns:
Uint8Array
MPMask.getAsWebGLTexture()
Returns the underlying mask as a WebGLTexture
object. Note that this involves a CPU to GPU transfer if the current mask is only available as a CPU array. The returned texture is bound to the current canvas (see .canvas
).
The current mask as a WebGLTexture.
Signature:
getAsWebGLTexture(): WebGLTexture;
Returns:
WebGLTexture
MPMask.hasFloat32Array()
Returns whether this MPMask
contains a mask of type Float32Array
.
Signature:
hasFloat32Array(): boolean;
Returns:
boolean
MPMask.hasUint8Array()
Returns whether this MPMask
contains a mask of type Uint8Array
.
Signature:
hasUint8Array(): boolean;
Returns:
boolean
MPMask.hasWebGLTexture()
Returns whether this MPMask
contains a mask of type WebGLTexture
.
Signature:
hasWebGLTexture(): boolean;
Returns:
boolean