The runner of any MediaPipe Tasks.
mp.tasks.text.core.base_text_task_api.task_runner.TaskRunner(
*args, **kwargs
)
TaskRunner is the MediaPipe Tasks core component for running MediaPipe task graphs. TaskRunner has two processing modes: synchronous mode and asynchronous mode. In the synchronous mode, clients send input data using the blocking API, Process(), and wait until the results are returned from the same method. In the asynchronous mode, clients send input data using the non-blocking method, Send(), and receive the results in the user-defined packets callback at a later point in time. As the two processing modes are incompatible, each TaskRunner instance can operate in only one processing mode, which is defined at construction time based on whether a packets callback is provided (asynchronous mode) or not (synchronous mode).
Methods
close
close()
close(self: mediapipe.python._framework_bindings.task_runner.TaskRunner) -> None
Shuts down the TaskRunner instance.
After the runner is closed, any calls that send input data to the runner are illegal and will receive errors.
Raises | |
---|---|
RuntimeError
|
The underlying medipaipe graph fails to close any input streams or calculators. |
create
create()
create(graph_config: mediapipe::CalculatorGraphConfig, packets_callback: Optional[function] = None) -> mediapipe.python._framework_bindings.task_runner.TaskRunner
Creates a TaskRunner instance from a CalculatorGraphConfig proto and an optional user-defined packets callback.
When a user-defined packets callback is provided, callers must use the asynchronous method, send(), to provide the input packets. If the packets callback is absent, clients must use the synchronous method, process(), to provide the input packets and receive the output packets.
Args | |
---|---|
graph_config
|
A MediaPipe task graph config protobuf object. |
packets_callback
|
A user-defined packets callback function that takes a list of output packets as the input argument. |
Raises | |
---|---|
RuntimeError
|
Any of the following: a) The graph config proto is invalid. b) The underlying medipaipe graph fails to initialize and start. |
get_graph_config
get_graph_config()
get_graph_config(self: mediapipe.python._framework_bindings.task_runner.TaskRunner) -> mediapipe::CalculatorGraphConfig
Returns the canonicalized CalculatorGraphConfig of the underlying graph.
process
process()
process(self: mediapipe.python._framework_bindings.task_runner.TaskRunner, input_packets: dict) -> Dict[str, mediapipe.python._framework_bindings.packet.Packet]
A synchronous method for processing batch data or offline streaming data.
This method is designed for processing either batch data such as unrelated images and texts or 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. If the input packets have no timestamp, an internal timestamp will be assigned per invocation. Otherwise, when the timestamp is set in the input packets, the caller must ensure that the input packet timestamps are greater than the timestamps of the previous invocation. 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.
Args | |
---|---|
input_packets
|
A dict contains (input stream name, data packet) pairs. |
Raises | |
---|---|
RuntimeError
|
Any of the following: a) TaskRunner is in the asynchronous mode (the packets callback is set). b) Any input stream name is not valid. c) The underlying medipaipe graph occurs any error during this call. |
restart
restart()
restart(self: mediapipe.python._framework_bindings.task_runner.TaskRunner) -> None
Resets and restarts the TaskRunner instance.
This can be useful for resetting a stateful task graph to process new data.
Raises | |
---|---|
RuntimeError
|
The underlying medipaipe graph fails to reset and restart. |
send
send()
send(self: mediapipe.python._framework_bindings.task_runner.TaskRunner, input_packets: dict) -> None
An asynchronous method for handling live streaming data.
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.
Args | |
---|---|
input_packets
|
A dict contains (input stream name, data packet) pairs. |
Raises | |
---|---|
RuntimeError
|
Any of the following: a) TaskRunner is in the synchronous mode (the packets callback is not set). b) Any input stream name is not valid. c) The packet can't be added into the input stream due to the limited queue size or the wrong packet type. d) The timestamp of any packet is invalid or is not greater than the previously received timestamps. e) The underlying medipaipe graph occurs any error during adding input packets. |