使用protobuf序列化图像流

Serializing image stream using protobuf

本文关键字:图像 序列化 protobuf 使用      更新时间:2023-10-16

我在Ubuntu中有两个程序:一个c++程序(TORCS游戏)和一个python程序。c++程序总是生成图像。我想把这些实时图像转换成python(也许是numpy。ndarray格式)。因此,我认为使用Google protobuf将图像序列化为字符串,并通过ZMQ将字符串发送给python客户端是一种可行的方法。

问题:哪个值类型适合.proto文件中的图像(指针)?换句话说,在下面的例子中,我应该使用哪种值类型来替换string类型?

message my_image{
     repeated string image = 1
     }

这是我写图像到内存的方式(uint8_t* image_data):

glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, (GLvoid*)image_data);

最后,也许有一个更好的方法来传输图像(在内存中)到python客户端?

如果我必须这样做,我会使用:

message image {
    int width = 1;
    int height = 2;
    bytes image_data = 3;
}
message image {
    int width = 1;
    int height = 2;
    bytes red_data = 3;
    bytes green_data = 4;
    bytes blue_data = 5;
}

或者可能使用中间的ScanRow消息,由交错的R, G, B字节或分隔的R, G, B字节组成。第一个版本可能是最快的生成和显示。

根据这篇博文:https://medium.com/@brynmathias/kafka-and-google-protobuf-a-match-made-in-python-a1bc3381da1a

可以使用以下模式:

syntax = 'proto3';
package protobuf.data;
import "protobuf/common/messageData.proto";
message Image {
    bytes image_data = 1;
    int32 height = 2;
    int32 width = 3;
    int64 frame = 4;
    protobuf.common.messageData _message_data = 5;
}