Beispiel #1
0
  def testGraphInitializedWithProtoConfig(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)
    graph = mp.CalculatorGraph(graph_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, timestamp=0)
    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.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')
Beispiel #2
0
  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()
Beispiel #3
0
 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)
Beispiel #4
0
 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)
Beispiel #5
0
 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)
Beispiel #6
0
    def __init__(self):
        """The init method of MediaPipe upper body pose tracker.

    The method reads the upper body pose tracking cpu binary graph and
    initializes a CalculatorGraph from it. The output packets of pose_landmarks
    and output_video output streams will be observed by callbacks. The graph
    will be started at the end of this method, waiting for input packets.
    """
        # MediaPipe package root path
        root_path = os.sep.join(os.path.abspath(__file__).split(os.sep)[:-4])
        mp.resource_util.set_resource_dir(root_path)

        self._graph = mp.CalculatorGraph(binary_graph_path=os.path.join(
            root_path,
            'mediapipe/graphs/pose_tracking/upper_body_pose_tracking_cpu.binarypb'
        ))
        self._outputs = {}
        for stream_name in [POSE_LANDMARKS, OUTPUT_VIDEO]:
            self._graph.observe_output_stream(stream_name, self._assign_packet)
        self._graph.start_run()
Beispiel #7
0
 def testInvalidBinaryGraphFile(self):
   with self.assertRaisesRegex(FileNotFoundError, 'No such file or directory'):
     mp.CalculatorGraph(binary_graph_path='/tmp/abc.binarypb')