Node.js - Google Cloud Vision API Examples

Image processing technology makes it possible to extract information from an image. Deveoping your own image processing takes a lot of time, effort and knowledge. As an alternative, you can use image processing services, one of them is Google Cloud Vision. This tutorial shows you how to use Google Cloud Vision API from Node.js application.


1. Create or select a Google Cloud project

A Google Cloud project is required to use this service. Open Google Cloud console, then create a new project or select existing project

2. Enable billing for the project

Like other cloud platforms, Google requires you to enable billing for your project. If you haven't set up billing, open billing page.

3. Enable Cloud Vision API

To use an API, you must enable it first. Open this page to enable Cloud Vision API.

4. Set up service account for authentication

As for authentication, you need to create a new service account. Create a new one on the service account management page and download the credentials, or you can use your already created service account.

In your .env file, you have to add a new variable



This tutorial uses @google-cloud/vision and also dotenv for loading environment. Add the following dependencies to your package.json and run npm install

  "@google-cloud/vision": "~0.23.0"
  "dotenv": "~4.0.0"

Code Examples

First, we need to create a client of @google-cloud/vision, which makes it easy to call Cloud Vision APIs. The library needs to read the credentials file whose path has been set on .env variable named GOOGLE_APPLICATION_CREDENTIALS. So we need load the .env file as well.

  // Load .env

  // Import the Google Cloud Vision library
  const vision = require('@google-cloud/vision');

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

Label Detection

Label Detection is used to determine the type of image.

    .then(results => {
       const result = results[0].labelAnnotations;

       console.log(`Label Annotations Result: ${JSON.stringify(result, null, 2)}`);
    .catch(err => {
        console.error('ERROR:', err);

Text Detection

Text Detection is used to extract texts from an image.

    .then(results => {
      const result = results[0].textAnnotations;

      console.log(`Text Annotations Result: ${JSON.stringify(result, null, 2)}`);
    .catch(err => {
        console.error('ERROR:', err);

Logo Detection

Logo Detection is used to detect logo of popular brands from an image.

    .then(results => {
      const result = results[0].logoAnnotations;

      console.log(`Logo Annotations Result: ${JSON.stringify(result, null, 2)}`);
    .catch(err => {
        console.error('ERROR:', err);

List of Supported Annotations

Beside those three annotations above, currenty Google Cloud Vision also supports face detection, landmark detection, document text detection, safe search detection, etc.. Below is the list of supported annotations.

Annotation Method Description
faceDetection Detect multiple faces including facial attributes such as emotional state or headwear
landmarkDetection Detect popular structures, both natural and man-made
logoDetection Detect logo of popular brands
labelDetection Determine the type of image
textDetection Detect text within images using Optical Character Recognition (OCR)
documentTextDetection Detect dense document text using Optical Character Recognition (OCR)
safeSearchDetection Moderate and detect inappropriate content