[Google AutoML]未记录发送PHP中的行有效载荷以使用表模型进行预测的错误

时间:2019-06-27 12:38:26

标签: php automl google-cloud-automl

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

使用CURL做到这一点没有问题,在CURL中,我将以下数据发布(使用承载令牌)到该路径https://automl.googleapis.com/v1beta1/projects/{{project-id}}/locations/us-central1/models/{{model-id}}:predict

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

我正在尝试对PHP执行相同操作,但是无法生成正确的有效负载。

请注意,作为表格模型,它需要一个行;这里有更多信息: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"
            }
        }
    ]
}

1 个答案:

答案 0 :(得分: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;
        }