如何通过ZeroMQ套接字发送OpenCV视频素材? [英] How to send OpenCV video footage over ZeroMQ sockets?

查看:259
本文介绍了如何通过ZeroMQ套接字发送OpenCV视频素材?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的网络摄像头,我使用OpenCV读取了该摄像头,现在我正尝试使用 ZeroMQ .因此,我有以下简单的脚本来读取网络摄像头并使用ZeroMQ套接字将其发送:

I've got a simple webcam which I read out using OpenCV and I'm now trying to send this video footage to a different (Python) program using ZeroMQ. So I've got the following simple script to read out the webcam and send it using a ZeroMQ socket:

import cv2
import os
import zmq
import base64

context = zmq.Context()
footage_socket = context.socket(zmq.PUB)
footage_socket.connect('tcp://localhost:5555')

# init the camera
camera = cv2.VideoCapture(0)

while True:
    try:
        (grabbed, frame) = camera.read()            # grab the current frame
        frame = cv2.resize(frame, (640, 480))       # resize the frame
        footage_socket.send_string(base64.b64encode(frame))

        # Show the video in a window
        cv2.imshow("Frame", frame)                  # show the frame to our screen
        cv2.waitKey(1)                              # Display it at least one ms
        #                                           # before going to the next frame

    except KeyboardInterrupt:
        camera.release()
        cv2.destroyAllWindows()
        print "\n\nBye bye\n"
        break

效果很好,因为它可以显示视频并且不会出现任何错误.

This works well in that it shows the video and doesn't give any errors.

我注释掉了显示图像的两行(cv2.imshow()cv2.waitKey(1)).然后,我以并行方式启动下面的脚本.第二个脚本应该接收视频片段并显示它.

I commented out the two lines which show the image (cv2.imshow() and cv2.waitKey(1)). I then started the script below in paralel. This second script should receive the video footage and show it.

import cv2
import zmq
import base64
import numpy as np

context = zmq.Context()
footage_socket = context.socket(zmq.SUB)
footage_socket.bind('tcp://*:5555')
footage_socket.setsockopt_string(zmq.SUBSCRIBE, unicode(''))

# camera = cv2.VideoCapture("output.avi")

while True:
    try:
        frame = footage_socket.recv_string()
        frame = np.fromstring(base64.b64decode(frame), dtype=np.uint8)
        cv2.imshow("Frame", frame)                  # show the frame to our screen
        cv2.waitKey(1)                              # Display it at least one ms
        #                                           # before going to the next frame
    except KeyboardInterrupt:
        cv2.destroyAllWindows()
        break

print "\n\nBye bye\n"

不幸的是,这冻结在cv2.waitKey(1)上.

Unfortunately, this freezes on cv2.waitKey(1).

有人知道我在做什么错吗?我需要对素材进行不同的解码吗?欢迎所有提示!

Does anybody know what I'm doing wrong here? Do I need to decode the footage differently? All tips are welcome!

推荐答案

最后,我通过采取中间步骤解决了该问题.我首先将单个图像写入磁盘,然后再次读出这些图像.这导致我需要将帧编码为图像(我选择了jpg),并使用魔术方法cv2.imencode('.jpg', frame)cv2.imdecode(npimg, 1)可以使它工作.我在下面粘贴了完整的工作代码.

In the end I solved the problem by taking intermediate steps. I first wrote individual images to disk, and then I read out those images again. That led me to the fact that I needed to encode the frame as an image (I opted for jpg), and with the magic methods cv2.imencode('.jpg', frame) and cv2.imdecode(npimg, 1) I could make it work. I pasted the full working code below.

第一个脚本读取网络摄像头,并通过Zeromq套接字发送素材:

This first script reads out the webcam and sends the footage over a zeromq socket:

import cv2
import zmq
import base64

context = zmq.Context()
footage_socket = context.socket(zmq.PUB)
footage_socket.connect('tcp://localhost:5555')

camera = cv2.VideoCapture(0)  # init the camera

while True:
    try:
        (grabbed, frame) = camera.read()  # grab the current frame
        frame = cv2.resize(frame, (640, 480))  # resize the frame
        encoded, buffer = cv2.imencode('.jpg', frame)
        footage_socket.send_string(base64.b64encode(buffer))

    except KeyboardInterrupt:
        camera.release()
        cv2.destroyAllWindows()
        print "\n\nBye bye\n"
        break

第二个脚本接收帧图像并显示它们:

and this second script receives the frame images and displays them:

import cv2
import zmq
import base64
import numpy as np

context = zmq.Context()
footage_socket = context.socket(zmq.SUB)
footage_socket.bind('tcp://*:5555')
footage_socket.setsockopt_string(zmq.SUBSCRIBE, unicode(''))

while True:
    try:
        frame = footage_socket.recv_string()
        img = base64.b64decode(frame)
        npimg = np.fromstring(img, dtype=np.uint8)
        source = cv2.imdecode(npimg, 1)
        cv2.imshow("image", source)
        cv2.waitKey(1)

    except KeyboardInterrupt:
        cv2.destroyAllWindows()
        print "\n\nBye bye\n"
        break

无论如何,我希望你有个美好的一天!

In any case I wish you a beautiful day!

这篇关于如何通过ZeroMQ套接字发送OpenCV视频素材?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆