Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions functions/imagemagick/composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"require": {
"google/cloud-functions-framework": "^1.0",
"google/cloud-storage": "^1.23",
"google/cloud-storage": "^2.0",
"google/cloud-vision": "^2.0",
"ext-imagick": "*"
},
Expand All @@ -12,6 +12,6 @@
]
},
"require-dev": {
"google/cloud-logging": "^1.21"
"google/cloud-logging": "^2.0"
}
}
21 changes: 19 additions & 2 deletions functions/imagemagick/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@
// [START functions_imagemagick_setup]
use Google\CloudFunctions\CloudEvent;
use Google\Cloud\Storage\StorageClient;
use Google\Cloud\Vision\V1\ImageAnnotatorClient;
use Google\Cloud\Vision\V1\AnnotateImageRequest;
use Google\Cloud\Vision\V1\BatchAnnotateImagesRequest;
use Google\Cloud\Vision\V1\Client\ImageAnnotatorClient;
use Google\Cloud\Vision\V1\Feature;
use Google\Cloud\Vision\V1\Feature\Type;
use Google\Cloud\Vision\V1\Image;
use Google\Cloud\Vision\V1\ImageSource;
use Google\Cloud\Vision\V1\Likelihood;
use Google\Rpc\Code;

Expand All @@ -40,7 +46,18 @@ function blurOffensiveImages(CloudEvent $cloudevent): void
$storage = new StorageClient();

try {
$response = $annotator->safeSearchDetection($filePath);
$source = (new ImageSource())
->setImageUri($filePath);
$image = (new Image())
->setSource($source);
$feature = (new Feature())
->setType(Type::SAFE_SEARCH_DETECTION);
$request = (new AnnotateImageRequest())
->setImage($image)
->setFeatures([$feature]);
$batchRequest = (new BatchAnnotateImagesRequest())
->setRequests([$request]);
$response = $annotator->batchAnnotateImages($batchRequest)->getResponses()[0];

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The ImageAnnotatorClient instance should be closed to free up resources and avoid potential connection leaks, especially since this is a Cloud Function where the container and process can be reused across multiple invocations. Other Vision samples in this repository explicitly call close() on the client.

        $response = $annotator->batchAnnotateImages($batchRequest)->getResponses()[0];
        $annotator->close();


// Handle error
if ($response->hasError()) {
Expand Down
4 changes: 2 additions & 2 deletions vision/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "google/vision",
"type": "project",
"require": {
"google/cloud-vision": "^1.7",
"google/cloud-storage": "^1.20.1"
"google/cloud-vision": "^2.0",
"google/cloud-storage": "^2.0"
}
}
17 changes: 15 additions & 2 deletions vision/quickstart.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@
require __DIR__ . '/vendor/autoload.php';

# imports the Google Cloud client library
use Google\Cloud\Vision\V1\AnnotateImageRequest;
use Google\Cloud\Vision\V1\BatchAnnotateImagesRequest;
use Google\Cloud\Vision\V1\Client\ImageAnnotatorClient;
use Google\Cloud\Vision\V1\Feature;
use Google\Cloud\Vision\V1\Feature\Type;
use Google\Cloud\Vision\V1\Image;

# instantiates a client
$imageAnnotator = new ImageAnnotatorClient();
Expand All @@ -29,10 +34,18 @@
$fileName = 'test/data/wakeupcat.jpg';

# prepare the image to be annotated
$image = file_get_contents($fileName);
$image = (new Image())
->setContent(file_get_contents($fileName));
$feature = (new Feature())
->setType(Type::LABEL_DETECTION);
$request = (new AnnotateImageRequest())
->setImage($image)
->setFeatures([$feature]);
$batchRequest = (new BatchAnnotateImagesRequest())
->setRequests([$request]);

# performs label detection on the image file
$response = $imageAnnotator->labelDetection($image);
$response = $imageAnnotator->batchAnnotateImages($batchRequest)->getResponses()[0];
$labels = $response->getLabelAnnotations();

if ($labels) {
Expand Down
17 changes: 15 additions & 2 deletions vision/src/detect_document_text.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@
// [START vision_fulltext_detection]
namespace Google\Cloud\Samples\Vision;

use Google\Cloud\Vision\V1\AnnotateImageRequest;
use Google\Cloud\Vision\V1\BatchAnnotateImagesRequest;
use Google\Cloud\Vision\V1\Client\ImageAnnotatorClient;
use Google\Cloud\Vision\V1\Feature;
use Google\Cloud\Vision\V1\Feature\Type;
use Google\Cloud\Vision\V1\Image;

/**
* @param string $path Path to the image, e.g. "path/to/your/image.jpg"
Expand All @@ -28,8 +33,16 @@ function detect_document_text(string $path)
$imageAnnotator = new ImageAnnotatorClient();

# annotate the image
$image = file_get_contents($path);
$response = $imageAnnotator->documentTextDetection($image);
$image = (new Image())
->setContent(file_get_contents($path));
$feature = (new Feature())
->setType(Type::DOCUMENT_TEXT_DETECTION);
$request = (new AnnotateImageRequest())
->setImage($image)
->setFeatures([$feature]);
$batchRequest = (new BatchAnnotateImagesRequest())
->setRequests([$request]);
$response = $imageAnnotator->batchAnnotateImages($batchRequest)->getResponses()[0];
$annotation = $response->getFullTextAnnotation();

# print out detailed and structured information about document text
Expand Down
19 changes: 18 additions & 1 deletion vision/src/detect_document_text_gcs.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@
// [START vision_fulltext_detection_gcs]
namespace Google\Cloud\Samples\Vision;

use Google\Cloud\Vision\V1\AnnotateImageRequest;
use Google\Cloud\Vision\V1\BatchAnnotateImagesRequest;
use Google\Cloud\Vision\V1\Client\ImageAnnotatorClient;
use Google\Cloud\Vision\V1\Feature;
use Google\Cloud\Vision\V1\Feature\Type;
use Google\Cloud\Vision\V1\Image;
use Google\Cloud\Vision\V1\ImageSource;

/**
* @param string $path GCS path to the image, e.g. "gs://path/to/your/image.jpg"
Expand All @@ -28,7 +34,18 @@ function detect_document_text_gcs(string $path)
$imageAnnotator = new ImageAnnotatorClient();

# annotate the image
$response = $imageAnnotator->documentTextDetection($path);
$source = (new ImageSource())
->setImageUri($path);
$image = (new Image())
->setSource($source);
$feature = (new Feature())
->setType(Type::DOCUMENT_TEXT_DETECTION);
$request = (new AnnotateImageRequest())
->setImage($image)
->setFeatures([$feature]);
$batchRequest = (new BatchAnnotateImagesRequest())
->setRequests([$request]);
$response = $imageAnnotator->batchAnnotateImages($batchRequest)->getResponses()[0];
$annotation = $response->getFullTextAnnotation();

# print out detailed and structured information about document text
Expand Down
17 changes: 15 additions & 2 deletions vision/src/detect_face.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@
namespace Google\Cloud\Samples\Vision;

// [START vision_face_detection_tutorial_imports]
use Google\Cloud\Vision\V1\AnnotateImageRequest;
use Google\Cloud\Vision\V1\BatchAnnotateImagesRequest;
use Google\Cloud\Vision\V1\Client\ImageAnnotatorClient;
use Google\Cloud\Vision\V1\Feature;
use Google\Cloud\Vision\V1\Feature\Type;
use Google\Cloud\Vision\V1\Image;

// [END vision_face_detection_tutorial_imports]
/**
Expand All @@ -35,8 +40,16 @@ function detect_face(string $path, string $outFile = null)
// [START vision_face_detection_tutorial_send_request]
# annotate the image
// $path = 'path/to/your/image.jpg'
$image = file_get_contents($path);
$response = $imageAnnotator->faceDetection($image);
$image = (new Image())
->setContent(file_get_contents($path));
$feature = (new Feature())
->setType(Type::FACE_DETECTION);
$request = (new AnnotateImageRequest())
->setImage($image)
->setFeatures([$feature]);
$batchRequest = (new BatchAnnotateImagesRequest())
->setRequests([$request]);
$response = $imageAnnotator->batchAnnotateImages($batchRequest)->getResponses()[0];
$faces = $response->getFaceAnnotations();
// [END vision_face_detection_tutorial_send_request]

Expand Down
19 changes: 18 additions & 1 deletion vision/src/detect_face_gcs.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@
// [START vision_face_detection_gcs]
namespace Google\Cloud\Samples\Vision;

use Google\Cloud\Vision\V1\AnnotateImageRequest;
use Google\Cloud\Vision\V1\BatchAnnotateImagesRequest;
use Google\Cloud\Vision\V1\Client\ImageAnnotatorClient;
use Google\Cloud\Vision\V1\Feature;
use Google\Cloud\Vision\V1\Feature\Type;
use Google\Cloud\Vision\V1\Image;
use Google\Cloud\Vision\V1\ImageSource;

/**
* @param string $path GCS path to the image, e.g. "gs://path/to/your/image.jpg"
Expand All @@ -28,7 +34,18 @@ function detect_face_gcs(string $path)
$imageAnnotator = new ImageAnnotatorClient();

# annotate the image
$response = $imageAnnotator->faceDetection($path);
$source = (new ImageSource())
->setImageUri($path);
$image = (new Image())
->setSource($source);
$feature = (new Feature())
->setType(Type::FACE_DETECTION);
$request = (new AnnotateImageRequest())
->setImage($image)
->setFeatures([$feature]);
$batchRequest = (new BatchAnnotateImagesRequest())
->setRequests([$request]);
$response = $imageAnnotator->batchAnnotateImages($batchRequest)->getResponses()[0];
$faces = $response->getFaceAnnotations();

# names of likelihood from google.cloud.vision.enums
Expand Down
17 changes: 15 additions & 2 deletions vision/src/detect_image_property.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@
// [START vision_image_property_detection]
namespace Google\Cloud\Samples\Vision;

use Google\Cloud\Vision\V1\AnnotateImageRequest;
use Google\Cloud\Vision\V1\BatchAnnotateImagesRequest;
use Google\Cloud\Vision\V1\Client\ImageAnnotatorClient;
use Google\Cloud\Vision\V1\Feature;
use Google\Cloud\Vision\V1\Feature\Type;
use Google\Cloud\Vision\V1\Image;

/**
* @param string $path Path to the image, e.g. "path/to/your/image.jpg"
Expand All @@ -28,8 +33,16 @@ function detect_image_property(string $path)
$imageAnnotator = new ImageAnnotatorClient();

# annotate the image
$image = file_get_contents($path);
$response = $imageAnnotator->imagePropertiesDetection($image);
$image = (new Image())
->setContent(file_get_contents($path));
$feature = (new Feature())
->setType(Type::IMAGE_PROPERTIES);
$request = (new AnnotateImageRequest())
->setImage($image)
->setFeatures([$feature]);
$batchRequest = (new BatchAnnotateImagesRequest())
->setRequests([$request]);
$response = $imageAnnotator->batchAnnotateImages($batchRequest)->getResponses()[0];
$props = $response->getImagePropertiesAnnotation();

print('Properties:' . PHP_EOL);
Expand Down
19 changes: 18 additions & 1 deletion vision/src/detect_image_property_gcs.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@
// [START vision_image_property_detection_gcs]
namespace Google\Cloud\Samples\Vision;

use Google\Cloud\Vision\V1\AnnotateImageRequest;
use Google\Cloud\Vision\V1\BatchAnnotateImagesRequest;
use Google\Cloud\Vision\V1\Client\ImageAnnotatorClient;
use Google\Cloud\Vision\V1\Feature;
use Google\Cloud\Vision\V1\Feature\Type;
use Google\Cloud\Vision\V1\Image;
use Google\Cloud\Vision\V1\ImageSource;

/**
* @param string $path GCS path to the image, e.g. "gs://path/to/your/image.jpg"
Expand All @@ -28,7 +34,18 @@ function detect_image_property_gcs(string $path)
$imageAnnotator = new ImageAnnotatorClient();

# annotate the image
$response = $imageAnnotator->imagePropertiesDetection($path);
$source = (new ImageSource())
->setImageUri($path);
$image = (new Image())
->setSource($source);
$feature = (new Feature())
->setType(Type::IMAGE_PROPERTIES);
$request = (new AnnotateImageRequest())
->setImage($image)
->setFeatures([$feature]);
$batchRequest = (new BatchAnnotateImagesRequest())
->setRequests([$request]);
$response = $imageAnnotator->batchAnnotateImages($batchRequest)->getResponses()[0];
$props = $response->getImagePropertiesAnnotation();

if ($props) {
Expand Down
17 changes: 15 additions & 2 deletions vision/src/detect_label.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@
// [START vision_label_detection]
namespace Google\Cloud\Samples\Vision;

use Google\Cloud\Vision\V1\AnnotateImageRequest;
use Google\Cloud\Vision\V1\BatchAnnotateImagesRequest;
use Google\Cloud\Vision\V1\Client\ImageAnnotatorClient;
use Google\Cloud\Vision\V1\Feature;
use Google\Cloud\Vision\V1\Feature\Type;
use Google\Cloud\Vision\V1\Image;

/**
* @param string $path Path to the image, e.g. "path/to/your/image.jpg"
Expand All @@ -28,8 +33,16 @@ function detect_label(string $path)
$imageAnnotator = new ImageAnnotatorClient();

# annotate the image
$image = file_get_contents($path);
$response = $imageAnnotator->labelDetection($image);
$image = (new Image())
->setContent(file_get_contents($path));
$feature = (new Feature())
->setType(Type::LABEL_DETECTION);
$request = (new AnnotateImageRequest())
->setImage($image)
->setFeatures([$feature]);
$batchRequest = (new BatchAnnotateImagesRequest())
->setRequests([$request]);
$response = $imageAnnotator->batchAnnotateImages($batchRequest)->getResponses()[0];
$labels = $response->getLabelAnnotations();

if ($labels->count()) {
Expand Down
19 changes: 18 additions & 1 deletion vision/src/detect_label_gcs.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@
// [START vision_label_detection_gcs]
namespace Google\Cloud\Samples\Vision;

use Google\Cloud\Vision\V1\AnnotateImageRequest;
use Google\Cloud\Vision\V1\BatchAnnotateImagesRequest;
use Google\Cloud\Vision\V1\Client\ImageAnnotatorClient;
use Google\Cloud\Vision\V1\Feature;
use Google\Cloud\Vision\V1\Feature\Type;
use Google\Cloud\Vision\V1\Image;
use Google\Cloud\Vision\V1\ImageSource;

/**
* @param string $path GCS path to the image, e.g. "gs://path/to/your/image.jpg"
Expand All @@ -28,7 +34,18 @@ function detect_label_gcs(string $path)
$imageAnnotator = new ImageAnnotatorClient();

# annotate the image
$response = $imageAnnotator->labelDetection($path);
$source = (new ImageSource())
->setImageUri($path);
$image = (new Image())
->setSource($source);
$feature = (new Feature())
->setType(Type::LABEL_DETECTION);
$request = (new AnnotateImageRequest())
->setImage($image)
->setFeatures([$feature]);
$batchRequest = (new BatchAnnotateImagesRequest())
->setRequests([$request]);
$response = $imageAnnotator->batchAnnotateImages($batchRequest)->getResponses()[0];
$labels = $response->getLabelAnnotations();

if ($labels->count()) {
Expand Down
17 changes: 15 additions & 2 deletions vision/src/detect_landmark.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@
// [START vision_landmark_detection]
namespace Google\Cloud\Samples\Vision;

use Google\Cloud\Vision\V1\AnnotateImageRequest;
use Google\Cloud\Vision\V1\BatchAnnotateImagesRequest;
use Google\Cloud\Vision\V1\Client\ImageAnnotatorClient;
use Google\Cloud\Vision\V1\Feature;
use Google\Cloud\Vision\V1\Feature\Type;
use Google\Cloud\Vision\V1\Image;

/**
* @param string $path Path to the image, e.g. "path/to/your/image.jpg"
Expand All @@ -28,8 +33,16 @@ function detect_landmark(string $path)
$imageAnnotator = new ImageAnnotatorClient();

# annotate the image
$image = file_get_contents($path);
$response = $imageAnnotator->landmarkDetection($image);
$image = (new Image())
->setContent(file_get_contents($path));
$feature = (new Feature())
->setType(Type::LANDMARK_DETECTION);
$request = (new AnnotateImageRequest())
->setImage($image)
->setFeatures([$feature]);
$batchRequest = (new BatchAnnotateImagesRequest())
->setRequests([$request]);
$response = $imageAnnotator->batchAnnotateImages($batchRequest)->getResponses()[0];
$landmarks = $response->getLandmarkAnnotations();

printf('%d landmark found:' . PHP_EOL, count($landmarks));
Expand Down
Loading
Loading