gRPC如何使⽤python表⽰多维数组
在使⽤gRPC作为远程调⽤框架时,如何使⽤python来表⽰多维数组呢?gRPC中定义proto⽂件时,有⼀个参数是repeated,⽤来表⽰重复的数据类型,使⽤这个参数可以表⽰list类型。如下,我想表⽰⼀个三维数组,这个数组表⽰的是⼀个三通道RGB彩⾊图像,proto⽂件如下所⽰,proto⽂件中GrayFeature表⽰的是⼀个通道的灰度图像。
syntax = "proto3";
service DataListServer {
rpc TransDataList(stream GrayFeature) returns (Reply) {}
}
message GrayFeature {
repeated Feature feature = 1;
}
message Feature {
repeated int32 pixel = 2;
}
对应的Python代码如下:
data_list = list()
python 定义数组lenth = len(data_list)
for j in range(0, lenth):
matrix = data_list[j]
gray_feature = data_list_pb2.GrayFeature()
for column in range(0, len(matrix)):
feature = gray_feature.feature.add()
d(matrix[column])
yield gray_feature
在服务端对应的解析的Python代码如下:
data_list = [[[]]] * 3
for gray_feature in features:
j = 0
feature = gray_feature.feature
length = len(feature)
matrix = [[]] * length
for i in range(0, length):
matrix[i] = feature[i].pixel
data_list[j] = matrix
j += 1
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论