semiflex semiflex - 3 years ago 322
Python Question

Google Cloud Vision API - Python

I can't seem to find where to add the API key or where I need to locate to the google credentials file in my google cloud vision code:

import argparse
import base64
import httplib2
import validators
import requests

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)

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)

photo_file = "image_of_bottle.jpg"
main(photo_file)


Does anyone know where I can add the API key or locate to the credentials file?

EDIT: Added changes recommended by Eray Balkanli and I added my image file in the call. I'm not sure if I did it correctly:

import argparse
import base64
import httplib2
import validators
import requests

from apiclient.discovery import build
from oauth2client.client import GoogleCredentials


def main(photo_file,developerkey):
'''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,developerkey=INSERT API KEY)

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)

photo_file = "image_file.jpg"
main(photo_file,developerkey)


I received the following error:

usage: googleimagetest_v.4.py [-h] image_file
googleimagetest_v.4.py: error: too few arguments


Does anyone know how I can solve this error?

Answer Source

Google Vision API documentation for authenticating an API clearly gives a step by step guide of how you can get credentials and the last section in that page describes about Authenticating with Application Default Credentials:

The simplest way for applications to authenticate to a Google Cloud Platform API service is by using Application Default Credentials (ADC). Services using ADC first search for credentials within a GOOGLE_APPLICATION_CREDENTIALS environment variable. Unless you specifically wish to have ADC use other credentials (for example, user credentials), we recommend you set this environment variable to point to your service account key file (the .json file downloaded when you created a service account key, as explained in Set Up a Service Account.

$ export GOOGLE_APPLICATION_CREDENTIALS=<path_to_service_account_file>

If it matters (in case you haven't yet registered), Google gives you free trial access ($300 worth for 365 days) of their Cloud Platform. Their low pricing, imho, for Vision API should let you do a lot for your testing.


Now, the error in your code; even though things are not clear from what you described about the error, it looks like your code accepts an argument but you are trying to run your python script without one. You can run your code in the following manner:

python googleimagetest_v.4.py "image_file.jpg"

and you don't need the following 2 lines (because it is duplicate call for main())

photo_file = "image_file.jpg"
main(photo_file,developerkey)

You have defined main() function to accept 2 arguments but when you call it in __main__ section you are passing a single argument. You might want to fix that too.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download