examples.communication_patterns package

Submodules

examples.communication_patterns.request_reply_example module

A message requester and replier for native Python objects, and images using OpenCV.

This script demonstrates the capability to transmit native Python objects and images using the MiddlewareCommunicator within the Wrapyfi library. The communication follows the REQ/REP pattern allowing message requesting and replying functionalities between processes or machines.

Demonstrations:
  • Using the NativeObject, Image and AudioChunk messages

  • Transmitting a Python object, an image, and audio chunk

  • Applying the REQ/REP pattern with persistence

  • Reading and transmitting an OpenCV image which can be loaded, resized, and displayed on the client and server ends

  • Reading and transmitting a sounddevice (PortAudio with NumPy) audio chunk which can be played back on the client and server ends

Requirements:
  • Wrapyfi: Middleware communication wrapper (refer to the Wrapyfi documentation for installation instructions)

  • YARP, ROS, ROS 2, ZeroMQ (refer to the Wrapyfi documentation for installation instructions)

  • OpenCV: Used for handling and creating images (installed with Wrapyfi)

  • sounddevice, soundfile: Used for handling audio

Install using pip:

pip install sounddevice soundfile

Run:
# Alternative 1: Image and NativeObject transmission

# On machine 1 (or process 1): Requester sends a message and awaits a reply (image and native object)

python3 request_reply_example.py --mode request --stream image

# On machine 2 (or process 2): Replier waits for a message and sends a reply (image and native object)

python3 request_reply_example.py --mode reply --stream image

# Alternative 2: AudioChunk and NativeObject transmission

# On machine 1 (or process 1): Requester sends a message and awaits a reply (audio chunk and native object)

python3 request_reply_example.py --mode request --stream audio

# On machine 2 (or process 2): Replier waits for a message and sends a reply (audio chunk and native object)

python3 request_reply_example.py --mode reply --stream audio

class examples.communication_patterns.request_reply_example.ReqRep[source]

Bases: MiddlewareCommunicator

send_img_message(msg=None, img_width=320, img_height=240, mware=None, *args, **kwargs)[source]

Exchange messages with OpenCV images and other native Python objects.

send_aud_message(msg=None, aud_rate=-1, aud_chunk=-1, aud_channels=2, mware=None, *args, **kwargs)[source]

Exchange messages with sounddevice audio chunks and other native Python objects.

examples.communication_patterns.request_reply_example.parse_args()[source]

Parse command line arguments.

examples.communication_patterns.request_reply_example.sound_play(my_aud, blocking=True, device=0)[source]

Play audio using sounddevice.

Parameters:
  • my_aud – Tuple[np.ndarray, int]: The audio chunk and sampling rate to play

  • blocking – bool: Whether to block the execution until the audio is played

  • device – int: The sound device to use for audio playback

Returns:

bool: Whether the audio was played successfully

examples.communication_patterns.request_reply_example.main(args)[source]

examples.communication_patterns.transceive_reemit_example module

This example shows how to use the MiddlewareCommunicator to send and receive webcam images. It can be used to test the functionality of the middleware using the PUB/SUB pattern with transceive and reemit modes. The example can be run on a single machine or on multiple machines. In this example, the communication middleware is selected using the --mware argument. The default middleware is ZeroMQ, but YARP, ROS, and ROS 2 are also supported.

The send_image method captures frames from a webcam and publishes them. The webcam source, width, and height can be configured via command-line arguments. The apply_effect method subscribes to the published images, applies a selected effect (e.g., invert, grayscale, or blur), and republishes the processed image.

WARNING: The reemit script MUST be started before the transceive script when –should_wait is passed as an argument. By default, should_wait is False, which is less efficient as it reenters the method multiple times before receiving an image. When should wait is True (by passing –should_wait to both scripts) it is more efficient but restricts the script running order to reemit followed to transceive. Additionally, –should_wait guarantees that all messages are transmitted and received by the methods. When should_wait is False, the first of the returns to be received will be processed, while the others could potentially be ignored.

Combinations:
  • transceive: should_wait; reeimit: should_wait => run reemit before transceive. On stopping transceive, reemit has to be restarted

  • transceive: NO WAITING; reeimit: should_wait => run reemit before transceive. On stopping transceive, reemit has to be restarted

  • transceive: should_wait; reeimit: NO WAITING => run reemit before transceive. No need to restart reemit

  • transceive: NO WAITING; reeimit: NO WAITING => run reemit and transceive in any order. No need to restart reemit

Limitations:
  • Currently does not wait for second return argument in ZeroMQ even when should_wait is True (TODO: Fix)

Requirements:
  • Wrapyfi: Middleware communication wrapper (refer to the Wrapyfi documentation for installation instructions)

  • YARP, ROS, ROS 2, Zenoh (refer to the Wrapyfi documentation for installation instructions)

  • OpenCV (for capturing and processing images)

Run:
# Alternative 1: PUB/SUB mode (transceive and reemit)

# On machine 1 (or process 1): PUB/SUB mode - Listener receives images, applies effects, and republishes

python3 transceive_reemit_example.py --reemit --mware zenoh --effect invert --img_width -1 --img_height -1 --should_wait

# On machine 2 (or process 2): PUB/SUB mode - Publisher captures webcam images and transmits them

python3 transceive_reemit_example.py --transceive --mware zenoh --img_source 0 --img_width -1 --img_height -1 --should_wait

class examples.communication_patterns.transceive_reemit_example.CameraEffects(cam_id, img_width, img_height)[source]

Bases: MiddlewareCommunicator

__init__(cam_id, img_width, img_height)[source]

Initialises the middleware communicator.

calculate_fps(times)[source]
send_image(img_width=320, img_height=240, should_wait=True, mware=None)[source]

Captures and publishes an image frame from the webcam.

apply_effect(*data_from_pub, effect_type='none', img_width=320, img_height=240, should_wait=True, mware=None)[source]

Applies an effect to the received image frame.

debug(should_wait=False, mware=None)[source]
examples.communication_patterns.transceive_reemit_example.parse_args()[source]

Module contents