【问题标题】:How to iterate in google ocr textannotation object with php?如何使用 php 在 google ocr textannotation 对象中进行迭代?
【发布时间】:2020-11-15 06:30:06
【问题描述】:

我正在尝试获取页面、段落、块等 类似于下面的代码,但在 PHP 中。

https://cloud.google.com/vision/docs/fulltext-annotations

"""Returns document bounds given an image."""
client = vision.ImageAnnotatorClient()

bounds = []

with io.open(image_file, 'rb') as image_file:
    content = image_file.read()

image = types.Image(content=content)

response = client.document_text_detection(image=image)
document = response.full_text_annotation

# Collect specified feature bounds by enumerating all document features
for page in document.pages:
    for block in page.blocks:
        for paragraph in block.paragraphs:
            for word in paragraph.words:
                for symbol in word.symbols:
                    if (feature == FeatureType.SYMBOL):
                        bounds.append(symbol.bounding_box)

                if (feature == FeatureType.WORD):
                    bounds.append(word.bounding_box)

            if (feature == FeatureType.PARA):
                bounds.append(paragraph.bounding_box)

        if (feature == FeatureType.BLOCK):
            bounds.append(block.bounding_box)

# The list `bounds` contains the coordinates of the bounding boxes.

我能够创建这个

require AUTOLOAD; 
use Google\Cloud\Vision\VisionClient;
use Google\Cloud\Vision\V1\ImageAnnotatorClient;
use Google\Cloud\Vision\V1\TextAnnotation;

$path = 'img scr';

$imageAnnotator = new ImageAnnotatorClient();

$image = file_get_contents($path);
$response = $imageAnnotator->documentTextDetection($image);
$texts = $response->getFullTextAnnotation();

但我在尝试迭代 TextAnnotation 对象时遇到了困难。 要么我收到消息Cannot use object of type Google\Cloud\Vision\V1\TextAnnotation as array。即使我尝试将对象强制为数组。

这是来自 google 的 documentation 对象

【问题讨论】:

    标签: php ocr google-vision


    【解决方案1】:

    要从$response->getFullTextAnnotation(); 获取结果,请尝试以下操作:

    $response->getFullTextAnnotation()->getText();
    

    【讨论】:

      猜你喜欢
      • 2022-01-19
      • 2020-10-15
      • 2014-09-03
      • 1970-01-01
      • 2016-08-01
      • 2012-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多