我似乎无法在我的谷歌云视觉代码中找到添加 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
有谁知道我该如何解决这个错误?