作为新手,我正在尝试使用 Google Cloud Vision API 来定位给定图片(base64)中的对象。我正在使用 Google Cloud shell 环境来实现此目的,并将所有文件(图像文件和 JSON 文件)保留在此 Shell 环境中。当我使用以下参数运行
Curl
命令时,我总是收到错误:
curl -X POST -H "Authorization: Bearer $(gcloud auth print-access-token)" -H "x-goog-user-project: cogent-anvil-417312" -H "Content-Type: application/json; charset=utf-8" --data-binary -d @request.json "https://vision.googleapis.com/v1/images:annotate"
错误代码: 400
消息:“收到无效的 JSON 负载。无法解析数字。 -d ^”
状态:“INVALID_ARGUMENT”
如果有人能帮助我解决这个问题,我将不胜感激:
我在 Google Cloud Shell 上尝试了以下操作,但找不到避免此 INVALID_ARGUMENT 问题的解决方案。您还可以在下面看到 JSON 文件的内容。
ccamkoy@cloudshell:~/aidat (cogent-anvil-417312)$ clear
ccamkoy@cloudshell:~/aidat (cogent-anvil-417312)$ pwd
/home/ccamkoy/aidat
ccamkoy@cloudshell:~/aidat (cogent-anvil-417312)$ ls -al
total 192
drwxr-xr-x 2 ccamkoy ccamkoy 4096 Mar 16 01:38 .
drwxr-xr-x 9 ccamkoy ccamkoy 4096 Mar 16 01:21 ..
-rw-r--r-- 1 ccamkoy ccamkoy 182129 Mar 16 00:22 IMG_2058_1024x768.jpeg
-rw-r--r-- 1 ccamkoy ccamkoy 240 Mar 16 01:06 request.json
ccamkoy@cloudshell:~/aidat (cogent-anvil-417312)$ base64 IMG_2058_1024x768.jpeg -w 0 > IMG_2058_1024x768_Base64.jpeg
ccamkoy@cloudshell:~/aidat (cogent-anvil-417312)$ ls -al
total 432
drwxr-xr-x 2 ccamkoy ccamkoy 4096 Mar 16 01:39 .
drwxr-xr-x 9 ccamkoy ccamkoy 4096 Mar 16 01:21 ..
-rw-r--r-- 1 ccamkoy ccamkoy 242840 Mar 16 01:39 IMG_2058_1024x768_Base64.jpeg
-rw-r--r-- 1 ccamkoy ccamkoy 182129 Mar 16 00:22 IMG_2058_1024x768.jpeg
-rw-r--r-- 1 ccamkoy ccamkoy 240 Mar 16 01:06 request.json
ccamkoy@cloudshell:~/aidat (cogent-anvil-417312)$ cat request.json
{
"requests":[
{
"image":{
"content":"/home/ccamkoy/aidat/IMG_5028_1064x768_Base64.jpeg"
},
"features":[
{
"type":"OBJECT_LOCALIZATION",
"maxResults":1
}
]
}
]
}
ccamkoy@cloudshell:~/aidat (cogent-anvil-417312)$ curl -X POST -H "Authorization: Bearer $(gcloud auth print-access-token)" -H "x-goog-user-project: cogent-anvil-417312" -H "Content-Type: application/json; charset=utf-8" --data-binary -d @request.json "https://vision.googleapis.com/v1/images:annotate"
curl: (6) Could not resolve host: request.json
{
"error": {
"code": 400,
"message": "Invalid JSON payload received. Unable to parse number.\n-d\n^",
"status": "INVALID_ARGUMENT"
}
}
ccamkoy@cloudshell:~/aidat (cogent-anvil-417312)$
我尝试过:
“内容”:“https://storage.cloud.google.com/aidat/IMG_2058_1024x768_Base64.jpeg”
但它也不起作用:(
也许我犯了一个简单的错误,但我还看不到......
我终于找到了解决问题的方法。 “request.json”文件需要如下:
{
"requests":[
{
"image":{
"source":{
"imageUri":
"gs://aidat/IMG_2058_1024x768_Base64.jpeg"
}
},
"features":[
{
"type":"OBJECT_LOCALIZATION",
"maxResults":1
}
]
}
]
}