如何使用 ZeroMQ 获取 opencv 帧?

How to fetch opencv frames with ZeroMQ?

我正在使用以下代码从客户端获取帧:

def fetch_frame(self):

    context = zmq.Context()
    footage_socket = context.socket(zmq.REP)
    footage_socket.bind('tcp://*:5555')

    while True:
        frame = footage_socket.recv_string()

        frame = frame.encode()

        img = base64.b64decode(frame)

        npimg = np.fromstring(img, dtype=np.uint8)
        source = cv2.imdecode(npimg, 1)

不幸的是,它抛出以下错误:

zmq.error.ZMQError: Operation cannot be accomplished in current state

使用 zmq 检索图像的正确方法是什么?

更新:

回溯如下:

 File "/home/receive/main.py", line 77, in <module>
    main()
  File "/home/receive/main.py", line 73, in main
    ci.fetch_frame()
  File "/home/receive/main.py", line 59, in fetch_frame
    frame = footage_socket.recv_string()
  File "/home/.local/lib/python3.8/site-packages/zmq/sugar/socket.py", line 608, in recv_string
    msg = self.recv(flags=flags)
  File "zmq/backend/cython/socket.pyx", line 791, in zmq.backend.cython.socket.Socket.recv
  File "zmq/backend/cython/socket.pyx", line 827, in zmq.backend.cython.socket.Socket.recv
  File "zmq/backend/cython/socket.pyx", line 191, in zmq.backend.cython.socket._recv_copy
  File "zmq/backend/cython/socket.pyx", line 186, in zmq.backend.cython.socket._recv_copy
  File "zmq/backend/cython/checkrc.pxd", line 26, in zmq.backend.cython.checkrc._check_rc
zmq.error.ZMQError: Operation cannot be accomplished in current state

Q : "ZMQError: Operation cannot be accomplished in current state"

核心原因是,您的代码违反了 ZeroMQ REQ/REP 记录的属性。

REQ-实例已经首先“询问”其交易对手之前,向 REP-实例询问 .send()-ed 消息, API 必须抛出这样的错误。

类似地,如果试图 .recv() REQ 端的消息,在它首先被 .send() REP 端“询问”之前(回复),API 必须抛出这样的错误。


解决方案?

在不需要其他交互的情况下,PUSH/PULL 可扩展的正式通信模式原型,其中 PUSH 端将 .send()PULL 端将.recv(),恕我直言,这是最好的方法。


您可能想在 this.

中阅读更多关于此的提示和链接