gRPC -- 在 Protobuf 中将 .png 图像从 Java 客户端发送到 Python 服务器

gRPC -- Send .png image from Java client to Python server in Protobuf

我正在尝试使用 gRPC 和 ProtoBuf 将图像从 Java 客户端发送到 Python 服务器。我将图像编码为 ByteString 并使用 blockingStub 发送它。 Python 服务器接收 ProtoBuf,但 Java ByteString 现在是 Python str。我不确定如何从 str.

恢复图像

当我调用服务器时:

request.ParseFromString(request.png_encoded)  # Throws error

它提高了:

Traceback (most recent call last):

File "/usr/local/lib/python2.7/dist-packages/grpc/_server.py", line 364, in _call_behavior

return behavior(argument, context), True

File "process_server.py", line 57, in Process request.ParseFromString(request.png_encoded)

File "/usr/local/lib/python2.7/dist-packages/google/protobuf/message.py", line 185, in ParseFromString self.MergeFromString(serialized)

File "/usr/local/lib/python2.7/dist-packages/google/protobuf/internal/python_message.py", line 1082, in MergeFromString if self._InternalParse(serialized, 0, length) != length:

File "/usr/local/lib/python2.7/dist-packages/google/protobuf/internal/python_message.py", line 1108, in InternalParse new_pos = local_SkipField(buffer, new_pos, end, tag_bytes)

File "/usr/local/lib/python2.7/dist-packages/google/protobuf/internal/decoder.py", line 850, in SkipField return WIRETYPE_TO_SKIPPER[wire_type](buffer, pos, end)

File "/usr/local/lib/python2.7/dist-packages/google/protobuf/internal/decoder.py", line 820, in _RaiseInvalidWireType

raise _DecodeError('Tag had invalid wire type.')

DecodeError: Tag had invalid wire type.

Java 客户:

public void run_process(String imPath) {
    logger.info("Will try to retrieve boxes with image " + imPath + " ...");
    File path = new File(imPath);
    byte[] imageInByte = null;
    ByteString byteIm = null;
    try {
        BufferedImage bufferedIm = null;
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        bufferedIm = ImageIO.read(path);
        ImageIO.write(bufferedIm, "png", out);
        out.flush();
        imageInByte = out.toByteArray();
        out.close();
        ByteBuffer buf = ByteBuffer.wrap(imageInByte);
        byteIm = ByteString.copyFrom(buf);
    } catch (IOException e) {
        e.printStackTrace();
    }

    private final ManagedChannel channel;
    channel = ManagedChannelBuilder.forAddress("localhost", 50051).usePlaintext(true).build();
    private final ProcessServiceGrpc.ProcessServiceBlockingStub blockingStub;
    blockingStub = ProcessServiceGrpc.newBlockingStub(channel);

    ProcessRequest request = ProcessRequest.newBuilder().setPngEncoded(byteIm).build();
    ProcessResponse response;
    try {
        response = blockingStub.process(request);
    } catch (StatusRuntimeException e) {
        logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus());
        return;
    }
    logger.info("Boxes retrieved: " + response.toString());
}

Python 服务器:

def Process(self, request, context):
    print(type(request.png_encoded))  # prints <type 'str'>
    request.ParseFromString(request.png_encoded)  # Throws error

    # Return empty response for now
    process_response = inference_pb2.ProcessResponse()
    return process_response

def serve():
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
    inference_pb2.add_ProcessServiceServicer_to_server(
        ProcessServiceServicer(), server)
    print "Starting Process Server"
    server.add_insecure_port('localhost:50051')
    server.start()
    try:
        while True:
            time.sleep(_ONE_DAY_IN_SECONDS)
    except KeyboardInterrupt:
        server.stop(0)

if __name__ == '__main__':
    serve()

我已经尝试 google "Decode Java Protobuf in Python" 和 "decode java protobuf Bytestring in python" 但没有成功。提前致谢!

ByteString in Java 只是一个不可变的 byte[]。 Python 2 等价于 str。它是 binary 表示。如果您想将图像保存到磁盘,您可以:

with open('out.png', 'wb') as file:
    file.write(request.png_encoded)