限制来自 Google Cloud Vision API 的响应

Limiting the response from Google Cloud Vision API

目前正在使用 google 云视觉 api 从文档图像中提取文本。

当前情况 - API 效果很好,returns 大量数据,包括单词所在位置的边界框。

期望的结果 - 只查询从图像中提取的单词,而不是所有关于单词的边界框和顶点位置的元数据(大约 99%响应结果大约是 250k,当我想要的只是单词时,这是一个巨大的浪费)

const vision = require('@google-cloud/vision');
const client = new vision.ImageAnnotatorClient();

// Performs label detection on the image file
client
  .documentTextDetection('../assets/images_to_ocr/IMG_0942-min.jpg')
  .then(results => {
    console.log('result:', result);

  })
  .catch(err => {
    console.error('ERROR:', err);
  });

目前,nodeJS 的 Google Cloud Vision 客户端库没有像您所要求的那样请求部分响应的选项。 无论如何,如果您只想显示文本而不是任何其他元数据,您可以像这样过滤响应:

const fullTextAnnotation = results[0].fullTextAnnotation;
console.log(`Full text: ${fullTextAnnotation.text}`);

您将在 'fullTextAnnotation' 中获得完整的响应,然后您可以在 fullTextAnnotation.text 中获得仅使用 '\n' 字符分隔文本块的文本,没有任何元数据。

如果您有兴趣使用其他东西而不是 nodeJS,Java 客户端库具有用于 Annotate class 的 setFields() 方法以及来自 API 资源管理器你可以使用部分字段掩码来查看效果。