TaskRunner

public class TaskRunner implements AutoCloseable


The runner of MediaPipe task graphs.

Summary

Public fields

final AndroidPacketCreator

Public methods

void

Cancels the underlying MediaPipe graph.

void

Closes and cleans up the TaskRunner instance.

static TaskRunner
create(
    Context context,
    TaskInfo<TaskOptions> taskInfo,
    OutputHandler<TaskResult, Object> outputHandler
)

Create a TaskRunner instance.

static TaskRunner
create(
    Context context,
    TaskInfo<TaskOptions> taskInfo,
    OutputHandler<TaskResult, Object> outputHandler,
    Map<String, Packet> extraSidePackets
)

Create a TaskRunner instance.

void

Flushes the graph and waits until processing is complete.

CalculatorProto.CalculatorGraphConfig
AndroidPacketCreator

Returns the AndroidPacketCreator associated to the TaskRunner instance.

synchronized TaskResult
process(Map<String, Packet> inputs)

A synchronous method for processing batch data.

synchronized TaskResult
process(Map<String, Packet> inputs, long inputTimestamp)

A synchronous method for processing offline streaming data.

void

Resets and restarts the TaskRunner instance.

synchronized TaskResult

Retrieves the cached task result.

synchronized void
send(Map<String, Packet> inputs, long inputTimestamp)

An asynchronous method for handling live streaming data.

void

Sets a callback to be invoked when exceptions are thrown by the TaskRunner instance.

Public fields

packetCreator

public final AndroidPacketCreator packetCreator

Public methods

cancel

public void cancel()

Cancels the underlying MediaPipe graph.

Note: This is for internal use only.

close

public void close()

Closes and cleans up the TaskRunner instance.

create

public static TaskRunner create(
    Context context,
    TaskInfo<TaskOptions> taskInfo,
    OutputHandler<TaskResult, Object> outputHandler
)

Create a TaskRunner instance.

Parameters
Context context

an Android Context.

TaskInfo<TaskOptions> taskInfo

a TaskInfo instance contains task graph name, task options, and graph input and output stream names.

OutputHandler<TaskResult, Object> outputHandler

a OutputHandler instance handles task result object and runtime exception.

Throws
com.google.mediapipe.framework.MediaPipeException

for any error during TaskRunner creation.

create

public static TaskRunner create(
    Context context,
    TaskInfo<TaskOptions> taskInfo,
    OutputHandler<TaskResult, Object> outputHandler,
    Map<String, Packet> extraSidePackets
)

Create a TaskRunner instance.

Parameters
Context context

an Android Context.

TaskInfo<TaskOptions> taskInfo

a TaskInfo instance contains task graph name, task options, and graph input and output stream names.

OutputHandler<TaskResult, Object> outputHandler

a OutputHandler instance handles task result object and runtime exception.

Map<String, Packet> extraSidePackets

a map of extra input side packets to initialize graph with.

Throws
com.google.mediapipe.framework.MediaPipeException

for any error during TaskRunner creation.

flush

public void flush()

Flushes the graph and waits until processing is complete.

getCalculatorGraphConfig

public CalculatorProto.CalculatorGraphConfig getCalculatorGraphConfig()

getPacketCreator

public AndroidPacketCreator getPacketCreator()

Returns the AndroidPacketCreator associated to the TaskRunner instance.

process

synchronized public TaskResult process(Map<String, Packet> inputs)

A synchronous method for processing batch data.

Note: This method is designed for processing batch data such as unrelated images and texts. The call blocks the current thread until a failure status or a successful result is returned. An internal timestamp will be assigned per invocation. This method is thread-safe and allows clients to call it from different threads.

Parameters
Map<String, Packet> inputs

a map contains (input stream String, data Packet) pairs.

process

synchronized public TaskResult process(Map<String, Packet> inputs, long inputTimestamp)

A synchronous method for processing offline streaming data.

Note: This method is designed for processing offline streaming data such as the decoded frames from a video file and an audio file. The call blocks the current thread until a failure status or a successful result is returned. The caller must ensure that the input timestamp is greater than the timestamps of previous invocations. This method is thread-unsafe and it is the caller's responsibility to synchronize access to this method across multiple threads and to ensure that the input packet timestamps are in order.

Parameters
Map<String, Packet> inputs

a map contains (input stream String, data Packet) pairs.

long inputTimestamp

the timestamp of the input packets.

restart

public void restart()

Resets and restarts the TaskRunner instance. This can be useful for resetting a stateful task graph to process new data.

retrieveCachedTaskResult

synchronized public TaskResult retrieveCachedTaskResult()

Retrieves the cached task result.

Note: This is for internal use only.

send

synchronized public void send(Map<String, Packet> inputs, long inputTimestamp)

An asynchronous method for handling live streaming data.

Note: This method that is designed for handling live streaming data such as live camera and microphone data. A user-defined packets callback function must be provided in the constructor to receive the output packets. The caller must ensure that the input packet timestamps are monotonically increasing. This method is thread-unsafe and it is the caller's responsibility to synchronize access to this method across multiple threads and to ensure that the input packet timestamps are in order.

Parameters
Map<String, Packet> inputs

a map contains (input stream String, data Packet) pairs.

long inputTimestamp

the timestamp of the input packets.

setErrorListener

public void setErrorListener(ErrorListener listener)

Sets a callback to be invoked when exceptions are thrown by the TaskRunner instance.

Parameters
ErrorListener listener

an ErrorListener callback.