簡體   English   中英

"如何將base64圖像發送到gRPC tf服務服務器而不是http請求?"

[英]How to send base64 image to gRPC tf serving server instead of http request?

當我在 tf-serving 中提供對象檢測模型並通過 http 請求訪問它時,我有一個工作代碼<\/strong>。 相同的代碼是:

import requests
import base64
import json

URL = "http://10.10.1.38:8501/v1/models/test_model:predict" 
headers = {"content-type": "application/json"}
image = "test111.jpg"
image_content = base64.b64encode(open(image,'rb').read())
encodedStr = str(image_content, "utf-8")
body = {"signature_name": "predict_images","inputs": [{"b64":encodedStr}]}
response = requests.post(URL, data=json.dumps(body), headers = headers) 

如果有人仍在尋找答案,您只需將圖像字節對象作為列表傳遞給 dtype=tf.string 的 tf.make_tensor_proto。

以下代碼應該可以工作:

import grpc
from tensorflow_serving.apis import predict_pb2
from tensorflow_serving.apis import prediction_service_pb2_grpc

image = "test_img.jpg"
image_bytes = open(image,'rb').read()

server = 'localhost:8500'
channel = grpc.insecure_channel(server)
stub = prediction_service_pb2_grpc.PredictionServiceStub(channel)
request = predict_pb2.PredictRequest()
request.model_spec.name = 'default'
request.model_spec.signature_name = 'serving_default'
request.inputs['bytes_inputs'].CopyFrom(tf.make_tensor_proto([image_bytes], dtype=tf.string))
result_future = stub.Predict(request)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM