FaceDetector

public final class FaceDetector extends BaseVisionTaskApi


Performs face detection on images.

The API expects a TFLite model with TFLite Model Metadata..

  • Input image MPImage
    • The image that the face detector runs on.
  • Output FaceDetectorResult FaceDetectorResult
    • A FaceDetectorResult containing detected faces.

Summary

Nested types

public abstract class FaceDetector.FaceDetectorOptions extends TaskOptions

Options for setting up a FaceDetector.

Builder for FaceDetectorOptions.

Public methods

static FaceDetector
createFromBuffer(Context context, ByteBuffer modelBuffer)

Creates a FaceDetector instance from a model buffer and the default FaceDetectorOptions.

static FaceDetector
createFromFile(Context context, File modelFile)

Creates a FaceDetector instance from a model file and the default FaceDetectorOptions.

static FaceDetector
createFromFile(Context context, String modelPath)

Creates a FaceDetector instance from a model file and the default FaceDetectorOptions.

static FaceDetector
createFromOptions(
    Context context,
    FaceDetector.FaceDetectorOptions detectorOptions
)

Creates a FaceDetector instance from a FaceDetectorOptions.

FaceDetectorResult
detect(MPImage image)

Performs face detection on the provided single image with default image processing options, i.e. without any rotation applied.

FaceDetectorResult
detect(MPImage image, ImageProcessingOptions imageProcessingOptions)

Performs face detection on the provided single image.

void
detectAsync(MPImage image, long timestampMs)

Sends live image data to perform face detection with default image processing options, i.e. without any rotation applied, and the results will be available via the ResultListener provided in the FaceDetectorOptions.

void
detectAsync(
    MPImage image,
    ImageProcessingOptions imageProcessingOptions,
    long timestampMs
)

Sends live image data to perform face detection, and the results will be available via the ResultListener provided in the FaceDetectorOptions.

FaceDetectorResult
detectForVideo(MPImage image, long timestampMs)

Performs face detection on the provided video frame with default image processing options, i.e. without any rotation applied.

FaceDetectorResult
detectForVideo(
    MPImage image,
    ImageProcessingOptions imageProcessingOptions,
    long timestampMs
)

Performs face detection on the provided video frame.

Inherited methods

From java.lang.AutoCloseable
abstract void
From com.google.mediapipe.tasks.vision.core.BaseVisionTaskApi
void

Closes and cleans up the MediaPipe vision task.

static RectProto.NormalizedRect
convertToNormalizedRect(
    ImageProcessingOptions imageProcessingOptions,
    MPImage image
)

Converts an ImageProcessingOptions instance into a NormalizedRect protobuf message.

static long

Generates the timestamp of a vision task result object from the vision task running mode and the output packet.

TaskResult
processImageData(Map<String, Packet> inputPackets)

A synchronous method to process single image inputs.

TaskResult
processImageData(
    MPImage image,
    ImageProcessingOptions imageProcessingOptions
)

A synchronous method to process single image inputs.

TaskResult
processVideoData(Map<String, Packet> inputPackets, long timestampMs)

A synchronous method to process continuous video frames.

TaskResult
processVideoData(
    MPImage image,
    ImageProcessingOptions imageProcessingOptions,
    long timestampMs
)

A synchronous method to process continuous video frames.

void
sendLiveStreamData(Map<String, Packet> inputPackets, long timestampMs)

An asynchronous method to send live stream data to the TaskRunner.

void
sendLiveStreamData(
    MPImage image,
    ImageProcessingOptions imageProcessingOptions,
    long timestampMs
)

An asynchronous method to send live stream data to the TaskRunner.

Public methods

createFromBuffer

public static FaceDetector createFromBuffer(Context context, ByteBuffer modelBuffer)

Creates a FaceDetector instance from a model buffer and the default FaceDetectorOptions.

Parameters
Context context

an Android Context.

ByteBuffer modelBuffer

a direct ByteBuffer or a MappedByteBuffer of the detection model.

Throws
MediaPipeException

if there is an error during FaceDetector creation.

createFromFile

public static FaceDetector createFromFile(Context context, File modelFile)

Creates a FaceDetector instance from a model file and the default FaceDetectorOptions.

Parameters
Context context

an Android Context.

File modelFile

the detection model File instance.

Throws
java.io.IOException

if an I/O error occurs when opening the tflite model file.

MediaPipeException

if there is an error during FaceDetector creation.

createFromFile

public static FaceDetector createFromFile(Context context, String modelPath)

Creates a FaceDetector instance from a model file and the default FaceDetectorOptions.

Parameters
Context context

an Android Context.

String modelPath

path to the detection model with metadata in the assets.

Throws
MediaPipeException

if there is an error during FaceDetector creation.

createFromOptions

public static FaceDetector createFromOptions(
    Context context,
    FaceDetector.FaceDetectorOptions detectorOptions
)

Creates a FaceDetector instance from a FaceDetectorOptions.

Parameters
Context context

an Android Context.

FaceDetector.FaceDetectorOptions detectorOptions

a FaceDetectorOptions instance.

Throws
MediaPipeException

if there is an error during FaceDetector creation.

detect

public FaceDetectorResult detect(MPImage image)

Performs face detection on the provided single image with default image processing options, i.e. without any rotation applied. Only use this method when the FaceDetector is created with RunningMode.IMAGE.

FaceDetector supports the following color space types:

  • Bitmap.Config.ARGB_8888
Parameters
MPImage image

a MediaPipe MPImage object for processing.

Throws
MediaPipeException

if there is an internal error.

detect

public FaceDetectorResult detect(MPImage image, ImageProcessingOptions imageProcessingOptions)

Performs face detection on the provided single image. Only use this method when the FaceDetector is created with RunningMode.IMAGE.

FaceDetector supports the following color space types:

  • Bitmap.Config.ARGB_8888
Parameters
MPImage image

a MediaPipe MPImage object for processing.

ImageProcessingOptions imageProcessingOptions

the ImageProcessingOptions specifying how to process the input image before running inference. Note that region-of-interest is not supported by this task: specifying regionOfInterest will result in this method throwing an IllegalArgumentException.

Throws
java.lang.IllegalArgumentException

if the ImageProcessingOptions specify a region-of-interest.

MediaPipeException

if there is an internal error.

detectAsync

public void detectAsync(MPImage image, long timestampMs)

Sends live image data to perform face detection with default image processing options, i.e. without any rotation applied, and the results will be available via the ResultListener provided in the FaceDetectorOptions. Only use this method when the FaceDetector is created with RunningMode.LIVE_STREAM.

It's required to provide a timestamp (in milliseconds) to indicate when the input image is sent to the face detector. The input timestamps must be monotonically increasing.

FaceDetector supports the following color space types:

  • Bitmap.Config.ARGB_8888
Parameters
MPImage image

a MediaPipe MPImage object for processing.

long timestampMs

the input timestamp (in milliseconds).

Throws
MediaPipeException

if there is an internal error.

detectAsync

public void detectAsync(
    MPImage image,
    ImageProcessingOptions imageProcessingOptions,
    long timestampMs
)

Sends live image data to perform face detection, and the results will be available via the ResultListener provided in the FaceDetectorOptions. Only use this method when the FaceDetector is created with RunningMode.LIVE_STREAM.

It's required to provide a timestamp (in milliseconds) to indicate when the input image is sent to the face detector. The input timestamps must be monotonically increasing.

FaceDetector supports the following color space types:

  • Bitmap.Config.ARGB_8888
Parameters
MPImage image

a MediaPipe MPImage object for processing.

ImageProcessingOptions imageProcessingOptions

the ImageProcessingOptions specifying how to process the input image before running inference. Note that region-of-interest is not supported by this task: specifying regionOfInterest will result in this method throwing an IllegalArgumentException.

long timestampMs

the input timestamp (in milliseconds).

Throws
java.lang.IllegalArgumentException

if the ImageProcessingOptions specify a region-of-interest.

MediaPipeException

if there is an internal error.

detectForVideo

public FaceDetectorResult detectForVideo(MPImage image, long timestampMs)

Performs face detection on the provided video frame with default image processing options, i.e. without any rotation applied. Only use this method when the FaceDetector is created with RunningMode.VIDEO.

It's required to provide the video frame's timestamp (in milliseconds). The input timestamps must be monotonically increasing.

FaceDetector supports the following color space types:

  • Bitmap.Config.ARGB_8888
Parameters
MPImage image

a MediaPipe MPImage object for processing.

long timestampMs

the input timestamp (in milliseconds).

Throws
MediaPipeException

if there is an internal error.

detectForVideo

public FaceDetectorResult detectForVideo(
    MPImage image,
    ImageProcessingOptions imageProcessingOptions,
    long timestampMs
)

Performs face detection on the provided video frame. Only use this method when the FaceDetector is created with RunningMode.VIDEO.

It's required to provide the video frame's timestamp (in milliseconds). The input timestamps must be monotonically increasing.

FaceDetector supports the following color space types:

  • Bitmap.Config.ARGB_8888
Parameters
MPImage image

a MediaPipe MPImage object for processing.

ImageProcessingOptions imageProcessingOptions

the ImageProcessingOptions specifying how to process the input image before running inference. Note that region-of-interest is not supported by this task: specifying regionOfInterest will result in this method throwing an IllegalArgumentException.

long timestampMs

the input timestamp (in milliseconds).

Throws
java.lang.IllegalArgumentException

if the ImageProcessingOptions specify a region-of-interest.

MediaPipeException

if there is an internal error.