* Given a set of image file paths, extract the text and run them through the * Cloud Vision API. * @param {Index} index The stateful `Index` Object. * @param {string[]} inputFiles The list of files to process. * @returns {Promise }
(index, inputFiles)
| 181 | * @returns {Promise<void>} |
| 182 | */ |
| 183 | async function getTextFromFiles(index, inputFiles) { |
| 184 | // Read all of the given files and provide request objects that will be |
| 185 | // passed to the Cloud Vision API in a batch request. |
| 186 | const requests = await Promise.all( |
| 187 | inputFiles.map(async filename => { |
| 188 | const content = await fs.readFile(filename); |
| 189 | console.log(` 👉 ${filename}`); |
| 190 | return { |
| 191 | image: { |
| 192 | content: content.toString('base64'), |
| 193 | }, |
| 194 | features: [{type: 'TEXT_DETECTION'}], |
| 195 | }; |
| 196 | }) |
| 197 | ); |
| 198 | |
| 199 | // Make a call to the Vision API to detect text |
| 200 | const results = await client.batchAnnotateImages({requests}); |
| 201 | const detections = results[0].responses; |
| 202 | await Promise.all( |
| 203 | inputFiles.map(async (filename, i) => { |
| 204 | const response = detections[i]; |
| 205 | if (response.error) { |
| 206 | console.info(`API Error for ${filename}`, response.error); |
| 207 | return; |
| 208 | } |
| 209 | await extractDescriptions(filename, index, response); |
| 210 | }) |
| 211 | ); |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Main entry point for the program. |
no test coverage detected