python - 使用 protobuf 序列化图像流

标签 python c++ protocol-buffers

我在 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 字节组成。第一个版本的生成和显示速度可能最快。

关于python - 使用 protobuf 序列化图像流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39244589/

相关文章:

python - scipy中二维数组的最小值计算

python - 使用 pandas 进行时间戳过滤

C++ - 成对显示的整数因子

C++在字符串中查找重复的子字符串

python - 如何在 macOS 上创建和存储 matplotlib 样式文件?

python - isinstance(foo,bar) vs type(foo) is bar

c++ - 编译程序时的错误信息

c++ - protobuf嵌入式消息最佳实践

c++ - 在 protobuf 列表的开头插入一个项目

c++ - Protocol Buffer 在解析时是否重用字符串指针?