So go ahead and set the environment variable value as shown below:
On a Mac / Linux, you can do the following:
$ export GOOGLE_APPLICATION_CREDENTIALS=<Path To Your JSON Key File>
On Windows, you can do the following:
SET GOOGLE_APPLICATION_CREDENTIALS=<Path To Your JSON Key File>
5. Python Code
The code shown below has been adopted from the official Label tutorial that is present in the Cloud Vision API Documentation. The modifications made are to retrieve 5 Label features instead of just one and to print out all the labels instead of just one.
label.py
import argparse
import base64
import httplib2
from apiclient.discovery import build
from oauth2client.client import GoogleCredentials
def main(photo_file):
'''Run a label request on a single image'''
API_DISCOVERY_FILE = ' HTTPS://vision.googleapis.com/$discovery/rest?version=v1'
HTTP = httplib2.Http()
credentials = GoogleCredentials.get_application_default().create_scoped(
['https://www.googleapis.com/auth/cloud- Platform'])
credentials.authorize(http)
service = build('vision', 'v1', http, discoveryServiceUrl=API_DISCOVERY_FILE)
with open(photo_file, 'rb') as image:
image_content = base64.b64encode(image.read())
service_request = service.images().annotate(
body={
'requests': [{
'image': {
'content': image_content
},
'features': [{
'type': 'LABEL_DETECTION',
'maxResults': 5,
}]
}]
})
response = service_request.execute()
for results in response['responses']:
if 'labelAnnotations' in results:
for annotations in results['labelAnnotations']:
print('Found label %s, score = %s' % (annotations['description'],annotations['score']))
return 0
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
'image_file', help='The image you\'d like to label.')
args = parser.parse_args()
main(args.image_file)
The Python code is straightforward as explained below:
- The image file to analyze is provided as a command line argument to the program.
- The first step is to use the Google Application Default Credentials to validate the program. Remember we had set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the JSON Key file before we ran this program. Not doing that will result in an Not Authorized Error.
- The image file bytes are then read and base64 encoded.
- We then make a request to the Cloud Vision API by making the images.annotate() method where we pass in the request object that contains the image bytes and the Feature Detection that we are interested in. In our case, it is just one i.e. the LABEL_DETECTION and we ask for 5 results, which will be given to us in increasing order of probability.
- Finally, we print out the labels and their scores that were returned by the Cloud Vision API.
Sample Run
To run the above code, ensure that you have the Python development environment setup along with some libraries that are required. The Prerequisites section of the Label Tutorial has the details. You can run the label analysis on any image file name via image.py <filename>.
Shown below are two images of the same spot. One of the images does not have a person in it and the other one does. The LABEL_DETECTION output for each of the images is shown too:
Found label waterway, score = 0.81399214 Found label road, score = 0.79218566 Found label wall, score = 0.74516481 Found label flooring, score = 0.58240849 Found label sidewalk, score = 0.51532209 |
Found label man, score = 0.8414287 Found label portrait photography, score = 0.81915128 Found label male, score = 0.79989046 Found label child, score = 0.76658708 Found label people, score = 0.73413825 |
The results are not very accurate for some of the labels that it found but you can see where things stand today and the possibilities that this opens up.
Sample Projects
We use the Python API above to invoke the Google Cloud Vision API but client libraries and samples exist in multiple other languages with more samples. Developers have started writing wrappers around the core REST API in multiple languages. One such is the pigeon API, which is a wrapper around Cloud Vision API in Go Language.
Visit the Github page for Cloud Vision API samples including sample applications written in multiple languages like Python, Java, Go, Node.js, PHP and mobile platforms Android and iOS, at the time of writing this article.