How to use google cloud Vision API using python

Hari Bhosale

Harikrushna Bhosale

Aug 1, 2018

Prerequisites

  • Install python here
  • Install google cloud – pip install google-cloud
  • Setting up google api key and make a service key account to use vision api
  • Setting environmental variable of google application credentials for dynamic authentication
  • Creating python script
  • Testing the api with image

Check python version & status

Install google cloud – pip install google-cloud

Setting up google api key and make a service key account to use vision api

Create New Project

Go to Dashboard

Create Credential Key

Enable google cloud vision api here

API status is enable and option for disable

Setting environmental variable of google application credentials for dynamic authentication

Store/Copy the API key file on project location/path after that open CMD with same path and use given below syntax for the setting environmental variable.

set GOOGLE_APPLICATION_CREDENTIALS=visionapikey.json

Note : You have to do every time this if you close the CMD

Creating python script

visiondemo.py

import io
import os
from google.cloud import vision
from google.cloud.vision import types
#vision.Client()

client = vision.ImageAnnotatorClient()
file_name = os.path.join(
	os.path.dirname(_file_),’car.jpg’)

with io.open(file_name,’rb’) as image_file:
      content = image_file.read()
image = types.Image(content=content)
response = client.label_detection(image=image)
labels = response.label_annotations
print(“Labels:”)
for label in labels:
     print(label.description)

Demo Image "car.jpg"

Testing the api with image

Output

Output with different image "fruit.jpg"

Thank You