Google 愿景 API

Google Vision API

我正在尝试 post 数据到 Google Vision API。但我总是收到 400 响应代码 - 错误请求。我还没有想法。

我已阅读并尝试使用这些链接:

https://cloud.google.com/vision/docs/requests-and-responses https://cloud.google.com/vision/docs/detecting-text https://developers.google.com/apis-explorer/?hl=ru#p/vision/v1/vision.images.annotate

我想到了这个:

这是我给 post 的数据:

var dataToSend = {
        "requests": [
           {
               "image": {
                   "content": imageData
               },
               "features": [
                  {
                      "type": "TYPE_UNSPECIFIED",
                      "maxResults": 50
                  },
                  {
                      "type": "LANDMARK_DETECTION",
                      "maxResults": 50
                  },
                  {
                      "type": "FACE_DETECTION",
                      "maxResults": 50
                  },
                  {
                      "type": "LOGO_DETECTION",
                      "maxResults": 50
                  },
                  {
                      "type": "LABEL_DETECTION",
                      "maxResults": 50
                  },
                  {
                      "type": "TEXT_DETECTION",
                      "maxResults": 50
                  },
                  {
                      "type": "SAFE_SEARCH_DETECTION",
                      "maxResults": 50
                  },
                  {
                      "type": "IMAGE_PROPERTIES",
                      "maxResults": 50
                  }
               ]
           }
        ]
    };

这是我的 post:

$.ajax({
        url: "https://vision.googleapis.com/v1/images:annotate?fields=responses&key={MY CREATED KEY}",
        type: "POST",
        data: dataToSend,
        success: function (reponse) {
            console.log(reponse);
        },
    });

这是来自控制台的数据(示例内容为空(不是 post wole base64):

这是回复:

{
  "error": {
    "code": 400,
    "message": "Invalid JSON payload received. Unknown name \"requests[0][features][0][type]\": Cannot bind query parameter. Field 'requests[0][features][0][type]' could not be found in request message.\nInvalid JSON payload received. Unknown name \"requests[0][features][0][maxResults]\": Cannot bind query parameter. Field 'requests[0][features][0][maxResults]' could not be found in request message.\nInvalid JSON payload received. Unknown name \"requests[0][image][content]\": Cannot bind query parameter. Field 'requests[0][image][content]' could not be found in request message.",
    "status": "INVALID_ARGUMENT",
    "details": [
      {
        "@type": "type.googleapis.com/google.rpc.BadRequest",
        "fieldViolations": [
          {
            "description": "Invalid JSON payload received. Unknown name \"requests[0][features][0][type]\": Cannot bind query parameter. Field 'requests[0][features][0][type]' could not be found in request message."
          },
          {
            "description": "Invalid JSON payload received. Unknown name \"requests[0][features][0][maxResults]\": Cannot bind query parameter. Field 'requests[0][features][0][maxResults]' could not be found in request message."
          },
          {
            "description": "Invalid JSON payload received. Unknown name \"requests[0][image][content]\": Cannot bind query parameter. Field 'requests[0][image][content]' could not be found in request message."
          }
        ]
      }
    ]
  }
}

我哪里错了?

所以我发现了问题所在。我的照片超过了大约 4MB 的数据大小限制

是的,您还可以缩小现有照片的尺寸。 为此,您可以使用可用的工具。 它们提供缩小尺寸的照片,以便您的视觉 api 可以给出准确的结果。 我使用了 imagix,它提供了缩小尺寸,你也可以提供像 500*500 这样的像素尺寸。

可能需要进行以下处理

var dataToSend = JSON.stringify({ 你的 json });