嗨,首先也是最重要的,这是我第一次使用谷歌服务。我正在尝试开发一个与谷歌AutoML视觉Api (自定义模型)的应用程序。我已经构建了一个自定义模型并生成了API密钥(我希望我这样做是正确的)。
经过多次尝试,通过离子和Android开发,但未能连接到API。
我现在已经使用了Python (在Google Colab上)中给定代码的预测建模,即使这样,我仍然收到一条错误消息,说无法自动确定凭据。我不确定我在这方面做错了什么。请帮帮忙。快死了。
#installing & importing libraries
!pip3 install google-cloud-automl
import sys
from google.cloud import automl_v1beta1
from google.cloud.automl_v1beta1.proto import service_pb2
#import key.json file generated by GOOGLE_APPLICATION_CREDENTIALS
from google.colab import files
credentials = files.upload()
#explicit function given by Google accounts
[https://cloud.google.com/docs/authentication/production#auth-cloud-implicit-python][1]
def explicit():
from google.cloud import storage
# Explicitly use service account credentials by specifying the private key
# file.
storage_client = storage.Client.from_service_account_json(credentials)
# Make an authenticated API request
buckets = list(storage_client.list_buckets())
print(buckets)
#import image for prediction
from google.colab import files
YOUR_LOCAL_IMAGE_FILE = files.upload()
#prediction code from modelling
def get_prediction(content, project_id, model_id):
prediction_client = automl_v1beta1.PredictionServiceClient()
name = 'projects/{}/locations/uscentral1/models/{}'.format(project_id,
model_id)
payload = {'image': {'image_bytes': content }}
params = {}
request = prediction_client.predict(name, payload, params)
return request # waits till request is returned
#print function substitute with values
content = YOUR_LOCAL_IMAGE_FILE
project_id = "REDACTED_PROJECT_ID"
model_id = "REDACTED_MODEL_ID"
print (get_prediction(content, project_id, model_id))运行最后一行代码时出现错误消息:

发布于 2019-04-19 22:59:25
credentials = files.upload()
storage_client = storage.Client.from_service_account_json(credentials)我认为这两行就是问题所在。第一个函数实际加载文件的内容,而第二个函数需要文件的路径,而不是内容。
让我们先来看第一行:我发现仅仅传递调用credentials = files.upload()之后得到的credentials是不会像the docs for it中解释的那样工作的。像您这样做,credentials实际上并不直接包含文件的值,而是一个文件名和内容的字典。
假设您只上传了1个凭据文件,您可以获得该文件的内容,如下所示的(stolen from this SO answer)
from google.colab import files
uploaded = files.upload()
credentials_as_string = uploaded[uploaded.keys()[0]]现在,我们实际上已经将上传文件的内容作为字符串,下一步是在其中创建一个实际的credentials对象。
This answer on Github展示了如何从转换为json的字符串创建credentials对象。
import json
from google.oauth2 import service_account
credentials_as_dict = json.loads(credentials_as_string)
credentials = service_account.Credentials.from_service_account_info(credentials_as_dict)最后,我们可以使用此credentials对象创建存储客户端对象:
storage_client = storage.Client(credentials=credentials)请注意,我还没有测试过这一点,所以请试一试,看看它是否真的有效。
https://stackoverflow.com/questions/55702947
复制相似问题