Deteksi label pada file lokal

Menjalankan deteksi label pada file dokumen lokal.

Contoh kode

Node.js

Sebelum mencoba contoh ini, ikuti petunjuk penyiapan Node.js di Panduan memulai Vision menggunakan library klien. Untuk mengetahui informasi selengkapnya, lihat dokumentasi referensi Vision Node.js API.

Untuk melakukan autentikasi ke Vision, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, baca Menyiapkan autentikasi untuk lingkungan pengembangan lokal.

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

Langkah selanjutnya

Untuk menelusuri dan memfilter contoh kode untuk produk Google Cloud lainnya, lihat browser contoh Google Cloud.