使用nodejs客户端库将图像作为base64编码发送到google云视觉API

问题描述 投票:2回答:2

我正在使用Google云端Web检测API的nodejs客户端库,我想将base64编码的图像传递给它,但没有关于它的文档。我尝试使用简单的API调用它,但它工作,但由于客户端库更整洁我想知道他们是否也在那里实现了它。任何的想法?

node.js base64 google-cloud-platform google-cloud-vision client-library
2个回答
1
投票

事实上,你应该能够使用base64-encoded image发送Cloud Vision API Client Library for Node.js

当使用webDetection() method实例中的ImageAnnotatorClient时,request字段应该包含一个AnnotateImageRequest对象,其中包括一个image对象。 image对象具有此documentation page中表示的结构。正如您所看到的,它可以包括图像所在的sourcecontent表示为字节流。

此外,如果你看一下google.cloud.vision.v1.Image definition in proto format,你会发现它确实需要作为图像定义来源或内容。


0
投票

因此,解决方法是使用API​​传递base64:

request({
    url: "https://vision.googleapis.com/v1/images:annotate",
    method: "POST",
    qs: {
      key: "your key"
    },
    json: true,
    body: {
  "requests": [
    {
      "image": {
        "content": place your base64 here(without prefix)
      },
      "features": [
        {
          "type": "WEB_DETECTION"
        }
      ]
    }
  ]
}
© www.soinside.com 2019 - 2024. All rights reserved.