Google Node.js 使用 base64 编码的视觉文本检测

Google Vision text detection for Node.js using base64 encoding

刚刚开始探索 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);方法。我尝试在 const [result] = await client.textDetection( 方法上使用相同的技术,但它给了我一个错误。

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

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

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

您可以将 quickstart guide and from there edit the lines after the creation of the 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,阅读请求参数的说明,特别是以下部分:

A dictionary-like object representing the image. This should have a single key (source, content).

If the key is content, the value should be a Buffer.

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

所示