Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

如何修改使http请求能支持传入图片的base64编码? #2476

Open
ignore1999 opened this issue Jun 19, 2024 · 4 comments
Open

如何修改使http请求能支持传入图片的base64编码? #2476

ignore1999 opened this issue Jun 19, 2024 · 4 comments
Assignees

Comments

@ignore1999
Copy link

  • 【FastDeploy版本】: 最新版镜像
  • 【系统平台】: Linux x64(Ubuntu 18.04)
  • 【硬件】: Nvidia GPU 3080TI, CUDA 11.2 CUDNN 8.3
payload = {
  "inputs" : [
    {
      "name" : "INPUT",
      "shape" : image.shape,
      "datatype" : "UINT8",
      "data" : image.tolist()
    }
  ],
  "outputs" : [
    {
      "name" : ""
    }
  ]
}

在http服务中传递list太慢了,如果希望datatype是base64编码,要如何修改代码?

@KyleWang-Hunter
Copy link

哈哈,我之前也提过,让自己改。看你用的是什么模型,以图片分类为例,只需要修改preprocess模型中model.py的execute函数,将读取输入图片的格式修改为base64就可以了,如下所示:

获取输入的base64字符串

input_tensor = pb_utils.get_input_tensor_by_name(request, self.input_names[0])
base64_string = input_tensor.as_numpy()[0][0]

解码 base64 字符串为图像

image_bytes = base64.b64decode(base64_string)
image = Image.open(io.BytesIO(image_bytes)).convert('RGB')
data = np.array(image)
outputs = self.preprocess_.run([data, ])

@ignore1999
Copy link
Author

哈哈,我之前也提过,让自己改。看你用的是什么模型,以图片分类为例,只需要修改preprocess模型中model.py的execute函数,将读取输入图片的格式修改为base64就可以了,如下所示:

获取输入的base64字符串

input_tensor = pb_utils.get_input_tensor_by_name(request, self.input_names[0]) base64_string = input_tensor.as_numpy()[0][0]

解码 base64 字符串为图像

image_bytes = base64.b64decode(base64_string) image = Image.open(io.BytesIO(image_bytes)).convert('RGB') data = np.array(image) outputs = self.preprocess_.run([data, ])

OK非常感谢,我试下

@ignore1999
Copy link
Author

@KyleWang-Hunter 我修改代码后出现'{"error":"Unable to parse \'data\': attempt to access JSON non-unsigned-integer as unsigned-integer"}'的问题,请问是否要修改"datatype" : "UINT8"或配置文件config.pbtxt的内容?我的json请求体如下:

payload = {
  "inputs" : [
    {
      "name" : "INPUT",
      "shape" : image.shape,
      "datatype" : "UINT8",
      "data" : [base64_string] # image.tolist() # 
    }
  ],
  "outputs" : [
    {
      "name" : "rec_texts"
    }
  ]
}

@KyleWang-Hunter
Copy link

UINT8

是的哦,请求的时候需要修改"datatype" : "BYTES",

另外,preprocess模型的config.pbtxt文件的数据类型也需要修改
name: "preprocess"
backend: "python"
max_batch_size: 16

input [
{
name: "preprocess_input"
data_type: TYPE_STRING
dims: [ -1 ]
}
]

output [
{
name: "preprocess_output"
data_type: TYPE_FP32
dims: [ 3, 224, 224 ]
}
]

version_policy: { all { }}

version_policy: { specific: { versions: [1,3]}}

version_policy: { latest: { num_versions: 2}}

instance_group [
{
# The number of instances is 1
count: 1
# Use CPU, GPU inference option is:KIND_GPU
kind: KIND_CPU
# The instance is deployed on the 0th GPU card
# gpus: [0]
}
]
runtime模型的config.pbtxt也需要修改
input [
{
# input name
name: "x"# 这里需要根据实际的模型参数来修改
# input type such as TYPE_FP32、TYPE_UINT8、TYPE_INT8、TYPE_INT16、TYPE_INT32、TYPE_INT64、TYPE_FP16、TYPE_STRING
data_type: TYPE_FP32
# input shape, The batch dimension is omitted and the actual shape is [batch, c, h, w]
dims: [ 3, 224, 224 ]
}
]

The output of the model is configured in the same format as the input

output [
{
name: "softmax_1.tmp_0"#这里也是,需要根据实际的模型参数来修改
data_type: TYPE_FP32
dims: [ 14 ]
}
]
postprocess也是一样

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants