Node.js - Google Cloud Vision API Examples

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.

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.


Preparation

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

GOOGLE_APPLICATION_CREDENTIALS=/path/to/the/credentials

Dependencies

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
require('dotenv').config();

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

client
.labelDetection('an_image.png')
.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.

client
.textDetection('http://digitalnativestudios.com/textmeshpro/docs/rich-text/line-indent.png')
.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.

client
.logoDetection('http://allenmeyerdesign.com/wp-content/uploads/2013/09/new-yahoo-logo.jpg')
.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.


Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

How to Use Express.js, Node.js and MongoDB.js

In this post, I will show you how to use Express.js, Node.js and MongoDB.js. We will be creating a very simple Node application, that will allow users to input data that they want to store in a MongoDB database. It will also show all items that have been entered into the database.

Node.js for Beginners - Learn Node.js from Scratch (Step by Step)

Node.js for Beginners - Learn Node.js from Scratch (Step by Step) - Learn the basics of Node.js. This Node.js tutorial will guide you step by step so that you will learn basics and theory of every part. Learn to use Node.js like a professional. You’ll learn: Basic Of Node, Modules, NPM In Node, Event, Email, Uploading File, Advance Of Node.

Top 7 Most Popular Node.js Frameworks You Should Know

Node.js is an open-source, cross-platform, runtime environment that allows developers to run JavaScript outside of a browser. In this post, you'll see top 7 of the most popular Node frameworks at this point in time (ranked from high to low by GitHub stars).