コード例 #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)
コード例 #2
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))
コード例 #3
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
コード例 #4
0
server = pyigtl.OpenIGTLinkServer(port=18944)

image_size = [500, 300]
radius = 60

timestep = 0
while True:

    if not server.is_connected():
        # Wait for client to connect
        sleep(0.1)
        continue

    # Generate image
    timestep += 1
    cx = (1+sin(timestep*0.05)) * 0.5 * (image_size[0]-2*radius)+radius
    cy = (1+sin(timestep*0.06)) * 0.5 * (image_size[1]-2*radius)+radius
    y, x = np.ogrid[-cx:image_size[0]-cx, -cy:image_size[1]-cy]
    mask = x*x + y*y <= radius*radius
    voxels = np.ones((image_size[0], image_size[1], 1))
    voxels[mask] = 255

    # numpy image axes are in kji order, while we generated the image with ijk axes
    voxels = np.transpose(voxels, axes=(2, 1, 0))

    # Send image
    print(f"time: {timestep}   position: ({cx}, {cy})")
    image_message = pyigtl.ImageMessage(voxels, device_name="Image")
    server.send_message(image_message, wait=True)
    # Since we wait until the message is actually sent, the message queue will not be flooded