ImageClassifier

public final class ImageClassifier extends BaseVisionTaskApi


Performs classification on images.

The API expects a TFLite model with optional, but strongly recommended, TFLite Model Metadata..

The API supports models with one image input tensor and one or more output tensors. To be more specific, here are the requirements.

  • Input image tensor (kTfLiteUInt8/kTfLiteFloat32)
    • image input of size [batch x height x width x channels].
    • batch inference is not supported (batch is required to be 1).
    • only RGB inputs are supported (channels is required to be 3).
    • if type is kTfLiteFloat32, NormalizationOptions are required to be attached to the metadata for input normalization.
  • At least one output tensor (kTfLiteUInt8/kTfLiteFloat32) with:
    • N classes and either 2 or 4 dimensions, i.e. [1 x N] or [1 x 1 x 1 x N]
    • optional (but recommended) label map(s) as AssociatedFile-s with type TENSOR_AXIS_LABELS, containing one label per line. The first such AssociatedFile (if any) is used to fill the class_name field of the results. The display_name field is filled from the AssociatedFile (if any) whose locale matches the display_names_locale field of the ImageClassifierOptions used at creation time ("en" by default, i.e. English). If none of these are available, only the index field of the results will be filled.
    • optional score calibration can be attached using ScoreCalibrationOptions and an AssociatedFile with type TENSOR_AXIS_SCORE_CALIBRATION. See metadata_schema.fbs for more details.

An example of such model can be found TensorFlow Hub.

Summary

Nested types

Options for setting up and ImageClassifier.

Builder for ImageClassifierOptions.

Public methods

ImageClassifierResult

Performs classification on the provided single image with default image processing options, i.e. using the whole image as region-of-interest and without any rotation applied.

ImageClassifierResult
classify(MPImage image, ImageProcessingOptions imageProcessingOptions)

Performs classification on the provided single image.

void
classifyAsync(MPImage image, long timestampMs)

Sends live image data to perform classification with default image processing options, i.e. using the whole image as region-of-interest and without any rotation applied, and the results will be available via the ResultListener provided in the ImageClassifierOptions.

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

Sends live image data to perform classification, and the results will be available via the ResultListener provided in the ImageClassifierOptions.

ImageClassifierResult
classifyForVideo(MPImage image, long timestampMs)

Performs classification on the provided video frame with default image processing options, i.e. using the whole image as region-of-interest and without any rotation applied.

ImageClassifierResult
classifyForVideo(
    MPImage image,
    ImageProcessingOptions imageProcessingOptions,
    long timestampMs
)

Performs classification on the provided video frame.

static ImageClassifier
createFromBuffer(Context context, ByteBuffer modelBuffer)

Creates an ImageClassifier instance from a model buffer and default ImageClassifierOptions.

static ImageClassifier
createFromFile(Context context, File modelFile)

Creates an ImageClassifier instance from a model file and default ImageClassifierOptions.

static ImageClassifier
createFromFile(Context context, String modelPath)

Creates an ImageClassifier instance from a model file and default ImageClassifierOptions.

static ImageClassifier

Creates an ImageClassifier instance from an ImageClassifierOptions instance.

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
generateResultTimestampMs(RunningMode runningMode, Packet packet)

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

classify

public ImageClassifierResult classify(MPImage image)

Performs classification on the provided single image with default image processing options, i.e. using the whole image as region-of-interest and without any rotation applied. Only use this method when the ImageClassifier is created with RunningMode.IMAGE.

ImageClassifier supports the following color space types:

  • Bitmap.Config.ARGB_8888
Parameters
MPImage image

a MediaPipe MPImage object for processing.

Throws
com.google.mediapipe.framework.MediaPipeException

if there is an internal error.

classify

public ImageClassifierResult classify(MPImage image, ImageProcessingOptions imageProcessingOptions)

Performs classification on the provided single image. Only use this method when the ImageClassifier is created with RunningMode.IMAGE.

ImageClassifier 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.

Throws
com.google.mediapipe.framework.MediaPipeException

if there is an internal error.

classifyAsync

public void classifyAsync(MPImage image, long timestampMs)

Sends live image data to perform classification with default image processing options, i.e. using the whole image as region-of-interest and without any rotation applied, and the results will be available via the ResultListener provided in the ImageClassifierOptions. Only use this method when the ImageClassifier 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 object detector. The input timestamps must be monotonically increasing.

ImageClassifier 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
com.google.mediapipe.framework.MediaPipeException

if there is an internal error.

classifyAsync

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

Sends live image data to perform classification, and the results will be available via the ResultListener provided in the ImageClassifierOptions. Only use this method when the ImageClassifier 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 object detector. The input timestamps must be monotonically increasing.

ImageClassifier 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.

long timestampMs

the input timestamp (in milliseconds).

Throws
com.google.mediapipe.framework.MediaPipeException

if there is an internal error.

classifyForVideo

public ImageClassifierResult classifyForVideo(MPImage image, long timestampMs)

Performs classification on the provided video frame with default image processing options, i.e. using the whole image as region-of-interest and without any rotation applied. Only use this method when the ImageClassifier is created with RunningMode.VIDEO.

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

ImageClassifier 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
com.google.mediapipe.framework.MediaPipeException

if there is an internal error.

classifyForVideo

public ImageClassifierResult classifyForVideo(
    MPImage image,
    ImageProcessingOptions imageProcessingOptions,
    long timestampMs
)

Performs classification on the provided video frame. Only use this method when the ImageClassifier is created with RunningMode.VIDEO.

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

ImageClassifier 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.

long timestampMs

the input timestamp (in milliseconds).

Throws
com.google.mediapipe.framework.MediaPipeException

if there is an internal error.

createFromBuffer

public static ImageClassifier createFromBuffer(Context context, ByteBuffer modelBuffer)

Creates an ImageClassifier instance from a model buffer and default ImageClassifierOptions.

Parameters
Context context

an Android Context.

ByteBuffer modelBuffer

a direct ByteBuffer or a MappedByteBuffer of the classification model.

Throws
com.google.mediapipe.framework.MediaPipeException

if there is an error during ImageClassifier creation.

createFromFile

public static ImageClassifier createFromFile(Context context, File modelFile)

Creates an ImageClassifier instance from a model file and default ImageClassifierOptions.

Parameters
Context context

an Android Context.

File modelFile

the classification model File instance.

Throws
java.io.IOException

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

com.google.mediapipe.framework.MediaPipeException

if there is an error during ImageClassifier creation.

createFromFile

public static ImageClassifier createFromFile(Context context, String modelPath)

Creates an ImageClassifier instance from a model file and default ImageClassifierOptions.

Parameters
Context context

an Android Context.

String modelPath

path to the classification model in the assets.

Throws
com.google.mediapipe.framework.MediaPipeException

if there is an error during ImageClassifier creation.

createFromOptions

public static ImageClassifier createFromOptions(
    Context context,
    ImageClassifier.ImageClassifierOptions options
)

Creates an ImageClassifier instance from an ImageClassifierOptions instance.

Parameters
Context context

an Android Context.

ImageClassifier.ImageClassifierOptions options

an ImageClassifierOptions instance.

Throws
com.google.mediapipe.framework.MediaPipeException

if there is an error during ImageClassifier creation.