Skip to content

A set of Python classes that transport OpenCV images from one computer to another using PyZMQ messaging.

License

Notifications You must be signed in to change notification settings

jeffbass/imagezmq

Repository files navigation

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 image streams from 8 Raspberry Pi cameras:

docs/images/screenshottest.png

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 receiver program on the Mac (or other display computer):

# run this program on the Mac to display image streams from multiple RPis
import cv2
import imagezmq
image_hub = imagezmq.ImageHub()
while True:  # show streamed images until Ctrl-C
    rpi_name, image = image_hub.recv_image()
    cv2.imshow(rpi_name, image) # 1 window for each RPi
    cv2.waitKey(1)
    image_hub.send_reply(b'OK')

Then, on each Raspberry Pi, run this sender program:

# run this program on each RPi to send a labelled image stream
# you can run it on multiple RPi's; 8 RPi's running in above example
import socket
import time
from picamera2 import Picamera2
import imagezmq

sender = imagezmq.ImageSender(connect_to='tcp://jeff-macbook:5555')

rpi_name = socket.gethostname() # send RPi hostname with each image
picam = Picamera2()
picam.start()
time.sleep(2)  # allow camera sensor to warm up
while True:  # send images as stream until Ctrl-C
    image = picam.capture_array()
    sender.send_image(rpi_name, image)

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

See About the multiple RPi video streaming examples for more details about this example.

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. An example of using imageZMQ can be found at Using imageZMQ in distributed computer vision projects.

Each imageZMQ message is a (text_message, image) tuple. The text portion of the tuple identifies the source and other info about the image. In the example above, the text_message portion identifies which RPi is sending the the image so that the receiver can put each unique RPi image stream into a specific window. More details about the imageZMQ tuples in the above example are here.

  • 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.

There are a number of high quality and well maintained messaging protocols for passing messages between computers. I looked at MQTT, RabbitMQ, AMQP and ROS as alternatives. I chose ZMQ and its Python PyZMQ bindings for several reasons:

  • ZMQ does not require a message broker. It is a peer to peer protocol that does not need to pass an image first to a message broker and then to the imagehub. This means fewer running processes and less “double handling” of images. OpenCV images are large compared to simple text messages, so the absence of a message broker is important.
  • ZMQ is very fast for passing OpenCV images. It enables high throughput between image senders and image hubs.
  • ZMQ and its PyZMQ bindings are easy to install.

imageZMQ has been transporting images from a dozen Raspberry Pi computers scattered around my farm to 2 linux image hub servers for over 5 years. The RPi's capture and send dozens to thousands of frames frames a day. imageZMQ has worked very reliably and is very fast. You can learn more about my "science experiment urban permaculture farm" project at Yin Yang Ranch project overview.

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. For further details, see: REQ/REP versus PUB/SUB Messaging Patterns. REQ/REP is the default.

pyversions pypi releasedate license doi

imageZMQ has been tested with:

  • Python 3.5, 3.6, 3.7, 3.8, 3.9, 3.10 and 3.11
  • PyZMQ 16.0, 17.1, 19.0 and 26.0
  • Numpy 1.13, 1.16, 1.18 and 1.24
  • OpenCV 3.3, 4.0, 4.1 and 4.6
  • Raspberry Pi OS Bookworm and Bullseye using PiCamera2
  • Raspbian OS Buster, Stretch and Raspbian Jessie using legacy PiCamera

OpenCV can be challenging to install. There are many example tutorials on the web. For Raspberry Pi computers with current Raspberry Pi OS versions, the Picamera2 documentation recommends installing OpenCV using apt.

Be sure to install OpenCV, including Numpy, into a Python Virtual Environment. Be sure to install imageZMQ into the same virtual environment. For example, on a Raspberry Pi running Raspberry Pi OS Bookworm, my virtual environment is named py311cv4.

Install imageZMQ using pip:

workon py311cv4  # use your virtual environment name
pip install imagezmq

imageZMQ has a directory of tests organized into sender and receiver pairs. You will get the "tests" directory containing all the test programs by cloning the GitHub repository:

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

Once you have cloned the imagezmq directory to a directory on your local machine, you can run the tests per the instructions below. You can use imageZMQ in your own code by importing it (import imagezmq).

imageZMQ and all of the software dependencies must be installed on the display computer that will be receiving the images AND it must all be installed on every Raspberry Pi that will be sending images. If you will be using multiple Raspberry Pis to capture and send images it is may be helpful to install the software on a single Raspberry Pi and test it using the tests below. Once all the tests have run successfully, clone the SD card as needed to use the software on multiple Raspberry Pis.

After you have installed imageZMQ you will want to verify that it installed correctly. The best way to do this is to run some of the test programs that are in the tests folder. The most basic test is a matched pair of sending and receiving programs. The sender program creates a series of OpenCV numbered images and sends them via imageZMQ. The receiving program displays the numbered images. You can run both of these programs on the same computer first, then run them on 2 different computers on the same network. This will confirm that imageZMQ installed correctly and that you are able to specify and open ports for transferring OpenCV images between computers.

There are also test programs that send images from cameras:

  1. Raspberry Pi camera module using the PiCamera2 library with Raspberry Pi OS
  2. Webcam or USB camera using OpenCV's cv2.VideoCapture to capture images

The Picamera2 library requires Raspberry Pi OS Bullseye or later. There are also some test programs that use the original Picamera library for older Raspberry Pi OS versions (Buster and older).

Further details are in Running the Test Programs.

I gave a talk about imageZMQ and its use in my Yin Yang Ranch project at PyCon 2020: Jeff Bass - Yin Yang Ranch: Building a Distributed Computer Vision Pipeline using Python, OpenCV and ZMQ

PyCon 2020 Talk Video about the project

PyCon 2020 Talk Presentation slides

imageZMQ is still in active development. I welcome open issues and pull requests, but because the programs are still evolving, it is best to open an issue for some discussion before submitting pull requests. We can exchange ideas about your potential pull request and open a development branch where you can develop your code and get feedback and testing help from myself and others. imageZMQ is used in my own long running projects and the projects of others, so backwards compatibility with the existing API is important.

Thanks for all contributions big and small. Some significant ones:

Contribution Name GitHub
Initial code & docs Jeff Bass @jeffbass
Added PUB / SUB option Maksym Bodnar @bigdaddymax
HTTP Streaming example Maksym Bodnar @bigdaddymax
Fast PUB / SUB example Philipp Schmidt @philipp-schmidt

Some users have come up with Forks of imageZMQ that I think will be helpful to others, either by using their code or reading their changed code. If you have developed a fork of imageZMQ that demonstrates a concept that would be helpful to others, please open an issue describing your fork so we can have a discussion first rather than opening a pull request. Thanks!

Helpful Fork Name GitHub repository of fork
Add timeouts to image sender to fix restarts or non-response of ImageHub Pat Ryan @youngsoul See CHANGES.md
  • ZeroMQ is a great messaging library with great documentation at ZeroMQ.org.
  • PyZMQ serialization examples provided a starting point for imageZMQ. See the PyZMQ documentation.
  • OpenCV and its Python bindings provide great scaffolding for computer vision projects large or small: OpenCV.org.
  • Picamera2 is a well documented library for accessing the features and settings of the PiCamera modules for various Raspberry Pi single board computers.

To cite this software in publications, refer to the CITATION.cff file. Or use either of the following:

APA:

Bass, J. (2024). imageZMQ: Transporting OpenCV Images (Version 1.2.0) [Computer software]. https://doi.org/10.5281/zenodo.12770292

BibTex:

@software{Bass_imageZMQ_Transporting_OpenCV_2024, author = {Bass, Jeff}, doi = {10.5281/zenodo.12770292}, month = July, title = {{imageZMQ: Transporting OpenCV Images}}, url = {https://github.com/jeffbass/imagezmq}, version = {1.2.0}, year = {2024} }