Dong Li
Dong Li

Reputation: 558

Serializing image stream using protobuf

I have two programs in Ubuntu: a C++ program (TORCS game) and a python program. The C++ program always generates images. I want to transfer these real-time images into python(maybe the numpy.ndarray format). So I think that maybe using Google protobuf to serialize the image to string and send string to python client by ZMQ is a feasible method.

Question: which value type is suitable for the image(a pointer) in .proto file? In another words, which value type I should use to replace string type in the below example?

message my_image{
     repeated string image = 1
     }

This is the way I write image to memory (uint8_t* image_data):

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

At last, maybe there is a better way to transfer image (in the memory) to a python client?

Any suggestions are appreciated.

Upvotes: 15

Views: 18133

Answers (2)

tom
tom

Reputation: 1835

According to this blog post : https://medium.com/@brynmathias/kafka-and-google-protobuf-a-match-made-in-python-a1bc3381da1a

you could use the following schema :

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;

}

Upvotes: 1

Vatine
Vatine

Reputation: 21248

If I had to do this, I would use one of:

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;
}

Or possibly use an intermediate ScanRow message, composed either of interleaved R, G, B bytes or separated R, G, B bytes. The first version is likely going to be fastest to generate and display.

Upvotes: 11

Related Questions