def testInsertPacketsWithSameTimestamp(self): text_config = """ max_queue_size: 1 input_stream: 'in' output_stream: 'out' node { calculator: 'PassThroughCalculator' input_stream: 'in' output_stream: 'out' } """ config_proto = calculator_pb2.CalculatorGraphConfig() text_format.Parse(text_config, config_proto) hello_world_packet = mp.packet_creator.create_string('hello world') out = [] graph = mp.CalculatorGraph(graph_config=config_proto) graph.observe_output_stream('out', lambda _, packet: out.append(packet)) graph.start_run() graph.add_packet_to_input_stream(stream='in', packet=hello_world_packet.at(0)) graph.wait_until_idle() graph.add_packet_to_input_stream(stream='in', packet=hello_world_packet.at(0)) with self.assertRaisesRegex( ValueError, 'Current minimum expected timestamp is 1 but received 0.'): graph.wait_until_idle()
def testGraphInitializedWithTextConfig(self): text_config = """ max_queue_size: 1 input_stream: 'in' output_stream: 'out' node { calculator: 'PassThroughCalculator' input_stream: 'in' output_stream: 'out' } """ hello_world_packet = mp.packet_creator.create_string('hello world') out = [] graph = mp.CalculatorGraph(graph_config=text_config) graph.observe_output_stream('out', lambda _, packet: out.append(packet)) graph.start_run() graph.add_packet_to_input_stream(stream='in', packet=hello_world_packet.at(0)) 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.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')
def test_sequence_input(self): text_config = """ max_queue_size: 1 input_stream: 'in' output_stream: 'out' node { calculator: 'PassThroughCalculator' input_stream: 'in' output_stream: 'out' } """ hello_world_packet = mp.packet_creator.create_string('hello world') out = [] graph = mp.CalculatorGraph(graph_config=text_config) graph.observe_output_stream('out', lambda _, packet: out.append(packet)) graph.start_run() sequence_size = 1000 for i in range(sequence_size): graph.add_packet_to_input_stream(stream='in', packet=hello_world_packet, timestamp=i) graph.wait_until_idle() 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')
def testInvalidCalculatorType(self): text_config = """ node { calculator: 'SomeUnknownCalculator' input_stream: 'in' output_stream: 'out' } """ config_proto = calculator_pb2.CalculatorGraphConfig() text_format.Parse(text_config, config_proto) with self.assertRaisesRegex( RuntimeError, 'Unable to find Calculator \"SomeUnknownCalculator\"'): mp.CalculatorGraph(graph_config=config_proto)
def testInvalidNodeConfig(self): text_config = """ node { calculator: 'PassThroughCalculator' input_stream: 'in' input_stream: 'in' output_stream: 'out' } """ config_proto = calculator_pb2.CalculatorGraphConfig() text_format.Parse(text_config, config_proto) with self.assertRaisesRegex( ValueError, 'Input and output streams to PassThroughCalculator must use matching tags and indexes.' ): mp.CalculatorGraph(graph_config=config_proto)
def test_image_frame_packet_creation_reference_mode(self): w, h, channels = random.randrange(3, 100), random.randrange(3, 100), 3 rgb_data = np.random.randint(255, size=(h, w, channels), dtype=np.uint8) rgb_data.flags.writeable = False initial_ref_count = sys.getrefcount(rgb_data) image_frame_packet = mp.packet_creator.create_image_frame( image_format=mp.ImageFormat.SRGB, data=rgb_data) # Reference mode increase the ref count of the rgb_data by 1. self.assertEqual(sys.getrefcount(rgb_data), initial_ref_count + 1) del image_frame_packet gc.collect() # Deleting image_frame_packet should decrese the ref count of rgb_data by 1. self.assertEqual(sys.getrefcount(rgb_data), initial_ref_count) rgb_data_copy = np.copy(rgb_data) # rgb_data_copy is a copy of rgb_data and should not increase the ref count. self.assertEqual(sys.getrefcount(rgb_data), initial_ref_count) text_config = """ node { calculator: 'PassThroughCalculator' input_side_packet: "in" output_side_packet: "out" } """ graph = mp.CalculatorGraph(graph_config=text_config) graph.start_run( input_side_packets={ 'in': mp.packet_creator.create_image_frame( image_format=mp.ImageFormat.SRGB, data=rgb_data) }) # reference mode increase the ref count of the rgb_data by 1. self.assertEqual(sys.getrefcount(rgb_data), initial_ref_count + 1) graph.wait_until_done() output_packet = graph.get_output_side_packet('out') del rgb_data del graph gc.collect() # The pixel data of the output image frame packet should still be valid # after the graph and the original rgb_data data are deleted. self.assertTrue( np.array_equal( mp.packet_getter.get_image_frame(output_packet).numpy_view(), rgb_data_copy))
def testSidePacketGraph(self): text_config = """ node { calculator: 'StringToUint64Calculator' input_side_packet: "string" output_side_packet: "number" } """ config_proto = calculator_pb2.CalculatorGraphConfig() text_format.Parse(text_config, config_proto) graph = mp.CalculatorGraph(graph_config=config_proto) graph.start_run(input_side_packets={ 'string': mp.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)
def testInvalidBinaryGraphFile(self): with self.assertRaisesRegex(FileNotFoundError, 'No such file or directory'): mp.CalculatorGraph(binary_graph_path='/tmp/abc.binarypb')
def test_invalid_binary_graph_file(self): with self.assertRaisesRegex( FileNotFoundError, '(No such file or directory|The path does not exist)'): mp.CalculatorGraph(binary_graph_path='/tmp/abc.binarypb')