【问题标题】:Where does API key go in Google Cloud Vision API?Google Cloud Vision API 中的 API 密钥在哪里?
【发布时间】:2017-12-19 20:39:06
【问题描述】:

想要使用 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 密钥应该放在哪里?没有它我(显然)无法进行身份验证。

【问题讨论】:

  • 我也有同样的问题。有文档说明如何直接将 API 密钥与 REST API 一起使用,但没有说明如何将其与提供的客户端一起使用。

标签: python ocr google-cloud-vision


【解决方案1】:

来自docs

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

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

例如:

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

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

【讨论】:

  • @Adam4HD 您需要一种特定于 windows 的方式来设置环境值
  • 有什么方法可以在不设置GOOGLE_APPLICATION_CREDENTIALS变量的情况下指定API_KEY?
  • @RovshanMusayev,看看cloud.google.com/docs/authentication,看看这些策略是否适合你。我不确定自从我写下这个答案以来事情的状态发生了怎样的变化,但它现在可能是不完整的。
【解决方案2】:

您可以通过两种方式进行身份验证

  1. 将凭据文件导出为环境变量。这是一个示例代码:
from google.cloud import vision

def get_text_from_image(image_file):
        os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = "./creds/xxxx-xxxxx.json"
        try:
            # process_image is a method to convert numpy array to bytestream
            # (not of interest in this context hence not including it here)
            byte_img = process_image_to_bytes(image_file)
            client = vision.ImageAnnotatorClient()
            image = vision.Image(content=byte_img)
            response = client.text_detection(image=image)
            texts = response.text_annotations
            return texts
        except BaseException as e:
            print(str(e))

os.environ['GOOGLE_APPLICATION_CREDENTIALS'] 在这里工作

  1. 使用 oauth2
from google.cloud import vision
from google.oauth2 import service_account

def get_text_from_image(image_file):
        creds = service_account.Credentials.from_service_account_file("./creds/xxx-xxxxx.json")
        try:
            # process_image is a method to convert numpy array to bytestream
            # (not of interest in this context hence not including it here)
            byte_img = process_image_to_bytes(image_file)
            client = vision.ImageAnnotatorClient(credentials=creds)
            image = vision.Image(content=byte_img)
            response = client.text_detection(image=image)
            texts = response.text_annotations
            return texts
        except BaseException as e:
            print(str(e))

这里我们使用 google-auth 库从 JSON 凭据文件创建一个凭据文件,并将该对象传递给 ImageAnnotatorClient 进行身份验证。

希望这些示例 sn-ps 对您有所帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-26
    • 2017-10-25
    • 2020-03-20
    • 1970-01-01
    • 1970-01-01
    • 2017-07-07
    • 2011-09-28
    • 1970-01-01
    相关资源
    最近更新 更多