|
14 | 14 |
|
15 | 15 | 'use strict'; |
16 | 16 |
|
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 | + |
19 | 26 | // Imports the Google Cloud client libraries |
20 | | - const {ImageAnnotatorClient} = require('@google-cloud/vision').v1p4beta1; |
| 27 | + const {ImageAnnotatorClient} = require('@google-cloud/vision').v1; |
21 | 28 | const fs = require('fs'); |
22 | 29 | const {promisify} = require('util'); |
23 | 30 | const readFileAsync = promisify(fs.readFile); |
24 | 31 |
|
25 | | - // Creates a client |
| 32 | + // Instantiates a client |
26 | 33 | const client = new ImageAnnotatorClient(); |
27 | 34 |
|
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 | + }; |
32 | 45 |
|
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'}]; |
50 | 49 |
|
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 | + }; |
53 | 62 |
|
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(''); |
67 | 87 | console.log( |
68 | | - ` Symbol: ${symbol.text} (confidence: ${symbol.confidence})` |
| 88 | + ` Word text: ${word_text} (confidence: ${word.confidence})` |
69 | 89 | ); |
| 90 | + for (const symbol of word.symbols) { |
| 91 | + console.log( |
| 92 | + ` Symbol: ${symbol.text} (confidence: ${symbol.confidence})` |
| 93 | + ); |
| 94 | + } |
70 | 95 | } |
71 | 96 | } |
72 | 97 | } |
73 | 98 | } |
74 | 99 | } |
75 | 100 | } |
76 | | - // [END vision_batch_annotate_files_beta] |
| 101 | + |
| 102 | + batchAnnotateFiles(); |
| 103 | + // [END vision_batch_annotate_files] |
77 | 104 | } |
78 | 105 |
|
79 | | -main(...process.argv.slice(2)).catch(console.error); |
| 106 | +main(...process.argv.slice(2)); |
0 commit comments