Label detection on a local file

Performs label detection on a local document file.

Explore further

For detailed documentation that includes this code sample, see the following:

Code sample

Node.js

To learn how to install and use the client library for Vision API Product Search, see Vision API Product Search client libraries. For more information, see the Vision API Product Search Node.js API reference documentation.

To authenticate to Vision API Product Search, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

// 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.