0

Установил google/cloud-vision через композер Устанавливаю переменную окружения с путем до файла с ключем, выполняю код из примера:

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)
    /srv/www/site.ru/htdocs/vendor/google/protobuf/src/Google/Protobuf/Internal/GPBWire.php:288
#2: Google\Protobuf\Internal\GPBWire::readMessage(object, object)
    /srv/www/site.ru/htdocs/vendor/google/protobuf/src/Google/Protobuf/Internal/Message.php:420
#3: Google\Protobuf\Internal\Message::parseFieldFromStreamNoTag(object, object, object)
    /srv/www/site.ru/htdocs/vendor/google/protobuf/src/Google/Protobuf/Internal/Message.php:500
#4: Google\Protobuf\Internal\Message->parseFieldFromStream(integer, object, object)
    /srv/www/site.ru/htdocs/vendor/google/protobuf/src/Google/Protobuf/Internal/Message.php:796
#5: Google\Protobuf\Internal\Message->parseFromStream(object)
    /srv/www/site.ru/htdocs/vendor/google/protobuf/src/Google/Protobuf/Internal/Message.php:760
    /srv/www/site.ru/htdocs/vendor/google/protobuf/src/Google/Protobuf/Internal/DescriptorPool.php:61
#7: Google\Protobuf\Internal\DescriptorPool->internalAddGeneratedFile(string, boolean)
    /srv/www/site.ru/htdocs/vendor/google/common-protos/metadata/Api/Annotations.php:26
#8: GPBMetadata\Google\Api\Annotations::initOnce()
    /srv/www/site.ru/htdocs/vendor/google/cloud-vision/metadata/V1/ImageAnnotator.php:17
#9: GPBMetadata\Google\Cloud\Vision\V1\ImageAnnotator::initOnce()
    /srv/www/site.ru/htdocs/vendor/google/cloud-vision/src/V1/Image.php:52
#10: Google\Cloud\Vision\V1\Image->__construct()
    /srv/www/site.ru/htdocs/vendor/google/cloud-vision/src/VisionHelpersTrait.php:101
#11: Google\Cloud\Vision\V1\ImageAnnotatorClient->createImageHelper(string, string, string)
    /srv/www/site.ru/htdocs/vendor/google/cloud-vision/src/V1/ImageAnnotatorClient.php:82
#12: Google\Cloud\Vision\V1\ImageAnnotatorClient->createImageObject(string)
    /srv/www/site.ru/htdocs/vendor/google/cloud-vision/src/V1/ImageAnnotatorClient.php:118
#13: Google\Cloud\Vision\V1\ImageAnnotatorClient->annotateImage(string, array, array)
    /srv/www/site.ru/htdocs/vendor/google/cloud-vision/src/V1/ImageAnnotatorClient.php:575
#14: Google\Cloud\Vision\V1\ImageAnnotatorClient->annotateSingleFeature(string, integer, array)
    /srv/www/site.ru/htdocs/vendor/google/cloud-vision/src/V1/ImageAnnotatorClient.php:264
#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;
}

Если пропустить это условие и всегда выполнять

$this->current_limit = $current_position + $byte_limit;
$this->recomputeBufferLimits();

то дальше вылазит ошибка:

    Expect utf-8 encoding. (0)
/srv/www/site.ru/htdocs/vendor/google/protobuf/src/Google/Protobuf/Internal/GPBUtil.php:108
#0: Google\Protobuf\Internal\GPBUtil::checkString(string, boolean)
    /srv/www/site.ru/htdocs/vendor/google/protobuf/src/Google/Protobuf/Internal/FileOptions.php:549
#1: Google\Protobuf\Internal\FileOptions-&gt;setGoPackage(string)
    /srv/www/site.ru/htdocs/vendor/google/protobuf/src/Google/Protobuf/Internal/Message.php:525
#2: Google\Protobuf\Internal\Message-&gt;parseFieldFromStream(integer, object, object)
    /srv/www/site.ru/htdocs/vendor/google/protobuf/src/Google/Protobuf/Internal/Message.php:796
#3: Google\Protobuf\Internal\Message-&gt;parseFromStream(object)
    /srv/www/site.ru/htdocs/vendor/google/protobuf/src/Google/Protobuf/Internal/GPBWire.php:289
#4: Google\Protobuf\Internal\GPBWire::readMessage(object, object)
    /srv/www/site.ru/htdocs/vendor/google/protobuf/src/Google/Protobuf/Internal/Message.php:420
#5: Google\Protobuf\Internal\Message::parseFieldFromStreamNoTag(object, object, object)
    /srv/www/site.ru/htdocs/vendor/google/protobuf/src/Google/Protobuf/Internal/Message.php:500
#6: Google\Protobuf\Internal\Message-&gt;parseFieldFromStream(integer, object, object)
    /srv/www/site.ru/htdocs/vendor/google/protobuf/src/Google/Protobuf/Internal/Message.php:796
#7: Google\Protobuf\Internal\Message-&gt;parseFromStream(object)
    /srv/www/site.ru/htdocs/vendor/google/protobuf/src/Google/Protobuf/Internal/GPBWire.php:289
#8: Google\Protobuf\Internal\GPBWire::readMessage(object, object)
    /srv/www/site.ru/htdocs/vendor/google/protobuf/src/Google/Protobuf/Internal/Message.php:420
#9: Google\Protobuf\Internal\Message::parseFieldFromStreamNoTag(object, object, object)
    /srv/www/site.ru/htdocs/vendor/google/protobuf/src/Google/Protobuf/Internal/Message.php:500
#10: Google\Protobuf\Internal\Message-&gt;parseFieldFromStream(integer, object, object)
    /srv/www/site.ru/htdocs/vendor/google/protobuf/src/Google/Protobuf/Internal/Message.php:796
#11: Google\Protobuf\Internal\Message-&gt;parseFromStream(object)
    /srv/www/site.ru/htdocs/vendor/google/protobuf/src/Google/Protobuf/Internal/Message.php:760
#12: Google\Protobuf\Internal\Message-&gt;mergeFromString(string)
    /srv/www/site.ru/htdocs/vendor/google/protobuf/src/Google/Protobuf/Internal/DescriptorPool.php:61
#13: Google\Protobuf\Internal\DescriptorPool-&gt;internalAddGeneratedFile(string, boolean)
    /srv/www/site.ru/htdocs/vendor/google/common-protos/metadata/Api/Annotations.php:26
#14: GPBMetadata\Google\Api\Annotations::initOnce()
    /srv/www/site.ru/htdocs/vendor/google/cloud-vision/metadata/V1/ImageAnnotator.php:17
#15: GPBMetadata\Google\Cloud\Vision\V1\ImageAnnotator::initOnce()
    /srv/www/site.ru/htdocs/vendor/google/cloud-vision/src/V1/Image.php:52
#16: Google\Cloud\Vision\V1\Image-&gt;__construct()
    /srv/www/site.ru/htdocs/vendor/google/cloud-vision/src/VisionHelpersTrait.php:101
#17: Google\Cloud\Vision\V1\ImageAnnotatorClient-&gt;createImageHelper(string, string, boolean)
    /srv/www/site.ru/htdocs/vendor/google/cloud-vision/src/V1/ImageAnnotatorClient.php:82
#18: Google\Cloud\Vision\V1\ImageAnnotatorClient-&gt;createImageObject(boolean)
    /srv/www/site.ru/htdocs/vendor/google/cloud-vision/src/V1/ImageAnnotatorClient.php:118
#19: Google\Cloud\Vision\V1\ImageAnnotatorClient-&gt;annotateImage(boolean, array, array)
    /srv/www/site.ru/htdocs/vendor/google/cloud-vision/src/V1/ImageAnnotatorClient.php:575
#20: Google\Cloud\Vision\V1\ImageAnnotatorClient-&gt;annotateSingleFeature(boolean, integer, array)
    /srv/www/site.ru/htdocs/vendor/google/cloud-vision/src/V1/ImageAnnotatorClient.php:264
#21: Google\Cloud\Vision\V1\ImageAnnotatorClient-&gt;labelDetection(boolean)
    /srv/www/site.ru/htdocs/local/php_interface/GoogleCloud.php:38

Исключение выбрасывается тут:

public static function checkString(&$var, $check_utf8)
{
    if (is_array($var) || is_object($var)) {
        throw new \InvalidArgumentException("Expect string.");
    }
    if (!is_string($var)) {
        $var = strval($var);
    }
    if ($check_utf8 && !preg_match('//u', $var)) {
        throw new \Exception("Expect utf-8 encoding.");
    }
}

Сюда в переменной var попадают по очереди следующие значения:

google/api/http.proto
google.api
Http
rules
.google.api.HttpRule
fully_decode_reserved_expansion
HttpRule
selector
get
put
post
delete
patch
custom
.google.api.CustomHttpPattern
body
response_body
additional_bindings
.google.api.HttpRule
pattern
CustomHttpPattern
kind
path
com.google.api
HttpProto
google.golang.org/genproto/googleapis/api/annotations;annotations
GAPI
proto3
oogle/api/annotations.proto
oogle.api
oogle/protobuf/descriptor.protoB
om.google.apiB
nnotationsProtoP
oogle.golang.org/genproto/googleapis/api/annotations;annotations�

Если в этом месте менять кодировку, то после 'oogle.golang.org/genproto/googleapis/api/annotations;annotations?' еще приходит 'APIb' и затем вылетает ошибка:

    [Google\Protobuf\Internal\GPBDecodeException] 
Error occurred during parsing: Unexpected EOF inside string field. (0)
/srv/www/site.ru/htdocs/vendor/google/protobuf/src/Google/Protobuf/Internal/Message.php:406
#0: Google\Protobuf\Internal\Message::parseFieldFromStreamNoTag(object, object, NULL)
    /srv/www/site.ru/htdocs/vendor/google/protobuf/src/Google/Protobuf/Internal/Message.php:500
#1: Google\Protobuf\Internal\Message-&gt;parseFieldFromStream(integer, object, object)
    /srv/www/site.ru/htdocs/vendor/google/protobuf/src/Google/Protobuf/Internal/Message.php:796
#2: Google\Protobuf\Internal\Message-&gt;parseFromStream(object)
    /srv/www/site.ru/htdocs/vendor/google/protobuf/src/Google/Protobuf/Internal/GPBWire.php:289
#3: Google\Protobuf\Internal\GPBWire::readMessage(object, object)
    /srv/www/site.ru/htdocs/vendor/google/protobuf/src/Google/Protobuf/Internal/Message.php:420
#4: Google\Protobuf\Internal\Message::parseFieldFromStreamNoTag(object, object, object)
    /srv/www/site.ru/htdocs/vendor/google/protobuf/src/Google/Protobuf/Internal/Message.php:500
#5: Google\Protobuf\Internal\Message-&gt;parseFieldFromStream(integer, object, object)
    /srv/www/site.ru/htdocs/vendor/google/protobuf/src/Google/Protobuf/Internal/Message.php:796
#6: Google\Protobuf\Internal\Message-&gt;parseFromStream(object)
    /srv/www/site.ru/htdocs/vendor/google/protobuf/src/Google/Protobuf/Internal/Message.php:760
#7: Google\Protobuf\Internal\Message-&gt;mergeFromString(string)
    /srv/www/site.ru/htdocs/vendor/google/protobuf/src/Google/Protobuf/Internal/DescriptorPool.php:61
#8: Google\Protobuf\Internal\DescriptorPool-&gt;internalAddGeneratedFile(string, boolean)
    /srv/www/site.ru/htdocs/vendor/google/common-protos/metadata/Api/Annotations.php:26
#9: GPBMetadata\Google\Api\Annotations::initOnce()
    /srv/www/site.ru/htdocs/vendor/google/cloud-vision/metadata/V1/ImageAnnotator.php:17
#10: GPBMetadata\Google\Cloud\Vision\V1\ImageAnnotator::initOnce()
    /srv/www/site.ru/htdocs/vendor/google/cloud-vision/src/V1/Image.php:52
#11: Google\Cloud\Vision\V1\Image-&gt;__construct()
    /srv/www/site.ru/htdocs/vendor/google/cloud-vision/src/VisionHelpersTrait.php:101
#12: Google\Cloud\Vision\V1\ImageAnnotatorClient-&gt;createImageHelper(string, string, boolean)
    /srv/www/site.ru/htdocs/vendor/google/cloud-vision/src/V1/ImageAnnotatorClient.php:82
#13: Google\Cloud\Vision\V1\ImageAnnotatorClient-&gt;createImageObject(boolean)
    /srv/www/site.ru/htdocs/vendor/google/cloud-vision/src/V1/ImageAnnotatorClient.php:118
#14: Google\Cloud\Vision\V1\ImageAnnotatorClient-&gt;annotateImage(boolean, array, array)
    /srv/www/site.ru/htdocs/vendor/google/cloud-vision/src/V1/ImageAnnotatorClient.php:575
#15: Google\Cloud\Vision\V1\ImageAnnotatorClient-&gt;annotateSingleFeature(boolean, integer, array)
    /srv/www/site.ru/htdocs/vendor/google/cloud-vision/src/V1/ImageAnnotatorClient.php:264
#16: Google\Cloud\Vision\V1\ImageAnnotatorClient-&gt;labelDetection(boolean)
    /srv/www/site.ru/htdocs/local/php_interface/GoogleCloud.php:38

Кто-нибудь сталкивался? Полагаю, что корень зла лежит именно в $byte_limit из \Google\Protobuf\Internal\CodedInputStream::pushLimit, потому что потом начинаются обрезаться строки, которые приходят в \Google\Protobuf\Internal\GPBUtil::checkString

1 ответ 1

0

Решение: mbstring.func_overload 0

1
  • Будет более полезным написать подробно где Вы использовали даную конструкцию для решения возникшей у Вас проблемы.
    – V.March
    27 фев 2020 в 21:00

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.