Skip to content
This repository was archived by the owner on Jul 20, 2023. It is now read-only.

Commit 5ddbab9

Browse files
authored
docs: update beta samples to v1 and style (#553)
1 parent a80d966 commit 5ddbab9

File tree

3 files changed

+198
-128
lines changed

3 files changed

+198
-128
lines changed

samples/async-batch-annotate-images.js

Lines changed: 57 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -14,55 +14,72 @@
1414

1515
'use strict';
1616

17-
async function main(inputImageUri, outputUri) {
18-
// [START vision_async_batch_annotate_images_beta]
17+
function main(
18+
inputImageUri = 'gs://cloud-samples-data/vision/label/wakeupcat.jpg',
19+
outputUri = 'gs://YOUR_BUCKET_ID/path/to/save/results/'
20+
) {
21+
// [START vision_async_batch_annotate_images]
22+
/**
23+
* TODO(developer): Uncomment these variables before running the sample.
24+
*/
25+
// const inputImageUri = 'gs://cloud-samples-data/vision/label/wakeupcat.jpg';
26+
// const outputUri = 'gs://YOUR_BUCKET_ID/path/to/save/results/';
1927

2028
// Imports the Google Cloud client libraries
21-
const {ImageAnnotatorClient} = require('@google-cloud/vision').v1p4beta1;
29+
const {ImageAnnotatorClient} = require('@google-cloud/vision').v1;
2230

23-
// Creates a client
31+
// Instantiates a client
2432
const client = new ImageAnnotatorClient();
2533

26-
/**
27-
* TODO(developer): Uncomment the following lines before running the sample.
28-
*/
29-
// GCS path where the image resides
30-
// const inputImageUri = 'gs://my-bucket/my_image.jpg';
31-
// GCS path where to store the output json
32-
// const outputUri = 'gs://mybucket/out/'
33-
34-
const features = [
35-
{type: 'DOCUMENT_LABEL_DETECTION'},
36-
{type: 'DOCUMENT_TEXT_DETECTION'},
37-
{type: 'DOCUMENT_IMAGE_DETECTION'},
38-
];
39-
40-
const outputConfig = {
41-
gcsDestination: {
42-
uri: outputUri,
43-
},
44-
};
34+
// You can send multiple images to be annotated, this sample demonstrates how to do this with
35+
// one image. If you want to use multiple images, you have to create a request object for each image that you want annotated.
36+
async function asyncBatchAnnotateImages() {
37+
// Set the type of annotation you want to perform on the image
38+
// https://cloud.google.com/vision/docs/reference/rpc/google.cloud.vision.v1#google.cloud.vision.v1.Feature.Type
39+
const features = [
40+
{type: 'LABEL_DETECTION'},
41+
];
4542

46-
const request = {
47-
requests: [
48-
{
49-
image: {
50-
source: {
51-
imageUri: inputImageUri,
52-
},
43+
// Build the image request object for that one image. Note: for additional images you have to create
44+
// additional image request objects and store them in a list to be used below.
45+
const imageRequest = {
46+
image: {
47+
source: {
48+
imageUri: inputImageUri,
5349
},
54-
features: features,
5550
},
56-
],
57-
outputConfig,
58-
};
51+
features: features,
52+
}
53+
54+
// Set where to store the results for the images that will be annotated.
55+
const outputConfig = {
56+
gcsDestination: {
57+
uri: outputUri,
58+
},
59+
batchSize: 2, // The max number of responses to output in each JSON file
60+
};
61+
62+
// Add each image request object to the batch request and add the output config.
63+
const request = {
64+
requests: [
65+
imageRequest, // add additional request objects here
66+
],
67+
outputConfig,
68+
};
69+
70+
// Make the asynchronous batch request.
71+
const [operation] = await client.asyncBatchAnnotateImages(request);
72+
73+
// Wait for the operation to complete
74+
const [filesResponse] = await operation.promise();
5975

60-
const [operation] = await client.asyncBatchAnnotateImages(request);
61-
const [filesResponse] = await operation.promise();
76+
// The output is written to GCS with the provided output_uri as prefix
77+
const destinationUri = filesResponse.outputConfig.gcsDestination.uri;
78+
console.log(`Output written to GCS with prefix: ${destinationUri}`);
79+
}
6280

63-
const destinationUri = filesResponse.outputConfig.gcsDestination.uri;
64-
console.log(`Json saved to: ${destinationUri}`);
65-
// [END vision_async_batch_annotate_images_beta]
81+
asyncBatchAnnotateImages();
82+
// [END vision_async_batch_annotate_images]
6683
}
6784

68-
main(...process.argv.slice(2)).catch(console.error);
85+
main(...process.argv.slice(2));

samples/batch-annotate-files-gcs.js

Lines changed: 71 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -14,66 +14,92 @@
1414

1515
'use strict';
1616

17-
async function main(gcsSourceUri) {
18-
// [START vision_batch_annotate_files_gcs_beta]
17+
function main(
18+
gcsSourceUri = 'gs://cloud-samples-data/vision/document_understanding/kafka.pdf'
19+
) {
20+
// [START vision_batch_annotate_files_gcs]
21+
/**
22+
* TODO(developer): Uncomment these variables before running the sample.
23+
*/
24+
// const gcsSourceUri = 'gs://cloud-samples-data/vision/document_understanding/kafka.pdf';
25+
1926
// Imports the Google Cloud client libraries
20-
const {ImageAnnotatorClient} = require('@google-cloud/vision').v1p4beta1;
27+
const {ImageAnnotatorClient} = require('@google-cloud/vision').v1;
2128

22-
// Creates a client
29+
// Instantiates a client
2330
const client = new ImageAnnotatorClient();
2431

25-
/**
26-
* TODO(developer): Uncomment the following line before running the sample.
27-
*/
28-
// GCS path where the pdf file resides
29-
// const gcsSourceUri = 'gs://my-bucket/my_pdf.pdf';
30-
31-
const inputConfig = {
32-
// Supported mime_types are: 'application/pdf' and 'image/tiff'
33-
mimeType: 'application/pdf',
34-
gcsSource: {
35-
uri: gcsSourceUri,
36-
},
37-
};
38-
const features = [{type: 'DOCUMENT_TEXT_DETECTION'}];
39-
const request = {
40-
requests: [
41-
{
42-
inputConfig: inputConfig,
43-
features: features,
44-
// Annotate the first two pages and the last one (max 5 pages)
45-
// First page starts at 1, and not 0. Last page is -1.
46-
pages: [1, 2, -1],
32+
// You can send multiple files to be annotated, this sample demonstrates how to do this with
33+
// one file. If you want to use multiple files, you have to create a request object for each file that you want annotated.
34+
async function batchAnnotateFiles() {
35+
// First Specify the input config with the file's uri and its type.
36+
// Supported mime_type: application/pdf, image/tiff, image/gif
37+
// https://cloud.google.com/vision/docs/reference/rpc/google.cloud.vision.v1#inputconfig
38+
const inputConfig = {
39+
mimeType: 'application/pdf',
40+
gcsSource: {
41+
uri: gcsSourceUri,
4742
},
48-
],
49-
};
43+
};
5044

51-
const [result] = await client.batchAnnotateFiles(request);
52-
const responses = result.responses[0].responses;
45+
// Set the type of annotation you want to perform on the file
46+
// https://cloud.google.com/vision/docs/reference/rpc/google.cloud.vision.v1#google.cloud.vision.v1.Feature.Type
47+
const features = [{type: 'DOCUMENT_TEXT_DETECTION'}];
5348

54-
for (const response of responses) {
55-
for (const page of response.fullTextAnnotation.pages) {
56-
for (const block of page.blocks) {
57-
console.log(`Block confidence: ${block.confidence}`);
58-
for (const paragraph of block.paragraphs) {
59-
console.log(` Paragraph confidence: ${paragraph.confidence}`);
60-
for (const word of paragraph.words) {
61-
const symbol_texts = word.symbols.map(symbol => symbol.text);
62-
const word_text = symbol_texts.join('');
63-
console.log(
64-
` Word text: ${word_text} (confidence: ${word.confidence})`
65-
);
66-
for (const symbol of word.symbols) {
49+
// Build the request object for that one file. Note: for additional files you have to create
50+
// additional file request objects and store them in a list to be used below.
51+
// Since we are sending a file of type `application/pdf`, we can use the `pages` field to
52+
// specify which pages to process. The service can process up to 5 pages per document file.
53+
// https://cloud.google.com/vision/docs/reference/rpc/google.cloud.vision.v1#google.cloud.vision.v1.AnnotateFileRequest
54+
const fileRequest = {
55+
inputConfig: inputConfig,
56+
features: features,
57+
// Annotate the first two pages and the last one (max 5 pages)
58+
// First page starts at 1, and not 0. Last page is -1.
59+
pages: [1, 2, -1],
60+
};
61+
62+
// Add each `AnnotateFileRequest` object to the batch request.
63+
const request = {
64+
requests: [
65+
fileRequest,
66+
],
67+
};
68+
69+
// Make the synchronous batch request.
70+
const [result] = await client.batchAnnotateFiles(request);
71+
72+
// Process the results, just get the first result, since only one file was sent in this
73+
// sample.
74+
const responses = result.responses[0].responses;
75+
76+
for (const response of responses) {
77+
console.log(`Full text: ${response.fullTextAnnotation.text}`)
78+
for (const page of response.fullTextAnnotation.pages) {
79+
for (const block of page.blocks) {
80+
console.log(`Block confidence: ${block.confidence}`);
81+
for (const paragraph of block.paragraphs) {
82+
console.log(` Paragraph confidence: ${paragraph.confidence}`);
83+
for (const word of paragraph.words) {
84+
const symbol_texts = word.symbols.map(symbol => symbol.text);
85+
const word_text = symbol_texts.join('');
6786
console.log(
68-
` Symbol: ${symbol.text} (confidence: ${symbol.confidence})`
87+
` Word text: ${word_text} (confidence: ${word.confidence})`
6988
);
89+
for (const symbol of word.symbols) {
90+
console.log(
91+
` Symbol: ${symbol.text} (confidence: ${symbol.confidence})`
92+
);
93+
}
7094
}
7195
}
7296
}
7397
}
7498
}
7599
}
76-
// [END vision_batch_annotate_files_gcs_beta]
100+
101+
batchAnnotateFiles();
102+
// [END vision_batch_annotate_files_gcs]
77103
}
78104

79105
main(...process.argv.slice(2));

samples/batch-annotate-files.js

Lines changed: 70 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -14,66 +14,93 @@
1414

1515
'use strict';
1616

17-
async function main(fileName) {
18-
// [START vision_batch_annotate_files_beta]
17+
function main(
18+
fileName = 'path/to/your/file.pdf'
19+
) {
20+
// [START vision_batch_annotate_files]
21+
/**
22+
* TODO(developer): Uncomment these variables before running the sample.
23+
*/
24+
// const fileName = 'path/to/your/file.pdf';
25+
1926
// Imports the Google Cloud client libraries
20-
const {ImageAnnotatorClient} = require('@google-cloud/vision').v1p4beta1;
27+
const {ImageAnnotatorClient} = require('@google-cloud/vision').v1;
2128
const fs = require('fs');
2229
const {promisify} = require('util');
2330
const readFileAsync = promisify(fs.readFile);
2431

25-
// Creates a client
32+
// Instantiates a client
2633
const client = new ImageAnnotatorClient();
2734

28-
/**
29-
* TODO(developer): Uncomment the following line before running the sample.
30-
*/
31-
// const fileName = `/path/to/localDocument.pdf`;
35+
// You can send multiple files to be annotated, this sample demonstrates how to do this with
36+
// one file. If you want to use multiple files, you have to create a request object for each file that you want annotated.
37+
async function batchAnnotateFiles() {
38+
// First Specify the input config with the file's path and its type.
39+
// Supported mime_type: application/pdf, image/tiff, image/gif
40+
// https://cloud.google.com/vision/docs/reference/rpc/google.cloud.vision.v1#inputconfig
41+
const inputConfig = {
42+
mimeType: 'application/pdf',
43+
content: await readFileAsync(fileName),
44+
};
3245

33-
const inputConfig = {
34-
// Other supported mime_types: image/tiff' or 'image/gif'
35-
mimeType: 'application/pdf',
36-
content: await readFileAsync(fileName),
37-
};
38-
const features = [{type: 'DOCUMENT_TEXT_DETECTION'}];
39-
const request = {
40-
requests: [
41-
{
42-
inputConfig,
43-
features: features,
44-
// Annotate the first two pages and the last one (max 5 pages)
45-
// First page starts at 1, and not 0. Last page is -1.
46-
pages: [1, 2, -1],
47-
},
48-
],
49-
};
46+
// Set the type of annotation you want to perform on the file
47+
// https://cloud.google.com/vision/docs/reference/rpc/google.cloud.vision.v1#google.cloud.vision.v1.Feature.Type
48+
const features = [{type: 'DOCUMENT_TEXT_DETECTION'}];
5049

51-
const [result] = await client.batchAnnotateFiles(request);
52-
const responses = result.responses[0].responses;
50+
// Build the request object for that one file. Note: for additional files you have to create
51+
// additional file request objects and store them in a list to be used below.
52+
// Since we are sending a file of type `application/pdf`, we can use the `pages` field to
53+
// specify which pages to process. The service can process up to 5 pages per document file.
54+
// https://cloud.google.com/vision/docs/reference/rpc/google.cloud.vision.v1#google.cloud.vision.v1.AnnotateFileRequest
55+
const fileRequest = {
56+
inputConfig: inputConfig,
57+
features: features,
58+
// Annotate the first two pages and the last one (max 5 pages)
59+
// First page starts at 1, and not 0. Last page is -1.
60+
pages: [1, 2, -1],
61+
};
5362

54-
for (const response of responses) {
55-
for (const page of response.fullTextAnnotation.pages) {
56-
for (const block of page.blocks) {
57-
console.log(`Block confidence: ${block.confidence}`);
58-
for (const paragraph of block.paragraphs) {
59-
console.log(` Paragraph confidence: ${paragraph.confidence}`);
60-
for (const word of paragraph.words) {
61-
const symbol_texts = word.symbols.map(symbol => symbol.text);
62-
const word_text = symbol_texts.join('');
63-
console.log(
64-
` Word text: ${word_text} (confidence: ${word.confidence})`
65-
);
66-
for (const symbol of word.symbols) {
63+
// Add each `AnnotateFileRequest` object to the batch request.
64+
const request = {
65+
requests: [
66+
fileRequest,
67+
],
68+
};
69+
70+
// Make the synchronous batch request.
71+
const [result] = await client.batchAnnotateFiles(request);
72+
73+
// Process the results, just get the first result, since only one file was sent in this
74+
// sample.
75+
const responses = result.responses[0].responses;
76+
77+
for (const response of responses) {
78+
console.log(`Full text: ${response.fullTextAnnotation.text}`)
79+
for (const page of response.fullTextAnnotation.pages) {
80+
for (const block of page.blocks) {
81+
console.log(`Block confidence: ${block.confidence}`);
82+
for (const paragraph of block.paragraphs) {
83+
console.log(` Paragraph confidence: ${paragraph.confidence}`);
84+
for (const word of paragraph.words) {
85+
const symbol_texts = word.symbols.map(symbol => symbol.text);
86+
const word_text = symbol_texts.join('');
6787
console.log(
68-
` Symbol: ${symbol.text} (confidence: ${symbol.confidence})`
88+
` Word text: ${word_text} (confidence: ${word.confidence})`
6989
);
90+
for (const symbol of word.symbols) {
91+
console.log(
92+
` Symbol: ${symbol.text} (confidence: ${symbol.confidence})`
93+
);
94+
}
7095
}
7196
}
7297
}
7398
}
7499
}
75100
}
76-
// [END vision_batch_annotate_files_beta]
101+
102+
batchAnnotateFiles();
103+
// [END vision_batch_annotate_files]
77104
}
78105

79-
main(...process.argv.slice(2)).catch(console.error);
106+
main(...process.argv.slice(2));

0 commit comments

Comments
 (0)