Imagezmq 过滤器以 REQ_REP 模式接收有关文本消息的数据

Imagezmq filter received data on text message in REQ_REP pattern

我想根据附加的短信过滤掉图像数据。

集线器代码

import imagezmq
import cv2

hub = imagezmq.ImageHub()
print('Listening')
while True:
    rpi_name, image = hub.recv_jpg()
    print('Received from ' + rpi_name)
    hub.send_reply(b'OK')

服务器 1

import imagezmq
import cv2
import simplejpeg

hub = imagezmq.ImageSender()
feed = cv2.VideoCapture(url)
while True:
    ret, frame = feed.read()
    image = simplejpeg.encode_jpeg(frame, quality=60, colorspace='BGR')
    hub.send_jpg('1',image)

服务器 2

import imagezmq
import cv2
import simplejpeg

hub = imagezmq.ImageSender()
feed = cv2.VideoCapture(url)
while True:
    ret, frame = feed.read()
    image = simplejpeg.encode_jpeg(frame, quality=60, colorspace='BGR')
    hub.send_jpg('2',image)

在集线器中,我同时从两个服务器接收数据。我想要的是过滤掉数据。就像我只想接收有消息的数据(这是用来区分服务器) 1 only.

或者如果集线器从许多服务器接收数据,从服务器获取数据的最快方法是什么。

对于收到的每一对(文本、图像),rpi_name中包含的短信 包含“1”或“2”,具体取决于发送服务器。要通过发送服务器过滤图像,您需要 使用文本根据每条消息的文本部分区分要执行的操作。

这是一个如何做到这一点的例子。我添加了一个示例函数 do_something() 使用 if 语句根据情况采取不同的操作 图片来源

集线器代码(添加了根据发送服务器采取不同操作的功能)

# Here is example Hub code showing how to Filter or take other
# action based on the source of rpi_name, image
#
import imagezmq
import cv2

def do_something(rpi_name, image):
    # parameter rpi_name contains the "text message" which differs by server
    # parameter image contains the image that arrived from that server
    if rpi_name == '1':
        pass  # or do something like save the image or process it somehow
    elif rpi_name == '2':
        pass  # or do something like save the image or process it somehow
    else:
        pass  # what do you want to do if server is not one of the 2 above?

hub = imagezmq.ImageHub()
print('Listening')
while True:
    rpi_name, image = hub.recv_jpg()
    print('Received from ' + rpi_name)
    do_something(rpi_name, image)
    hub.send_reply(b'OK')

仅供参考,我是 imageZMQ 的作者。