2

我正在爲Google Cloud Vision API執行「標籤檢測」教程。
當我將圖像傳遞給命令時,我希望能找回一些json,告訴我圖像中有什麼。警告:oauth2client.util:build()至多需要2個位置參數(給出3個)

但是,我得到這個錯誤,而不是。

>python label_request.py faulkner.jpg 
No handlers could be found for logger "oauth2client.util" 
WARNING:root:No module named locked_file 
Traceback (most recent call last): 
    File "/usr/local/lib/python2.7/site-packages/googleapiclient/discovery_cache/__init__.py", line 38, in autodetect 
    from . import file_cache 
    File "/usr/local/lib/python2.7/site-packages/googleapiclient/discovery_cache/file_cache.py", line 32, in <module> 
    from oauth2client.locked_file import LockedFile 
ImportError: No module named locked_file 
Traceback (most recent call last): 
    File "label_request.py", line 44, in <module> 
    main(args.image_file) 
    File "label_request.py", line 18, in main 
    service = build('vision', 'v1', http, discoveryServiceUrl=API_DISCOVERY_FILE) 
    File "/usr/local/lib/python2.7/site-packages/oauth2client/util.py", line 140, in positional_wrapper 
    return wrapped(*args, **kwargs) 
    File "/usr/local/lib/python2.7/site-packages/googleapiclient/discovery.py", line 202, in build 
    raise e 
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://vision.googleapis.com/$discovery/rest?version=v1 returned "Project has not activated the vision.googleapis.com API. Please enable the API for project google.com:cloudsdktool (#32555940559)."> 

很多在這裏。
但項目API 已啓用
所以這是錯誤信息的一部分是錯誤的。

看來,「最新版本的oauth2client v2.0.0發生了變化,它打破了與google-api-python-client模塊的兼容性」。
https://stackoverflow.com/a/35492604/2341218

我在應用此修正...

pip install --upgrade git+https://github.com/google/google-api-python-client 

應用此修復後,我得到更少的錯誤...

>python label_request.py faulkner.jpg 
No handlers could be found for logger "oauth2client.util" 
Traceback (most recent call last): 
    File "label_request.py", line 44, in <module> 
    main(args.image_file) 
    File "label_request.py", line 18, in main 
    service = build('vision', 'v1', http, discoveryServiceUrl=API_DISCOVERY_FILE) 
    File "/usr/local/lib/python2.7/site-packages/oauth2client/util.py", line 137, in positional_wrapper 
    return wrapped(*args, **kwargs) 
    File "/usr/local/lib/python2.7/site-packages/googleapiclient/discovery.py", line 209, in build 
    raise e 
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://vision.googleapis.com/$discovery/rest?version=v1 returned "Project has not activated the vision.googleapis.com API. Please enable the API for project google.com:cloudsdktool (#32555940559)."> 

看來,這個錯誤消息: 「無可以找到處理程序的記錄器「oauth2client.util」 實際上是掩蓋更詳細的警告/錯誤消息 ,我可以看到更詳細d一個通過將此代碼...

import logging 
logging.basicConfig() 

https://stackoverflow.com/a/29966147/2341218

>python label_request.py faulkner.jpg 
WARNING:oauth2client.util:build() takes at most 2 positional arguments (3 given) 
Traceback (most recent call last): 
    File "label_request.py", line 47, in <module> 
    main(args.image_file) 
    File "label_request.py", line 21, in main 
    service = build('vision', 'v1', http, discoveryServiceUrl=API_DISCOVERY_FILE) 
    File "/usr/local/lib/python2.7/site-packages/oauth2client/util.py", line 137, in positional_wrapper 
    return wrapped(*args, **kwargs) 
    File "/usr/local/lib/python2.7/site-packages/googleapiclient/discovery.py", line 209, in build 
    raise e 
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://vision.googleapis.com/$discovery/rest?version=v1 returned "Project has not activated the vision.googleapis.com API. Please enable the API for project google.com:cloudsdktool (#32555940559)."> 

因此,沒有我被困在此錯誤消息:
警告:oauth2client.util:建立()採用最多2位置參數(給出3)

有人建議,可以通過使用命名參數而不是位置表示法來避免此錯誤。
https://stackoverflow.com/a/16643215/2341218

但是,我不確定究竟在哪裏可以進行此更改。
我實際上沒有在代碼中看到oauth2client.util:build()函數。
下面是谷歌代碼(略有修改):

>cat label_request.py 
import argparse 
import base64 
import httplib2 

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

import logging 
logging.basicConfig() 

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) 

    with open(photo_file, 'rb') as image: 
    image_content = base64.b64encode(image.read()) 
    service_request = service.images().annotate(
     body={ 
     'requests': [{ 
      'image': { 
      'content': image_content 
      }, 
      'features': [{ 
      'type': 'LABEL_DETECTION', 
      'maxResults': 1, 
      }] 
     }] 
     }) 
    response = service_request.execute() 
    label = response['responses'][0]['labelAnnotations'][0]['description'] 
    print('Found label: %s for %s' % (label, photo_file)) 
    return 0 

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) 
+0

調用堆棧指示'build'方法可能會將參數傳遞給堆棧。你會修改第21行('service = build(...)'以使用命名參數。 – TehCorwiz

+0

很好的建議。謝謝。構建方法似乎不喜歡命名參數。也許我會嘗試恢復到版本google-client-api that works。 –

回答

4

我有完全相同的問題,我只是解決了做這行代碼(你必須已經安裝gcloud):

gcloud auth activate-service-account --key-file <service-account file.json> 

然後:

$ export GOOGLE_APPLICATION_CREDENTIALS=<path_to_service_account_file> 

希望有所幫助!

+0

非常有趣,我仍然得到警告,但異常和堆棧跟蹤已經消失。 –

4

的發現模塊的構建方法的文檔位於: https://google-api-python-client.googlecode.com/hg/docs/epy/apiclient.discovery-module.html#build

的調用語法是:

build(serviceName, version, http=None, discoveryServiceUrl=DISCOVERY_URI, 
developerKey=None, model=None, requestBuilder=HttpRequest) 

特別是建立需要兩個位置參數和多達五種可選命名參數。如果你想通過所謂的HTTP HTTP處理程序,您將調用與

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

建設如果你作出這樣

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

構建將使用默認HTTP處理程序,而不是通話。無論哪種方式,你都不應該看到警告了。

相關問題