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

Function main

vision/batch-annotate-files.js:17–98  ·  view source on GitHub ↗
(fileName = 'path/to/your/file.pdf')

Source from the content-addressed store, hash-verified

15'use strict';
16
17function main(fileName = 'path/to/your/file.pdf') {
18 // [START vision_batch_annotate_files]
19 /**
20 * TODO(developer): Uncomment these variables before running the sample.
21 */
22 // const fileName = 'path/to/your/file.pdf';
23
24 // Imports the Google Cloud client libraries
25 const {ImageAnnotatorClient} = require('@google-cloud/vision').v1;
26 const fs = require('fs').promises;
27
28 // Instantiates a client
29 const client = new ImageAnnotatorClient();
30
31 // You can send multiple files to be annotated, this sample demonstrates how to do this with
32 // one file. If you want to use multiple files, you have to create a request object for each file that you want annotated.
33 async function batchAnnotateFiles() {
34 // First Specify the input config with the file's path and its type.
35 // Supported mime_type: application/pdf, image/tiff, image/gif
36 // https://cloud.google.com/vision/docs/reference/rpc/google.cloud.vision.v1#inputconfig
37 const inputConfig = {
38 mimeType: 'application/pdf',
39 content: await fs.readFile(fileName),
40 };
41
42 // Set the type of annotation you want to perform on the file
43 // https://cloud.google.com/vision/docs/reference/rpc/google.cloud.vision.v1#google.cloud.vision.v1.Feature.Type
44 const features = [{type: 'DOCUMENT_TEXT_DETECTION'}];
45
46 // Build the request object for that one file. Note: for additional files you have to create
47 // additional file request objects and store them in a list to be used below.
48 // Since we are sending a file of type `application/pdf`, we can use the `pages` field to
49 // specify which pages to process. The service can process up to 5 pages per document file.
50 // https://cloud.google.com/vision/docs/reference/rpc/google.cloud.vision.v1#google.cloud.vision.v1.AnnotateFileRequest
51 const fileRequest = {
52 inputConfig: inputConfig,
53 features: features,
54 // Annotate the first two pages and the last one (max 5 pages)
55 // First page starts at 1, and not 0. Last page is -1.
56 pages: [1, 2, -1],
57 };
58
59 // Add each `AnnotateFileRequest` object to the batch request.
60 const request = {
61 requests: [fileRequest],
62 };
63
64 // Make the synchronous batch request.
65 const [result] = await client.batchAnnotateFiles(request);
66
67 // Process the results, just get the first result, since only one file was sent in this
68 // sample.
69 const responses = result.responses[0].responses;
70
71 for (const response of responses) {
72 console.log(`Full text: ${response.fullTextAnnotation.text}`);
73 for (const page of response.fullTextAnnotation.pages) {
74 for (const block of page.blocks) {

Callers 1

Calls 1

batchAnnotateFilesFunction · 0.70

Tested by

no test coverage detected