Getting started with Machine Learning – Node.js integrated with Google Video-Intelligence

Getting started with Machine Learning – Node.js integrated with Google Video-Intelligence
Getting started with Machine Learning – Node.js integrated with Google Video-Intelligence
Getting started with Machine Learning – Node.js integrated with Google Video-Intelligence
Getting started with Machine Learning – Node.js integrated with Google Video-Intelligence
Getting started with Machine Learning – Node.js integrated with Google Video-Intelligence

Machine Learning is an idea to learn from examples and experience, without being explicitly programmed. Instead of writing code, you feed data to the generic algorithm, and it builds logic based on the datasets given.

Artificial Intelligence by Techaorha Solutions Private LImited
Artificial Intelligence

In this article, we are going to cover a simple example of Machine Learning by integrating “Google Video-Intelligence” API with Node.js application.

What is Google Video-Intelligence?

Google under the project of Machine Learning has introduced Video-Intelligence. This makes video searchable and discoverable by extracting contents of a video with an easy to use REST API. furthermore, you can read more about google video intelligence on the google page.

Prerequisites for Machine Learning

  • Basic knowledge of Node.js applications
  • Basic knowledge of Google Cloud Platform

Step 1: Enable the Google Video-Intelligence API

  1. Sign into your google account with valid user credentials
  2. In the Google Cloud Platform Console, go to the Manage resources page and select or create a new project.
  3. Make sure that billing is enabled for your project. Also, you can find guidelines on the google docs link.
  4. Enable the Cloud Video Intelligence API.

Step 2: Authenticating to a Cloud API Service

To allow your application code to use a Cloud API, you will need to set up the proper credentials for your application to authenticate its identity to the service and to obtain authorization to perform tasks. The simplest authentication scheme is that of an API key.

However, an API key does not allow you to authorize to the service, so it is only usable on public data or data you pass directly to the RPC API.

Set up an API key

After you enable a Cloud API, Go to “API & Services” through navigation menu.

Click “Go to Credentials” to click on the Create Credentials button. Select “API key” from the options. You may wish to copy your key and keep it secure (you can also retrieve it from the API Manager→Credentials page).

Set up a service account for machine learning

Google Cloud Platform API authentication and authorization (commonly grouped together as “auth”) is typically done using a service account. in addition, A service account allows your code to send application credentials directly to the Cloud API.

Go to “API & Services” through navigation menu. Click on “Create Credentials” and select “Service account key”.

Select service account as “Compute engine default service account”, select key type as JSON and hit the Create button. Finally, GCP Console will generate a JSON key (as a .json text file), prompt you to download the file to your computer.

The generated JSON key will be similar to the following sample JSON key:

{ “type”: “service_account”,
“project_id”: “project-id”,
“private_key_id”: “some_number”,
“private_key”: “—–BEGIN PRIVATE KEY—–\n…. =\n—–END PRIVATE KEY—–\n”,
“client_email”: “<api-name>api@project-id.iam.gserviceaccount.com”,
“client_id”: “…”,
“auth_uri”: “https://accounts.google.com/o/oauth2/auth”,
“token_uri”: “https://accounts.google.com/o/oauth2/token”,
“auth_provider_x509_cert_url”: “https://www.googleapis.com/oauth2/v1/certs”,
“client_x509_cert_url”: “https://www.googleapis.com/…<api-name>api%40project-id.iam.gserviceaccount.com”
}

Install and initialize the Cloud SDK

Download appropriate google cloud SDK to your OS platform. The installer starts a terminal window and runs the gcloud init command in windows (for other operating systems, manually run the same command). Go through the online guide for detailed information.

Provide authentication credentials to your application code by running the following command. Replace [PATH] with the location of the JSON file that contains your credentials.

gcloud auth activate-service-account --key-file=[PATH]

Obtain an authorization token using the command:

gcloud auth print-access-token

Copy the access token at somewhere safe.

Step 3: Integration with Node.js Application

Considering you have already a basic Node.js application ready, we will explain how to use google REST APIs.

In order to use video-intelligence, we need to install npm package containing client library. Open the command prompt in the project directory and install google cloud library using the following command

npm install --save @google-cloud/video-intelligence

Once the package is installed, we will write a code providing local storage video URL to the function. This function will convert the video into the base64 format and pass to the google video intelligence API.

Code for Implementation

// Imports the Google Cloud Video Intelligence library + Node’s fs library
const video = require(‘@google-cloud/video-intelligence’).v1;
const fs = require(‘fs’);

// Creates a client
const client = new video.VideoIntelligenceServiceClient();

/**
* TODO(developer): Uncomment the following line before running the sample.
*/
// const path = ‘Local file to analyze, e.g. ./my-file.mp4’;
const path = ‘avengers_trailer.mp4’;

// Reads a local video file and converts it to base64
const file = fs.readFileSync(path);
const inputContent = file.toString(‘base64’);

// Constructs request
const request = {
inputContent: inputContent,
features: [‘LABEL_DETECTION’],
};

// Detects labels in a video
client
.annotateVideo(request)
.then(results => {
const operation = results[0];
console.log(‘Waiting for operation to complete…’);
return operation.promise();
})
.then(results => {
// Gets annotations for video
const annotations = results[0].annotationResults[0];

const labels = annotations.segmentLabelAnnotations;
labels.forEach(label => {
console.log(`Label ${label.entity.description} occurs at:`);
label.segments.forEach(segment => {
let time = segment.segment;
if (time.startTimeOffset.seconds === undefined) {
time.startTimeOffset.seconds = 0;
}
if (time.startTimeOffset.nanos === undefined) {
time.startTimeOffset.nanos = 0;
}
if (time.endTimeOffset.seconds === undefined) {
time.endTimeOffset.seconds = 0;
}
if (time.endTimeOffset.nanos === undefined) {
time.endTimeOffset.nanos = 0;
}
console.log(
`\tStart: ${time.startTimeOffset.seconds}` +
`.${(time.startTimeOffset.nanos / 1e6).toFixed(0)}s`
);
console.log(
`\tEnd: ${time.endTimeOffset.seconds}.` +
`${(time.endTimeOffset.nanos / 1e6).toFixed(0)}s`
);
console.log(`\tConfidence: ${segment.confidence}`);
});
});
callback(labels);
})
.catch(err => {
console.error(‘ERROR:’, err);
});

After giving the request some time (about a minute, typically), the same request returns annotation results in JSON format.

Congratulations! You’ve sent your first request to Cloud Video Intelligence API.

Note:- Google Video-Intelligence API is not a free service. You can get free credits for demo purpose.

You want any assitance or want to develop you application in Machine Learning you can always contact us here.

Resource- Google Video-Intelligence Docs