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处理程序,而不是通话。无论哪种方式,你都不应该看到警告了。

相关问题