我试图通过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页面底部的摘录:
request.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
谢谢大家,在这个问题上停留了一段时间。
答案 0 :(得分:0)
您可以尝试使用base64字节字符串吗?提到了here。
答案 1 :(得分: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
答案 2 :(得分: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);