Google 云视界 API - Python

Google Cloud Vision API - Python

我似乎无法在我的 google 云视觉代码中找到添加 API 密钥或我需要找到 google 凭据文件的位置:

    import argparse
    import base64
    import httplib2
    import validators
    import requests

    from apiclient.discovery import build
    from oauth2client.client import GoogleCredentials


    def main(photo_file):
      '''Run a label request on a single image'''

      API_DISCOVERY_FILE = 'https://vision.googleapis.com/$discovery/rest?version=v1'
      http = httplib2.Http()

      credentials = GoogleCredentials.get_application_default().create_scoped(
          ['https://www.googleapis.com/auth/cloud-platform'])
      credentials.authorize(http)

      service = build('vision', 'v1', http, discoveryServiceUrl=API_DISCOVERY_FILE)

    if __name__ == '__main__':
      parser = argparse.ArgumentParser()
      parser.add_argument(
        'image_file', help='The image you\'d like to label.')
      args = parser.parse_args()
      main(args.image_file)

    photo_file = "image_of_bottle.jpg"
    main(photo_file)

有谁知道我可以在哪里添加 API 密钥或定位到凭据文件?

编辑:添加了 Eray Balkanli 推荐的更改,并且我在调用中添加了我的图像文件。我不确定我是否做对了:

import argparse
import base64
import httplib2
import validators
import requests

from apiclient.discovery import build
from oauth2client.client import GoogleCredentials


def main(photo_file,developerkey):
  '''Run a label request on a single image'''

  API_DISCOVERY_FILE = 'https://vision.googleapis.com/$discovery/rest?version=v1'
  http = httplib2.Http()

  credentials = GoogleCredentials.get_application_default().create_scoped(
      ['https://www.googleapis.com/auth/cloud-platform'])
  credentials.authorize(http)

  service = build('vision', 'v1', http, discoveryServiceUrl=API_DISCOVERY_FILE,developerkey=INSERT API KEY)

if __name__ == '__main__':
  parser = argparse.ArgumentParser()
  parser.add_argument(
    'image_file', help='The image you\'d like to label.')
  args = parser.parse_args()
  main(args.image_file)

photo_file = "image_file.jpg"
main(photo_file,developerkey)

我收到以下错误:

usage: googleimagetest_v.4.py [-h] image_file
googleimagetest_v.4.py: error: too few arguments

有谁知道我该如何解决这个错误?

Google 愿景 API authenticating an API clearly gives a step by step guide of how you can get credentials and the last section in that page describes about Authenticating with Application Default Credentials 的文档:

The simplest way for applications to authenticate to a Google Cloud Platform API service is by using Application Default Credentials (ADC). Services using ADC first search for credentials within a GOOGLE_APPLICATION_CREDENTIALS environment variable. Unless you specifically wish to have ADC use other credentials (for example, user credentials), we recommend you set this environment variable to point to your service account key file (the .json file downloaded when you created a service account key, as explained in Set Up a Service Account.

$ export GOOGLE_APPLICATION_CREDENTIALS=<path_to_service_account_file>

如果重要(如果你还没有注册),Google 给你 free trial access (0 worth for 365 days) of their Cloud Platform. Their low pricing,恕我直言,因为 Vision API 应该让你做很多测试.


现在,您的代码中的错误;尽管从您对错误的描述中还不清楚,但看起来您的代码接受了一个参数,但您正试图 运行 您的 python 脚本没有参数。您可以通过以下方式 运行 您的代码:

python googleimagetest_v.4.py "image_file.jpg"

并且您不需要以下两行(因为它是 main() 的重复调用)

photo_file = "image_file.jpg"
main(photo_file,developerkey)

您已将 main() 函数定义为接受 2 个参数,但是当您在 __main__ 部分调用它时,您传递的是一个参数。您可能也想解决这个问题。