Skip to content

Commit f5296c4

Browse files
committed
Update to Vision client
Update to new Vision client
1 parent 8d21038 commit f5296c4

File tree

2 files changed

+26
-32
lines changed

2 files changed

+26
-32
lines changed

vision/src/detect_image_property.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,29 @@
1818
// [START vision_image_property_detection]
1919
namespace Google\Cloud\Samples\Vision;
2020

21-
use Google\Cloud\Vision\V1\ImageAnnotatorClient;
21+
use Google\Cloud\Vision\VisionClient;
2222

2323
// $path = 'path/to/your/image.jpg'
2424

2525
function detect_image_property($path)
2626
{
27-
$imageAnnotator = new ImageAnnotatorClient();
27+
$vision = new VisionClient();
2828

2929
# annotate the image
30-
$image = file_get_contents($path);
31-
$response = $imageAnnotator->imagePropertiesDetection($image);
32-
$props = $response->getImagePropertiesAnnotation();
30+
$imagePhotoResource = file_get_contents($path);
31+
$image = $vision->image($imagePhotoResource, ['IMAGE_PROPERTIES']);
32+
$annotations = $vision->annotate($image);
33+
$props = $annotations->imageProperties();
3334

3435
print("Properties:" . PHP_EOL);
35-
foreach ($props->getDominantColors()->getColors() as $colorInfo) {
36-
printf("Fraction: %s" . PHP_EOL, $colorInfo->getPixelFraction());
37-
$color = $colorInfo->getColor();
38-
printf("Red: %s" . PHP_EOL, $color->getRed());
39-
printf("Green: %s" . PHP_EOL, $color->getGreen());
40-
printf("Blue: %s" . PHP_EOL, $color->getBlue());
36+
foreach ((array) $props->colors() as $colorInfo) {
37+
printf("Fraction: %s" . PHP_EOL, $colorInfo['pixelFraction']);
38+
$color = $colorInfo['color'];
39+
printf("Red: %s" . PHP_EOL, $color['red']);
40+
printf("Green: %s" . PHP_EOL, $color['green']);
41+
printf("Blue: %s" . PHP_EOL, $color['blue']);
4142
print(PHP_EOL);
4243
}
4344

44-
$imageAnnotator->close();
4545
}
4646
// [END vision_image_property_detection]

vision/src/detect_image_property_gcs.php

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,33 +18,27 @@
1818
// [START vision_image_property_detection_gcs]
1919
namespace Google\Cloud\Samples\Vision;
2020

21-
use Google\Cloud\Vision\V1\ImageAnnotatorClient;
21+
use Google\Cloud\Vision\VisionClient;
2222

2323
// $path = 'gs://path/to/your/image.jpg'
2424

2525
function detect_image_property_gcs($path)
2626
{
27-
$imageAnnotator = new ImageAnnotatorClient();
27+
$vision = new VisionClient();
2828

2929
# annotate the image
30-
$response = $imageAnnotator->imagePropertiesDetection($path);
31-
$props = $response->getImagePropertiesAnnotation();
32-
33-
34-
if ($props) {
35-
print("Properties:" . PHP_EOL);
36-
foreach ($props->getDominantColors()->getColors() as $colorInfo) {
37-
printf("Fraction: %s" . PHP_EOL, $colorInfo->getPixelFraction());
38-
$color = $colorInfo->getColor();
39-
printf("Red: %s" . PHP_EOL, $color->getRed());
40-
printf("Green: %s" . PHP_EOL, $color->getGreen());
41-
printf("Blue: %s" . PHP_EOL, $color->getBlue());
42-
print(PHP_EOL);
43-
}
44-
} else {
45-
print('No Results.' . PHP_EOL);
30+
$image = $vision->image($path, ['IMAGE_PROPERTIES']);
31+
$annotations = $vision->annotate($image);
32+
$props = $annotations->imageProperties();
33+
34+
print("Properties:" . PHP_EOL);
35+
foreach ((array) $props->colors() as $colorInfo) {
36+
printf("Fraction: %s" . PHP_EOL, $colorInfo['pixelFraction']);
37+
$color = $colorInfo['color'];
38+
printf("Red: %s" . PHP_EOL, $color['red']);
39+
printf("Green: %s" . PHP_EOL, $color['green']);
40+
printf("Blue: %s" . PHP_EOL, $color['blue']);
41+
print(PHP_EOL);
4642
}
47-
48-
$imageAnnotator->close();
4943
}
5044
// [END vision_image_property_detection_gcs]

0 commit comments

Comments
 (0)