API密钥在Google Cloud Vision API中的位置在哪里?

时间:2017-12-19 20:39:06

标签: python ocr google-cloud-vision

想要使用Google的Cloud Vision API进行OCR。使用python示例代码here,我们有:

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

with io.open(path, '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)))

我在哪里放置API密钥?我(显然)没有它就可以进行身份​​验证。

1 个答案:

答案 0 :(得分:0)

来自docs

  

如果您计划将服务帐户与客户端库代码一起使用,则需要设置环境变量。

     

通过设置环境变量GOOGLE_APPLICATION_CREDENTIALS为您的应用程序代码提供凭据。将[PATH]替换为您在上一步中下载的JSON文件的位置。

     

例如:

export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/service-account-file.json"

所以看起来你应该创建一个服务帐户,下载一个凭证文件,并设置一个环境变量来指向它。