首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用python向gprc服务器发送图像旋转请求?

如何使用python向gprc服务器发送图像旋转请求?
EN

Stack Overflow用户
提问于 2021-01-08 22:17:53
回答 1查看 499关注 0票数 2

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

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

代码语言:javascript
复制
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_pb2.py文件中生成了image.proto,并生成了image_pb2_grpc.py。

现在,我不得不将图像旋转请求从客户机发送到服务器,并得到适当的响应。

以下是我到目前为止在我的client.py中尝试过的东西

代码语言:javascript
复制
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:

代码语言:javascript
复制
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()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-10 20:12:15

看着你的代码,它看起来还好;我还没有运行它。

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

请查看这个链接与Python一起使用protobuf,因为每种语言都是不同的,而且Python在创建消息方面有一些古怪之处。

在某种程度上,您需要以字节形式读取inputfile,以填充imagedata属性。

如果没有,可以在grpc.io上运行Python示例。它是基本的,但它将使您开始使用gRPC。然后回顾我上面包含的链接和Python的protobuf部分。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65637153

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档