谷歌云视觉API - Python

3

我似乎找不到在我的谷歌云视觉代码中添加API密钥的位置,也不知道需要在哪里找到谷歌凭证文件:

    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

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


我试图询问您如何确切地运行您的类。您应该运行这个类:$ python googleimagetest_v.4.py image_file ......请插入您尝试运行的方式。请检查这个问题:http://stackoverflow.com/questions/30638974/google-prediction-api-hello-prediction-error-too-few-arguments - Eray Balkanli
你看过这个Python示例吗?https://github.com/GoogleCloudPlatform/cloud-vision/tree/master/python?这个示例包含有关凭据设置的信息:https://github.com/GoogleCloudPlatform/cloud-vision/tree/master/python/landmark_detection/ - Padraic Cunningham
1个回答

2
Google Vision API文档中关于验证API的部分提供了逐步指南,清楚地介绍了如何获取凭据,该页面的最后一部分描述了"使用应用程序默认凭据进行身份验证"。使用应用程序默认凭据(ADC)是应用程序对Google Cloud Platform API服务进行身份验证的最简单方法。使用ADC的服务首先在GOOGLE_APPLICATION_CREDENTIALS环境变量中搜索凭据。除非您特别希望ADC使用其他凭据(例如用户凭据),否则建议将此环境变量设置为指向您的服务帐户密钥文件(即在创建服务帐户密钥时下载的.json文件,如《设置服务帐号》中所解释的)。如果需要(如果您尚未注册),Google为您提供免费试用(价值365天300美元)其云平台的访问权限。在我看来,对Vision API的低廉定价应该可以让您进行大量测试。现在谈论您代码中的错误;尽管从您对该错误的描述中无法明确事情,但似乎您的代码接受一个参数,但您正在尝试没有参数运行Python脚本。您可以按以下方式运行代码:
python googleimagetest_v.4.py "image_file.jpg"

你不需要以下两行代码(因为这是对main()的重复调用)

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

您定义了main()函数接受2个参数,但是当您在__main__部分调用它时,只传递了一个参数。您可能也想修复这个问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接