Google Cloud Vision - 解析期间发生 PHP 错误

我正在使用适用于 PHP 的 Vision API 客户端库。这是我的代码:


use Google\Cloud\Vision\V1\ImageAnnotatorClient;

putenv("GOOGLE_APPLICATION_CREDENTIALS=/json.json");

$imageAnnotator = new ImageAnnotatorClient();

$fileName = 'textinjpeg.jpg';

$image = file_get_contents($fileName);

$response = $imageAnnotator->labelDetection($image);

$labels = $response->getLabelAnnotations();

if ($labels) {

    echo("Labels:" . PHP_EOL);

    foreach ($labels as $label) {

        echo($label->getDescription() . PHP_EOL);

    }

} else {

    echo('No label found' . PHP_EOL);

}

我收到这个错误:


Error occurred during parsing: Fail to push limit. (0)

/srv/www/site.ru/htdocs/vendor/google/protobuf/src/Google/Protobuf/Internal/CodedInputStream.php:345

#0: Google\Protobuf\Internal\CodedInputStream->pushLimit(integer)

    /srv/www/site.ru/htdocs/vendor/google/protobuf/src/Google/Protobuf/Internal/CodedInputStream.php:368

#1: Google\Protobuf\Internal\CodedInputStream->incrementRecursionDepthAndPushLimit(integer, integer, integer)

....

....

....

#15: Google\Cloud\Vision\V1\ImageAnnotatorClient->labelDetection(string)

/srv/www/site.ru/htdocs/local/php_interface/GoogleCloud.php:41

这是异常来自的地方:


public function pushLimit($byte_limit)

{

    // Current position relative to the beginning of the stream.

    $current_position = $this->current();

    $old_limit = $this->current_limit;


    // security: byte_limit is possibly evil, so check for negative values

    // and overflow.

    if ($byte_limit >= 0 &&

        $byte_limit <= PHP_INT_MAX - $current_position &&

        $byte_limit <= $this->current_limit - $current_position) {

        $this->current_limit = $current_position + $byte_limit;

        $this->recomputeBufferLimits();

    } else {

        throw new GPBDecodeException("Fail to push limit.");

    }


    return $old_limit;

}

$byte_limit <= $this->current_limit - $current_position是真的


我应该增加 current_position 吗?如果我应该,我该怎么做?在服务器或 PHP 配置中更改某些内容?


婷婷同学_
浏览 185回答 3
3回答

青春有我

这是一件很疯狂的事情!论坛中不时出现“无法推送限制”错误。关于问题可能出在哪里,那里给出了各种想法。一个原因可能是源代码通过 Composer 在本地 PC 上编写,然后通过 (S)FTP 传输到服务器。FTP 程序根据文件扩展名决定将数据以 ASCII 格式还是二进制格式保存在服务器上。在 vendor/google/protobuf/src/Google/Protobuf/ 中,有各种生成的文件具有 .php 扩展名,但实际上是二进制文件!(如果您打开文件,您可以立即看到它,例如:vendor/google/protobuf/src/GPBMetadata/Google/Protobuf/Any.php)在我的案例中,通过二进制文件将这些文件显式传输到服务器的解决方案有效!如果有疑问,请将 Google/protobuf 中的完整模块作为二进制文件传输...

慕码人8056858

mbstring.func_overload 为 2 这是错误的原因 更改为 0 并且有效

暮色呼如

你提到那$byte_limit <= $this->current_limit - $current_position是真的,所以要么$byte_limit >= 0或要么$byte_limit <= PHP_INT_MAX - $current_position是假的。如果$byte_limit <= PHP_INT_MAX - $current_position是假的,那么增加$current_position,不会变成真的。如果要调整值,使表达式被评估为真,则需要增加 PHP_INT_MAX 的值。如果$byte_limit >= 0为假,则修改$current_limit不会避免异常。无论哪种方式,错误似乎都是protobufphp 库的问题,所以我建议您在那里报告问题,而不是尝试直接修改值。
打开App,查看更多内容
随时随地看视频慕课网APP