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.

First you will need to make sure that you have the latest docker images:

user@host:~$ docker-compose pull

To run the example:

user@host:~$ docker-compose up

Code Breakdown

The publisher establishes a connection to the zmq publisher socket at a port (picked for this sample demo)

context = zmq.Context()
data_socket = context.socket(zmq.PUB)
data_socket.setsockopt(zmq.SNDHWM, 5)
data_socket.setsockopt(zmq.SNDBUF, 31457280)
data_socket.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.