Skip to content

Commit 13b8e75

Browse files
committed
Added web detection sample, addressed code review comments, improved printing style, added tests
1 parent ed314e3 commit 13b8e75

14 files changed

+452
-94
lines changed

vision/api/composer.lock

Lines changed: 75 additions & 66 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vision/api/images/landmark.jpg

158 KB
Loading

vision/api/images/trump.jpg

421 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 DetectWebCommand extends Command
31+
{
32+
protected function configure()
33+
{
34+
$this
35+
->setName('web')
36+
->setDescription('Detect web references to an image using '
37+
. 'Google Cloud Vision API')
38+
->setHelp(<<<EOF
39+
The <info>%command.name%</info> command prints web references to 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_web_gcs.php';
67+
} else {
68+
$result = require __DIR__ . '/snippets/detect_web.php';
69+
}
70+
}
71+
}

vision/api/src/snippets/detect_crop_hints.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,20 @@
2626
$vision = new VisionClient([
2727
'projectId' => $projectId,
2828
]);
29+
30+
# Annotate the image
2931
$image = $vision->image(file_get_contents($path), ['CROP_HINTS']);
30-
$result = $vision->annotate($image);
31-
print("Crop Hints:\n");
32-
foreach ((array) $result->cropHints() as $hint) {
32+
$annotation = $vision->annotate($image);
33+
34+
# Print the crop hints from the annotation
35+
printf("Crop Hints:\n");
36+
foreach ((array) $annotation->cropHints() as $hint) {
3337
$boundingPoly = $hint->boundingPoly();
3438
$vertices = $boundingPoly['vertices'];
3539
foreach ((array) $vertices as $vertice) {
3640
if (!isset($vertice['x'])) $vertice['x'] = 0;
3741
if (!isset($vertice['y'])) $vertice['y'] = 0;
38-
print('X: ' . $vertice['x'] . ' Y: ' . $vertice['y'] . PHP_EOL);
42+
printf('X: %s Y: %s' . PHP_EOL, $vertice['x'], $vertice['y']);
3943
}
4044
}
4145
// [END vision_crop_hint_detection]

vision/api/src/snippets/detect_crop_hints_gcs.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,20 @@
3232
'projectId' => $projectId,
3333
]);
3434

35-
// fetch the storage object and annotate the image
35+
# Fetch the storage object and annotate the image
3636
$object = $storage->bucket($bucketName)->object($objectName);
3737
$image = $vision->image($object, ['CROP_HINTS']);
38-
$result = $vision->annotate($image);
38+
$annotation = $vision->annotate($image);
3939

40-
// print the response
41-
print("Crop Hints:\n");
42-
foreach ((array) $result->cropHints() as $hint) {
40+
# Print the crop hints from the annotation
41+
printf("Crop Hints:\n");
42+
foreach ((array) $annotation->cropHints() as $hint) {
4343
$boundingPoly = $hint->boundingPoly();
4444
$vertices = $boundingPoly['vertices'];
4545
foreach ((array) $vertices as $vertice) {
4646
if (!isset($vertice['x'])) $vertice['x'] = 0;
4747
if (!isset($vertice['y'])) $vertice['y'] = 0;
48-
print('X: ' . $vertice['x'] . ' Y: ' . $vertice['y'] . PHP_EOL);
48+
printf('X: %s Y: %s' . PHP_EOL, $vertice['x'], $vertice['y']);
4949
}
5050
}
5151
# [END vision_crop_hint_detection_gcs]

vision/api/src/snippets/detect_document_text.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,15 @@
2626
$vision = new VisionClient([
2727
'projectId' => $projectId,
2828
]);
29+
30+
# Annotate the image
2931
$image = $vision->image(file_get_contents($path), ['DOCUMENT_TEXT_DETECTION']);
3032
$annotation = $vision->annotate($image);
31-
$document = $annotation->fullText();
3233

33-
# Print out unstructured document text
34+
# Print out document text
35+
$document = $annotation->fullText();
3436
$text = $document->text();
35-
print('Document text: ' . $text . PHP_EOL);
37+
printf('Document text: %s' . PHP_EOL, $text);
3638

3739
# Print out more detailed and structured information about document text
3840
foreach ($document->pages() as $page) {
@@ -41,16 +43,18 @@
4143
foreach ($block['paragraphs'] as $paragraph) {
4244
foreach ($paragraph['words'] as $word) {
4345
foreach ($word['symbols'] as $symbol) {
44-
$block_text = $block_text . $symbol['text'];
46+
$block_text .= $symbol['text'];
4547
}
48+
$block_text .= ' ';
4649
}
50+
$block_text .= "\n";
4751
}
48-
print('Block text: ' . $block_text . PHP_EOL);
49-
print('Block bounds:' . PHP_EOL);
52+
printf('Block text: %s' . PHP_EOL, $block_text);
53+
printf('Block bounds:' . PHP_EOL);
5054
foreach ($block['boundingBox']['vertices'] as $vertice) {
51-
print('X: ' . $vertice['x'] . ' Y: ' . $vertice['y'] . PHP_EOL);
55+
printf('X: %s Y: %s' . PHP_EOL, $vertice['x'], $vertice['y']);
5256
}
53-
print(PHP_EOL);
57+
printf(PHP_EOL);
5458
}
5559
}
5660
// [END vision_fulltext_detection]

vision/api/src/snippets/detect_document_text_gcs.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@
3232
'projectId' => $projectId,
3333
]);
3434

35-
// fetch the storage object and annotate the image
35+
# Fetch the storage object and annotate the image
3636
$object = $storage->bucket($bucketName)->object($objectName);
3737
$image = $vision->image($object, ['DOCUMENT_TEXT_DETECTION']);
3838
$annotation = $vision->annotate($image);
39-
$document = $annotation->fullText();
4039

41-
# Print out unstructured document text
40+
# Print out document text
41+
$document = $annotation->fullText();
4242
$text = $document->text();
43-
print('Document text: ' . $text . PHP_EOL);
43+
printf('Document text: %s' . PHP_EOL, $text);
4444

4545
# Print out more detailed and structured information about document text
4646
foreach ($document->pages() as $page) {
@@ -49,16 +49,18 @@
4949
foreach ($block['paragraphs'] as $paragraph) {
5050
foreach ($paragraph['words'] as $word) {
5151
foreach ($word['symbols'] as $symbol) {
52-
$block_text = $block_text . $symbol['text'];
52+
$block_text .= $symbol['text'];
5353
}
54+
$block_text .= ' ';
5455
}
56+
$block_text .= "\n";
5557
}
56-
print('Block text: ' . $block_text . PHP_EOL);
57-
print('Block bounds:' . PHP_EOL);
58+
printf('Block text: %s' . PHP_EOL, $block_text);
59+
printf('Block bounds:' . PHP_EOL);
5860
foreach ($block['boundingBox']['vertices'] as $vertice) {
59-
print('X: ' . $vertice['x'] . ' Y: ' . $vertice['y'] . PHP_EOL);
61+
printf('X: %s Y: %s' . PHP_EOL, $vertice['x'], $vertice['y']);
6062
}
61-
print(PHP_EOL);
63+
printf(PHP_EOL);
6264
}
6365
}
6466
# [END vision_fulltext_detection_gcs]
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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_web_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+
30+
# Annotate the image
31+
$image = $vision->image(file_get_contents($path), ['WEB_DETECTION']);
32+
$annotation = $vision->annotate($image);
33+
$web = $annotation->web();
34+
35+
if ($web->pages()) {
36+
printf('%d Pages with matching images found:' . PHP_EOL, count($web->pages()));
37+
foreach ($web->pages() as $page) {
38+
printf('URL: %s' . PHP_EOL, $page->url());
39+
}
40+
print(PHP_EOL);
41+
}
42+
43+
if ($web->matchingImages()) {
44+
printf('%d Full Matching Images found:' . PHP_EOL, count($web->matchingImages()));
45+
foreach ($web->matchingImages() as $matchingImage) {
46+
printf('URL: %s' . PHP_EOL, $matchingImage->url());
47+
}
48+
print(PHP_EOL);
49+
}
50+
51+
if ($web->partialMatchingImages()) {
52+
printf('%d Partial Matching Images found:' . PHP_EOL, count($web->partialMatchingImages()));
53+
foreach ($web->partialMatchingImages() as $partialMatchingImage) {
54+
printf('URL: %s' . PHP_EOL, $partialMatchingImage->url());
55+
}
56+
print(PHP_EOL);
57+
}
58+
59+
if ($web->entities()) {
60+
printf('%d Web Entities found:' . PHP_EOL, count($web->entities()));
61+
foreach ($web->entities() as $entity) {
62+
printf('Score: %f' . PHP_EOL, $entity->score());
63+
printf('Description: %s' . PHP_EOL, $entity->description());
64+
printf(PHP_EOL);
65+
}
66+
}
67+
// [END vision_web_detection]
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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_web_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, ['WEB_DETECTION']);
38+
$annotation = $vision->annotate($image);
39+
$web = $annotation->web();
40+
41+
if ($web->pages()) {
42+
printf('%d Pages with matching images found:' . PHP_EOL, count($web->pages()));
43+
foreach ($web->pages() as $page) {
44+
printf('URL: %s' . PHP_EOL, $page->url());
45+
}
46+
print(PHP_EOL);
47+
}
48+
49+
if ($web->matchingImages()) {
50+
printf('%d Full Matching Images found:' . PHP_EOL, count($web->matchingImages()));
51+
foreach ($web->matchingImages() as $matchingImage) {
52+
printf('URL: %s' . PHP_EOL, $matchingImage->url());
53+
}
54+
print(PHP_EOL);
55+
}
56+
57+
if ($web->partialMatchingImages()) {
58+
printf('%d Partial Matching Images found:' . PHP_EOL, count($web->partialMatchingImages()));
59+
foreach ($web->partialMatchingImages() as $partialMatchingImage) {
60+
printf('URL: %s' . PHP_EOL, $partialMatchingImage->url());
61+
}
62+
print(PHP_EOL);
63+
}
64+
65+
if ($web->entities()) {
66+
printf('%d Web Entities found:' . PHP_EOL, count($web->entities()));
67+
foreach ($web->entities() as $entity) {
68+
printf('Score: %f' . PHP_EOL, $entity->score());
69+
printf('Description: %s' . PHP_EOL, $entity->description());
70+
printf(PHP_EOL);
71+
}
72+
}
73+
# [END vision_web_detection_gcs]

0 commit comments

Comments
 (0)