Skip to content

Commit 0afc577

Browse files
committed
Add generated sample
1 parent ab194fe commit 0afc577

File tree

3 files changed

+327
-0
lines changed

3 files changed

+327
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
2+
/*
3+
* Copyright 2019 Google LLC
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+
* https://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+
* DO NOT EDIT! This is a generated sample ("LongRunningRequest", "vision_async_batch_annotate_images")
20+
*/
21+
22+
// sample-metadata
23+
// title: Async Batch Image Annotation
24+
// description: Perform async batch image annotation
25+
// usage: php samples/V1/VisionAsyncBatchAnnotateImages.php [--input_image_uri "gs://cloud-samples-data/vision/label/wakeupcat.jpg"] [--output_uri "gs://your-bucket/prefix/"]
26+
// [START vision_async_batch_annotate_images]
27+
require __DIR__.'/../../vendor/autoload.php';
28+
29+
use Google\Cloud\Vision\V1\ImageAnnotatorClient;
30+
use Google\Cloud\Vision\V1\AnnotateImageRequest;
31+
use Google\Cloud\Vision\V1\Feature;
32+
use Google\Cloud\Vision\V1\Feature\Type;
33+
use Google\Cloud\Vision\V1\GcsDestination;
34+
use Google\Cloud\Vision\V1\Image;
35+
use Google\Cloud\Vision\V1\ImageSource;
36+
use Google\Cloud\Vision\V1\OutputConfig;
37+
38+
/** Perform async batch image annotation */
39+
function sampleAsyncBatchAnnotateImages($inputImageUri, $outputUri)
40+
{
41+
// [START vision_async_batch_annotate_images_core]
42+
43+
$imageAnnotatorClient = new ImageAnnotatorClient();
44+
45+
// $inputImageUri = 'gs://cloud-samples-data/vision/label/wakeupcat.jpg';
46+
// $outputUri = 'gs://your-bucket/prefix/';
47+
$source = new ImageSource();
48+
$source->setImageUri($inputImageUri);
49+
$image = new Image();
50+
$image->setSource($source);
51+
$type = Type::LABEL_DETECTION;
52+
$featuresElement = new Feature();
53+
$featuresElement->setType($type);
54+
$type2 = Type::IMAGE_PROPERTIES;
55+
$featuresElement2 = new Feature();
56+
$featuresElement2->setType($type2);
57+
$features = [$featuresElement, $featuresElement2];
58+
$requestsElement = new AnnotateImageRequest();
59+
$requestsElement->setImage($image);
60+
$requestsElement->setFeatures($features);
61+
$requests = [$requestsElement];
62+
$gcsDestination = new GcsDestination();
63+
$gcsDestination->setUri($outputUri);
64+
65+
// The max number of responses to output in each JSON file
66+
$batchSize = 2;
67+
$outputConfig = new OutputConfig();
68+
$outputConfig->setGcsDestination($gcsDestination);
69+
$outputConfig->setBatchSize($batchSize);
70+
71+
try {
72+
$operationResponse = $imageAnnotatorClient->asyncBatchAnnotateImages($requests, $outputConfig);
73+
$operationResponse->pollUntilComplete();
74+
if ($operationResponse->operationSucceeded()) {
75+
$response = $operationResponse->getResult();
76+
// The output is written to GCS with the provided output_uri as prefix
77+
$gcsOutputUri = $response->getOutputConfig()->getGcsDestination()->getUri();
78+
printf('Output written to GCS with prefix: %s'.PHP_EOL, $gcsOutputUri);
79+
} else {
80+
$error = $operationResponse->getError();
81+
// handleError($error)
82+
}
83+
} finally {
84+
$imageAnnotatorClient->close();
85+
}
86+
87+
// [END vision_async_batch_annotate_images_core]
88+
}
89+
// [END vision_async_batch_annotate_images]
90+
91+
$opts = [
92+
'input_image_uri::',
93+
'output_uri::',
94+
];
95+
96+
$defaultOptions = [
97+
'input_image_uri' => 'gs://cloud-samples-data/vision/label/wakeupcat.jpg',
98+
'output_uri' => 'gs://your-bucket/prefix/',
99+
];
100+
101+
$options = getopt('', $opts);
102+
$options += $defaultOptions;
103+
104+
$inputImageUri = $options['input_image_uri'];
105+
$outputUri = $options['output_uri'];
106+
107+
sampleAsyncBatchAnnotateImages($inputImageUri, $outputUri);
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
2+
/*
3+
* Copyright 2019 Google LLC
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+
* https://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+
* DO NOT EDIT! This is a generated sample ("Request", "vision_batch_annotate_files")
20+
*/
21+
22+
// sample-metadata
23+
// title:
24+
// description: Perform batch file annotation
25+
// usage: php samples/V1/VisionBatchAnnotateFiles.php [--file_path "resources/kafka.pdf"]
26+
// [START vision_batch_annotate_files]
27+
require __DIR__.'/../../vendor/autoload.php';
28+
29+
use Google\Cloud\Vision\V1\ImageAnnotatorClient;
30+
use Google\Cloud\Vision\V1\AnnotateFileRequest;
31+
use Google\Cloud\Vision\V1\Feature;
32+
use Google\Cloud\Vision\V1\Feature\Type;
33+
use Google\Cloud\Vision\V1\InputConfig;
34+
35+
/**
36+
* Perform batch file annotation.
37+
*
38+
* @param string $filePath Path to local pdf file, e.g. /path/document.pdf
39+
*/
40+
function sampleBatchAnnotateFiles($filePath)
41+
{
42+
// [START vision_batch_annotate_files_core]
43+
44+
$imageAnnotatorClient = new ImageAnnotatorClient();
45+
46+
// $filePath = 'resources/kafka.pdf';
47+
48+
// Supported mime_type: application/pdf, image/tiff, image/gif
49+
$mimeType = 'application/pdf';
50+
$content = file_get_contents($filePath);
51+
$inputConfig = new InputConfig();
52+
$inputConfig->setMimeType($mimeType);
53+
$inputConfig->setContent($content);
54+
$type = Type::DOCUMENT_TEXT_DETECTION;
55+
$featuresElement = new Feature();
56+
$featuresElement->setType($type);
57+
$features = [$featuresElement];
58+
59+
// The service can process up to 5 pages per document file. Here we specify the first, second, and
60+
// last page of the document to be processed.
61+
$pagesElement = 1;
62+
$pagesElement2 = 2;
63+
$pagesElement3 = -1;
64+
$pages = [$pagesElement, $pagesElement2, $pagesElement3];
65+
$requestsElement = new AnnotateFileRequest();
66+
$requestsElement->setInputConfig($inputConfig);
67+
$requestsElement->setFeatures($features);
68+
$requestsElement->setPages($pages);
69+
$requests = [$requestsElement];
70+
71+
try {
72+
$response = $imageAnnotatorClient->batchAnnotateFiles($requests);
73+
foreach ($response->getResponses()[0]->getResponses() as $imageResponse) {
74+
printf('Full text: %s'.PHP_EOL, $imageResponse->getFullTextAnnotation()->getText());
75+
foreach ($imageResponse->getFullTextAnnotation()->getPages() as $page) {
76+
foreach ($page->getBlocks() as $block) {
77+
printf("\nBlock confidence: %s".PHP_EOL, $block->getConfidence());
78+
foreach ($block->getParagraphs() as $par) {
79+
printf("\tParagraph confidence: %s".PHP_EOL, $par->getConfidence());
80+
foreach ($par->getWords() as $word) {
81+
printf("\t\tWord confidence: %s".PHP_EOL, $word->getConfidence());
82+
foreach ($word->getSymbols() as $symbol) {
83+
printf("\t\t\tSymbol: %s, (confidence: %s)".PHP_EOL, $symbol->getText(), $symbol->getConfidence());
84+
}
85+
}
86+
}
87+
}
88+
}
89+
}
90+
} finally {
91+
$imageAnnotatorClient->close();
92+
}
93+
94+
// [END vision_batch_annotate_files_core]
95+
}
96+
// [END vision_batch_annotate_files]
97+
98+
$opts = [
99+
'file_path::',
100+
];
101+
102+
$defaultOptions = [
103+
'file_path' => 'resources/kafka.pdf',
104+
];
105+
106+
$options = getopt('', $opts);
107+
$options += $defaultOptions;
108+
109+
$filePath = $options['file_path'];
110+
111+
sampleBatchAnnotateFiles($filePath);
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
2+
/*
3+
* Copyright 2019 Google LLC
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+
* https://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+
* DO NOT EDIT! This is a generated sample ("Request", "vision_batch_annotate_files_gcs")
20+
*/
21+
22+
// sample-metadata
23+
// title:
24+
// description: Perform batch file annotation
25+
// usage: php samples/V1/VisionBatchAnnotateFilesGcs.php [--storage_uri "gs://cloud-samples-data/vision/document_understanding/kafka.pdf"]
26+
// [START vision_batch_annotate_files_gcs]
27+
require __DIR__.'/../../vendor/autoload.php';
28+
29+
use Google\Cloud\Vision\V1\ImageAnnotatorClient;
30+
use Google\Cloud\Vision\V1\AnnotateFileRequest;
31+
use Google\Cloud\Vision\V1\Feature;
32+
use Google\Cloud\Vision\V1\Feature\Type;
33+
use Google\Cloud\Vision\V1\GcsSource;
34+
use Google\Cloud\Vision\V1\InputConfig;
35+
36+
/**
37+
* Perform batch file annotation.
38+
*
39+
* @param string $storageUri Cloud Storage URI to source image in the format gs://[bucket]/[file]
40+
*/
41+
function sampleBatchAnnotateFiles($storageUri)
42+
{
43+
// [START vision_batch_annotate_files_gcs_core]
44+
45+
$imageAnnotatorClient = new ImageAnnotatorClient();
46+
47+
// $storageUri = 'gs://cloud-samples-data/vision/document_understanding/kafka.pdf';
48+
$gcsSource = new GcsSource();
49+
$gcsSource->setUri($storageUri);
50+
$inputConfig = new InputConfig();
51+
$inputConfig->setGcsSource($gcsSource);
52+
$type = Type::DOCUMENT_TEXT_DETECTION;
53+
$featuresElement = new Feature();
54+
$featuresElement->setType($type);
55+
$features = [$featuresElement];
56+
57+
// The service can process up to 5 pages per document file.
58+
// Here we specify the first, second, and last page of the document to be processed.
59+
$pagesElement = 1;
60+
$pagesElement2 = 2;
61+
$pagesElement3 = -1;
62+
$pages = [$pagesElement, $pagesElement2, $pagesElement3];
63+
$requestsElement = new AnnotateFileRequest();
64+
$requestsElement->setInputConfig($inputConfig);
65+
$requestsElement->setFeatures($features);
66+
$requestsElement->setPages($pages);
67+
$requests = [$requestsElement];
68+
69+
try {
70+
$response = $imageAnnotatorClient->batchAnnotateFiles($requests);
71+
foreach ($response->getResponses()[0]->getResponses() as $imageResponse) {
72+
printf('Full text: %s'.PHP_EOL, $imageResponse->getFullTextAnnotation()->getText());
73+
foreach ($imageResponse->getFullTextAnnotation()->getPages() as $page) {
74+
foreach ($page->getBlocks() as $block) {
75+
printf("\nBlock confidence: %s".PHP_EOL, $block->getConfidence());
76+
foreach ($block->getParagraphs() as $par) {
77+
printf("\tParagraph confidence: %s".PHP_EOL, $par->getConfidence());
78+
foreach ($par->getWords() as $word) {
79+
printf("\t\tWord confidence: %s".PHP_EOL, $word->getConfidence());
80+
foreach ($word->getSymbols() as $symbol) {
81+
printf("\t\t\tSymbol: %s, (confidence: %s)".PHP_EOL, $symbol->getText(), $symbol->getConfidence());
82+
}
83+
}
84+
}
85+
}
86+
}
87+
}
88+
} finally {
89+
$imageAnnotatorClient->close();
90+
}
91+
92+
// [END vision_batch_annotate_files_gcs_core]
93+
}
94+
// [END vision_batch_annotate_files_gcs]
95+
96+
$opts = [
97+
'storage_uri::',
98+
];
99+
100+
$defaultOptions = [
101+
'storage_uri' => 'gs://cloud-samples-data/vision/document_understanding/kafka.pdf',
102+
];
103+
104+
$options = getopt('', $opts);
105+
$options += $defaultOptions;
106+
107+
$storageUri = $options['storage_uri'];
108+
109+
sampleBatchAnnotateFiles($storageUri);

0 commit comments

Comments
 (0)