| 57 | * @returns {Promise} |
| 58 | */ |
| 59 | const detectText = async (bucketName, filename) => { |
| 60 | console.log(`Looking for text in image ${filename}`); |
| 61 | const [textDetections] = await vision.textDetection( |
| 62 | `gs://${bucketName}/${filename}` |
| 63 | ); |
| 64 | const [annotation] = textDetections.textAnnotations; |
| 65 | const text = annotation ? annotation.description.trim() : ''; |
| 66 | console.log('Extracted text from image:', text); |
| 67 | |
| 68 | let [translateDetection] = await translate.detect(text); |
| 69 | if (Array.isArray(translateDetection)) { |
| 70 | [translateDetection] = translateDetection; |
| 71 | } |
| 72 | console.log( |
| 73 | `Detected language "${translateDetection.language}" for ${filename}` |
| 74 | ); |
| 75 | |
| 76 | // Submit a message to the bus for each language we're going to translate to |
| 77 | const TO_LANGS = process.env.TO_LANG.split(','); |
| 78 | const topicName = process.env.TRANSLATE_TOPIC; |
| 79 | |
| 80 | const tasks = TO_LANGS.map(lang => { |
| 81 | const messageData = { |
| 82 | text: text, |
| 83 | filename: filename, |
| 84 | lang: lang, |
| 85 | }; |
| 86 | |
| 87 | // Helper function that publishes translation result to a Pub/Sub topic |
| 88 | // For more information on publishing Pub/Sub messages, see this page: |
| 89 | // https://cloud.google.com/pubsub/docs/publisher |
| 90 | return publishResult(topicName, messageData); |
| 91 | }); |
| 92 | |
| 93 | return Promise.all(tasks); |
| 94 | }; |
| 95 | // [END functions_ocr_detect] |
| 96 | |
| 97 | /** |