【问题标题】:How do I send an Image rotate request to gprc server with python?如何使用 python 向 grpc 服务器发送图像旋转请求?
【发布时间】:2021-04-14 15:24:48
【问题描述】:

我是 grpc 的新手。我很难理解它。无论如何,我正在基于一个名为的 protobuf 文件工作:image.proto

以下是我的 image.proto 文件的内容:

syntax = "proto3";

option java_multiple_files = true;

message NLImage {
    bool color = 1;
    bytes data = 2;
    int32 width = 3;
    int32 height = 4;
}


message NLImageRotateRequest {
    enum Rotation {
        NONE = 0;
        NINETY_DEG = 1;
        ONE_EIGHTY_DEG = 2;
        TWO_SEVENTY_DEG = 3;
    }

    Rotation rotation = 1;
    NLImage image = 2;
}

service NLImageService {
    rpc RotateImage(NLImageRotateRequest) returns (NLImage);
    rpc MeanFilter(NLImage) returns (NLImage);
}

我能够创建 server.py 文件和 client.py。我还从 image.proto 文件生成了 image_pb2.py,并生成了 image_pb2_grpc.py。

现在,我被困在从客户端向服务器发送图像旋转请求,并得到适当的响应。

这是我迄今为止在 client.py 中尝试过的内容

import grpc
# import the generated files
import image_pb2
import image_pb2_grpc

#.......
#.......SEVERAL LINES OF CODE LATER
#.......

print('Input image path is', inputfile)
print('Output path is', outputfile)
print('Mean  is', mean)
print("Rotate is", rotate)
channel = grpc.insecure_channel('localhost:5000')
stub = image_pb2_grpc.NLImageServiceStub(channel)


# SEND ROTATION REQUEST IF rotate
img_rotate_request = image_pb2.NLImageRotateRequest()
stub.RotateImage(img_rotate_request, inputfile)

我不确定如何正确发送 RotateImage 请求。

下面是我的 server.py:

import sys, getopt
from google.protobuf.descriptor import EnumDescriptor
import grpc
from concurrent import futures
import time
import numpy as np

# importing grpc generated classes
import image_pb2
import image_pb2_grpc

class NLImageServiceServicer(image_pb2_grpc.NLImageServiceServicer):
   def RotateImage(self, request, context):
      print("Request to rotate image received")
      return image_pb2._NLIMAGE()

   def MeanFilter(self, request, context):
      print("Request to filter received")
      return image_pb2.NLImage()

   server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
   image_pb2_grpc.add_NLImageServiceServicer_to_server(NLImageServiceServicer, server)
   server.add_insecure_port( host + ":" + str(port))
   server.start()
   print("Server started ...on " + host + ":" + port)
   server.wait_for_termination()

【问题讨论】:

    标签: python protocol-buffers grpc rpc


    【解决方案1】:

    观察您的代码,它看起来不错;我没有运行它。

    您正确地调用了该方法:stub.RotateImage,但您没有正确地为其创建消息类型 (NLImageRotateRequest)。它将是protoc 为您生成的类的一个实例,可能是image_pb2.NLImageRotateRequest,其中包含rotationimage 属性。 image 本身就是 image_pb2.Image 等类的实例。

    请参阅link 了解如何将 protobuf 与 Python 结合使用,因为每种语言都是不同的,而且 Python 在创建消息的方式上存在一些怪癖。

    在某些时候,您需要以字节形式读取 inputfile 以填充 imagedata 属性。

    如果你还没有,也许可以通过 grpc.io 上的 Python HelloWorld 示例运行。这是基本的,但它会让你开始使用 gRPC。然后查看我上面包含的链接和 Python 上的 protobuf 部分。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-26
      • 1970-01-01
      • 2018-07-02
      • 2018-08-28
      • 1970-01-01
      • 1970-01-01
      • 2021-02-09
      • 1970-01-01
      相关资源
      最近更新 更多