Google Cloud Vision API 中的 API 密钥位于何处?

Eva*_*ler 6 python ocr google-cloud-vision

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

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)))
Run Code Online (Sandbox Code Playgroud)

我应该把 API 密钥放在哪里?没有它我(显然)无法进行身份验证。

Ave*_*ery 0

文档中,

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

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

例如:

export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/service-account-file.json"
Run Code Online (Sandbox Code Playgroud)

因此,您似乎应该创建一个服务帐户,下载一个凭据文件,并设置一个环境变量来指向它。