我正在尝试使用 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 请求出了什么问题,那将有很大帮助。