1

我试图通过 REST API 发送图像,在 C# 中实现我的自定义 AutoML 模型,但我不断收到不同的错误。

我目前拥有的是:

远程服务器返回错误:(400) 错误请求。

我已经拍摄了一张图像并转换为一串字节byteString,并创建了这样的 jsonRequest 对象:

string jsonRequest = "{\"payload\":{\"image\":{\"imageBytes\":\"" + byteString + "\"},}}";

然后我正在执行如下的 POST 请求:

WebRequest request = WebRequest.Create(@"https://automl.googleapis.com/v1beta1/projects/PROJECT_ID/locations/us-central1/models/MODEL_ID:predict");
request.Method = "POST";
request.ContentType = "application/json";
request.Headers.Add("Authorization", "Bearer GCLOUD_ACCESS_TOKEN");

using (var streamWriter = new StreamWriter(request.GetRequestStream()))
{
    streamWriter.Write(jsonRequest);
}

然后当它命中时,request.GetResponse();如果没有其他信息给我上述错误。

作为参考,这些是从我的自定义 AutoML 模型的 PREDICT 页面底部截取的片段:

请求.json:

{
  "payload": {
    "image": {
      "imageBytes": "YOUR_IMAGE_BYTE"
    },
  }
}

执行请求:

curl -X POST -H "Content-Type: application/json" \
  -H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \
  https://automl.googleapis.com/v1beta1/projects/PROJECT_ID/locations/us-central1/models/MODEL_ID:predict -d @request.json

谢谢各位,这个问题卡了好久。

4

3 回答 3

0

我能够用 RestSharp( https://www.nuget.org/packages/RestSharp ) 库解决这个问题

例子:

var client = new RestClient("https://automl.googleapis.com/v1beta1/projects/{project-id}/locations/us-central1/models/{model-id}:predict":
var request = new RestRequest(Method.POST);
request.AddHeader("authorization", $"Bearer {Access-Token}");
request.AddHeader("content-type", "application/json");
request.AddParameter("application/json", "{\"payload\":{\"image\":{\"imageBytes\":\"{Image-Base64}""}}}", ParameterType.RequestBody);

IRestResponse response = client.Execute(request);
于 2019-05-25T18:25:08.457 回答
0

您可以尝试对字节字符串进行base64吗?这里提到了这一点。

于 2018-08-13T16:48:34.813 回答
0

在我的用例中,我通过为同事创建具有正确角色的服务帐户并为他们提供此 URL 以进行预测,从而与同事分享我的视觉模型:

curl -X POST -H "Authorization: Bearer add_access_token " -H "Content-Type: application/json" https://automl.googleapis.com/v1beta1/projects/id_project/locations/us-central1/models/:model_idpredict -d @path_of_file_image_in_base64
于 2018-10-12T08:01:16.310 回答