September 9, 2019 No Comments

Google Cloud offers Video Intelligence API service which analyzes the input video and provides labelling. Pre-trained models of Video Intelligence API can help detect more than 2000 objects, places and actions from the video.

We are going to take you through how to use Video Intelligence API with Python on Google Colab and local computer. First part will cover the Google Colab and then we’ll check how to set the same up on local machine.

WHAT YOU NEED?​

  1. A google cloud project with billing enabled in it.

  2. An enabled video intelligence API associated with the project.

  3. A video residing in a google bucket for processing.

  4. A Service account which has permission to access the data in bucket and the project.

  5. Some knowledge of python.

Using Video Intelligence API of Google Cloud with Python on Colab

1) Select or Create new Google Cloud project for Video Intelligence API https://console.cloud.google.com/cloud-resource-manager

Create a new project by clicking on create project.

Enter your project name, then click on create button as shown in below figure.

2) Enable Billing for the project
For that go to google cloud console ⇾ https://console.cloud.google.com/

NOTE: You can either follow Step3(a) or Step3(b) to enable the API

3(a) Enable the API by selecting your project
https://console.cloud.google.com/flows/enableapi?piid=videointelligence.googleapis.com
[ select the project that you have selected in step-2 ]

Select the project for which you want to enable API.

Select cloud video intelligence API and click on ‘what credentials do I need’ button.

Now click on done button.

3(b) To enable API from https://console.cloud.google.com then follow:

Click on video intelligence.

Now click on view API docs.

Then click on right side corner button ‘Get Started for free’.

Now select the country and mark the checkbox.

Select account type, tax status and other information.

Enter card method and click on start my free trial.

4) Create bucket on google cloud with necessary permission
https://console.cloud.google.com/storage/

Enter name for bucket and click on create.

To upload your video, click on upload file button.

Now, add/edit permission for the uploaded video.

IF YOU DON’T HAVE SERVICE ACCOUNT THEN FOLLOW THE STEPS TO CREATE IT​

Why do I need to create a service account ?

To access google cloud services like video intelligence API you should have service account and a secret key [JSON file].

How can I create service account?​

To create service account go to ⇾ http://console.cloud.google.com now click on IAM & admin in left sidebar and then click on service accounts. No need to provide role for service account. Just create key for your service account in JSON format. Keep in mind, you won’t be able to download this file again so store it securely.

Now follow the given steps:

To create service account key click on service account key option.

Select new service account.

Select JSON type and click on create button to create the service account.

5) Now for coding part open Google colab from https://colab.research.google.com/
Before you start your coding part, please upload service key JSON file to your Google drive. ‘Service key JSON file’ is the one you have downloaded in step-4.

Create a new Colab File and execute following commands

				
					!pip install google-cloud
!pip install google-cloud-videointelligence
!gcloud init
				
			

And then follow the steps prompted on screen after !gcloud init command.

6) To Mount your drive.

				
					from google.colab import drive
drive.mount('/content/drive')
				
			

7) Now copy and paste the code in file.

				
					#Import libraries
import argparsefrom google.cloud 
import videointelligence
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "GOOGLE_DRIVE_PATH_TO_JSON_FILE"                                                                       

#Call the API 
client = videointelligence.VideoIntelligenceServiceClient()
job = client.annotate_video(
     input_uri='gs://PATH_TO_YOUR_VIDEO',
     features=['LABEL_DETECTION'],
 )
 result = job.result()
 print(result)
				
			

8) OUTPUT
After executing all command successfully, you will get some result in JSON format given below.

Format shows the entity ‘nature’ appeared at the start time 0s to end time of 142s.

				
					{ "entity": { "description": "nature", "entityId": "/m/05h0n", "languageCode": "en-US" }, "segments": [ { "confidence": 0.7760365, "segment": { "endTimeOffset": "142s", "startTimeOffset": "0s" } } ]
				
			

THAT’S ALL.

Using Video Intelligence API of Google Cloud with Python on Local Machine

If you don’t want to use Google Colab, you can even do this procedure on your Local Machine. Here are the steps you need to follow.

First of all we are going to make virtual environment. It is recommended to make virtual environment because all the dependencies will be installed inside the environment. So this will help you to move your project without having any trouble with dependencies, as it have already dependencies in it.

1) Create Virtual environment [ not necessary but recommended, you can directly move to step 2 ].

  • To create virtual environment type

    virtualenv YOUR_ENV_NAME

  • Now set the python version for your virtual environment

    virtualenv YOUR_ENV_NAME

  • after setting python version for virtual environment activate your virtual environemnt

    virtualenv YOUR_ENV_NAME/bin/activate

  • that’s all you have created and activated virtual environment after you complete all your work related to the project you can disable it by typing

    virtualenv deactivate

NOTE: Next 4 steps are same for Google colab and local machine, hence you can follow the step-1, step-2, step-3, step-4 from above (i.e from Google Colab Part)

6) IF YOU DON’T HAVE SERVICE ACCOUNT FOLLOW THE STEPS TO CREATE

To create service account goto ⇒ http://console.cloud.google.com

Now click on IAM & admin in left sidebar and then click on service account.
No need to provide role for service account. Just create key for your service account in JSON format. Be sure to store this file securely, as you won’t be able to download it again.
Now proceed further with the following steps.

To create a key click on service account key.

Select new service account option.

Select json type and then click on create option.

7) Before going to code we will install necessary modules.

				
					pip install google-cloud
pip install google-cloud-videointelligence
gcloud init
				
			

On executing ‘gclout init’ command, you will be asked for some configuration, your email_id and at last select your project you are working with. [ The project you are selecting must have billing enabled and video intelligence API enabled, and your email account should have API keys with access to google cloud bucket else you will get an error like “ google.api_core.exceptions.PermissionDenied: 403 ” ]

8) Now for coding part open any editor you like and create
‘video_intelligence_demo.py’ file

9) Now insert the below code in the ‘video_intelligence_demo.py’ file.

				
					#Import libraries
import argparse

from google.cloud import videointelligence

os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "GOOGLE_DRIVE_PATH_TO_JSON_FILE"

#Call the API 
client = videointelligence.VideoIntelligenceServiceClient()

job = client.annotate_video(
    input_uri='gs://PATH_TO_YOUR_VIDEO',
    features=['LABEL_DETECTION'],
)
result = job.result()
# uncomment following line to see the result on cli
# print(result)
# uncomment following code to write result to as JSON file
# f = open(‘result.json’,’w’)
# f.write(str(result))
# fclose()
				
			

10) OUTPUT
After executing above all command successfully you will get some result in JSON format given below. Format shows the entity ‘nature’ appeared at the start time 0s to endtime of 142s.

				
					{ "entity": { "description": "nature", "entityId": "/m/05h0n", "languageCode": "en-US" }, "segments": [ { "confidence": 0.7760365, "segment": { "endTimeOffset": "142s", "startTimeOffset": "0s" } }
				
			

That’s it. Hope this tutorial will help you to use the Video Intelligence API of Google Cloud with Python on Google Colab/Local Machine. If you have any question then post it in the comment. We’ll respond to it soon.

Some Issues with their solutions

If you are facing any issues than check whether your service account has access to the Google bucket and the project:

To change bucket permissions run the following command.

				
					!gsutil acl ch -u SERVICE_ACCOUNT@PROJECT_ID.iam.gserviceaccount.com:READ gs://PATH_TO_YOUR_VIDEO
				
			

To check whether the Google bucket is working and the path you have given in code is correct, run the following command.

				
					!gcloud ml video detect-labels gs://YOUR_GOOGLE_BUCKET_VIDEO_PATH_HERE.mp4

				
			

Above command gives you the same result as your python code.

If you are facing any issue while installing the google-cloud-sdk then try using

				
					pip install snap
sudo snap install google-cloud-sdk  
				
			

if above command gives you error regarding some risk then execute following command

				
					sudo snap install google-cloud-sdk --clasic
				
			

If you have any question or doubt then feel free to post it in comment. We would be glad to assist you further.

Write a comment

Your email address will not be published. Required fields are marked *

Pragnakalp Techlabs: Your trusted partner in Python, AI, NLP, Generative AI, ML, and Automation. Our skilled experts have successfully delivered robust solutions to satisfied clients, driving innovation and success.