首页 > 解决方案 > 无法对 google automl 进行预测

问题描述

我训练了一个模型并想预测新图像的分数。

现在我执行以下函数,但它返回一个错误:

google.api_core.exceptions.PermissionDenied: 403 Permission 'automl.models.predict' denied on resource 'projects/project_id/locations/us-central1/models/model_id' (or it may not exist).

我不确定是不是因为位置不正确,即 us-central1?要检查的 gcloud 命令是什么?

如何解决这个问题呢?

非常感谢你。

def get_prediction(content, project_id, model_id):

    prediction_client = automl_v1beta1.PredictionServiceClient()
    name = 'projects/{}/locations/us-central1/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

标签: pythongoogle-app-enginegoogle-cloud-automl-nl

解决方案


权限错误消息通常在应用程序未正确验证时抛出;因此,需要验证您使用的服务帐户是否已分配所需的角色,并通过使用环境变量或在代码中明确指向您的服务帐户文件向您的应用程序提供凭据。请记住,当您在会话中设置环境变量值时,每次删除会话时都会重置它。

此外,AutoML Vision当前需要位置us-central1,如API 教程中所述。基于此,您在这方面应该没问题;但是,如果您想获取有关此配置的其他信息,可以查看projects.locations REST 方法。

您可以使用以下官方文档示例在代码中传递服务帐户密钥的路径以及快速入门指南,以了解有关开始使用 AutoML Vision 服务所需配置的更多信息。

namespace Google\Cloud\Samples\Auth;

// Imports the Google Cloud Storage client library.
use Google\Cloud\Storage\StorageClient;

function auth_cloud_explicit($projectId, $serviceAccountPath)
{
    # Explicitly use service account credentials by specifying the private key
    # file.
    $config = [
        'keyFilePath' => $serviceAccountPath,
        'projectId' => $projectId,
    ];
    $storage = new StorageClient($config);

    # Make an authenticated API request (listing storage buckets)
    foreach ($storage->buckets() as $bucket) {
        printf('Bucket: %s' . PHP_EOL, $bucket->name());
    }
}

推荐阅读