对本地文件执行标签检测

对本地文档文件执行标签检测。

代码示例

Node.js

试用此示例之前,请按照《Vision 快速入门:使用客户端库》中的 Node.js 设置说明进行操作。 如需了解详情,请参阅 Vision Node.js API 参考文档

如需向 Vision 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证

// Imports the Google Cloud client libraries
const vision = require('@google-cloud/vision').v1p1beta1;

// Creates a client
const client = new vision.ImageAnnotatorClient();

/**
 * TODO(developer): Uncomment the following line before running the sample.
 */
// const fileName = 'Local image file, e.g. /path/to/image.png';

// Performs label detection on the local file
const [result] = await client.textDetection(fileName);
const pages = result.fullTextAnnotation.pages;
pages.forEach(page => {
  page.blocks.forEach(block => {
    const blockWords = [];
    block.paragraphs.forEach(paragraph => {
      paragraph.words.forEach(word => blockWords.push(word));
      console.log(`Paragraph confidence: ${paragraph.confidence}`);
    });

    let blockText = '';
    const blockSymbols = [];
    blockWords.forEach(word => {
      word.symbols.forEach(symbol => blockSymbols.push(symbol));
      let wordText = '';
      word.symbols.forEach(symbol => {
        wordText = wordText + symbol.text;
        console.log(`Symbol text: ${symbol.text}`);
        console.log(`Symbol confidence: ${symbol.confidence}`);
      });
      console.log(`Word text: ${wordText}`);
      console.log(`Word confidence: ${word.confidence}`);
      blockText = blockText + ` ${wordText}`;
    });

    console.log(`Block text: ${blockText}`);
    console.log(`Block confidence: ${block.confidence}`);
  });
});

后续步骤

如需搜索和过滤其他 Google Cloud 产品的代码示例,请参阅 Google Cloud 示例浏览器