|
| 1 | +import numpy as np |
| 2 | +import pyarrow as pa |
| 3 | +from dora import Node |
| 4 | +import time |
| 5 | + |
| 6 | +def main(): |
| 7 | + node = Node() |
| 8 | + |
| 9 | + # Initialize some dummy data |
| 10 | + # Let's create some dummy data to send. |
| 11 | + # We have two images and a state vector, just like in your robotics setup. |
| 12 | + image1 = np.zeros((480, 640, 3), dtype=np.uint8) |
| 13 | + image2 = np.ones((480, 640, 3), dtype=np.uint8) * 255 |
| 14 | + state = np.array([[1.0, 2.0, 3.0, 4.0, 5.0, 6.0]], dtype=np.float32) |
| 15 | + |
| 16 | + for event in node: |
| 17 | + if event["type"] == "INPUT": |
| 18 | + if event["id"] == "tick": |
| 19 | + # Efficiently package the arrays using PyArrow. |
| 20 | + # We flatten the arrays with .ravel() which is super fast and avoids expensive logic. |
| 21 | + # Then we wrap them in a list of length 1 to create the arrow columns. |
| 22 | + |
| 23 | + t0 = time.time() |
| 24 | + |
| 25 | + # Fast path! |
| 26 | + encoded_image1 = pa.array([image1.ravel()]) |
| 27 | + encoded_image2 = pa.array([image2.ravel()]) |
| 28 | + encoded_state = pa.array([state.ravel()]) |
| 29 | + |
| 30 | + struct_data = pa.StructArray.from_arrays( |
| 31 | + [encoded_image1, encoded_image2, encoded_state], |
| 32 | + names=['image1', 'image2', 'state'] |
| 33 | + ) |
| 34 | + |
| 35 | + node.send_output("multi_array_msg", struct_data) |
| 36 | + |
| 37 | + proc_time = time.time() - t0 |
| 38 | + print(f"Sent message with 3 arrays. Encoding time: {proc_time:.6f}s", flush=True) |
| 39 | + |
| 40 | +if __name__ == "__main__": |
| 41 | + main() |
0 commit comments