mp.CalculatorGraph

The primary API for the MediaPipe Framework.

MediaPipe processing takes place inside a graph, which defines packet flow paths between nodes. A graph can have any number of inputs and outputs, and data flow can branch and merge. Generally data flows forward, but backward loops are possible.

binary_config

graph_input_stream_add_mode

max_queue_size

text_config

Methods

add_packet_to_input_stream

add_packet_to_input_stream(self: mediapipe.python._framework_bindings.calculator_graph.CalculatorGraph, stream: str, packet: mediapipe.python._framework_bindings.packet.Packet, timestamp: mediapipe.python._framework_bindings.timestamp.Timestamp = ) -> None

Add a packet to a graph input stream.

If the graph input stream add mode is ADD_IF_NOT_FULL, the packet will not be added if any queue exceeds the max queue size specified by the graph config and will raise a Python runtime error. The WAIT_TILL_NOT_FULL mode (default) will block until the queues fall below the max queue size before adding the packet. If the mode is max queue size is -1, then the packet is added regardless of the sizes of the queues in the graph. The input stream must have been specified in the configuration as a graph level input stream. On error, nothing is added.

Args: stream: The name of the graph input stream. packet: The packet to be added into the input stream. timestamp: The timestamp of the packet. If set, the original packet timestamp will be overwritten.

Raises: RuntimeError: If the stream is not a graph input stream or the packet can't be added into the input stream due to the limited queue size or the wrong packet type. ValueError: If the timestamp of the Packet is invalid to be the timestamp of a Packet in a stream.

Examples: graph.add_packet_to_input_stream( stream='in', packet=packet_creator.create_string('hello world').at(0))

graph.add_packet_to_input_stream(
    stream='in',
    packet=packet_creator.create_string('hello world'),
    timstamp=1)

close

close(self: mediapipe.python._framework_bindings.calculator_graph.CalculatorGraph) -> None

Close all the input sources and shutdown the graph.

close_all_packet_sources

close_all_packet_sources(self: mediapipe.python._framework_bindings.calculator_graph.CalculatorGraph) -> None

Closes all the graph input streams and source calculator nodes.

close_input_stream

close_input_stream(self: mediapipe.python._framework_bindings.calculator_graph.CalculatorGraph, arg0: str) -> None

Close the named graph input stream.

Args: stream: The name of the stream to be closed.

Raises: RuntimeError: If the stream is not a graph input stream.

get_combined_error_message

get_combined_error_message(self: mediapipe.python._framework_bindings.calculator_graph.CalculatorGraph) -> str

Combines error messages as a single string.

Examples: if graph.has_error(): print(graph.get_combined_error_message())

get_output_side_packet

get_output_side_packet(self: mediapipe.python._framework_bindings.calculator_graph.CalculatorGraph, arg0: str) -> mediapipe.python._framework_bindings.packet.Packet

Get output side packet by name after the graph is done.

Args: stream: The name of the outnput stream.

Raises: RuntimeError: If the graph is still running or the output side packet is not found or empty.

Examples: graph = mp.CalculatorGraph(graph_config=graph_config) graph.start_run() graph.close() output_side_packet = graph.get_output_side_packet('packet_name')

has_error

has_error(self: mediapipe.python._framework_bindings.calculator_graph.CalculatorGraph) -> bool

Quick non-locking means of checking if the graph has encountered an error

observe_output_stream

observe_output_stream(self: mediapipe.python._framework_bindings.calculator_graph.CalculatorGraph, stream_name: str, callback_fn: function, observe_timestamp_bounds: bool = False) -> None

Observe the named output stream.

callback_fn will be invoked on every packet emitted by the output stream. This method can only be called before start_run().

Args: stream_name: The name of the output stream. callback_fn: The callback function to invoke on every packet emitted by the output stream. observe_timestamp_bounds: If true, emits an empty packet at timestamp_bound -1 when timestamp bound changes.

Raises: RuntimeError: If the calculator graph isn't initialized or the stream doesn't exist.

Examples: out = [] graph = mp.CalculatorGraph(graph_config=graph_config) graph.observe_output_stream('out', lambda stream_name, packet: out.append(packet))

start_run

start_run(self: mediapipe.python._framework_bindings.calculator_graph.CalculatorGraph, input_side_packets: dict = {}) -> None

Start a run of the calculator graph.

A non-blocking call to start a run of the graph and will return when the graph is started. If input_side_packets is provided, the method will runs the graph after adding the given extra input side packets.

start_run(), wait_until_done(), has_error(), add_packet_to_input_stream(), and close() allow more control over the execution of the graph run. You can insert packets directly into a stream while the graph is running. Once start_run() has been called, the graph will continue to run until wait_until_done() is called.

If start_run() returns an error, then the graph is not started and a subsequent call to start_run() can be attempted.

Args: input_side_packets: A dict maps from the input side packet names to the packets.

Raises: RuntimeError: If the start run occurs any error, e.g. the graph config has errors, the calculator can't be found, and the streams are not properly connected.

Examples: graph = mp.CalculatorGraph(graph_config=video_process_graph) graph.start_run( input_side_packets={ 'input_path': packet_creator.create_string('/tmp/input.video'), 'output_path': packet_creator.create_string('/tmp/output.video') }) graph.close()

out = []
graph = mp.CalculatorGraph(graph_config=pass_through_graph)
graph.observe_output_stream('out',
                            lambda stream_name, packet: out.append(packet))
graph.start_run()
graph.add_packet_to_input_stream(
    stream='in', packet=packet_creator.create_int(0), timestamp=0)
graph.add_packet_to_input_stream(
    stream='in', packet=packet_creator.create_int(1), timestamp=1)
graph.close()

wait_for_observed_output

wait_for_observed_output(self: mediapipe.python._framework_bindings.calculator_graph.CalculatorGraph) -> None

Wait until a packet is emitted on one of the observed output streams.

Returns immediately if a packet has already been emitted since the last call to this function.

Raises: RuntimeError: If the graph occurs any error or the graph is terminated while waiting.

Examples: out = [] graph = mp.CalculatorGraph(graph_config=pass_through_graph) graph.observe_output_stream('out', lambda stream_name, packet: out.append(packet)) graph.start_run() graph.add_packet_to_input_stream( stream='in', packet=packet_creator.create_int(0), timestamp=0) graph.wait_for_observed_output() value = packet_getter.get_int(out[0]) graph.add_packet_to_input_stream( stream='in', packet=packet_creator.create_int(1), timestamp=1) graph.wait_for_observed_output() value = packet_getter.get_int(out[1])

wait_until_done

wait_until_done(self: mediapipe.python._framework_bindings.calculator_graph.CalculatorGraph) -> None

Wait for the current run to finish.

A blocking call to wait for the current run to finish (block the current thread until all source calculators are stopped, all graph input streams have been closed, and no more calculators can be run). This function can be called only after start_run(),

Raises: RuntimeError: If the graph occurs any error during the wait call.

Examples: out = [] graph = mp.CalculatorGraph(graph_config=pass_through_graph) graph.observe_output_stream('out', lambda stream_name, packet: out.append(packet)) graph.start_run() graph.add_packet_to_input_stream( stream='in', packet=packet_creator.create_int(0), timestamp=0) graph.close_all_packet_sources() graph.wait_until_done()

wait_until_idle

wait_until_idle(self: mediapipe.python._framework_bindings.calculator_graph.CalculatorGraph) -> None

Wait until the running graph is in the idle mode.

Wait until the running graph is in the idle mode, which is when nothing can be scheduled and nothing is running in the worker threads. This function can be called only after start_run().

Raises: RuntimeError: If the graph occurs any error during the wait call.

Examples: out = [] graph = mp.CalculatorGraph(graph_config=pass_through_graph) graph.observe_output_stream('out', lambda stream_name, packet: out.append(packet)) graph.start_run() graph.add_packet_to_input_stream( stream='in', packet=packet_creator.create_int(0), timestamp=0) graph.wait_until_idle()