2016-04-12 12 views
2

私は、APIキーを追加する場所を見つけることができないようまたは私はGoogleの資格情報を検索する必要がある場所に私のGoogleのクラウドビジョンコードでファイル:GoogleクラウドビジョンAPI - Pythonの

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キーを追加したり、資格情報ファイルを見つけることができますか?

EDIT: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 

誰もが、私はこのエラーを解決する方法を知っています私は、私はそれを正しくやったかどうかわからないんだけど?

+0

あなたのクラスがどのくらい正確に走っているのか聞いてみました。このクラスを実行する必要があります:$ python googleimagetest_v.4.py image_file ......どのように実行しようとしていますか質問を挿入してください。この質問をチェックする:http://stackoverflow.com/questions/30638974/google-prediction-api-hello-prediction-error-to-few-arguments –

+0

pythonの例を見てきましたかhttps://github.com/GoogleCloudPlatform/cloud-vision/tree/master/python?この例には、資格情報の設定に関する情報があります。https://github.com/GoogleCloudPlatform/cloud-vision/tree/master/python/landmark_detection/ –

答えて

2
authenticating an APIため

Googleのビジョン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). 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のクラウドプラットフォームからのアクセス(365日間300ドル相当)を提供します。 Vision API用のpricing、imhoの低さは、テストのために多くのことを可能にするはずです。


ここで、コードに誤りがあります。あなたがエラーについて説明したことから物事が明らかではないにもかかわらず、あなたのコードが引数を受け入れるが、Pythonスクリプトを使わずにPythonスクリプトを実行しようとしているようです。あなたは次のようにあなたのコードを実行することができます。

python googleimagetest_v.4.py "image_file.jpg" 

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

あなたが受け入れることmain()機能を定義している(それはmain()の重複呼び出しがあるので)次の2行は必要ありません。 2引数がありますが、それを__main__セクションで呼び出すと、1つの引数が渡されます。あなたもそれを修正したいかもしれません。

関連する問題