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.


node-js programming

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Node JS Complete Course PDF | Node.js

There are some Features that choose Node.js the foremost decision of programming designers.

Node.js Live | Node.js Docker Tutorial | Dockerizing Node.js App|Node.js Training|Edureka

🔥 Node.js Certification Training: https://www.edureka.co/nodejs-certification-training This Edureka video on 'Node.js Docker Tutorial' will help you in learn...

How to Hire Node.js Developers And How Much Does It Cost?

A Guide to Hire Node.js Developers who can help you create fast and efficient web applications. Also, know how much does it cost to hire Node.js Developers.

Hands on with Node.Js Streams | Examples & Approach

The practical implications of having Streams in Node.js are vast. Nodejs Streams are a great way to handle data chunks and uncomplicate development.

Node.js Performance: Node.js vs. Io.js

You may already be aware that Raygun uses Node.JS for our API nodes that receive your precious crash reporting data (we also do node.js crash reporting if you’re interested). We’ve peaked in the past at more than 110,000 requests per second coming...