Skip to content

Instantly share code, notes, and snippets.

@jeffbass
Last active February 5, 2020 07:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffbass/d5b611ee49fba7904aafcdee5939df27 to your computer and use it in GitHub Desktop.
Save jeffbass/d5b611ee49fba7904aafcdee5939df27 to your computer and use it in GitHub Desktop.

imagezmq: Transporting OpenCV images

imagezmq is a set of Python classes that transport OpenCV images from one computer to another using PyZMQ messaging. For example, here is a screen on a Mac computer showing simultaneous video streams from 8 Raspberry Pi cameras:

image

Using imagezmq, this is possible with 11 lines of Python on each Raspberry Pi and with 8 lines of Python on the Mac.

First, run this code on the Mac (or other display computer):

Then, on each Raspberry Pi, run:

Wow! A video surveillance system with 8 (or more!) Raspberry Pi cameras in 19 lines of Python.

Why use imagezmq?

imagezmq is an easy to use image transport mechanism for a distributed image processing network. For example, a network of a dozen Raspberry Pis with cameras can send images to a more powerful central computer. The Raspberry Pis perform image capture and simple image processing like flipping, blurring and motion detection. Then the images are passed via imagezmq to the central computer for more complex image processing like image tagging, text extraction, feature recognition, etc.

Features

  • Sends OpenCV images from one computer to another using ZMQ.
  • Can send jpeg compressed OpenCV images, to lighten network loads.
  • Uses the powerful ZMQ messaging library through PyZMQ bindings.
  • Allows a choice of 2 different ZMQ messaging patterns (REQ/REP or PUB/SUB).
  • Enables the image hub to receive and process images from multiple image senders simultaneously.

Messaging Patterns: REQ/REP versus PUB/SUB

ZMQ allows many different messaging patterns. Two are implemented in imagezmq:

  • REQ/REP: Each RPi sends an image and waits for a REPLY from the central image hub. The RPi sends a new image only when the REPLY is received. In the REQ/REP messaging pattern, each image sender must await a REPLY before continuing. It is a "blocking" pattern for the sender.
  • PUB/SUB: Each RPi sends an image, but does not expect a REPLY from the central image hub. It can continue sending images without awaiting any acknowledgement from the image hub. The image hub provides no REPLY. It is a "non-blocking" pattern for the sender.

There are advantages and disadvantages for each pattern. REQ/REP is the default.

Dependencies and Installation

imagezmq has been tested with:

  • Python 3.5, 3.6, 3.7 and 3.8
  • PyZMQ 16.0 and 17.1
  • Numpy 1.13 and 1.16
  • OpenCV 3.3 and 4.0
  • Raspbian Buster, Raspbian Stretch and Raspbian Jessie
  • picamera 1.13 (used to capture images for the tests)
  • imutils 0.4.6 and 0.5.2 (used to capture images from PiCamera)

Install OpenCV, including Numpy, into a Python Virtual Environment. Then be sure to install imagezmq into the same virtual environment. For example, if the virtual environment is named py3cv3, you would install imagezmq using pip like this:

workon py3cv3  # use your virtual environment name
pip install imagezmq

imagezmq has a directory of tests organized into sender and receiver pairs. You will get all the source code for imagezmq including all the test programs by cloning the GitHub repository:

git clone https://github.com/jeffbass/imagezmq.git

Source Code and Full Documentation

imagezmq is open source. The source code, tests and documentation are at Imagezmq on GitHub. The documentation, including links to application examples, starts from the table of contents in the README.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment