gpt4 book ai didi

python - 使用 WebSocket 以字节形式发送带有图像的 JSON

转载 作者:太空宇宙 更新时间:2023-11-03 22:44:11 26 4
gpt4 key购买 nike

我想使用 WebSocket 从 cv2.Videocapture 发送和接收图像。

可以获取json,但无法解码。

我们需要可以使用 cv2.imshow() 打开的结果。

有人帮我...

这是客户

ret, image_np = cap.read()
IMAGE_SHAPE = image_np.shape
encoded_image = base64.b64encode(image_np)
print(type(encoded_image))
payload = {
'from': 'rasp',
'image': str(encoded_image),
'shape': IMAGE_SHAPE,
}

data = json.dumps(payload)

try:
# Send encoded image data.
await websocket.send(data)

# receive server message
received_data = await websocket.recv()
print('< {}'.format(received_data))

# image = base64.b64decode(received_data)
# np_image = np.fromstring(image, dtype=np.uint8)
# source = np_image.reshape(IMAGE_SHAPE)
return websocket
except Exception:
print('WebSocket send or receive error.')
exit(1)

这是服务器

async def server_on(websocket, path):
payload = {
'from': 'image-server',
# 'result': {
# # Default is null.
# 'isPerson': <bool>,
# 'centerPoint': <(x, y)>,
# },
}

data = json.dumps(payload)

try:
payload = await websocket.recv()
receive_data = json.loads(payload)
# At this line doesnt work...
decoded_image = base64.b64decode(receive_data['image'])
image_np = np.fromstring(decoded_image, dtype=np.uint8)
source = image_np.reshape(receive_data['shape'])


await websocket.send(data)
except Exception:
websocket.close()
return

最佳答案

在您的客户端 我会说您不需要额外的操作。根据您的最新评论,您可能不需要使用:str(encoded_image)

您可以尝试使用:base64.encodestring(image_np) 它将向您发送一个字符串容器。

ret, image_np = cap.read()
IMAGE_SHAPE = image_np.shape
encoded_image = base64.encodestring(image_np)
print(type(encoded_image))
payload = {
'from': 'rasp',
'image': encoded_image.decode('utf-8'),
'shape': IMAGE_SHAPE,
}

关于python - 使用 WebSocket 以字节形式发送带有图像的 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50266553/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com