Skip to content

Commit ed314e3

Browse files
committed
detect document text samples
1 parent 180b524 commit ed314e3

File tree

5 files changed

+192
-0
lines changed

5 files changed

+192
-0
lines changed

vision/api/images/text.jpg

122 KB
Loading
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
/**
3+
* Copyright 2016 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
19+
namespace Google\Cloud\Samples\Vision;
20+
21+
use Symfony\Component\Console\Command\Command;
22+
use Symfony\Component\Console\Input\InputArgument;
23+
use Symfony\Component\Console\Input\InputOption;
24+
use Symfony\Component\Console\Input\InputInterface;
25+
use Symfony\Component\Console\Output\OutputInterface;
26+
27+
/**
28+
* Command line utility to detect which language some text is written in.
29+
*/
30+
class DetectDocumentTextCommand extends Command
31+
{
32+
protected function configure()
33+
{
34+
$this
35+
->setName('document-text')
36+
->setDescription('Detect document text in an image using '
37+
. 'Google Cloud Vision API')
38+
->setHelp(<<<EOF
39+
The <info>%command.name%</info> command prints document text for an image using
40+
the Google Cloud Vision API.
41+
42+
<info>php %command.full_name% -k YOUR-API-KEY path/to/image.png</info>
43+
44+
EOF
45+
)
46+
->addArgument(
47+
'path',
48+
InputArgument::REQUIRED,
49+
'The image to examine.'
50+
)
51+
->addOption(
52+
'project',
53+
'p',
54+
InputOption::VALUE_REQUIRED,
55+
'Your Project ID.'
56+
)
57+
;
58+
}
59+
60+
protected function execute(InputInterface $input, OutputInterface $output)
61+
{
62+
$projectId = $input->getOption('project');
63+
$path = $input->getArgument('path');
64+
if (preg_match('/^gs:\/\/([a-z0-9\._\-]+)\/(\S+)$/', $path, $matches)) {
65+
list($bucketName, $objectName) = array_slice($matches, 1);
66+
$result = require __DIR__ . '/snippets/detect_document_text_gcs.php';
67+
} else {
68+
$result = require __DIR__ . '/snippets/detect_document_text.php';
69+
}
70+
}
71+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* Copyright 2016 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace Google\Cloud\Samples\Vision;
19+
20+
// [START vision_fulltext_detection]
21+
use Google\Cloud\Vision\VisionClient;
22+
23+
// $projectId = 'YOUR_PROJECT_ID';
24+
// $path = 'path/to/your/image.jpg'
25+
26+
$vision = new VisionClient([
27+
'projectId' => $projectId,
28+
]);
29+
$image = $vision->image(file_get_contents($path), ['DOCUMENT_TEXT_DETECTION']);
30+
$annotation = $vision->annotate($image);
31+
$document = $annotation->fullText();
32+
33+
# Print out unstructured document text
34+
$text = $document->text();
35+
print('Document text: ' . $text . PHP_EOL);
36+
37+
# Print out more detailed and structured information about document text
38+
foreach ($document->pages() as $page) {
39+
foreach ($page['blocks'] as $block) {
40+
$block_text = '';
41+
foreach ($block['paragraphs'] as $paragraph) {
42+
foreach ($paragraph['words'] as $word) {
43+
foreach ($word['symbols'] as $symbol) {
44+
$block_text = $block_text . $symbol['text'];
45+
}
46+
}
47+
}
48+
print('Block text: ' . $block_text . PHP_EOL);
49+
print('Block bounds:' . PHP_EOL);
50+
foreach ($block['boundingBox']['vertices'] as $vertice) {
51+
print('X: ' . $vertice['x'] . ' Y: ' . $vertice['y'] . PHP_EOL);
52+
}
53+
print(PHP_EOL);
54+
}
55+
}
56+
// [END vision_fulltext_detection]
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
/**
3+
* Copyright 2017 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace Google\Cloud\Samples\Vision;
19+
20+
# [START vision_fulltext_detection_gcs]
21+
use Google\Cloud\Vision\VisionClient;
22+
use Google\Cloud\Storage\StorageClient;
23+
24+
// $projectId = 'YOUR_PROJECT_ID';
25+
// $bucketName = 'your-bucket-name'
26+
// $objectName = 'your-object-name'
27+
28+
$vision = new VisionClient([
29+
'projectId' => $projectId,
30+
]);
31+
$storage = new StorageClient([
32+
'projectId' => $projectId,
33+
]);
34+
35+
// fetch the storage object and annotate the image
36+
$object = $storage->bucket($bucketName)->object($objectName);
37+
$image = $vision->image($object, ['DOCUMENT_TEXT_DETECTION']);
38+
$annotation = $vision->annotate($image);
39+
$document = $annotation->fullText();
40+
41+
# Print out unstructured document text
42+
$text = $document->text();
43+
print('Document text: ' . $text . PHP_EOL);
44+
45+
# Print out more detailed and structured information about document text
46+
foreach ($document->pages() as $page) {
47+
foreach ($page['blocks'] as $block) {
48+
$block_text = '';
49+
foreach ($block['paragraphs'] as $paragraph) {
50+
foreach ($paragraph['words'] as $word) {
51+
foreach ($word['symbols'] as $symbol) {
52+
$block_text = $block_text . $symbol['text'];
53+
}
54+
}
55+
}
56+
print('Block text: ' . $block_text . PHP_EOL);
57+
print('Block bounds:' . PHP_EOL);
58+
foreach ($block['boundingBox']['vertices'] as $vertice) {
59+
print('X: ' . $vertice['x'] . ' Y: ' . $vertice['y'] . PHP_EOL);
60+
}
61+
print(PHP_EOL);
62+
}
63+
}
64+
# [END vision_fulltext_detection_gcs]

vision/api/vision.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@
2828
$application->add(new Google\Cloud\Samples\Vision\DetectSafeSearchCommand());
2929
$application->add(new Google\Cloud\Samples\Vision\DetectImagePropertyCommand());
3030
$application->add(new Google\Cloud\Samples\Vision\DetectCropHintsCommand());
31+
$application->add(new Google\Cloud\Samples\Vision\DetectDocumentTextCommand());
3132
$application->run();

0 commit comments

Comments
 (0)