Beispiel #1
0
    def test_pack_unpack(self):
        device_name = "Test"

        test_messages = [
            pyigtl.StringMessage("some message", device_name=device_name),
            pyigtl.ImageMessage(np.random.randn(30, 10, 5) * 50 + 100,
                                device_name=device_name),
            pyigtl.PointMessage(
                [[20, 30, 10], [2, -5, -10], [12.4, 11.3, 0.3]],
                device_name=device_name),
            pyigtl.TransformMessage(np.array([[1, 2, 3, 4], [5, 6, 7, 8],
                                              [9, 10, 11, 12], [0, 0, 0, 1]]),
                                    device_name=device_name),
        ]

        pack_unpack_inconsistencies_found = 0
        for message in test_messages:
            print("Original message:\n" + str(message))
            packed = message.pack()
            header_fields = pyigtl.MessageBase.parse_header(
                packed[:pyigtl.MessageBase.IGTL_HEADER_SIZE])
            new_message = pyigtl.MessageBase.create_message(
                header_fields['message_type'])
            new_message.unpack(header_fields,
                               packed[pyigtl.MessageBase.IGTL_HEADER_SIZE:])
            print("Packed/unpacked message:\n" + str(new_message))
            new_packed = new_message.pack()
            if packed == new_packed:
                print(" -- Correct")
            else:
                print(" -- Mismatch")
                pack_unpack_inconsistencies_found += 1

        self.assertEqual(pack_unpack_inconsistencies_found, 0)
Beispiel #2
0
    def test_send_messages(self):
        """Test if both server and client can send and receive messages"""

        # Server send
        original_message = pyigtl.StringMessage("TEST Message",
                                                device_name="TestString")
        self.assertTrue(self.server.send_message(original_message))
        time.sleep(1.0)

        # Client receive
        received_messages = self.client.get_latest_messages()
        self.assertTrue(len(received_messages) > 0)

        # Client send
        for message in received_messages:
            self.client.send_message(message)
        time.sleep(1.0)

        # Server receive
        received_messages = self.server.get_latest_messages()
        self.assertTrue(len(received_messages) > 0)

        # Verify message content
        message_matched = False
        for message in received_messages:
            if message.device_name == original_message.device_name:
                # Original message was received
                self.assertEqual(message.string, original_message.string)
                message_matched = True
                break
        self.assertTrue(message_matched)
Beispiel #3
0
    def test_send_receive(self):
        device_name = "Test"

        test_messages = [
            pyigtl.StringMessage("some message", device_name=device_name),
            pyigtl.ImageMessage(np.random.randn(30, 10, 5) * 50 + 100,
                                device_name=device_name),
            pyigtl.PointMessage(
                [[20, 30, 10], [2, -5, -10], [12.4, 11.3, 0.3]],
                device_name=device_name),
            pyigtl.TransformMessage(np.eye(4), device_name=device_name),
        ]

        for message in test_messages:
            self.assertTrue(self.server.send_message(message))
            self.assertIsNotNone(
                self.client.wait_for_message(device_name=device_name,
                                             timeout=5))
Beispiel #4
0
def main():
    try:
        networkModuleName = FLAGS.network_module_name
        sys.path.append(os.path.join(FLAGS.model_directory,os.pardir))
        importStatement = "from " + networkModuleName + " import " + networkModuleName + " as NeuralNetwork"
        exec(importStatement,globals())
    except ModuleNotFoundError:
        logging.info("Could not find model folder " + str(FLAGS.model_name))
        errorMessage = "Could not find model folder " + str(FLAGS.model_name)
        print(errorMessage)
    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
    model_name = FLAGS.model_name
    modelFolder =FLAGS.model_directory
    currentTime = time.time()
    model = NeuralNetwork()
    model.loadModel(modelFolder,model_name)

    print("Server starting...")
    if FLAGS.outgoing_host == "localhost":
        server = pyigtl.OpenIGTLinkServer(port=FLAGS.outgoing_port,local_server=True)
    else:
        server = pyigtl.OpenIGTLinkServer(port=FLAGS.outgoing_port, local_server=False)
    server.start()
    print("Server running on " + str(server.host) + " : " + str(server.port) + "...")


    print("Client starting...")
    client = pyigtl.OpenIGTLinkClient(host=FLAGS.incoming_host, port=FLAGS.incoming_port)
    client.start()
    print(FLAGS.incoming_host)
    print(FLAGS.incoming_port)
    print("Client running...")
    lastMessageTime = time.time()
    ImageReceived = False
    frameCount = 0
    try:
        while (not ImageReceived) or (ImageReceived and time.time() - lastMessageTime < FLAGS.timeout):

            #if server.is_connected() and client.is_connected():

            messages = client.get_latest_messages()
            if len(messages) > 0:
                for message in messages:
                    if message._message_type == "IMAGE":
                        frameCount +=1
                        ImageReceived = True
                        lastMessageTime = time.time()
                        image = message.image
                        image = image[0]
                        print(time.time())
                        (networkOutput) = model.predict(image)
                        print(time.time())
                        if FLAGS.output_type == 'STRING':
                            labelMessage = pyigtl.StringMessage(networkOutput, device_name=FLAGS.device_name)
                            server.send_message(labelMessage)
                        elif FLAGS.output_type == 'IMAGE':
                            labelMessage = pyigtl.ImageMessage(networkOutput, device_name=FLAGS.device_name)
                            server.send_message(labelMessage)
                        elif FLAGS.output_type == 'TRANSFORM':
                            pass

                        print(frameCount)
                    if message._message_type == "STRING":
                        print("Received stop message")
                        text = message.string
                        if text == "STOP":
                            client.stop()
                            server.stop()
            else:
                pass
            time.sleep(0.25)
    except KeyboardInterrupt:
        pass
    # Generate transform
    matrix = np.eye(4)
    matrix[0, 3] = sin(timestep * 0.01) * 20.0
    rotation_angle_rad = timestep * 0.5 * pi / 180.0
    matrix[1, 1] = cos(rotation_angle_rad)
    matrix[2, 1] = -sin(rotation_angle_rad)
    matrix[1, 2] = sin(rotation_angle_rad)
    matrix[2, 2] = cos(rotation_angle_rad)
    transform_message = pyigtl.TransformMessage(
        matrix,
        device_name="ImageToReference",
        timestamp=image_message.timestamp)

    # Generate string
    string_message = pyigtl.StringMessage("TestingString_" + str(timestep),
                                          device_name="Text",
                                          timestamp=image_message.timestamp)

    # Send messages
    server.send_message(image_message)
    server.send_message(transform_message)
    server.send_message(string_message)

    # Print received messages
    messages = server.get_latest_messages()
    for message in messages:
        print(message.device_name)

    # Do not flood the message queue,
    # but allow a little time for background network transfer
    sleep(0.01)