我有一个在Python上运行的Keras模型,我想从Unity 3D摄像机向该模型发送帧。我可以通过使用外部库轻松地在它们之间传输字符串。该外部库将字节数组发送给Python。因此,我将Unity的相机帧转换为字节数组。但是,我不知道如何在Python上读取发送的图像(字节数组)。
为了解决此问题,我仅使用Python尝试了一些发件人-收件人代码。但是,这些无效。
以下是代码:
发件人:
import zmq
import base64
context = zmq.Context()
socket = context.socket(zmq.REP)
socket.bind("tcp://*:5555")
f = open("sample.png", 'rb')
bytes = bytearray(f.read())
strng = base64.b64encode(bytes)
socket.send(strng)
f.close()
接收方:
import zmq
import base64
context = zmq.Context()
socket = context.socket(zmq.REQ)
socket.connect("tcp://localhost:5555")
import base64
message = socket.recv()
f = open("sample.png", 'wb')
ba = bytearray(base64.b64decode(message))
f.write(ba)
f.close()
这是将帧从Unity发送到Python的好方法吗?还是有其他方法?我该如何处理我的问题?上面的代码有什么问题?
问:“这是将帧从Unity发送到Python的好方法吗?”
我敢说。
但是,我不知道如何在Python上读取发送的图像(字节数组)。
为什么有人会使用一种工具“编码”某些东西,
而没有匹配的工具“解码”呢?
问:“或者还有其他方法吗?”
哦,肯定有。
问:“我该如何处理我的问题?”
最好学习在编码和解码阶段都可以安全使用的工具。大量此类可用的Python标准模块-numpy
用于pil/pillow
或cv
用于图像转换,dill
或pickle
用于SER / DES甚至是struct
。
Having used these for ages with ZeroMQ, you may be sure to go in the right direction using safe tools.
Q : "What is wrong with the codes above?"
This will never fly.
The "Sender",
having been proposed as a REP
-instance can never start with a call to the .send()
-method.
Never.
The "Receiver",
having been proposed as a REQ
-instance can never start with a call to the .recv()
-method.
Never.
Sounds complicated? Yes, the choice of REQ/REP
was not mine.
ZeroMQREQ/REP
正式的可扩展通信原型是最复杂的模型之一(操作内部分布式有限状态自动机(dFSA),实际上是1:N,没有风险,但可以肯定地确定任何人都无法自我解锁这些N dFSA的故事结束了...)
不要误会我的意思,我一直以来都非常喜欢ZeroMQ,并且佩服Pieter HINTJENS和Martin SUSTRIK的大量发明和性能提升,他们已经融入ZeroMQ的Zen of Zero(零之禅),但是我尝试警告您花些时间来掌握禅开始。
上面的链接提供了一个完整的解决方案,即使用PUB/SUB
for进行图像发送,以及一些性能调整提示以供进一步考虑。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句