Google text API text recognition Python example uses a project: "google.com:cloudsdktool", not my own project

I am working on a python example for the Cloud Vision API from github repo .

I already set up the project and activated the service account with its key. I also called gcloud auth and entered my credentials.

Here is my code (derived from the python sample text recognition Vision API):

 import base64 import os import re import sys from googleapiclient import discovery from googleapiclient import errors import nltk from nltk.stem.snowball import EnglishStemmer from oauth2client.client import GoogleCredentials import redis DISCOVERY_URL = 'https://{api}.googleapis.com/$discovery/rest?version={apiVersion}' # noqa BATCH_SIZE = 10 class VisionApi: """Construct and use the Google Vision API service.""" def __init__(self, api_discovery_file='/home/saadq/Dev/Projects/TM-visual-search/credentials-key.json'): self.credentials = GoogleCredentials.get_application_default() print self.credentials.to_json() self.service = discovery.build( 'vision', 'v1', credentials=self.credentials, discoveryServiceUrl=DISCOVERY_URL) print DISCOVERY_URL def detect_text(self, input_filenames, num_retries=3, max_results=6): """Uses the Vision API to detect text in the given file. """ images = {} for filename in input_filenames: with open(filename, 'rb') as image_file: images[filename] = image_file.read() batch_request = [] for filename in images: batch_request.append({ 'image': { 'content': base64.b64encode( images[filename]).decode('UTF-8') }, 'features': [{ 'type': 'TEXT_DETECTION', 'maxResults': max_results, }] }) request = self.service.images().annotate( body={'requests': batch_request}) try: responses = request.execute(num_retries=num_retries) if 'responses' not in responses: return {} text_response = {} for filename, response in zip(images, responses['responses']): if 'error' in response: print("API Error for %s: %s" % ( filename, response['error']['message'] if 'message' in response['error'] else '')) continue if 'textAnnotations' in response: text_response[filename] = response['textAnnotations'] else: text_response[filename] = [] return text_response except errors.HttpError as e: print("Http Error for %s: %s" % (filename, e)) except KeyError as e2: print("Key error: %s" % e2) vision = VisionApi() print vision.detect_text(['test_article.png']) 

This is the error message I get:

 Http Error for test_article.png: <HttpError 403 when requesting https://vision.googleapis.com/v1/images:annotate?alt=json returned "Google Cloud Vision API has not been used in project google.com:cloudsdktool before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/vision.googleapis.com/overview?project=google.com:cloudsdktool then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry."> 

I want to be able to use my own project as an example, and not by default (google.com:cloudsdktool).

+1
source share
2 answers

Download the credentials you created and update the GOOGLE_APPLICATION_CREDENTIALS environment variable to point to this file:

 export GOOGLE_APPLICATION_CREDENTIALS=/path/to/your/credentials-key.json 

Link: https://github.com/GoogleCloudPlatform/cloud-vision/tree/master/python/text#set-up-to-authenticate-with-your-projects-credentials

+4
source

Export did not work for me, even setting it in code:

 import os ... os.environ['GOOGLE APPLICATION_CREDENTIALS'] = 'path to servvice account json' os.environ['GCLOUD_PROJECT'] = 'project id' ... 

But this fix worked: Google cloud speech api throws 403 when trying to use it

+1
source

All Articles