ImageSegmenter

public final class ImageSegmenter extends BaseVisionTaskApi


Performs image segmentation on images.

Note that, in addition to the standard segmentation API, segment and segmentForVideo, that take an input image and return the outputs, but involves deep copy of the returns, ImageSegmenter also supports the callback API, segmentWithResultListener and segmentForVideoWithResultListener, which allow you to access the outputs through zero copy.

The callback API is available for all RunningMode in ImageSegmenter. Set ResultListener in ImageSegmenterOptions properly to use the callback API.

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

  • Input image MPImage
    • The image that image segmenter runs on.
  • Output ImageSegmenterResult ImageSegmenterResult
    • An ImageSegmenterResult containing segmented masks.

Summary

Nested types

Options for setting up an ImageSegmenter.

Builder for ImageSegmenterOptions.

Options for configuring runtime behavior of ImageSegmenter.

Builder for SegmentationOptions

Public fields

List<String>

Public methods

static ImageSegmenter
createFromOptions(
    Context context,
    ImageSegmenter.ImageSegmenterOptions segmenterOptions
)

Creates an ImageSegmenter instance from an ImageSegmenterOptions.

List<String>

Get the category label list of the ImageSegmenter can recognize.

ImageSegmenterResult

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

ImageSegmenterResult
segment(MPImage image, ImageProcessingOptions imageProcessingOptions)

Performs image segmentation on the provided single image.

ImageSegmenterResult
segment(
    MPImage image,
    ImageSegmenter.SegmentationOptions segmentationOptions
)

Performs image segmentation on the provided single image.

void
segmentAsync(MPImage image, long timestampMs)

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

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

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

void
segmentAsync(
    MPImage image,
    ImageSegmenter.SegmentationOptions segmentationOptions,
    long timestampMs
)

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

ImageSegmenterResult
segmentForVideo(MPImage image, long timestampMs)

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

ImageSegmenterResult
segmentForVideo(
    MPImage image,
    ImageProcessingOptions imageProcessingOptions,
    long timestampMs
)

Performs image segmentation on the provided video frame.

ImageSegmenterResult
segmentForVideo(
    MPImage image,
    ImageSegmenter.SegmentationOptions segmentationOptions,
    long timestampMs
)

Performs image segmentation on the provided video frame.

void
segmentForVideoWithResultListener(MPImage image, long timestampMs)

Performs image segmentation on the provided video frame with default image processing options, i.e. without any rotation applied, and provides zero-copied results via ResultListener in ImageSegmenterOptions.

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

Performs image segmentation on the provided video frame, and provides zero-copied results via ResultListener in ImageSegmenterOptions.

void
segmentForVideoWithResultListener(
    MPImage image,
    ImageSegmenter.SegmentationOptions segmentationOptions,
    long timestampMs
)

Performs image segmentation on the provided video frame, and provides zero-copied results via ResultListener in ImageSegmenterOptions.

void

Performs image segmentation on the provided single image with default image processing options, i.e. without any rotation applied, and provides zero-copied results via ResultListener in ImageSegmenterOptions.

void
segmentWithResultListener(
    MPImage image,
    ImageProcessingOptions imageProcessingOptions
)

Performs image segmentation on the provided single image, and provides zero-copied results via ResultListener in ImageSegmenterOptions.

void
segmentWithResultListener(
    MPImage image,
    ImageSegmenter.SegmentationOptions segmentationOptions
)

Performs image segmentation on the provided single image, and provides zero-copied results via ResultListener in ImageSegmenterOptions.

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 fields

labels

public List<String> labels

Public methods

createFromOptions

public static ImageSegmenter createFromOptions(
    Context context,
    ImageSegmenter.ImageSegmenterOptions segmenterOptions
)

Creates an ImageSegmenter instance from an ImageSegmenterOptions.

Parameters
Context context

an Android Context.

ImageSegmenter.ImageSegmenterOptions segmenterOptions

an ImageSegmenterOptions instance.

Throws
com.google.mediapipe.framework.MediaPipeException

if there is an error during ImageSegmenter creation.

getLabels

public List<String> getLabels()

Get the category label list of the ImageSegmenter can recognize. For CATEGORY_MASK type, the index in the category mask corresponds to the category in the label list. For CONFIDENCE_MASK type, the output mask list at index corresponds to the category in the label list.

If there is no labelmap provided in the model file, empty label list is returned.

segment

public ImageSegmenterResult segment(MPImage image)

Performs image segmentation on the provided single image with default image processing options, i.e. without any rotation applied. The output mask has the same size as the input image. Only use this method when the ImageSegmenter is created with RunningMode.IMAGE. TODO update java doc for input image format.

ImageSegmenter 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. Or if ImageSegmenter is created with a ResultListener.

segment

public ImageSegmenterResult segment(MPImage image, ImageProcessingOptions imageProcessingOptions)

Performs image segmentation on the provided single image. The output mask has the same size as the input image. Only use this method when the ImageSegmenter is created with RunningMode.IMAGE. TODO update java doc for input image format.

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

com.google.mediapipe.framework.MediaPipeException

if there is an internal error. Or if ImageSegmenter is created with a ResultListener.

segment

public ImageSegmenterResult segment(
    MPImage image,
    ImageSegmenter.SegmentationOptions segmentationOptions
)

Performs image segmentation on the provided single image. Only use this method when the ImageSegmenter is created with RunningMode.IMAGE. TODO update java doc for input image format.

ImageSegmenter supports the following color space types:

  • Bitmap.Config.ARGB_8888
Parameters
MPImage image

a MediaPipe MPImage object for processing.

ImageSegmenter.SegmentationOptions segmentationOptions

the SegmentationOptions used to configure the runtime behavior of the ImageSegmenter.

Throws
com.google.mediapipe.framework.MediaPipeException

if there is an internal error. Or if ImageSegmenter is created with a ResultListener.

segmentAsync

public void segmentAsync(MPImage image, long timestampMs)

Sends live image data to perform image segmentation with default image processing options, i.e. without any rotation applied, and the results will be available via the ResultListener provided in the ImageSegmenterOptions. The output mask has the same size as the input image. Only use this method when the ImageSegmenter 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 image segmenter. The input timestamps must be monotonically increasing.

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

segmentAsync

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

Sends live image data to perform image segmentation, and the results will be available via the ResultListener provided in the ImageSegmenterOptions. The output mask has the same size as the input image. Only use this method when the ImageSegmenter 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 image segmenter. The input timestamps must be monotonically increasing.

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

com.google.mediapipe.framework.MediaPipeException

if there is an internal error.

segmentAsync

public void segmentAsync(
    MPImage image,
    ImageSegmenter.SegmentationOptions segmentationOptions,
    long timestampMs
)

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

ImageSegmenter supports the following color space types:

  • Bitmap.Config.ARGB_8888
Parameters
MPImage image

a MediaPipe MPImage object for processing.

ImageSegmenter.SegmentationOptions segmentationOptions

the SegmentationOptions used to configure the runtime behavior of the ImageSegmenter.

long timestampMs

the input timestamp (in milliseconds).

Throws
com.google.mediapipe.framework.MediaPipeException

if there is an internal error.

segmentForVideo

public ImageSegmenterResult segmentForVideo(MPImage image, long timestampMs)

Performs image segmentation on the provided video frame with default image processing options, i.e. without any rotation applied. The output mask has the same size as the input image. Only use this method when the ImageSegmenter is created with RunningMode.VIDEO.

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

ImageSegmenter 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. Or if ImageSegmenter is created with a ResultListener.

segmentForVideo

public ImageSegmenterResult segmentForVideo(
    MPImage image,
    ImageProcessingOptions imageProcessingOptions,
    long timestampMs
)

Performs image segmentation on the provided video frame. The output mask has the same size as the input image. Only use this method when the ImageSegmenter is created with RunningMode.VIDEO.

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

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

com.google.mediapipe.framework.MediaPipeException

if there is an internal error. Or if ImageSegmenter is created with a ResultListener.

segmentForVideo

public ImageSegmenterResult segmentForVideo(
    MPImage image,
    ImageSegmenter.SegmentationOptions segmentationOptions,
    long timestampMs
)

Performs image segmentation on the provided video frame. Only use this method when the ImageSegmenter is created with RunningMode.VIDEO.

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

ImageSegmenter supports the following color space types:

  • Bitmap.Config.ARGB_8888
Parameters
MPImage image

a MediaPipe MPImage object for processing.

ImageSegmenter.SegmentationOptions segmentationOptions

the SegmentationOptions used to configure the runtime behavior of the ImageSegmenter.

long timestampMs

the input timestamp (in milliseconds).

Throws
com.google.mediapipe.framework.MediaPipeException

if there is an internal error. Or if ImageSegmenter is created with a ResultListener.

segmentForVideoWithResultListener

public void segmentForVideoWithResultListener(MPImage image, long timestampMs)

Performs image segmentation on the provided video frame with default image processing options, i.e. without any rotation applied, and provides zero-copied results via ResultListener in ImageSegmenterOptions. The output mask has the same size as the input image. Only use this method when the ImageSegmenter is created with RunningMode.VIDEO.

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

ImageSegmenter 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. Or if ImageSegmenter is not created with ResultListener set in ImageSegmenterOptions.

segmentForVideoWithResultListener

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

Performs image segmentation on the provided video frame, and provides zero-copied results via ResultListener in ImageSegmenterOptions. The output mask has the same size as the input image. Only use this method when the ImageSegmenter is created with RunningMode.VIDEO.

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

ImageSegmenter 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
java.lang.IllegalArgumentException

if the ImageProcessingOptions specify a region-of-interest.

com.google.mediapipe.framework.MediaPipeException

if there is an internal error. Or if ImageSegmenter is not created with ResultListener set in ImageSegmenterOptions.

segmentForVideoWithResultListener

public void segmentForVideoWithResultListener(
    MPImage image,
    ImageSegmenter.SegmentationOptions segmentationOptions,
    long timestampMs
)

Performs image segmentation on the provided video frame, and provides zero-copied results via ResultListener in ImageSegmenterOptions. Only use this method when the ImageSegmenter is created with RunningMode.VIDEO.

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

ImageSegmenter supports the following color space types:

  • Bitmap.Config.ARGB_8888
Parameters
MPImage image

a MediaPipe MPImage object for processing.

ImageSegmenter.SegmentationOptions segmentationOptions

the SegmentationOptions used to configure the runtime behavior of the ImageSegmenter.

long timestampMs

the input timestamp (in milliseconds).

Throws
com.google.mediapipe.framework.MediaPipeException

if there is an internal error. Or if ImageSegmenter is not created with ResultListener set in ImageSegmenterOptions.

segmentWithResultListener

public void segmentWithResultListener(MPImage image)

Performs image segmentation on the provided single image with default image processing options, i.e. without any rotation applied, and provides zero-copied results via ResultListener in ImageSegmenterOptions. The output mask has the same size as the input image. Only use this method when the ImageSegmenter is created with RunningMode.IMAGE.

TODO update java doc for input image format.

ImageSegmenter 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. Or if ImageSegmenter is not created with ResultListener set in ImageSegmenterOptions.

segmentWithResultListener

public void segmentWithResultListener(
    MPImage image,
    ImageProcessingOptions imageProcessingOptions
)

Performs image segmentation on the provided single image, and provides zero-copied results via ResultListener in ImageSegmenterOptions. The output mask has the same size as the input image. Only use this method when the ImageSegmenter is created with RunningMode.IMAGE.

TODO update java doc for input image format.

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

com.google.mediapipe.framework.MediaPipeException

if there is an internal error. Or if ImageSegmenter is not created with ResultListener set in ImageSegmenterOptions.

segmentWithResultListener

public void segmentWithResultListener(
    MPImage image,
    ImageSegmenter.SegmentationOptions segmentationOptions
)

Performs image segmentation on the provided single image, and provides zero-copied results via ResultListener in ImageSegmenterOptions. Only use this method when the ImageSegmenter is created with RunningMode.IMAGE.

TODO update java doc for input image format.

ImageSegmenter supports the following color space types:

  • Bitmap.Config.ARGB_8888
Parameters
MPImage image

a MediaPipe MPImage object for processing.

ImageSegmenter.SegmentationOptions segmentationOptions

the SegmentationOptions used to configure the runtime behavior of the ImageSegmenter.

Throws
com.google.mediapipe.framework.MediaPipeException

if there is an internal error. Or if ImageSegmenter is not created with ResultListener set in ImageSegmenterOptions.