0

我正在尝试对我创建的 Auto ML Table 模型进行预测。

https://automl.googleapis.com/v1beta1/projects/{{project-id}}/locations/us-central1/models/{{model-id}}:predict使用 CURL 执行此操作没有问题,其中(使用不记名令牌)我将以下数据发布到此路径:

{
  "payload": {
    "row": {
      "values": [
        "ARIZONA",
        "JUICE-READY TO DRINK",
        "4",
        "1",
        "23",
        "oz",
        "2.00",
        "4.00",
        "/$con arizona cc/u"
      ]
    }
  }
}

我正在尝试对 PHP 做同样的事情,但无法生成正确的有效负载。

请注意,作为 Table 模型,它需要一个 Row;这里有更多信息:https ://cloud.google.com/vision/automl/docs/reference/rest/v1beta1/projects.locations.models/predict#examplepayload

这是错误:

Fatal error: Uncaught Exception: Expect Google\Cloud\Dlp\V2\Value. in /Users/torlanco/Documents/www/facts-data-entry/vendor/google/protobuf/src/Google/Protobuf/Internal/GPBUtil.php:197
Stack trace: 
#0 /Users/torlanco/Documents/www/facts-data-entry/vendor/google/protobuf/src/Google/Protobuf/Internal/RepeatedField.php(183): Google\Protobuf\Internal\GPBUtil::checkMessage('ARIZONA', 'Google\\Cloud\\Dl...') 
#1 /Users/torlanco/Documents/www/facts-data-entry/vendor/google/protobuf/src/Google/Protobuf/Internal/GPBUtil.php(209): Google\Protobuf\Internal\RepeatedField->offsetSet(NULL, 'ARIZONA') 
#2 /Users/torlanco/Documents/www/facts-data-entry/vendor/google/cloud/Dlp/src/V2/Table/Row.php(51): Google\Protobuf\Internal\GPBUtil::checkRepeatedField(Array, 11, 'Google\\Cloud\\Dl...') 
#3 /Users/torlanco/Documents/www/facts-data-entry/vendor/google/protobuf/src/Google/Protobuf/Internal/Message.php(1009): Google\Cloud\Dlp\V2\Table\Row->setValues(Array) 
#4 /Users/torlanco/Documents/www/facts-data-entry/vendor/google/protobuf/src/Google/Pro in /Users/torlanco/Documents/www/facts-data-entry/vendor/google/protobuf/src/Google/Protobuf/Internal/GPBUtil.php on line 197

这是我的代码:

<?php
require 'vendor/autoload.php';

use Google\Cloud\AutoMl\V1beta1\ExamplePayload;
use Google\Cloud\AutoMl\V1beta1\Image;
use Google\Cloud\AutoMl\V1beta1\TextSnippet;
use Google\Cloud\AutoMl\V1beta1\PredictionServiceClient;
use Google\Cloud\Dlp\V2\Table\Row;

startPredict('sapient-spark-240001', 'TBL4416632655659925504');

function startPredict($projectId, $modelId){
  try {

    $predictionServiceClient = new PredictionServiceClient([
      'credentials' => 'assets/google-cloud-cred.json',
      'transport' => 'rest'
    ]);

    $formattedName = $predictionServiceClient->modelName($projectId, 'us-central1', $modelId);

    $row = new Row( ['values' => ["ARIZONA","JUICE-READY TO DRINK","4","1","23","oz","2.00","4.00","/$con arizona cc/u"]]);
    $payload = new ExamplePayload(['row'=> $row]);

    $response = $predictionServiceClient->predict($formattedName, $payload);
    $res = $response->serializeToJsonString();
    $predictResult = json_decode($res, true);

  } finally {
    $predictionServiceClient->close();
  }

}
?>

我期待以下回应:

{
    "payload": [
        {
            "tables": {
                "score": 0.0025087874,
                "value": "REFRIGERATED"
            }
        },
        {
            "tables": {
                "score": 0.29803053,
                "value": "BOTTLED"
            }
        },
        {
            "tables": {
                "score": 0.6979729,
                "value": "CANNED"
            }
        },
        {
            "tables": {
                "score": 0.0014877517,
                "value": "ASEPTIC"
            }
        }
    ]
}
4

1 回答 1

0

我没有给出整个代码,但这就是我在项目中所做的。在你的函数中替换它。

        $image = file_get_contents($image_url);

        $formattedName = $predictionServiceClient->modelName(env('GCLOUD_PROJECT'), env('GCLOUD_COMPUTE'), env('GCLOUD_MODEL_ID'));

        $googleImage = new Image();
        $googleImage->setImageBytes($image);

        $payload = new ExamplePayload(['image' => $googleImage]);

        $response = $predictionServiceClient->predict($formattedName, $payload, ['score_threshold' => 0.01]);
        $gAML_results = $response->getPayload();

        $results = [];
        foreach ($gAML_results as $row) {
            $data['label'] = $row->getDisplayName();
            $data['score'] = $row->getClassification()->getScore();
            $results[] = $data;
        }
于 2019-07-05T09:44:04.557 回答