Project import generated by Copybara.

GitOrigin-RevId: 1e13be30e2c6838d4a2ff768a39c414bc80534bb
This commit is contained in:
MediaPipe Team
2022-09-06 21:46:17 +00:00
committed by Sebastian Schmidt
parent 63e679d99c
commit 4dc4b19ddb
639 changed files with 71327 additions and 2078 deletions
+45 -37
View File
@@ -14,12 +14,17 @@
"""Tests for mediapipe.python._framework_bindings.calculator_graph."""
# Dependency imports
from absl.testing import absltest
import mediapipe as mp
from google.protobuf import text_format
from mediapipe.framework import calculator_pb2
from mediapipe.python import packet_creator
from mediapipe.python import packet_getter
from mediapipe.python._framework_bindings import calculator_graph
from mediapipe.python._framework_bindings import validated_graph_config
CalculatorGraph = calculator_graph.CalculatorGraph
ValidatedGraphConfig = validated_graph_config.ValidatedGraphConfig
class GraphTest(absltest.TestCase):
@@ -28,7 +33,7 @@ class GraphTest(absltest.TestCase):
with self.assertRaisesRegex(
FileNotFoundError,
'(No such file or directory|The path does not exist)'):
mp.CalculatorGraph(binary_graph_path='/tmp/abc.binarypb')
CalculatorGraph(binary_graph_path='/tmp/abc.binarypb')
def test_invalid_node_config(self):
text_config = """
@@ -45,7 +50,7 @@ class GraphTest(absltest.TestCase):
ValueError,
'Input and output streams to PassThroughCalculator must use matching tags and indexes.'
):
mp.CalculatorGraph(graph_config=config_proto)
CalculatorGraph(graph_config=config_proto)
def test_invalid_calculator_type(self):
text_config = """
@@ -59,7 +64,7 @@ class GraphTest(absltest.TestCase):
text_format.Parse(text_config, config_proto)
with self.assertRaisesRegex(
RuntimeError, 'Unable to find Calculator \"SomeUnknownCalculator\"'):
mp.CalculatorGraph(graph_config=config_proto)
CalculatorGraph(graph_config=config_proto)
def test_graph_initialized_with_proto_config(self):
text_config = """
@@ -74,11 +79,11 @@ class GraphTest(absltest.TestCase):
"""
config_proto = calculator_pb2.CalculatorGraphConfig()
text_format.Parse(text_config, config_proto)
graph = mp.CalculatorGraph(graph_config=config_proto)
graph = CalculatorGraph(graph_config=config_proto)
hello_world_packet = mp.packet_creator.create_string('hello world')
hello_world_packet = packet_creator.create_string('hello world')
out = []
graph = mp.CalculatorGraph(graph_config=config_proto)
graph = CalculatorGraph(graph_config=config_proto)
graph.observe_output_stream('out', lambda _, packet: out.append(packet))
graph.start_run()
graph.add_packet_to_input_stream(
@@ -86,15 +91,16 @@ class GraphTest(absltest.TestCase):
graph.add_packet_to_input_stream(
stream='in', packet=hello_world_packet.at(1))
graph.close()
self.assertEqual(graph.graph_input_stream_add_mode,
mp.GraphInputStreamAddMode.WAIT_TILL_NOT_FULL)
self.assertEqual(
graph.graph_input_stream_add_mode,
calculator_graph.GraphInputStreamAddMode.WAIT_TILL_NOT_FULL)
self.assertEqual(graph.max_queue_size, 1)
self.assertFalse(graph.has_error())
self.assertLen(out, 2)
self.assertEqual(out[0].timestamp, 0)
self.assertEqual(out[1].timestamp, 1)
self.assertEqual(mp.packet_getter.get_str(out[0]), 'hello world')
self.assertEqual(mp.packet_getter.get_str(out[1]), 'hello world')
self.assertEqual(packet_getter.get_str(out[0]), 'hello world')
self.assertEqual(packet_getter.get_str(out[1]), 'hello world')
def test_graph_initialized_with_text_config(self):
text_config = """
@@ -108,9 +114,9 @@ class GraphTest(absltest.TestCase):
}
"""
hello_world_packet = mp.packet_creator.create_string('hello world')
hello_world_packet = packet_creator.create_string('hello world')
out = []
graph = mp.CalculatorGraph(graph_config=text_config)
graph = CalculatorGraph(graph_config=text_config)
graph.observe_output_stream('out', lambda _, packet: out.append(packet))
graph.start_run()
graph.add_packet_to_input_stream(
@@ -118,15 +124,16 @@ class GraphTest(absltest.TestCase):
graph.add_packet_to_input_stream(
stream='in', packet=hello_world_packet, timestamp=1)
graph.close()
self.assertEqual(graph.graph_input_stream_add_mode,
mp.GraphInputStreamAddMode.WAIT_TILL_NOT_FULL)
self.assertEqual(
graph.graph_input_stream_add_mode,
calculator_graph.GraphInputStreamAddMode.WAIT_TILL_NOT_FULL)
self.assertEqual(graph.max_queue_size, 1)
self.assertFalse(graph.has_error())
self.assertLen(out, 2)
self.assertEqual(out[0].timestamp, 0)
self.assertEqual(out[1].timestamp, 1)
self.assertEqual(mp.packet_getter.get_str(out[0]), 'hello world')
self.assertEqual(mp.packet_getter.get_str(out[1]), 'hello world')
self.assertEqual(packet_getter.get_str(out[0]), 'hello world')
self.assertEqual(packet_getter.get_str(out[1]), 'hello world')
def test_graph_validation_and_initialization(self):
text_config = """
@@ -140,14 +147,14 @@ class GraphTest(absltest.TestCase):
}
"""
hello_world_packet = mp.packet_creator.create_string('hello world')
hello_world_packet = packet_creator.create_string('hello world')
out = []
validated_graph_config = mp.ValidatedGraphConfig()
self.assertFalse(validated_graph_config.initialized())
validated_graph_config.initialize(graph_config=text_config)
self.assertTrue(validated_graph_config.initialized())
validated_graph = ValidatedGraphConfig()
self.assertFalse(validated_graph.initialized())
validated_graph.initialize(graph_config=text_config)
self.assertTrue(validated_graph.initialized())
graph = mp.CalculatorGraph(validated_graph_config=validated_graph_config)
graph = CalculatorGraph(validated_graph_config=validated_graph)
graph.observe_output_stream('out', lambda _, packet: out.append(packet))
graph.start_run()
graph.add_packet_to_input_stream(
@@ -155,15 +162,16 @@ class GraphTest(absltest.TestCase):
graph.add_packet_to_input_stream(
stream='in', packet=hello_world_packet, timestamp=1)
graph.close()
self.assertEqual(graph.graph_input_stream_add_mode,
mp.GraphInputStreamAddMode.WAIT_TILL_NOT_FULL)
self.assertEqual(
graph.graph_input_stream_add_mode,
calculator_graph.GraphInputStreamAddMode.WAIT_TILL_NOT_FULL)
self.assertEqual(graph.max_queue_size, 1)
self.assertFalse(graph.has_error())
self.assertLen(out, 2)
self.assertEqual(out[0].timestamp, 0)
self.assertEqual(out[1].timestamp, 1)
self.assertEqual(mp.packet_getter.get_str(out[0]), 'hello world')
self.assertEqual(mp.packet_getter.get_str(out[1]), 'hello world')
self.assertEqual(packet_getter.get_str(out[0]), 'hello world')
self.assertEqual(packet_getter.get_str(out[1]), 'hello world')
def test_insert_packets_with_same_timestamp(self):
text_config = """
@@ -179,9 +187,9 @@ class GraphTest(absltest.TestCase):
config_proto = calculator_pb2.CalculatorGraphConfig()
text_format.Parse(text_config, config_proto)
hello_world_packet = mp.packet_creator.create_string('hello world')
hello_world_packet = packet_creator.create_string('hello world')
out = []
graph = mp.CalculatorGraph(graph_config=config_proto)
graph = CalculatorGraph(graph_config=config_proto)
graph.observe_output_stream('out', lambda _, packet: out.append(packet))
graph.start_run()
graph.add_packet_to_input_stream(
@@ -203,13 +211,13 @@ class GraphTest(absltest.TestCase):
"""
config_proto = calculator_pb2.CalculatorGraphConfig()
text_format.Parse(text_config, config_proto)
graph = mp.CalculatorGraph(graph_config=config_proto)
graph = CalculatorGraph(graph_config=config_proto)
graph.start_run(
input_side_packets={'string': mp.packet_creator.create_string('42')})
input_side_packets={'string': packet_creator.create_string('42')})
graph.wait_until_done()
self.assertFalse(graph.has_error())
self.assertEqual(
mp.packet_getter.get_uint(graph.get_output_side_packet('number')), 42)
packet_getter.get_uint(graph.get_output_side_packet('number')), 42)
def test_sequence_input(self):
text_config = """
@@ -222,9 +230,9 @@ class GraphTest(absltest.TestCase):
output_stream: 'out'
}
"""
hello_world_packet = mp.packet_creator.create_string('hello world')
hello_world_packet = packet_creator.create_string('hello world')
out = []
graph = mp.CalculatorGraph(graph_config=text_config)
graph = CalculatorGraph(graph_config=text_config)
graph.observe_output_stream('out', lambda _, packet: out.append(packet))
graph.start_run()
@@ -236,7 +244,7 @@ class GraphTest(absltest.TestCase):
self.assertLen(out, sequence_size)
for i in range(sequence_size):
self.assertEqual(out[i].timestamp, i)
self.assertEqual(mp.packet_getter.get_str(out[i]), 'hello world')
self.assertEqual(packet_getter.get_str(out[i]), 'hello world')
if __name__ == '__main__':