Repository: jeffbass/imagezmq (language: python)

Issue:
Dev 1.2.0 initial changes
Updating with current versions of Python, Raspberry Pi OS Bookworm and Bullseye and Picamera2.


Discussion / hints:

Fix patch:
diff --git a/README.rst b/README.rst
index c2c098e..a3b3b03 100644
--- a/README.rst
+++ b/README.rst
@@ -69,7 +69,13 @@ 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. <docs/imagezmq-uses.rst>`_
+at `Using imageZMQ in distributed computer vision projects <docs/imagezmq-uses.rst>`_.
+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 <docs/more-details.rst>`_.
 
 Features
 ========
@@ -79,7 +85,7 @@ Features
 - 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.
+  simultaneously. 
 
 Why ZMQ? Why not some other messaging protocol?
 ===============================================
diff --git a/docs/more-details.rst b/docs/more-details.rst
index a8d1c7c..118db6e 100644
--- a/docs/more-details.rst
+++ b/docs/more-details.rst
@@ -8,12 +8,14 @@ cameras:
 
 .. image:: images/screenshottest.png
 
-This display was generated by running ``test_2_send_images.py`` on 8
-Raspberry Pi computers with PiCamera modules and running
-``test_2_receive_images.py`` on a single Mac. Here is a more detailed
-description of the test programs and how they work. There is also an example
-below describing a matching pair of programs that generates the same display
-by using the PUB/SUB messaging pattern.
+This display was generated by running ``test_2_send_images.py`` simultaneously
+on 8 Raspberry Pi computers with PiCamera modules and then running
+``test_2_receive_images.py`` on a to receive all of those image streams and
+display them single Mac. Here is a more detailed
+description of the test programs and how they work. The example uses the 
+ZMQ REQ/REP messaging protocol which is the **imageZMQ** default.
+There is also an example below describing a matching pair of programs that
+can generate the same display by using the PUB/SUB messaging pattern.
 
 .. contents::
 
@@ -26,7 +28,10 @@ clients sending requests (REQ) and the Mac is acting as the server sending back
 replies (REP). Each "request" from the Raspberry Pi is a 2 part message
 consisting of an OpenCV image and the Raspberry Pi hostname. Each "reply" sent
 by the Mac message is an "OK" that tells the Raspberry Pi that it can send
-another image.
+another image. It is a very important **imageZMQ** design element that each 
+message is a ``(text_message, image)`` tuple. If the **imageZMQ** messages were 
+images only, there would be no way to identify and distinguish multiple image 
+streams.
 
 ZMQ is a powerful messaging library that allows many patterns for sending and
 receiving messages. **imageZMQ** provides access to both **REQ/REP** and


Test patch:
diff --git a/docs/running-tests.rst b/docs/running-tests.rst
index 6145458..d24e63e 100644
--- a/docs/running-tests.rst
+++ b/docs/running-tests.rst
@@ -11,7 +11,8 @@ Raspberry Pis, ssh to each Raspberry Pi in a new terminal window. **imageZMQ**
 and its dependencies must be installed on the Mac and on each Raspberry Pi that
 will be sending images.
 
-There are 3 tests. Each of the tests uses 2 programs in matched pairs. One
+There are 3 tests using the REQ/REP ZMQ message pattern. Each of the tests uses 2 programs in matched pairs. In each
+test, one
 program sends images and the other program displays images. Because of the
 REQ/REP pattern that is being used, it is important that the receiving program
 be started before the sending program.
diff --git a/tests/test_1_receive_images.py b/tests/test_1_receive_images.py
deleted file mode 100644
index 7500096..0000000
--- a/tests/test_1_receive_images.py
+++ /dev/null
@@ -1,37 +0,0 @@
-"""test_1_receive_images.py -- basic receive images test.
-
-A simple test program that uses imagezmq to receive images from a program that
-is sending images.
-
-1. Run this program in its own terminal window:
-python test_1_receive_images.py
-
-This "receive and display images" program must be running before starting the
-image sending program.
-
-2.Run the image sending program in a different terminal window:
-python test_1_send_images.py
-
-A cv2.imshow() window will appear showing the tramsmitted image. The sending
-program sends an images with imprinted incrementing couner value.
-
-If you end the receiving program check that sending program stops sending images
-and counter is not incrementing anymore.
-
-As soon as you start receiving script again it will pick images from the position
-where it was stopped and conter will resume.
-
-To end the programs, press Ctrl-C in the terminal window of the sending program
-first. Then press Ctrl-C in the terminal window of the receiving proram. You
-may have to press Ctrl-C in the display window as well.
-"""
-
-import cv2
-import imagezmq
-
-image_hub = imagezmq.ImageHub()
-while True:  # press Ctrl-C to stop image display program
-    image_name, image = image_hub.recv_image()
-    cv2.imshow(image_name, image)
-    cv2.waitKey(1)  # wait until a key is pressed
-    image_hub.send_reply(b'OK')
diff --git a/tests/test_1_send_images.py b/tests/test_1_send_images.py
deleted file mode 100644
index 463c5ff..0000000
--- a/tests/test_1_send_images.py
+++ /dev/null
@@ -1,36 +0,0 @@
-"""test_1_send_images.py -- basic send images test.
-
-A simple test program that uses imagezmq to send images to a receiving program
-that will display the images.
-
-This program requires that the image receiving program to be running first.
-Brief test instructions are in that program: test_1_receive_images.py.
-"""
-
-import sys
-import time
-import numpy as np
-import cv2
-import imagezmq
-
-# Create 2 different test images to send
-# A green square on a black background
-# A red square on a black background
-
-sender = imagezmq.ImageSender()
-i = 0
-image_window_name = 'From Sender'
-while True:  # press Ctrl-C to stop image sending program
-    # Increment a counter and print it's value to console
-    i = i + 1
-    print('Sending ' + str(i))
-
-    # Create a simple image
-    image = np.zeros((400, 400, 3), dtype='uint8')
-    green = (0, 255, 0)
-    cv2.rectangle(image, (50, 50), (300, 300), green, 5)
-
-    # Add counter value to the image and send it to the queue
-    cv2.putText(image, str(i), (100, 150), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 255, 255), 4)
-    sender.send_image(image_window_name, image)
-    time.sleep(1)
diff --git a/tests/test_2_mac_receive_images.py b/tests/test_2_mac_receive_images.py
deleted file mode 100644
index 2de2c37..0000000
--- a/tests/test_2_mac_receive_images.py
+++ /dev/null
@@ -1,33 +0,0 @@
-"""test_2_mac_receive_images.py -- receive & display image stream.
-
-A simple test program that uses imagezmq to receive an image stream from
-a Raspberry Pi and display it as a video steam.
-
-1. Run this program in its own terminal window on the mac:
-python test_2_mac_receive_images.py
-
-This "receive and display images" program must be running before starting
-the RPi sending program.
-
-2. Run the image sending program on the RPi:
-python test_2_rpi_send_images.py
-
-A cv2.imshow() window will appear on the Mac showing the tramsmitted images
-as a video stream. You can repeat Step 2 and start the test_2_rpi_send_images.py
-on multiple RPis and each one will cause a new cv2.imshow() window to open.
-
-To end the programs, press Ctrl-C in the terminal window of the RPi  first.
-Then press Ctrl-C in the terminal window of the receiving proram. You may have
-to press Ctrl-C in the display window as well.
-"""
-import sys
-
-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')
diff --git a/tests/test_receive_images.py b/tests/test_receive_images.py
new file mode 100644
index 0000000..51ae26d
--- /dev/null
+++ b/tests/test_receive_images.py
@@ -0,0 +1,9 @@
+# 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')
\ No newline at end of file
diff --git a/tests/test_send_num_images.py b/tests/test_send_num_images.py
new file mode 100644
index 0000000..5e558b0
--- /dev/null
+++ b/tests/test_send_num_images.py
@@ -0,0 +1,47 @@
+"""test_send_num_images.py -- send generated numbers images
+
+Tests sending class of imagezmq to send generated images to a receiving program
+that will display the images. Receiver program can be localhost on this computer
+OR another computer (if an address is provided)
+
+The sent images are a sequence of numbers 1, 2, 3 ...
+The numbers are Yellow and appear in a Green Rectangle
+OpenCV cv2.rectangle and cv2.putText are used to create the images
+
+This sending program uses the default REQ/REP message pattern 
+
+Image receiving program must be running first before starting this program.
+"""
+import time
+import numpy as np
+import cv2
+import imagezmq
+
+# image will be green square on a black background
+green = (0, 255, 0)
+
+# ascending yellow number will be written in the green square using cv2.putText
+yellow = (0, 255, 255)
+
+# uncomment only ONE ImageSender statement for each test; comment out the others
+sender = imagezmq.ImageSender()  # will send to localhost on THIS computer
+
+# 2 different ways to specify a different computer that will receive images
+# sender = imagezmq.ImageSender(connect_to='tcp://192.168.1.190:5555')
+# sender = imagezmq.ImageSender(connect_to='tcp:jeff-mac-2022:5555')
+
+i = 0
+image_window_name = 'Images from Sender'
+while True:  # press Ctrl-C to stop image sending program
+    # Increment a counter and print it's value in terminal window
+    i = i + 1
+    print('Sending ' + str(i))
+    
+    # create an image with a black background with a green rectangle
+    image = np.zeros((400, 400, 3), dtype='uint8')
+    cv2.rectangle(image, (50, 50), (300, 300), green, 5)
+
+    # put the counter value number into the image 
+    cv2.putText(image, str(i), (100, 150), cv2.FONT_HERSHEY_SIMPLEX, 2, yellow, 4)
+    sender.send_image(image_window_name, image)
+    time.sleep(1)
\ No newline at end of file