简体   繁体   English

如何在python程序中使用Google Vision API?

[英]How to use Google Vision API in python program?

I am trying to run the most basic text detection and OCR (Optical Character Recognition) program of Google Vision API in python. 我正在尝试在python中运行Google Vision API的最基本的文本检测和OCR(光学字符识别)程序。 My source code is taken from the Google Cloud tutorial for this API and it is the following: 我的源代码来自该API的Google Cloud教程,内容如下:

import  io
from google.cloud import vision
from google.cloud.vision import types

def detect_text(file):
    """Detects text in the file."""
    client = vision.ImageAnnotatorClient()

    with io.open(file, 'rb') as image_file:
        content = image_file.read()

    image = types.Image(content=content)

    response = client.text_detection(image=image)
    texts = response.text_annotations
    print('Texts:')

    for text in texts:
        print('\n"{}"'.format(text.description))

        vertices = (['({},{})'.format(vertex.x, vertex.y)
                    for vertex in text.bounding_poly.vertices])

        print('bounds: {}'.format(','.join(vertices)))

file_name = "prescription.jpg"
detect_text(file_name)

However I get the following error: 但是我收到以下错误:

google.auth.exceptions.DefaultCredentialsError: Could not automatically determine credentials. Please set GOOGLE_APPLICATION_CREDENTIALS or
explicitly create credential and re-run the application. For more
information, please see
https://developers.google.com/accounts/docs/application-default-credentials.

This is weird because: 这很奇怪,因为:

1) I created a new service account 1)我创建了一个新的服务帐户

2) I added export GOOGLE_APPLICATION_CREDENTIALS="/Users/User/PycharmProjects/GCP-OCR/Trial-e046e4bc6ce1.json" to my .bash_profile (I put the json file at the Pycharm file of this project) Perhaps the only weird thing is that the private key at the json file is around 20 lines while I would expect to be around 1 line. 2)我将export GOOGLE_APPLICATION_CREDENTIALS="/Users/User/PycharmProjects/GCP-OCR/Trial-e046e4bc6ce1.json"到了我的.bash_profile中(我将json文件放在了该项目的Pycharm文件中)也许唯一奇怪的是json文件的私钥约为20行,而我希望约为1行。

How can I fix this bug and make the program running? 如何解决此错误并使程序运行?

By the way the problem is solved if I simply add 顺便说一句,如果我简单地添加

import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "/Users/User/PycharmProjects/GCP-OCR/Trial-e046e4bc6ce1.json"

to my source code. 到我的源代码。

The credentials are fine if this works when you set up the variable in your Python code. 如果在Python代码中设置变量时可以使用凭据,则可以使用凭据。 I tested it myself and it works using your initial approach, too. 我自己进行了测试,它也可以使用您的初始方法工作。 Don't forget to update your .bash_profile with: 不要忘记使用以下命令更新.bash_profile:

source ~/.bash_profile

as explained in How to reload .bash_profile from the command line? 如何从命令行重新加载.bash_profile中所述?

Verify that the environment variable is properly set with: 验证是否正确设置了环境变量:

echo $GOOGLE_APPLICATION_CREDENTIALS

which should output the correct path to your credentials 这应该输出正确的路径到您的凭据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM