Skip to main content

Publisher Example

This tool is intended as an example for publishing decoded image frames in the (BGR HWC format) to the Nimble source data stream. This tool also demonstrates the feature in Nimble to ingest from a zmq source, configurable through the Nimble configuration json. You can find it under tools/zmq/publisher. This example also uses tools/zmq/subscriber to listen to the Nimble output, you can find the subscriber example here.

To run the example:

user@host:~$ docker-compose up --build

Code Breakdown

The publisher binds a port and awaits an incoming connection from the nimble zmq source, alternatively the publisher can initiate a connection to a proxy.

ctx = zmq.Context()
pub = ctx.socket(zmq.PUB)
pub.setsockopt(zmq.SNDHWM, 5)
pub.setsockopt(zmq.SNDBUF, 31457280)
if ENABLE_PROXY:
pub.connect("tcp://proxy:3500")
else:
pub.bind("tcp://*:3500")

The send buffer queue water marks are specifically set to sync with slow subscribers without causing queue overflows or mem leaks. The zmq publisher socket is setup to receive decoded frames in the HWC (rows x cols x channels) format, with the channels being in the BGR color order. This sample demonstrates a simple opencv videocapture from a video file source

cap = cv2.VideoCapture("videos/demo-tracking-28s.mp4")
ret, image = cap.read()

Note that a compact and fast serialization format is used to transfer image bytes over the zmq. The Nimble zmq source expects to receive the following multipart message:

  • the image byte array in the HWC (rows x cols x channels) format, with the channels being in the BGR color order, serialized through msgpack
  • the datatype of the mat/numpy representing the image, converted to str and utf-8 encoded
  • the # of rows of the image mat/numpy, converted to str and utf-8 encoded
  • the # of cols of the image mat/numpy, converted to str and utf-8 encoded
data_socket.send_multipart(
[
msgpack.packb(image, default=m.encode),
str(dtype).encode("utf-8"),
str(shape[0]).encode("utf-8"),
str(shape[1]).encode("utf-8"),
]
)

This sample publisher restarts on EOS and tries to publish image packets to Nimble indefinitely until termination.

Proxy

There is an additional example under tools/zmq/proxy which allows for a publisher to initiate a connection to proxy, instead of listening for incoming nimble requests. This is useful when the publisher's IP address isn't fixed and needs to be the initiator. The proxy microservice is given as a sample but can be integrated directly into nimble docker-compose.yaml.