[gRPC python发送图像+元数据

问题描述 投票:0回答:1

我需要将图像(numpy数组),有关图像的其他元信息(例如高度,宽度等)从python grpc客户端传递到python grpc服务器。

我需要运行此方法。

import numpy as np 

def predict(img, w, h):
    # some operations
    return img.shape[2], np.mean(img)

我查看了文档,但是protobuf中没有numpy数组的兼容数据类型。

https://developers.google.com/protocol-buffers/docs/proto3

image_procedure.proto

syntax = "proto3";

// input image, width, height
message Image {
    image_type image = 1;
    int32 width = 2;
    int32 height = 3;
}

// output prediction

message Prediction {
    int32 channel = 4;
    float mean = 5;
}

// service
service ImageProcedure {
    rpc ImageMeanWH(Image) returns (Prediction) {}
}

如何将图像和其他相关数据发送到服务器并获得响应?

python grpc
1个回答
0
投票

您总是可以将numpy数组编码为base64字符串,并将其传递给服务器。

您的.proto文件应如下所示:

syntax = "proto3";

// input image, width, height
message B64Image {
    string b64image = 1;
    int32 width = 2;
    int32 height = 3;
}

// output prediction

message Prediction {
    int32 channel = 4;
    float mean = 5;
}

// service
service ImageProcedure {
    rpc ImageMeanWH(B64Image) returns (Prediction) {}
}
© www.soinside.com 2019 - 2024. All rights reserved.