我正在为一个计算机视觉项目使用Kafka pipeline。我必须将JSON对象摄取到管道中,并在使用者端接收它们。我是这样做的:
>>> a = some_image_numpy_array
>>> b = base64.b64encode(a)
>>> c = b.decode('utf-8')如何在消费者端再次解码成NumPy数组格式?我不能将图像作为字节转储到JSON中。我必须使用utf解码或str()将其转换为字符串。
注意:我不能简单地进行base64编码并将其转储到json中,因为字节不是json可序列化的。
发布于 2021-07-06 18:21:18
您可以使用此base64.b64decode
>>> a = some_image_numpy_array
>>> b = base64.b64encode(a)
>>> c = base64.b64decode(b)发布于 2021-07-06 18:24:20
%python
import base64
encoded = base64.b64encode(b'a0JD0000006SxKJMA0')
print(base64.b64decode(encoded))将为您提供解码字符串
b'a0JD0000006SxKJMA0'发布于 2022-02-22 06:08:31
你可以这样做:
# convert image to string
image_to_kafka = base64.b64encode(cv2.imencode('.jpg', img)[1]).decode()
# put string image into dictionary
some_dict = {
'some_id': 123,
'image': image_to_kafka
}
# send using your producer
producer.send('some_topic', value=some_dict)在我的例子中,producer是这样创建的:
producer = KafkaProducer(bootstrap_servers=[kafka_server],
value_serializer=lambda x:json.dumps(x).encode('utf-8'))但你必须考虑到这一点:
IMHO,更好的选择是使用一些存储,在那里您保存图像,并只发送ID和/或路径到图像。
https://stackoverflow.com/questions/68268674
复制相似问题