I'm trying to use Google Cloud Vision's OCR as a substitute for Pytesseract. Part of this is defining the account key. The way google suggests doing this is by setting the GOOGLE_APPLICATION_CREDENTIALS
environmental variable to the path with the key. I use VSCode, so running $env:GOOGLE_APPLICATION_CREDENTIALS="C:\Users\User_1\key.json
command in powershell works just fine. However, the program I am writing will be used on my boss's computer, and I can't enter the powershell command every time. The example code Google gives to see if this worked is:
def implicit():
from google.cloud import storage
# If you don't specify credentials when constructing the client, the
# client library will look for credentials in the environment.
storage_client = storage.Client()
# Make an authenticated API request
buckets = list(storage_client.list_buckets())
print(buckets)
implicit()
Is there another way to pass the key to GCP without doing this? I know with the google sheets API, I use:
def auth():
creds = None
token_file = 'token.json'
SCOPES = ['https://www.googleapis.com/auth/drive', 'https://www.googleapis.com/auth/spreadsheets']
if os.path.exists(token_file):
creds = Credentials.from_authorized_user_file(token_file, SCOPES)
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
client_secrets_file='credentials.json', scopes=SCOPES)
creds = flow.run_local_server(port=0)
with open(token_file, 'w') as token:
token.write(creds.to_json())
return creds
However, I am under the impression that this is not applicable here. Or, if it is, I'm not sure how to apply it correctly. Are there any alternatives to running the powershell command every time?
The only alternative I've been able to think of is to find a way to run the powershell command through python, but the program will end up being a standalone .exe on my boss's computer (using pyinstaller), so I do not believe this will work.
CodePudding user response:
I think you have missed the documentation of passing key explicitly. From the doc :
from google.cloud import storage
# Explicitly use service account credentials by specifying the private key file.
storage_client = storage.Client.from_service_account_json('path_to_json_file')
# Make an authenticated API request
buckets = list(storage_client.list_buckets())
print(buckets)
Another simple way of doing this is setting the GOOGLE_APPLICATION_CREDENTIALS
in environment via python itself.
import os
from google.cloud import storage
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'path_to_json_file'
storage_client = storage.Client()
CodePudding user response:
The solution posted above by Sayan Bhattacharya is great. However, I've found an even better one; don't use Google Vision.
I ended up using an OCR called "Space OCR", you get up to 500 api calls per day, and up to 25,000 per month, however you manage to do that at a rate of 500/day I'm not sure, but those are the limitations on a free account. This OCR works great and is easy to use. You do need an account and an API key, but once you have those, the documentation is very easy to follow (and by that I mean ctrl c, ctrl v, and swap out the default api key for your own). It seems to be mostly accurate, and I would like to assume the reason it's missed the few times it has is because of me taking pictures with my phone instead of scanning them in through iOS notes or Genius Scan, but I haven't tested this. Space OCR does have a 2000-5000ms wait time from making the initial api call to receiving your results. This isn't a problem for me, as I'll probably only scan 40-50 documents per day, and the program I wrote looks through an entire directory, then processes all of the images in it, so I can just walk away for a few minutes.
Thanks all for the help.
EDIT
I did try using pytesseract, but it was so inaccurate using the exact same images I used with space OCR, it's almost laughable.