Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 926
Dong Li Avatar asked Aug 31 '16 08:08

Dong Li


People also ask

Does Protobuf handle endianness?

Protocol buffers messages always use little-endian encoding.

How do you serialize an object in Protobuf?

Protobuf is create for serialize purpose so I recommend you no need to serialize it again to a Serializable or Parcelable object. Instead of, serialize it to byte array and put byte array to to bundle.

Is Protobuf faster than JSON?

In one – protobufjs was faster, and in the second — JSON was faster. Looking at the schemas, the immediate suspect was the number of strings. We ran the benchmark with this payload (10,000 strings, of length 10 each).

How do I send Protobuf over HTTP?

You can certainly send even a binary payload with an HTTP request, or in an HTTP response. Just write the bytes of the protocol buffer directly into the request/response, and make sure to set the content type to "application/octet-stream". The client, and server, should be able to take care of the rest easily.


1 Answers

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.

like image 89
Vatine Avatar answered Sep 17 '22 13:09

Vatine