Passing a python numpy array

Is there a way to pass python numpy array from one exec node to the next exec node. Both exec nodes are executing python script. My first python script acquires an image from a Basler camera stored in numpy array. My second python script is running machine learning inferencing on the numpy array. I can ofcourse combine the two python scripts into one monolith but would like to keep them separate for modularity reasons.

1 Like

You can send it using MQTT provided you add support for MQTT in both scripts. As a MQTT broker you can install a local broker like Mosquitto (if you are familiar with and eventually already use MQTT and have a broker running, you can use that)

EDIT: One more thing to add here, it might be better to send the image data as a bytearray and then make the numpy array in the receiving script. Just checked my own code for the same type of usage and I do like below where img is the image data as bytearray

                    # convert string of image data to uint8
                    nparr = np.fromstring(img, np.uint8)

Thanks Walter. How do you receive the image as a bytearray within the python script? Do you access the bytearray via sys.argv? Is there a way to reference or get msg.payload within python?

As I wrote, I use MQTT, no need for sys.argv

In my case I have the following setup for the similar use case as yours

  • the first python script that acquires the image creates a bytearray and publish it to a MQTT broker topic
  • the second python script is running continuosly and waits for image data to be received via on_message from the same MQTT broker topic

So my second script with the AI stuff is always running, this makes it of course faster in processing image data since it always takes some time to load and initiate the engine & model

To pass a bytearray via sys.argv might be possible, you better just try, but that would mean you have to start the scipt from scratch every time you have new data to analyze, the AI engine & model needs to be loaded and initiated that takes time.

I would make a solution where the second scriopt is already running, ready to analyze, like I did using MQTT. If you are interested, you need to read up on MQTT

String arrays, lists, tuples, and dictionaries are passed to and from by value** . Numeric arrays are passed by reference when they are used as arguments or keywords to Python methods. Numeric arrays are passed by value when they are passed from IDL to the Python main level and when Python returns a result to IDL.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.