简体   繁体   English

Google Cloud Vision API - Python

[英]Google Cloud Vision API - Python

I can't seem to find where to add the API key or where I need to locate to the google credentials file in my google cloud vision code: 我似乎无法在我的谷歌云视觉代码中找到添加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)

Does anyone know where I can add the API key or locate to the credentials file? 有谁知道我可以在哪里添加API密钥或找到凭证文件?

EDIT: Added changes recommended by Eray Balkanli and I added my image file in the call. 编辑:添加了Eray Balkanli推荐的更改,我在通话中添加了我的图像文件。 I'm not sure if I did it correctly: 我不确定我是否做得正确:

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)

I received the following error: 我收到以下错误:

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

Does anyone know how I can solve this error? 有谁知道我怎么能解决这个错误?

Google Vision API documentation for 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 : 用于对API进行身份验证的 Google Vision API文档清楚地提供了如何获取凭据的分步指南,该页面的最后一部分介绍了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). 应用程序向Google Cloud Platform API服务进行身份验证的最简单方法是使用应用程序默认凭据(ADC)。 Services using ADC first search for credentials within a GOOGLE_APPLICATION_CREDENTIALS environment variable. 使用ADC的服务首先在GOOGLE_APPLICATION_CREDENTIALS环境变量中搜索凭据。 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. 除非您特别希望让ADC使用其他凭据(例如,用户凭据),否则我们建议您将此环境变量设置为指向您的服务帐户密钥文件(创建服务帐户密钥时下载的.json文件,如设置服务帐户。

$ export GOOGLE_APPLICATION_CREDENTIALS=<path_to_service_account_file> $ export GOOGLE_APPLICATION_CREDENTIALS = <path_to_service_account_file>

If it matters (in case you haven't yet registered), Google gives you free trial access ($300 worth for 365 days) of their Cloud Platform. 如果重要(如果您尚未注册),Google会为您提供免费试用访问权限(365天价值300美元)的云平台。 Their low pricing , imho, for Vision API should let you do a lot for your testing. 它们对Vision API的低价格 imho应该可以让你为测试做很多事情。


Now, the error in your code; 现在,代码中的错误; even though things are not clear from what you described about the error, it looks like your code accepts an argument but you are trying to run your python script without one. 即使你对错误描述的内容不清楚,看起来你的代码接受了一个参数,但你试图运行你的python脚本而没有一个。 You can run your code in the following manner: 您可以按以下方式运行代码:

python googleimagetest_v.4.py "image_file.jpg"

and you don't need the following 2 lines (because it is duplicate call for main() ) 并且您不需要以下2行(因为它是对main()重复调用)

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

You have defined main() function to accept 2 arguments but when you call it in __main__ section you are passing a single argument. 您已经定义了main()函数来接受2个参数,但是当您在__main__部分中调用它时,您传递的是一个参数。 You might want to fix that too. 您可能也想解决这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM