Skip to content

Commit fe390ea

Browse files
sirtorrybshaffer
authored andcommitted
Adds Vision object localisation samples (GoogleCloudPlatform#712)
1 parent 1d884c1 commit fe390ea

File tree

8 files changed

+246
-104
lines changed

8 files changed

+246
-104
lines changed

vision/composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "google/vision",
33
"type": "project",
44
"require": {
5-
"google/cloud-vision": "^0.13",
5+
"google/cloud-vision": "^0.17",
66
"google/cloud-storage": "^1.3",
77
"symfony/console": "^3.1"
88
},
@@ -31,6 +31,8 @@
3131
"src/detect_document_text_gcs.php",
3232
"src/detect_web.php",
3333
"src/detect_web_gcs.php",
34+
"src/detect_object.php",
35+
"src/detect_object_gcs.php",
3436
"src/detect_web_with_geo_metadata.php",
3537
"src/detect_web_with_geo_metadata_gcs.php",
3638
"src/detect_pdf_gcs.php"

vision/composer.lock

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

vision/src/detect_object.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
/**
3+
* Copyright 2018 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+
// [START vision_localize_object]
19+
namespace Google\Cloud\Samples\Vision;
20+
21+
use Google\Cloud\Vision\V1\ImageAnnotatorClient;
22+
23+
// $path = 'path/to/your/image.jpg'
24+
25+
function detect_object($path)
26+
{
27+
$imageAnnotator = new ImageAnnotatorClient();
28+
29+
# annotate the image
30+
$image = file_get_contents($path);
31+
$response = $imageAnnotator->objectLocalization($image);
32+
$objects = $response->getLocalizedObjectAnnotations();
33+
34+
foreach ($objects as $object) {
35+
$name = $object->getName();
36+
$score = $object->getScore();
37+
$vertices = $object->getBoundingPoly()->getNormalizedVertices();
38+
39+
printf('%s (confidence %d)):' . PHP_EOL, $name, $score);
40+
print('normalized bounding polygon vertices: ');
41+
foreach ($vertices as $vertex) {
42+
printf(' (%d, %d)', $vertex->getX(), $vertex->getY());
43+
}
44+
print(PHP_EOL);
45+
}
46+
47+
$imageAnnotator->close();
48+
}
49+
// [END vision_localize_object]

vision/src/detect_object_gcs.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
2+
/**
3+
* Copyright 2018 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+
// [START vision_localize_object_gcs]
19+
namespace Google\Cloud\Samples\Vision;
20+
21+
use Google\Cloud\Vision\V1\ImageAnnotatorClient;
22+
23+
// $path = 'gs://path/to/your/image.jpg'
24+
25+
function detect_object_gcs($path)
26+
{
27+
$imageAnnotator = new ImageAnnotatorClient();
28+
29+
# annotate the image
30+
$response = $imageAnnotator->objectLocalization($path);
31+
$objects = $response->getLocalizedObjectAnnotations();
32+
33+
foreach ($objects as $object) {
34+
$name = $object->getName();
35+
$score = $object->getScore();
36+
$vertices = $object->getBoundingPoly()->getNormalizedVertices();
37+
38+
printf('%s (confidence %d)):' . PHP_EOL, $name, $score);
39+
print('normalized bounding polygon vertices: ');
40+
foreach ($vertices as $vertex) {
41+
printf(' (%d, %d)', $vertex->getX(), $vertex->getY());
42+
}
43+
print(PHP_EOL);
44+
}
45+
46+
$imageAnnotator->close();
47+
}
48+
// [END vision_localize_object_gcs]

vision/src/detect_pdf_gcs.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
use Google\Cloud\Vision\V1\AnnotateFileResponse;
2323
use Google\Cloud\Vision\V1\AsyncAnnotateFileRequest;
2424
use Google\Cloud\Vision\V1\Feature;
25-
use Google\Cloud\Vision\V1\Feature_Type;
25+
use Google\Cloud\Vision\V1\Feature\Type;
2626
use Google\Cloud\Vision\V1\GcsDestination;
2727
use Google\Cloud\Vision\V1\GcsSource;
2828
use Google\Cloud\Vision\V1\ImageAnnotatorClient;
@@ -35,7 +35,7 @@ function detect_pdf_gcs($path, $output)
3535
{
3636
# select ocr feature
3737
$feature = (new Feature())
38-
->setType(Feature_Type::DOCUMENT_TEXT_DETECTION);
38+
->setType(Type::DOCUMENT_TEXT_DETECTION);
3939

4040
# set $path (file to OCR) as source
4141
$gcsSource = (new GcsSource())

vision/test/data/puppies.jpg

280 KB
Loading

vision/test/visionTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,22 @@ public function testLogoCommandGcs()
170170
$this->assertContains('Google', $output);
171171
}
172172

173+
public function testLocalizeObjectCommand()
174+
{
175+
$path = __DIR__ . '/data/puppies.jpg';
176+
$output = $this->runCommand('localize-object', $path);
177+
$this->assertContains('Dog', $output);
178+
}
179+
180+
public function testLocalizeObjectCommandGcs()
181+
{
182+
$this->requireCloudStorage();
183+
184+
$path = 'gs://' . $this->bucketName . '/vision/puppies.jpg';
185+
$output = $this->runCommand('localize-object', $path);
186+
$this->assertContains('Dog', $output);
187+
}
188+
173189
public function testLogoCommandWithImageLackingLogo()
174190
{
175191
$path = __DIR__ . '/data/tower.jpg';

vision/vision.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,29 @@
305305
})
306306
);
307307

308+
// localize object command
309+
$application->add((new Command('localize-object'))
310+
->setDefinition($inputDefinition)
311+
->setDescription('Localize object in an image using '
312+
. 'Google Cloud Vision API')
313+
->setHelp(<<
314+
The %command.name% command finds objects in an image using
315+
the Google Cloud Vision API.
316+
317+
php %command.full_name% path/to/image.png
318+
319+
EOF
320+
)
321+
->setCode(function ($input, $output) {
322+
$path = $input->getArgument('path');
323+
if (preg_match('/^gs:\/\/([a-z0-9\._\-]+)\/(\S+)$/', $path)) {
324+
detect_object_gcs($path);
325+
} else {
326+
detect_object($path);
327+
}
328+
})
329+
);
330+
308331
if (getenv('PHPUNIT_TESTS') === '1') {
309332
return $application;
310333
}

0 commit comments

Comments
 (0)