0

我正在尝试使用 Google 机器学习 API,但遇到了两个问题。在API 资源管理器中,我输入了正确的信息,但收到了响应错误:

代码 200 "error": "Missing \"instances\" field in request body: {\n \"httpBody\": \n
{\n \"data\": \"\\"instances\\" : \\ "teste\\"\",\n
\"contentType\": \"application/json\"\n }\n}"

该请求找到我的模型(如果我更改字段名称中的值,我会收到另一个错误)但不理解我的 json。那是json:

{"instances" : [{"key":"0", "image_bytes": {"b64": "mybase64"} }]}

当我使用 gcloud 在命令行上进行预测时,我没有收到任何错误,一切似乎都很好。我为 gcloud 创建的 Json 有点不同:

{"key":"0", "image_bytes": {"b64": "mybase64"} }

我已经在 API explorer 中尝试过,但没有成功。

所以,我决定使用 .Net Api 来尝试预测,我得到了其他情况:响应为空(???)。

这是我的代码:

   'get the service credential that I created
    Dim credential = Await GetCredential()


    Dim myService As New CloudMachineLearningEngineService(New BaseClientService.Initializer() With {
                                                                    .ApplicationName = "my Project Name (Is That It???)",
                                                                    .ApiKey = "my API Key",
                                                                    .HttpClientInitializer = credential
                                                               })

    Dim myBase64 As String = GetBase64("my image path to convert into a base64 String")
    Dim myJsonRequest As String = "{""instances"" : [{""key"":""0"", ""image_bytes"": {""b64"": """ + myBase64 + """}}]}"

    Dim myRequest = New GoogleCloudMlV1PredictRequest With {
        .HttpBody = New GoogleApiHttpBody With {.Data = myJsonRequest,
                                                .ContentType = "application/json"
                                                }
    }

    'If I change the model name I get error
    Dim myPredictRequest = myService.Projects.Predict(myRequest, "projects/myProject/models/myModel/versions/v1")
    myPredictRequest.AccessToken = credential.Token.AccessToken
    myPredictRequest.OauthToken = credential.Token.AccessToken
    myPredictRequest.Key = "my API Key

    'Execute the request
    Dim myResponse = myPredictRequest.Execute()

    'at this point, myResponse is Empty (myResponse.ContentType Is Nothing, myResponse.Data Is Nothing And myResponse.ETag Is Nothing)

如果我更改模型名称,我会收到一条错误消息,提示找不到我的模型,因此我的凭据是正确的。

我不知道我做错了什么。Someboby 可以帮助解决这些问题吗?

谢谢!

更新: - - - - - - - - - - - - -

我更改了这个执行命令: Dim myResponse = myPredictRequest.Execute()
到这个: Dim s = StreamToString(myPredictRequest.ExecuteAsStream())

现在我可以使用 .Net API 和谷歌开发人员界面(缺少实例字段......)得到同样的错误。因此,如果有人知道我的 Json 请求出了什么问题,那将有很大帮助。

4

3 回答 3

0

Github问题 #1068显示了针对此问题的两种解决方法。

总之,用于service.ModifyRequest插入原始 JSON 内容。

或者service.HttpClient.PostAsync(...)直接使用。

于 2017-07-20T09:19:31.090 回答
0

您放入 API 资源管理器中的 JSON 确实是正确的(当然,假设您的模型有输入keyimage_bytes)。这似乎是我将报告的资源管理器的一个错误。

您在 .NET 代码中收到错误的原因是因为您使用的是 .HttpBody 字段。这段代码:

Dim myJsonRequest As String = "{""instances"" : [{""key"":""0"", ""image_bytes"": {""b64"": """ + myBase64 + """}}]}"
Dim myRequest = New GoogleCloudMlV1PredictRequest With {
    .HttpBody = New GoogleApiHttpBody With {.Data = myJsonRequest,
                                            .ContentType = "application/json"
                                            }
}

将生成如下所示的 JSON 请求:

{
  "httpBody": {
    "data": "{\"instances\" : [{\"key\":\"0\", \"image_bytes\": {\"b64\": \"mybase64\"} }]}",
    "contentType": "application\/json"
  }
}

当你真正需要的是:

{"instances" : [{"key":"0", "image_bytes": {"b64": "mybase64"} }]}

因此,您会看到错误消息。

我不知道如何使用 .NET 库生成正确的响应;基于文档中的 Python 示例,我猜想:

Dim myJsonRequest As String = "{""instances"" : [{""key"":""0"", ""image_bytes"": {""b64"": """ + myBase64 + """}}]}"
Dim myPredictRequest = myService.Projects.Predict(myJsonRequest, "projects/myProject/models/myModel/versions/v1")

但是我没有很好的测试方法。作为参考,Python 等价物是:

response = service.projects().predict(
    name=name,
    body=myJsonRequest
).execute()
于 2017-05-05T15:32:39.987 回答
0

我用 .Net API 解决了这个问题。我创建了两个新类继承了 Google API 的类。像这样的东西:

Imports Google.Apis.CloudMachineLearningEngine.v1.Data
Imports Newtonsoft.Json

Public Class myGoogleCloudMlV1PredictRequest
    Inherits GoogleCloudMlV1PredictRequest

    <JsonProperty("instances")>
    Public Property MyHttpBody As List(Of myGoogleApiHttpBody)
End Class



Imports Google.Apis.CloudMachineLearningEngine.v1.Data
Imports Newtonsoft.Json

Public Class myGoogleApiHttpBody
    Inherits GoogleApiHttpBody

    <JsonProperty("image_bytes")>
    Public Property MyData As image_byte

    <JsonProperty("key")>
    Public Property key As String

End Class

因此,在我的原始代码中,我更改了这部分:

    Dim myBase64 As String = GetBase64("my_image_path_to_convert_into_a _base64_String")
    Dim myJsonRequest As String = "{""instances"" : [{""key"":""0"", ""image_bytes"": {""b64"": """ + myBase64 + """}}]}"

    Dim myRequest = New GoogleCloudMlV1PredictRequest With {
        .HttpBody = New GoogleApiHttpBody With {.Data = myJsonRequest,
                                               .ContentType = "application/json"
                                            }
    }

对于这个:

Dim myBase64 As String = GetBase64("my_image_path_to_convert_into_a _base64_String")
Dim myRequest = New myGoogleCloudMlV1PredictRequest With {
            .MyHttpBody = New List(Of myGoogleApiHttpBody)()
        }

        Dim item As myGoogleApiHttpBody = New myGoogleApiHttpBody With {
                                                    .key = "0",
                                                    .MyData = New image_byte With {
                                                        .b64 = myBase64
                                                    }
                                                 }
        myRequest.MyHttpBody.Add(item)

瞧,它的工作!

谢谢大家!!

于 2017-05-08T11:53:52.953 回答