Label detection on a local file

Performs label detection on a local document file.

Code sample

Node.js

Before trying this sample, follow the Node.js setup instructions in the Vision quickstart using client libraries. For more information, see the Vision Node.js API reference documentation.

// 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}`);
  });
});

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.