Skip to content

Latest commit

 

History

History
102 lines (81 loc) · 3.96 KB

api-examples.rst

File metadata and controls

102 lines (81 loc) · 3.96 KB

API and Usage Examples

The API for imagezmq consists of 2 classes with 2 methods each. The ImageSender class has 2 methods: one for sending an OpenCV image and one for sending a jpg compressed OpenCV image. The ImageHub class has 2 methods: one for receiving an OpenCV image and one for receiving a jpg compressed OpenCV image. 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.

 1 class ImageSender(connect_to='tcp://127.0.0.1:5555'):
 2     Opens a zmq REQ socket on the image sending computer, typically a
 3     Raspberry Pi, that will be sending OpenCV images and
 4     related text messages to the hub computer. Provides methods to
 5     send images or send jpg compressed images.
 6 
 7     Arguments:
 8       connect_to: the tcp address and port of the hub computer
 9            Example format: connect_to='tcp://192.168.1.17:5555'
10            Example format: connect_to='tcp://jeff-macbook:5555'
11 
12     send_image(self, msg, image):
13         Sends OpenCV image and msg to hub computer.
14 
15         Arguments:
16           msg: text message or image name.
17           image: OpenCV image to send to hub.
18         Returns:
19           A text reply from hub.
20 
21     send_jpg(self, msg, jpg_buffer):
22         Sends msg text and jpg buffer to hub computer.
23 
24         Arguments:
25           msg: image name or message text.
26           jpg_buffer: bytestring containing the jpg image to send to hub.
27         Returns:
28           A text reply from hub.
29 
30 class ImageHub(open_port='tcp://:5555'):
31     Opens a zmq REP socket on the hub computer, for example,
32     a Mac, that will be receiving and displaying or processing OpenCV images
33     and related text messages. Provides methods to receive images or receive
34     jpg compressed images.
35 
36     Arguments:
37       open_port: (optional) the socket to open for receiving REQ requests.
38 
39     recv_image(self, copy=False):
40         Receives OpenCV image and text msg.
41 
42         Arguments:
43           copy: (optional) zmq copy flag.
44         Returns:
45           msg: text msg, often the image name.
46           image: OpenCV image.
47 
48     recv_jpg(self, copy=False):
49         Receives text msg, jpg buffer.
50 
51         Arguments:
52           copy: (optional) zmq copy flag
53         Returns:
54           msg: text message, often image name
55           jpg_buffer: bytestring jpg compressed image
56 
57     send_reply(self, reply_message=b'OK'):
58         Sends the zmq REP reply message.
59 
60         Arguments:
61           reply_message: reply message text, often just the string 'OK'

Usage Examples

While additional programs using imagezmq are being developed, the programs mentioned below show how to use the API. The programs are found in the tests folder.

The programs timing_send_images.py and timing_receive_images.py provide examples of how to use the imagezmq API to send and receive OpenCV images. The programs show a simple imagezmq use case. Additional image processing in the sending program would typically be placed between the picam.read() and the sender.send_image() lines. Such processing would be done with calls to methods for image rotation, resizing, dilation, etc. from an application specific image processing class.

The programs timing_send_jpg_buf and timing_receive_jpg_buf show how imagezmq would be used to send jpg compressed OpenCV images to reduce network load. The current API requires that the conversion from OpenCV image format to a jpg bytestring be done by the application program. This will likely change in the future. The 2 example programs show how to perform the conversion using OpenCV's cv2.imencode() and cv2.imdecode() methods.

Return to main documentation page README.rst