【问题标题】:Google Vision text detection for Node.js using base64 encoding使用 base64 编码的 Node.js 的 Google Vision 文本检测
【发布时间】:2020-05-24 17:35:56
【问题描述】:

刚刚开始探索 Google Cloud Vision API。来自他们的指南:

const client = new vision.ImageAnnotatorClient();
const fileName = 'Local image file, e.g. /path/to/image.png';
const [result] = await client.textDetection(fileName);

但是,我想使用二进制图像数据的 base64 表示,因为他们声称可以使用。

我在 SO 上找到了这个参考: Google Vision API Text Detection with Node.js set Language hint

而不是imageUri,我使用"content": string,正如提到的here。但 SO 示例使用 const [result] = await client.batchAnnotateImages(request);method。我尝试在const [result] = await client.textDetection( 方法上使用相同的技术,但它给了我一个错误。

所以我的问题是:是否可以使用 base64 编码的字符串来表示图像以执行 TEXT_DETECTION ?如果有,怎么做?

非常感谢任何形式的帮助。

【问题讨论】:

    标签: node.js google-cloud-platform google-cloud-vision google-vision


    【解决方案1】:

    content 字段需要为Buffer

    您使用 nodejs 客户端库。该库在内部使用 grpc API,并且 grpc API 期望在 content 字段中键入 bytes

    但是,JSON API 期望 base64 string

    参考

    https://cloud.google.com/vision/docs/reference/rpc/google.cloud.vision.v1#image https://googleapis.dev/nodejs/vision/latest/v1.ImageAnnotatorClient.html#textDetection

    【讨论】:

    • 我想我先尝试使用字节数组...由于某种原因它不起作用,所以我去了base64路线...
    【解决方案2】:

    您可以使用quickstart 指南,并在创建client 之后从那里编辑以下行:

    // Value of the image in base64
    const img_base64 = '/9j/...';
    
    const request = {
      image: {
        content: Buffer.from(img_base64, 'base64')
      }
    };
    
    const [result] = await client.textDetection(request);
    console.log(result.textAnnotations);
    console.log(result.fullTextAnnotation);
    

    你可以看一下函数here,阅读请求参数的说明,特别是以下部分:

    表示图像的类字典对象。这应该有一个 单键(sourcecontent)。

    如果key是content,值应该是Buffer。

    这导致了之前示例代码中使用的结构。反对使用imageUrifilename时,它们必须在另一个对象内部,键为source,如sample所示。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-27
      • 1970-01-01
      • 2017-10-20
      • 2019-04-24
      • 2011-10-30
      • 2020-01-09
      • 1970-01-01
      相关资源
      最近更新 更多