imagezmq-master
所属分类:Java编程
开发工具:Asm
文件大小:1448KB
下载次数:0
上传日期:2021-04-27 15:58:42
上 传 者:
oufadel
说明: Thank you for submitting your resume. Our management team is reviewing your qualifications and will contact you if there is a match with our current open positions.
Meanwhile, using your email address and password, you can login to our careers website anytime to update your application information.
文件列表:
HISTORY.md (3071, 2021-02-10)
LICENSE.txt (1103, 2021-02-10)
MANIFEST.in (58, 2021-02-10)
docs (0, 2021-02-10)
docs\FAQ.rst (13141, 2021-02-10)
docs\advanced-pub-sub.rst (5060, 2021-02-10)
docs\api-examples.rst (6230, 2021-02-10)
docs\badge_fetch.rst (1099, 2021-02-10)
docs\examples.rst (9522, 2021-02-10)
docs\fast-pub-sub.rst (3398, 2021-02-10)
docs\images (0, 2021-02-10)
docs\images\doi.svg (1191, 2021-02-10)
docs\images\license.svg (950, 2021-02-10)
docs\images\pypi_version.svg (1004, 2021-02-10)
docs\images\python_versions.svg (992, 2021-02-10)
docs\images\release_date.svg (1026, 2021-02-10)
docs\images\screenshottest.png (1214072, 2021-02-10)
docs\images\water-meter.jpg (192293, 2021-02-10)
docs\imagezmq-uses.rst (6551, 2021-02-10)
docs\more-details.rst (12492, 2021-02-10)
docs\req-vs-pub.rst (6514, 2021-02-10)
examples (0, 2021-02-10)
examples\advanced_http.py (650, 2021-02-10)
examples\advanced_pub.py (693, 2021-02-10)
examples\advanced_send.py (801, 2021-02-10)
examples\faster_jpg_hub.py (2265, 2021-02-10)
examples\faster_jpg_send.py (2193, 2021-02-10)
examples\pub_sub_broadcast.py (1867, 2021-02-10)
examples\pub_sub_receive.py (2537, 2021-02-10)
examples\t2_recv_images_via_sub.py (1945, 2021-02-10)
examples\t2_send_images_via_pub.py (896, 2021-02-10)
examples\timeout_req_ImageSender.py (3294, 2021-02-10)
examples\with_ImageHub.py (1718, 2021-02-10)
examples\with_ImageSender.py (1814, 2021-02-10)
imagezmq (0, 2021-02-10)
... ...
====================================
imageZMQ: Transporting OpenCV images
====================================
Introduction
============
**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:: 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 code on the Mac (or other display computer):
.. code-block:: python
:number-lines:
# 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:
.. code-block:: python
:number-lines:
# run this program on each RPi to send a labelled image stream
import socket
import time
from imutils.video import VideoStream
import imagezmq
sender = imagezmq.ImageSender(connect_to='tcp://jeff-macbook:5555')
rpi_name = socket.gethostname() # send RPi hostname with each image
picam = VideoStream(usePiCamera=True).start()
time.sleep(2.0) # allow camera sensor to warm up
while True: # send images as stream until Ctrl-C
image = picam.read()
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.
.. contents::
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. An example of using **imageZMQ** can be found
at `Using imageZMQ in distributed computer vision projects. `_
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.
Why ZMQ? Why not some other messaging protocol?
===============================================
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 2
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. `_
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. For further details,
see: `REQ/REP versus PUB/SUB Messaging Patterns `_.
**REQ/REP is the default.**
Dependencies and Installation
=============================
+--------------+--------+---------------+-----------+-------+
| |pyversions| | |pypi| | |releasedate| | |license| | |doi| |
+--------------+--------+---------------+-----------+-------+
.. |pyversions| image:: /docs/images/python_versions.svg
:target: https://pypi.org/project/imagezmq/
.. |pypi| image:: /docs/images/pypi_version.svg
:target: https://pypi.org/project/imagezmq/
.. |releasedate| image:: /docs/images/release_date.svg
:target: https://github.com/jeffbass/imagezmq/releases/tag/v1.1.1
.. |license| image:: /docs/images/license.svg
:target: LICENSE.txt
.. |doi| image:: /docs/images/doi.svg
:target: https://doi.org/10.5281/zenodo.3840855
**imageZMQ** has been tested with:
- Python 3.5, 3.6, 3.7 and 3.8
- PyZMQ 16.0, 17.1 and 19.0
- Numpy 1.13, 1.16 and 1.18
- OpenCV 3.3, 4.0 and 4.1
- Raspbian Buster, Raspbian Stretch and Raspbian Jessie
- picamera 1.13 (used to capture images for the tests)
- imutils 0.4.6, 0.5.2 and 0.5.3 (used to capture images from PiCamera)
OpenCV is challenging to install. I recommend using the installation
instructions at `PyImageSearch `_.
Adrian Rosebrock's PyImageSearch blog and books are great resources for
learning about and installing OpenCV on Raspberry Pi's, Macs and
Linux computers.
- `Raspbian Stretch: Install OpenCV 3 + Python on your Raspberry Pi
`_
- `macOS: Install OpenCV 3 and Python 3.5
`_
- `Ubuntu 16.04: How to install OpenCV
`_
Be sure to install OpenCV, including Numpy, into a Python Virtual Environment,
as shown in the above tutorials. Be sure to install **imageZMQ**
into the **same** virtual environment. For example, my virtual
environment is named **py3cv3**.
Install **imageZMQ** using pip:
.. code-block:: bash
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 the "tests" directory containing all the test programs by
cloning the GitHub repository:
.. code-block:: bash
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 best 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.
Running the Tests
=================
When running the tests, use multiple terminal windows on the computer that will
be displaying the images (I used a Mac for these examples; in my descriptions
I use the term "Mac" to refer to any Mac or Linux computer, including a
Raspbery Pi). One terminal window is used to launch the programs that run on the
Mac to receive the images. Another terminal window on the Mac is used to ssh
into the Raspberry Pi and run the image sending program. If sending from multiple
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
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.
**imageZMQ** is in early development as part of a larger system. There are
currently separate methods for sending and receiving images vs. jpg compressed
images. Further development will refactor these into single methods for sending
and receiving. ::
Test 1: Simple generated images sent and displayed on Mac
---------------------------------------------------------
**The first test** runs both the sending program and the receiving program on
the Mac. This confirms that all the software is installed correctly and that
``cv2.imshow()`` works on the Mac. No Raspberry Pi or camera is involved. The
sending program generates test images and sends them to the receiving program.
First, in one terminal window, activate your virtual environment, then change to
the tests directory and run the receiving program, which will receive and
display images::
workon py3cv3 # use your virtual environment name
cd imagezmq/tests
python test_1_receive_images.py
Then, in a second terminal window on the same display computer (Mac), change to
the tests directory and run the sending program, which will generate and send
images::
workon py3cv3 # use your virtual environment name
cd imagezmq/tests
python test_1_send_images.py
After a few seconds, a ``cv2.imshow()`` window should open and display a green
square on a black background. There will be a yellow number in the green square
that will increase (1, 2, ...) once per second until you stop both
programs by pressing Ctrl-C. It is normal to get a cascade of error messages
when stopping the program with Ctrl-C. This simple test program has no
try / except error trapping.
Test 2: Sending stream of OpenCV images from RPi(s) to Mac
----------------------------------------------------------
**The second test** runs the sending program on a Raspberry Pi, capturing
images from the PiCamera at up to 32 frames a second and sending them via
**imageZMQ** to the Mac. The receiving program on the Mac displays a continuous
video stream of the images captured by the Raspberry Pi. First, in one terminal
window, activate your virtual environment, change to the tests directory and
run the receiving program which will display the images::
workon py3cv3 # use your virtual environment name
cd imagezmq/tests
python test_2_receive_images.py
Then, in a second terminal window on the Mac, ssh into the Raspberry Pi that
will be sending images. Activate your Python virtual environment, change to the
tests directory and **edit the test_2_send_images.py program to specify the tcp
address of your display computer.** There are 2 lines in the program that show
different ways of specifying the tcp address: by hostname or by tcp numeric address.
Pick one method, change the tcp address to that of your display computer and
comment out the method you are not using. Finally, run the program, which will
capture and send images::
workon py3cv3 # use your virtual environment name
cd imagezmq/tests
python test_2_send_images.py
In about 5 seconds, a ``cv2.imshow()`` window will appear on the Mac and display
the video stream being sent by the Raspberry Pi. You can repeat this step in
additional terminal windows, to ssh into additional Raspberry Pi computers and
start additional video streams. The receiving program can receive and display
images from multiple Raspberry Pis, with each Raspberry Pi's image stream
showing in a separate window. For this to work, each Raspberry Pi must have a
unique hostname because the images are sorted into different
``cv2.imshow()`` windows based on the hostname. The ``cv2.imshow()`` windows
for multiple Raspberry Pi streams will be stacked on top of each other until you
drag them and arrange them on your desktop. The example picture at the start of
this documentation shows 8 simultaneous video streams for 8 Raspberry Pi
computers with different hostnames. Each program must be stopped by pressing
Ctrl-C in its terminal window. It is normal to get a cascade of error messages
when stopping these programs with Ctrl-C. This simple test program has no try /
except error trapping.
Test 3: Sending stream of jpgs from RPi(s) to Mac
-------------------------------------------------
**The third test** runs a different pair of sending / receiving programs. The
program on the Raspberry Pi captures images from the PiCamera at up to 32
frames a second and **compresses them to jpeg form** before sending them via
**imageZMQ** to the Mac. The receiving program on the Mac converts the jpg
compressed frames back to OpenCV images and displays them as a continuous video
stream. This jpeg compression can greatly reduce the network load of sending many
images from multiple sources.
The programs that send and receive the images using jpg compression are run in
the same way as the above pair of programs that send uncompressed images. Use
the instructions above as a guide. The programs for Test 3 are::
test_3_receive_jpg.py # run on the Mac to receive & decompress images
test_3_send_jpg.py # ron on each Raspberry Pi to compress & send images
As with the previous Test 2 program pair, you will need to edit the "connect_to"
address in the sending program to the tcp address of your Mac (or other display
computer). You will also need to remember to start the *receive* program on the
Mac before you start the sending program on the Raspberry Pi. As before, each
program must be stopped by pressing Ctrl-C in its terminal window. It is normal
to get a cascade of error messages when stopping these programs with Ctrl-C.
This simple test program has no try / except error trapping. Be sure to activate
your virtual environment as you did for Test 2 (see above) before running these
tests.
Test 4: Using PUB/SUB to send simple generated images and display them on Mac
-----------------------------------------------------------------------------
**The fourth test** is a repeat of Test 1, but uses the PUB/SUB messaging
pattern instead of the REQ/REP messaging pattern. It shows the differences
in running PUB/SUB versus REQ/REP in the simplest possible test program.
Test 4 runs both the sending program and the receiving program on
the Mac. No Raspberry Pi or camera is involved. This test shows the start / stop
flexibility of the PUB/SUB pattern. All 3 of the above REQ/REP tests require
that the receiving program be started first, then the sending program. And they
require that the sending program be restarted if the receiving program is
restarted. This is standard behavior for the REQ/REP messaging pattern. But
this test shows that either PUB/SUB program can be started first and that
message sending will resume if either program is restarted. That is a feature
of the PUB/SUB messaging pattern. See other documentation listed below for
further differences, advantages and disadvantages of the REQ/REP versus PUB/SUB
messaging patterns.
The sending program generates test images and sends them to the receiving program.
First, in one terminal window, activate your virtual environment, then change to
the tests directory and run the receiving program, which will receive and
display images::
workon py3cv3 # use your virtual environment name
cd imagezmq/tests
python test_4_pub.py
Then, in a second terminal window on the same display computer (Mac), change to
the tests directory and run the sending program, which will generate and send
images::
workon py3cv3 # use your virtual environment name
cd imagezmq/tests
python test_4_sub.py
After a few seconds, a ``cv2.imshow()`` window should open and display a green
square on a black background. There will be a yellow number in the green square
that will increase (1, 2, ...) once per second. Now you can stop either
program and restart it and see that the sending of numbers continues and picks
up where it left off (though some transmitted images may have been skipped
during restart). It is normal to get a cascade of error messages
when starting and stopping the program with Ctrl-C. These simple test program
have no try / except error trapping, since their only purpose is this simple
demonstration.
Timing tests: Complete imageZMQ usage examples
==============================================
The test programs above are short and simple. They test that the software and
dependencies are installed correctly and that images transfer successfully between
a Raspberry Pi computer and a display computer such as a Mac. The tests
directory contains 2 more send / receive program pairs that provide a more
complete example of imageZMQ usage. Each of these programs includes
try / except blocks that enable ending the programs by typing Ctrl-C
without starting a cascade of error messages. They also perform frames per
second (FPS) timing tests that measure the speeds of image transfer using the
compressed versus the non-compressed transfer methods. They also show how to
capture the hub response in the sending program, which wasn't needed in the
simple tests.
One pair of programs transmits and receives **OpenCV images** and measures FPS::
timing_receive_images.py # run on Mac to display images
timing_send_images.py # run on Raspberry Pi to send images
Another pair of programs transmits and receives **jpg compressed images** and
measures FPS::
timing_send_jpg_buf.py # run on Raspberry Pi to send images
timing_receive_jpg_buf.py # run on Mac to display images
As with the other test program pairs, you will need to edit the "connect_to"
address in the sending program to the tcp address of your Mac (or other display
computer). You will also need to remember to start the *receive* program on the
Mac before you start the sending program on the Raspberry Pi. With these programs,
the try / except blocks will end the programs cleanly with no errors when you
press Ctrl-C. Be sure to activate your virtual environment before running these
tests.
Additional Documentation and Examples
=====================================
- `API and Two Simple Example Programs `_
- `More details about the multiple RPi video streaming example `_
- `REQ/REP versus PUB/SUB Messaging Patterns `_
- `Examples showing different techniques for using imageZMQ `_
- `Using imageZMQ in distributed computer vision projects
近期下载者:
相关文件:
收藏者: