【发布时间】: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