我正在尝试获取页面、段落、块等类似于下面的代码,但使用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 GoogleCloudVisionVisionClient;
use GoogleCloudVisionV1ImageAnnotatorClient;
use GoogleCloudVisionV1TextAnnotation;
$path = 'img scr';
$imageAnnotator = new ImageAnnotatorClient();
$image = file_get_contents($path);
$response = $imageAnnotator->documentTextDetection($image);
$texts = $response->getFullTextAnnotation();
但是我在尝试使用TextAnnotation对象进行迭代时遇到了困难。要么我收到消息Cannot use object of type GoogleCloudVisionV1TextAnnotation as array
。即使我试图将对象强制为数组。
以下是来自谷歌的对象的文档
要从$response->getFullTextAnnotation();
获得结果,请尝试以下操作:
$response->getFullTextAnnotation()->getText();