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

时间:2018-03-03 11:52:49

标签: node.js base64 google-cloud-platform google-cloud-vision client-library

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

2 个答案:

答案 0 :(得分:1)

事实上,您应该可以使用base64-encoded image发送Cloud Vision API Client Library for Node.js

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

此外,如果您查看google.cloud.vision.v1.Image definition in proto format,您会看到确实需要内容作为图像定义。

答案 1 :(得分: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"
        }
      ]
    }
  ]
}