Skip to content

Commit 8db280c

Browse files
authored
Add storage to vision samples (GoogleCloudPlatform#303)
1 parent bdb5cbf commit 8db280c

22 files changed

+566
-170
lines changed

vision/api/src/DetectFaceCommand.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,12 @@ protected function execute(InputInterface $input, OutputInterface $output)
8282
{
8383
$projectId = $input->getOption('project');
8484
$path = $input->getArgument('path');
85-
$result = require __DIR__ . '/snippets/detect_face.php';
85+
if (preg_match('/^gs:\/\/([a-z0-9\._\-]+)\/(\S+)$/', $path, $matches)) {
86+
list($bucketName, $objectName) = array_slice($matches, 1);
87+
$result = require __DIR__ . '/snippets/detect_face_gcs.php';
88+
} else {
89+
$result = require __DIR__ . '/snippets/detect_face.php';
90+
}
8691
if (
8792
isset($result->info()['faceAnnotations'])
8893
&& $outFile = $input->getArgument('output')

vision/api/src/DetectImagePropertyCommand.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,23 @@ protected function configure()
4949
'The image to examine.'
5050
)
5151
->addOption(
52-
'api-key',
53-
'k',
52+
'project',
53+
'p',
5454
InputOption::VALUE_REQUIRED,
55-
'Your API key.'
55+
'Your Project ID.'
5656
)
5757
;
5858
}
5959

6060
protected function execute(InputInterface $input, OutputInterface $output)
6161
{
62-
$apiKey = $input->getOption('api-key');
62+
$projectId = $input->getOption('project');
6363
$path = $input->getArgument('path');
64-
require(__DIR__ . '/snippets/detect_image_property.php');
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_image_property_gcs.php';
67+
} else {
68+
$result = require __DIR__ . '/snippets/detect_image_property.php';
69+
}
6570
}
6671
}

vision/api/src/DetectLabelCommand.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
5959
{
6060
$projectId = $input->getOption('project');
6161
$path = $input->getArgument('path');
62-
require(__DIR__ . '/snippets/detect_label.php');
62+
if (preg_match('/^gs:\/\/([a-z0-9\._\-]+)\/(\S+)$/', $path, $matches)) {
63+
list($bucketName, $objectName) = array_slice($matches, 1);
64+
$result = require __DIR__ . '/snippets/detect_label_gcs.php';
65+
} else {
66+
$result = require __DIR__ . '/snippets/detect_label.php';
67+
}
6368
}
6469
}

vision/api/src/DetectLandmarkCommand.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,23 @@ protected function configure()
4949
'The image to examine.'
5050
)
5151
->addOption(
52-
'api-key',
53-
'k',
52+
'project',
53+
'p',
5454
InputOption::VALUE_REQUIRED,
55-
'Your API key.'
55+
'Your Project ID.'
5656
)
5757
;
5858
}
5959

6060
protected function execute(InputInterface $input, OutputInterface $output)
6161
{
62-
$apiKey = $input->getOption('api-key');
62+
$projectId = $input->getOption('project');
6363
$path = $input->getArgument('path');
64-
require(__DIR__ . '/snippets/detect_landmark.php');
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_landmark_gcs.php';
67+
} else {
68+
$result = require __DIR__ . '/snippets/detect_landmark.php';
69+
}
6570
}
6671
}

vision/api/src/DetectLogoCommand.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,23 @@ protected function configure()
4949
'The image to examine.'
5050
)
5151
->addOption(
52-
'api-key',
53-
'k',
52+
'project',
53+
'p',
5454
InputOption::VALUE_REQUIRED,
55-
'Your API key.'
55+
'Your Project ID.'
5656
)
5757
;
5858
}
5959

6060
protected function execute(InputInterface $input, OutputInterface $output)
6161
{
62-
$apiKey = $input->getOption('api-key');
62+
$projectId = $input->getOption('project');
6363
$path = $input->getArgument('path');
64-
require(__DIR__ . '/snippets/detect_logo.php');
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_logo_gcs.php';
67+
} else {
68+
$result = require __DIR__ . '/snippets/detect_logo.php';
69+
}
6570
}
6671
}

vision/api/src/DetectSafeSearchCommand.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,23 @@ protected function configure()
4949
'The image to examine.'
5050
)
5151
->addOption(
52-
'api-key',
53-
'k',
52+
'project',
53+
'p',
5454
InputOption::VALUE_REQUIRED,
55-
'Your API key.'
55+
'Your Project ID.'
5656
)
5757
;
5858
}
5959

6060
protected function execute(InputInterface $input, OutputInterface $output)
6161
{
62-
$apiKey = $input->getOption('api-key');
62+
$projectId = $input->getOption('project');
6363
$path = $input->getArgument('path');
64-
require(__DIR__ . '/snippets/detect_safe_search.php');
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_safe_search_gcs.php';
67+
} else {
68+
$result = require __DIR__ . '/snippets/detect_safe_search.php';
69+
}
6570
}
6671
}

vision/api/src/DetectTextCommand.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,23 @@ protected function configure()
4949
'The image to examine.'
5050
)
5151
->addOption(
52-
'api-key',
53-
'k',
52+
'project',
53+
'p',
5454
InputOption::VALUE_REQUIRED,
55-
'Your API key.'
55+
'Your Project ID.'
5656
)
5757
;
5858
}
5959

6060
protected function execute(InputInterface $input, OutputInterface $output)
6161
{
62-
$apiKey = $input->getOption('api-key');
62+
$projectId = $input->getOption('project');
6363
$path = $input->getArgument('path');
64-
require(__DIR__ . '/snippets/detect_text.php');
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_text_gcs.php';
67+
} else {
68+
$result = require __DIR__ . '/snippets/detect_text.php';
69+
}
6570
}
6671
}

vision/api/src/snippets/detect_face.php

Lines changed: 5 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -33,44 +33,11 @@
3333
$image = $vision->image(file_get_contents($path), ['FACE_DETECTION']);
3434
$result = $vision->annotate($image);
3535
# [END detect_face]
36-
if (isset($result->info()['faceAnnotations'])) {
37-
foreach ($result->info()['faceAnnotations'] as $annotation) {
38-
print("FACE\n");
39-
if (isset($annotation['boundingPoly'])) {
40-
print(" BOUNDING POLY\n");
41-
foreach ($annotation['boundingPoly']['vertices'] as $vertex) {
42-
$x = isset($vertex['x']) ? $vertex['x'] : '';
43-
$y = isset($vertex['y']) ? $vertex['y'] : '';
44-
print(" x:$x\ty:$y\n");
45-
}
46-
}
47-
if (isset($annotation['landmarks'])) {
48-
print(" LANDMARKS\n");
49-
foreach ($annotation['landmarks'] as $landmark) {
50-
$pos = $landmark['position'];
51-
print(" $landmark[type]:\tx:$pos[x]\ty:$pos[y]\tz:$pos[z]\n");
52-
}
53-
}
54-
$scalar_features = [
55-
'rollAngle',
56-
'panAngle',
57-
'tiltAngle',
58-
'detectionConfidence',
59-
'landmarkingConfidence',
60-
'joyLikelihood',
61-
'sorrowLikelihood',
62-
'angerLikelihood',
63-
'surpriseLikelihood',
64-
'underExposedLikelihood',
65-
'blurredLikelihood',
66-
'headwearLikelihood'
67-
];
68-
foreach ($scalar_features as $feature) {
69-
if (isset($annotation[$feature])) {
70-
print(" $feature:\t$annotation[$feature]\n");
71-
}
72-
}
73-
}
36+
print("Faces:\n");
37+
foreach ((array) $result->faces() as $face) {
38+
printf("Anger: %s\n", $face->isAngry() ? 'yes' : 'no');
39+
printf("Joy: %s\n", $face->isJoyful() ? 'yes' : 'no');
40+
printf("Surprise: %s\n\n", $face->isSurprised() ? 'yes' : 'no');
7441
}
7542
# [END face_detection]
7643
return $result;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
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+
19+
namespace Google\Cloud\Samples\Vision;
20+
21+
# [START face_detection_gcs]
22+
use Google\Cloud\ServiceBuilder;
23+
24+
// $projectId = 'YOUR_PROJECT_ID';
25+
// $bucketName = 'your-bucket-name'
26+
// $objectName = 'your-object-name'
27+
28+
$builder = new ServiceBuilder([
29+
'projectId' => $projectId,
30+
]);
31+
$vision = $builder->vision();
32+
$storage = $builder->storage();
33+
34+
// fetch the storage object and annotate the image
35+
$object = $storage->bucket($bucketName)->object($objectName);
36+
$image = $vision->image($object, ['FACE_DETECTION']);
37+
$result = $vision->annotate($image);
38+
39+
// print the response
40+
print("Faces:\n");
41+
foreach ((array) $result->faces() as $face) {
42+
printf("Anger: %s\n", $face->isAngry() ? 'yes' : 'no');
43+
printf("Joy: %s\n", $face->isJoyful() ? 'yes' : 'no');
44+
printf("Surprise: %s\n\n", $face->isSurprised() ? 'yes' : 'no');
45+
}
46+
# [END face_detection_gcs]
47+
return $result;

vision/api/src/snippets/detect_image_property.php

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,21 @@
2121
// [START image_property_detection]
2222
use Google\Cloud\Vision\VisionClient;
2323

24-
// $apiKey = 'YOUR-API-KEY';
24+
// $projectId = 'YOUR_PROJECT_ID';
2525
// $path = 'path/to/your/image.jpg'
2626

2727
$vision = new VisionClient([
28-
'key' => $apiKey,
28+
'projectId' => $projectId,
29+
]);
30+
$image = $vision->image(file_get_contents($path), [
31+
'IMAGE_PROPERTIES'
2932
]);
30-
$image = $vision->image(file_get_contents($path),
31-
['IMAGE_PROPERTIES']);
3233
$result = $vision->annotate($image);
33-
if (isset($result->info()['imagePropertiesAnnotation'])) {
34-
$annotation = $result->info()['imagePropertiesAnnotation'];
35-
print("COLORS\n");
36-
foreach ($annotation['dominantColors']['colors'] as $color) {
37-
$rgb = $color['color'];
38-
print(" COLOR\n");
39-
print(" red:$rgb[red]\tgreen:$rgb[green]\tblue:$rgb[blue]\n");
40-
print(" score:$color[score]\n");
41-
print(" pixelFraction:$color[pixelFraction]\n");
42-
}
34+
print("Properties:\n");
35+
foreach ($result->imageProperties()->colors() as $color) {
36+
$rgb = $color['color'];
37+
printf("red:%s\n", $rgb['red']);
38+
printf("green:%s\n", $rgb['green']);
39+
printf("blue:%s\n\n", $rgb['blue']);
4340
}
4441
// [END image_property_detection]
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
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+
19+
namespace Google\Cloud\Samples\Vision;
20+
21+
# [START image_property_detection_gcs]
22+
use Google\Cloud\ServiceBuilder;
23+
24+
// $projectId = 'YOUR_PROJECT_ID';
25+
// $bucketName = 'your-bucket-name'
26+
// $objectName = 'your-object-name'
27+
28+
$builder = new ServiceBuilder([
29+
'projectId' => $projectId,
30+
]);
31+
$vision = $builder->vision();
32+
$storage = $builder->storage();
33+
34+
// fetch the storage object and annotate the image
35+
$object = $storage->bucket($bucketName)->object($objectName);
36+
$image = $vision->image($object, ['IMAGE_PROPERTIES']);
37+
$result = $vision->annotate($image);
38+
39+
// print the response
40+
print("Properties:\n");
41+
foreach ($result->imageProperties()->colors() as $color) {
42+
$rgb = $color['color'];
43+
printf("red:%s\n", $rgb['red']);
44+
printf("green:%s\n", $rgb['green']);
45+
printf("blue:%s\n\n", $rgb['blue']);
46+
}
47+
# [END image_property_detection]

vision/api/src/snippets/detect_label.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,9 @@
3838
# [END construct_request]
3939

4040
# [START parse_response]
41-
if (!isset($result->info()['labelAnnotations'])) {
42-
return;
43-
}
44-
foreach ($result->info()['labelAnnotations'] as $annotation) {
45-
print("LABEL\n");
46-
print(" mid: $annotation[mid]\n");
47-
print(" description: $annotation[description]\n");
48-
print(" score: $annotation[score]\n");
41+
print("LABELS:\n");
42+
foreach ($result->labels() as $label) {
43+
print($label->description() . PHP_EOL);
4944
}
5045
# [END parse_response]
5146
# [END detect_labels]

0 commit comments

Comments
 (0)