如何解决使用python通过ZMQ pubsub发送视频?
我想使用 Python 和 ZMQ 创建一个简单的客户端/服务器设置。 客户端必须从我的笔记本电脑网络摄像头向显示视频的服务器发送实时视频。
我遇到了一些“答案”,但除了以 base64 格式发送图像之外,我无法让它们中的任何一个工作。我不想作为 base64 发送 - 它很慢,有很多开销。我认为发送图像字节数据更有意义。
这是我的客户:
import base64
import zmq
import time
import cv2
from imutils.video import VideoStream
ctx = zmq.Context()
send_sock = ctx.socket(zmq.PUB)
send_sock.bind("tcp://*:1235")
vs = VideoStream(src=0,resolution=(640,480)).start()
time.sleep(2.0) # allow camera sensor to warm up
while True: # send images as stream until Ctrl-C
frame = vs.read()
encoded,buf = cv2.imencode('.jpg',frame)
image = base64.b64encode(buf)
send_sock.send(image)
# detect any kepresses
key = cv2.waitKey(1) & 0xFF
# if the `q` key was pressed,break from the loop
if key == ord("q"):
break
send_sock.close()
ctx.term()
这是我的服务器:
import zmq
import time
import base64
import numpy as np
import cv2
ctx = zmq.Context()
rcv_sock = ctx.socket(zmq.SUB)
rcv_sock.connect("tcp://127.0.0.1:1235")
rcv_sock.subscribe("")
while True:
image_string = rcv_sock.recv_string()
raw_image = base64.b64decode(image_string)
image = np.frombuffer(raw_image,dtype=np.uint8)
frame = cv2.imdecode(image,1)
cv2.imshow("frame",frame)
time.sleep(0.1)
# detect any kepresses
key = cv2.waitKey(1) & 0xFF
# if the `q` key was pressed,break from the loop
if key == ord("q"):
break
rcv_sock.close()
ctx.term()
cv2.destroyAllWindows()
上面使用 Base64 的代码确实有效。
如何在没有 Base64 的情况下执行相同操作(我正在寻找更快的解决方案)。
解决方法
答案很简单,就是去掉base64编码...
客户:
frame = vs.read()
encoded,buf = cv2.imencode('.jpg',frame)
send_sock.send(buf)
服务器:
raw_image = rcv_sock.recv()
image = np.frombuffer(raw_image,dtype=np.uint8)
frame = cv2.imdecode(image,1)
cv2.imshow("frame",frame)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。