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

标签 python opencv web websocket base64

我想使用 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/

相关文章:

python - 架构 python 问题

python - 选择 Pandas 多索引/多列 DataFrame 的列表切片

android - 使用 cv::Mat.submat opencv 时出错

c++ - 在 Mac OSX 上从 Python 脚本调用 C++ 函数

html - 如何将溢出设置为隐藏以保护元素正确超出页脚?

javascript - DOM字符串解析器

python - Python 中用于偏差校正的直方图匹配

python - 如何使用 python、pyparsing 按顺序解析文件中的行/匹配足够的结果?

c++ - 如何使用 cv::findcontours 和层次结构查找内孔数

html - 为什么有些浏览器使用错误的字体(显然是随机的)呈现这个 unicode 字符?