MCPcopy Create free account
hub / github.com/GoogleCloudPlatform/nodejs-docs-samples / main

Function main

vision/batch-annotate-files-gcs.js:17–101  ·  view source on GitHub ↗
(
  gcsSourceUri = 'gs://cloud-samples-data/vision/document_understanding/kafka.pdf'
)

Source from the content-addressed store, hash-verified

15'use strict';
16
17function 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
26 // Imports the Google Cloud client libraries
27 const {ImageAnnotatorClient} = require('@google-cloud/vision').v1;
28
29 // Instantiates a client
30 const client = new ImageAnnotatorClient();
31
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,
42 },
43 };
44
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'}];
48
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: [fileRequest],
65 };
66
67 // Make the synchronous batch request.
68 const [result] = await client.batchAnnotateFiles(request);
69
70 // Process the results, just get the first result, since only one file was sent in this
71 // sample.
72 const responses = result.responses[0].responses;
73
74 for (const response of responses) {

Callers 1

Calls 1

batchAnnotateFilesFunction · 0.70

Tested by

no test coverage detected